Skip to content

Commit 2ddb976

Browse files
committed
feat: send Toast instead of chat message on DevUtils methods
fix: copySideBar dev command is not working as intended
1 parent 218612e commit 2ddb976

File tree

3 files changed

+50
-53
lines changed

3 files changed

+50
-53
lines changed

src/main/java/com/fix3dll/skyblockaddons/commands/SkyblockAddonsCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ public static LiteralArgumentBuilder<FabricClientCommandSource> buildCommands()
311311
// COPY_SIDEBAR
312312
builder.then(literal("copySidebar").requires(rq -> Feature.DEVELOPER_MODE.isEnabled()).executes(ctx -> {
313313
DevUtils.setCopyMode(DevUtils.CopyMode.SIDEBAR);
314+
DevUtils.setSidebarFormatted(DevUtils.DEFAULT_SIDEBAR_FORMATTED);
314315
DevUtils.copyData();
315316
return 1;
316317
}).then(argument("formatted", BoolArgumentType.bool()).executes(ctx -> {
@@ -417,7 +418,7 @@ public static LiteralArgumentBuilder<FabricClientCommandSource> buildCommands()
417418
String command = ctx.getArgument("command", String.class);
418419
String arg = ctx.getArgument("arg", String.class).toLowerCase(Locale.US);
419420
if ("copy".equalsIgnoreCase(command)) {
420-
DevUtils.copyStringToClipboard(arg, Translations.getMessage("messages.copied"));
421+
DevUtils.copyStringToClipboard(arg, Translations.getMessage("messages.copied"), false);
421422
}
422423
return 1;
423424
}))));

src/main/java/com/fix3dll/skyblockaddons/mixin/transformers/ChatScreenMixin.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
import com.fix3dll.skyblockaddons.mixin.extensions.GuiMessageLineExtension;
99
import com.fix3dll.skyblockaddons.utils.DevUtils;
1010
import com.fix3dll.skyblockaddons.utils.TextUtils;
11-
import com.fix3dll.skyblockaddons.utils.Utils;
1211
import com.llamalad7.mixinextras.sugar.Local;
1312
import com.mojang.blaze3d.platform.InputConstants;
1413
import com.mojang.blaze3d.platform.Window;
1514
import net.minecraft.Util;
1615
import net.minecraft.client.Minecraft;
1716
import net.minecraft.client.gui.components.ChatComponent;
18-
import net.minecraft.client.gui.components.toasts.SystemToast;
1917
import net.minecraft.client.gui.screens.ChatScreen;
2018
import net.minecraft.client.input.MouseButtonEvent;
21-
import net.minecraft.network.chat.Component;
2219
import org.lwjgl.glfw.GLFW;
2320
import org.spongepowered.asm.mixin.Mixin;
2421
import org.spongepowered.asm.mixin.injection.At;
@@ -49,24 +46,16 @@ public class ChatScreenMixin {
4946
boolean isLeftShiftDown = InputConstants.isKeyDown(handle, GLFW.GLFW_KEY_LEFT_SHIFT);
5047
if (isLeftShiftDown) {
5148
DevUtils.copyStringToClipboard(
52-
TextUtils.getFormattedText(extendedLine.sba$getParentComponent()), null
49+
TextUtils.getFormattedText(extendedLine.sba$getParentComponent()),
50+
ColorCode.GREEN + Translations.getMessage("messages.chatMessageCopying.formatted"),
51+
true
5352
);
54-
Minecraft.getInstance().getToastManager().addToast(new SystemToast(
55-
new SystemToast.SystemToastId(2000L),
56-
Utils.COMPONENT_TITLE,
57-
Component.literal(Translations.getMessage("messages.chatMessageCopying.formatted"))
58-
.withColor(ColorCode.GREEN.getColor())
59-
));
6053
} else {
6154
DevUtils.copyStringToClipboard(
62-
TextUtils.stripColor(extendedLine.sba$getParentComponent().getString()), null
55+
TextUtils.stripColor(extendedLine.sba$getParentComponent().getString()),
56+
ColorCode.GREEN + Translations.getMessage("messages.chatMessageCopying.unformatted"),
57+
true
6358
);
64-
Minecraft.getInstance().getToastManager().addToast(new SystemToast(
65-
new SystemToast.SystemToastId(2000L),
66-
Utils.COMPONENT_TITLE,
67-
Component.literal(Translations.getMessage("messages.chatMessageCopying.unformatted"))
68-
.withColor(ColorCode.GREEN.getColor())
69-
));
7059
}
7160
}
7261
}

