From 671c094df67dd76f55eb970f33878b829364e723 Mon Sep 17 00:00:00 2001 From: xiaoliziawa <3682822069@qq.com> Date: Thu, 22 May 2025 00:21:34 +0800 Subject: [PATCH 1/4] Add a command kubejs /kjs quote_style double/single Used to choose between double quotes or single quotes when copying Items Id or Tag --- .../latvian/mods/kubejs/CommonProperties.java | 8 +++++++ .../kubejs/command/InformationCommands.java | 9 ++++++- .../mods/kubejs/command/KubeJSCommands.java | 24 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/CommonProperties.java b/src/main/java/dev/latvian/mods/kubejs/CommonProperties.java index 6dc20b4a7..4c024bcfc 100644 --- a/src/main/java/dev/latvian/mods/kubejs/CommonProperties.java +++ b/src/main/java/dev/latvian/mods/kubejs/CommonProperties.java @@ -40,6 +40,7 @@ public static void reload() { public int defaultMaxStackSize; public JsonElement creativeModeTabIcon; public JsonElement creativeModeTabName; + public boolean useDoubleQuotes; private CommonProperties() { super(KubeJSPaths.COMMON_PROPERTIES, "KubeJS Common Properties"); @@ -59,6 +60,7 @@ protected void load() { startupErrorReportUrl = get("startup_error_report_url", ""); removeSlotLimit = get("remove_slot_limit", false); defaultMaxStackSize = Math.max(0, Math.min(1_000_000_000, get("default_max_stack_size", 0))); + useDoubleQuotes = get("use_double_quotes", false); creativeModeTabIcon = get("creative_mode_tab_icon", new JsonObject()); creativeModeTabName = get("creative_mode_tab_name", JsonNull.INSTANCE); @@ -70,6 +72,12 @@ public void setPackMode(String s) { set("packmode", new JsonPrimitive(s)); save(); } + + public void setUseDoubleQuotes(boolean value) { + useDoubleQuotes = value; + set("use_double_quotes", new JsonPrimitive(value)); + save(); + } public Component getCreativeModeTabName() { if (!creativeModeTabName.isJsonNull()) { diff --git a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java index e8c2cccb7..5187695bb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java @@ -1,5 +1,6 @@ package dev.latvian.mods.kubejs.command; +import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.ingredient.NamespaceIngredient; import net.minecraft.ChatFormatting; import net.minecraft.core.HolderSet; @@ -27,9 +28,15 @@ private static Component copy(String s, ChatFormatting col, Component info) { } private static Component copy(Component c, Component info) { + String textToCopy = c.getString(); + + if (textToCopy.startsWith("'") && textToCopy.endsWith("'") && CommonProperties.get().useDoubleQuotes) { + textToCopy = "\"" + textToCopy.substring(1, textToCopy.length() - 1) + "\""; + } + return Component.literal("- ") .withStyle(ChatFormatting.GRAY) - .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, c.getString()))) + .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, textToCopy))) .withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, info.copy().append(" (Click to copy)")))) .append(c); } diff --git a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index 5141f2c98..4050ae121 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -198,6 +198,15 @@ public static void register(CommandDispatcher dispatcher) { .executes(context -> packmode(context.getSource(), StringArgumentType.getString(context, "name"))) ) ) + .then(Commands.literal("quote_style") + .executes(context -> toggleQuoteStyle(context.getSource())) + .then(Commands.literal("single") + .executes(context -> setQuoteStyle(context.getSource(), false)) + ) + .then(Commands.literal("double") + .executes(context -> setQuoteStyle(context.getSource(), true)) + ) + ) .then(Commands.literal("persistent-data") .requires(spOrOP) .then(PersistentDataCommands.addPersistentDataCommands(Commands.literal("server"), ctx -> Set.of(ctx.getSource().getServer()))) @@ -546,6 +555,21 @@ private static int packmode(CommandSourceStack source, String packmode) { return 1; } + + private static int toggleQuoteStyle(CommandSourceStack source) { + boolean current = CommonProperties.get().useDoubleQuotes; + CommonProperties.get().setUseDoubleQuotes(!current); + String newStyle = CommonProperties.get().useDoubleQuotes ? "double" : "single"; + source.sendSuccess(() -> Component.literal("Quote style switched to: " + newStyle + " quotes"), true); + return 1; + } + + private static int setQuoteStyle(CommandSourceStack source, boolean useDoubleQuotes) { + CommonProperties.get().setUseDoubleQuotes(useDoubleQuotes); + String style = useDoubleQuotes ? "double" : "single"; + source.sendSuccess(() -> Component.literal("Quote style set to: " + style + " quotes"), true); + return 1; + } private static int eval(CommandSourceStack source, String code) { var cx = (KubeJSContext) source.getServer().getServerResources().managers().kjs$getServerScriptManager().contextFactory.enter(); From a9a5f8f13bdcdc873c448f9c5580006ff0bdd486 Mon Sep 17 00:00:00 2001 From: xiaoliziawa <3682822069@qq.com> Date: Thu, 22 May 2025 00:53:54 +0800 Subject: [PATCH 2/4] Add a chat box prompt for users to reapply the kubejs hand command and apply new quotation mark changes --- .../java/dev/latvian/mods/kubejs/command/KubeJSCommands.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index 4050ae121..bea0dde4b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -561,6 +561,7 @@ private static int toggleQuoteStyle(CommandSourceStack source) { CommonProperties.get().setUseDoubleQuotes(!current); String newStyle = CommonProperties.get().useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style switched to: " + newStyle + " quotes"), true); + source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } @@ -568,6 +569,7 @@ private static int setQuoteStyle(CommandSourceStack source, boolean useDoubleQuo CommonProperties.get().setUseDoubleQuotes(useDoubleQuotes); String style = useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style set to: " + style + " quotes"), true); + source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } From 1ac9515c5fcd38c33786a08839e2814e8bd022af Mon Sep 17 00:00:00 2001 From: xiaoliziawa <3682822069@qq.com> Date: Thu, 22 May 2025 19:26:28 +0800 Subject: [PATCH 3/4] Switch to using mixin for modification, no longer requiring players to re-enter the /kjs hand command --- .../mods/kubejs/command/InformationCommands.java | 9 +-------- .../latvian/mods/kubejs/command/KubeJSCommands.java | 2 -- .../mods/kubejs/core/mixin/ClickEventMixin.java | 13 +++++++++++++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java index 5187695bb..e8c2cccb7 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.command; -import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.ingredient.NamespaceIngredient; import net.minecraft.ChatFormatting; import net.minecraft.core.HolderSet; @@ -28,15 +27,9 @@ private static Component copy(String s, ChatFormatting col, Component info) { } private static Component copy(Component c, Component info) { - String textToCopy = c.getString(); - - if (textToCopy.startsWith("'") && textToCopy.endsWith("'") && CommonProperties.get().useDoubleQuotes) { - textToCopy = "\"" + textToCopy.substring(1, textToCopy.length() - 1) + "\""; - } - return Component.literal("- ") .withStyle(ChatFormatting.GRAY) - .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, textToCopy))) + .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, c.getString()))) .withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, info.copy().append(" (Click to copy)")))) .append(c); } diff --git a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index bea0dde4b..4050ae121 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -561,7 +561,6 @@ private static int toggleQuoteStyle(CommandSourceStack source) { CommonProperties.get().setUseDoubleQuotes(!current); String newStyle = CommonProperties.get().useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style switched to: " + newStyle + " quotes"), true); - source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } @@ -569,7 +568,6 @@ private static int setQuoteStyle(CommandSourceStack source, boolean useDoubleQuo CommonProperties.get().setUseDoubleQuotes(useDoubleQuotes); String style = useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style set to: " + style + " quotes"), true); - source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java index 438cd0097..c36aeb377 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java @@ -1,16 +1,29 @@ package dev.latvian.mods.kubejs.core.mixin; import com.mojang.serialization.Codec; +import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.util.WithCodec; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.ClickEvent; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ClickEvent.class) public abstract class ClickEventMixin implements WithCodec { @Shadow public abstract String getValue(); + + @Inject(method = "getValue", at = @At("RETURN"), cancellable = true) + private void kubejs$getValueWithQuoteStyle(CallbackInfoReturnable cir) { + String value = cir.getReturnValue(); + + if (CommonProperties.get().useDoubleQuotes && value != null && value.startsWith("'") && value.endsWith("'")) { + cir.setReturnValue("\"" + value.substring(1, value.length() - 1) + "\""); + } + } @Override public Codec getCodec(Context cx) { From c7b1c23b81bf62f6fec95bcf8c3ee53df1056def Mon Sep 17 00:00:00 2001 From: xiaoliziawa <3682822069@qq.com> Date: Thu, 5 Mar 2026 10:25:53 +0800 Subject: [PATCH 4/4] revert: rollback to a9a5f8f --- .../mods/kubejs/command/InformationCommands.java | 9 ++++++++- .../latvian/mods/kubejs/command/KubeJSCommands.java | 2 ++ .../mods/kubejs/core/mixin/ClickEventMixin.java | 13 ------------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java index e8c2cccb7..5187695bb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/InformationCommands.java @@ -1,5 +1,6 @@ package dev.latvian.mods.kubejs.command; +import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.ingredient.NamespaceIngredient; import net.minecraft.ChatFormatting; import net.minecraft.core.HolderSet; @@ -27,9 +28,15 @@ private static Component copy(String s, ChatFormatting col, Component info) { } private static Component copy(Component c, Component info) { + String textToCopy = c.getString(); + + if (textToCopy.startsWith("'") && textToCopy.endsWith("'") && CommonProperties.get().useDoubleQuotes) { + textToCopy = "\"" + textToCopy.substring(1, textToCopy.length() - 1) + "\""; + } + return Component.literal("- ") .withStyle(ChatFormatting.GRAY) - .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, c.getString()))) + .withStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, textToCopy))) .withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, info.copy().append(" (Click to copy)")))) .append(c); } diff --git a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java index 4050ae121..bea0dde4b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/KubeJSCommands.java @@ -561,6 +561,7 @@ private static int toggleQuoteStyle(CommandSourceStack source) { CommonProperties.get().setUseDoubleQuotes(!current); String newStyle = CommonProperties.get().useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style switched to: " + newStyle + " quotes"), true); + source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } @@ -568,6 +569,7 @@ private static int setQuoteStyle(CommandSourceStack source, boolean useDoubleQuo CommonProperties.get().setUseDoubleQuotes(useDoubleQuotes); String style = useDoubleQuotes ? "double" : "single"; source.sendSuccess(() -> Component.literal("Quote style set to: " + style + " quotes"), true); + source.sendSuccess(() -> Component.literal("Please use /kubejs hand again to apply the new quote style to chat messages.").withStyle(ChatFormatting.YELLOW), false); return 1; } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java index c36aeb377..438cd0097 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClickEventMixin.java @@ -1,29 +1,16 @@ package dev.latvian.mods.kubejs.core.mixin; import com.mojang.serialization.Codec; -import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.util.WithCodec; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.ClickEvent; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ClickEvent.class) public abstract class ClickEventMixin implements WithCodec { @Shadow public abstract String getValue(); - - @Inject(method = "getValue", at = @At("RETURN"), cancellable = true) - private void kubejs$getValueWithQuoteStyle(CallbackInfoReturnable cir) { - String value = cir.getReturnValue(); - - if (CommonProperties.get().useDoubleQuotes && value != null && value.startsWith("'") && value.endsWith("'")) { - cir.setReturnValue("\"" + value.substring(1, value.length() - 1) + "\""); - } - } @Override public Codec getCodec(Context cx) {