src/main/java/com/fix3dll/skyblockaddons/utils/DevUtils.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import lombok.Setter;
1414
import net.minecraft.client.Minecraft;
1515
import net.minecraft.client.gui.Gui;
16+
import net.minecraft.client.gui.components.toasts.SystemToast;
1617
import net.minecraft.client.multiplayer.ClientLevel;
1718
import net.minecraft.client.multiplayer.ClientPacketListener;
1819
import net.minecraft.client.player.LocalPlayer;
@@ -70,7 +71,7 @@ public class DevUtils {
7071
// If you change this, please change it in the string "commands.usage.sba.help.copyEntity" as well.
7172
public static final int DEFAULT_ENTITY_COPY_RADIUS = 3;
7273
private static final List<Class<? extends Entity>> DEFAULT_ENTITY_NAMES = Collections.singletonList(LivingEntity.class);
73-
private static final boolean DEFAULT_SIDEBAR_FORMATTED = false;
74+
public static final boolean DEFAULT_SIDEBAR_FORMATTED = false;
7475

7576
@Getter @Setter private static boolean loggingActionBarMessages = false;
7677
@Getter @Setter private static boolean loggingSlayerTracker = false;
@@ -98,17 +99,8 @@ public class DevUtils {
9899

99100
/**
100101
* Copies the objective and scores that are being displayed on a scoreboard's sidebar.
101-
* When copying the sidebar, the control codes (e.g. §a) are removed.
102102
*/
103-
public static void copyScoreboardSideBar() {
104-
copyScoreboardSidebar(sidebarFormatted);
105-
}
106-
107-
/**
108-
* Copies the objective and scores that are being displayed on a scoreboard's sidebar.
109-
* @param stripControlCodes if {@code true}, the control codes will be removed, otherwise they will be copied
110-
*/
111-
private static void copyScoreboardSidebar(boolean stripControlCodes) {
103+
private static void copyScoreboardSidebar() {
112104
if (MC.level == null) return;
113105

114106
Scoreboard scoreboard = MC.level.getScoreboard();
@@ -123,9 +115,9 @@ private static void copyScoreboardSidebar(boolean stripControlCodes) {
123115
return;
124116
}
125117

126-
String title = stripControlCodes
127-
? TextUtils.stripColor(sideBarObjective.getDisplayName().getString())
128-
: TextUtils.getFormattedText(sideBarObjective.getDisplayName(), true);
118+
String title = sidebarFormatted
119+
? TextUtils.getFormattedText(sideBarObjective.getDisplayName(), true)
120+
: TextUtils.stripColor(sideBarObjective.getDisplayName().getString());
129121
StringBuilder stringBuilder = new StringBuilder(title).append("\n");
130122

131123
scoreboard.listPlayerScores(sideBarObjective).stream()
@@ -140,19 +132,18 @@ private static void copyScoreboardSidebar(boolean stripControlCodes) {
140132
Component decoratedName = PlayerTeam.formatNameForTeam(team, name);
141133

142134
// return fixed name
143-
String text = stripControlCodes
144-
? TextUtils.stripColor(decoratedName.getString())
145-
: TextUtils.getFormattedText(decoratedName, true);
146-
stringBuilder.append(text.replace(owner, ""));
147-
148-
if (!stripControlCodes) {
149-
stringBuilder.append(" [").append(scoreboardEntry.value()).append("]");
150-
}
135+
String text = sidebarFormatted
136+
? TextUtils.getFormattedText(decoratedName, true)
137+
: TextUtils.stripColor(decoratedName.getString());
151138

152-
stringBuilder.append("\n");
139+
// replace control codes (e.g. §j)
140+
stringBuilder.append(sidebarFormatted ? text : text.replace(owner, ""));
141+
stringBuilder.append(" [").append(scoreboardEntry.value()).append("]").append("\n");
153142
});
154143

155-
copyStringToClipboard(stringBuilder.toString(), ColorCode.GREEN + "Sidebar copied to clipboard!");
144+
copyStringToClipboard(
145+
stringBuilder.toString(), ColorCode.GREEN + "Sidebar copied to clipboard!", true
146+
);
156147
}
157148

158149
/**
@@ -212,7 +203,9 @@ private static void copyEntityData(List<Class<? extends Entity>> includedEntityC
212203
}
213204

214205
if (!stringBuilder.isEmpty()) {
215-
copyStringToClipboard(stringBuilder.toString(), ColorCode.GREEN + "Entity data was copied to clipboard!");
206+
copyStringToClipboard(
207+
stringBuilder.toString(), ColorCode.GREEN + "Entity data was copied to clipboard!", true
208+
);
216209
} else {
217210
Utils.sendErrorMessage("No entities matching the given parameters were found.");
218211
}
@@ -292,7 +285,7 @@ public static void copyData() {
292285
copyEntityData();
293286
break;
294287
case SIDEBAR:
295-
copyScoreboardSideBar();
288+
copyScoreboardSidebar();
296289
break;
297290
case TAB_LIST:
298291
copyTabListHeaderAndFooter();
@@ -311,7 +304,7 @@ public static void copyNBTTagToClipboard(Tag nbtTag, String message) {
311304
Utils.sendErrorMessage("This item has no NBT data!");
312305
return;
313306
}
314-
writeToClipboard(prettyPrintNBT(nbtTag), message);
307+
writeToClipboard(prettyPrintNBT(nbtTag), message, true);
315308
}
316309

317310
/**
@@ -342,7 +335,8 @@ public static void copyTabListHeaderAndFooter() {
342335

343336
copyStringToClipboard(
344337
output.toString(),
345-
ColorCode.GREEN + "Successfully copied the tab list header and footer to clipboard!"
338+
ColorCode.GREEN + "Successfully copied the tab list header and footer to clipboard!",
339+
true
346340
);
347341
}
348342

@@ -364,8 +358,7 @@ public static void copyOpenGLLogs() {
364358
```
365359
""".formatted(cpu, gpu, version, lwjgl);
366360
copyStringToClipboard(
367-
output,
368-
ColorCode.GREEN + "Successfully copied the OpenGL logs to clipboard!"
361+
output, ColorCode.GREEN + "Successfully copied the OpenGL logs to clipboard!", true
369362
);
370363
}
371364

@@ -375,9 +368,10 @@ public static void copyOpenGLLogs() {
375368
*
376369
* @param string the string to copy
377370
* @param successMessage the custom message to show after successful copy
371+
* @param showToast show {@link net.minecraft.client.gui.components.toasts.Toast} instead of chat message
378372
*/
379-
public static void copyStringToClipboard(String string, String successMessage) {
380-
writeToClipboard(string, successMessage);
373+
public static void copyStringToClipboard(String string, String successMessage, boolean showToast) {
374+
writeToClipboard(string, successMessage, showToast);
381375
}
382376

383377
/**
@@ -431,7 +425,7 @@ public static void copyBlockData() {
431425
}
432426
}
433427

434-
writeToClipboard(prettyPrintNBT(nbt), ColorCode.GREEN + "Successfully copied the block data!");
428+
writeToClipboard(prettyPrintNBT(nbt), ColorCode.GREEN + "Successfully copied the block data!", true);
435429
}
436430

437431
/**
@@ -601,11 +595,24 @@ public static void reloadResources() {
601595
}
602596

603597
// Internal methods
604-
private static void writeToClipboard(String text, String successMessage) {
598+
private static void writeToClipboard(String text, String successMessage, boolean showToast) {
605599
try {
606600
MC.keyboardHandler.setClipboard(text);
607601
if (successMessage != null) {
608-
Utils.sendMessage(successMessage);
602+
if (showToast) {
603+
try {
604+
MC.getToastManager().addToast(new SystemToast( // TODO custom Toast
605+
new SystemToast.SystemToastId(2000L),
606+
Utils.COMPONENT_TITLE,
607+
Component.literal(successMessage)
608+
));
609+
} catch (Exception e) {
610+
LOGGER.error("Couldn't add Toast!", e);
611+
Utils.sendMessage(successMessage);
612+
}
613+
} else {
614+
Utils.sendMessage(successMessage);
615+
}
609616
}
610617
} catch (IllegalStateException exception) {
611618
Utils.sendErrorMessage("Clipboard not available!");

0 commit comments

Comments
 (0)