From 8e97daee0cec978a27114c2530375350f07dfcd1 Mon Sep 17 00:00:00 2001 From: jchung01 Date: Tue, 25 Jun 2024 12:15:54 -0700 Subject: [PATCH] Improve copy seed tweak - Required on server and client - Changed to be default enabled --- .../universaltweaks/UniversalTweaks.java | 2 + .../config/UTConfigTweaks.java | 8 ++- .../universaltweaks/core/UTLoadingPlugin.java | 2 +- .../misc/commands/seed/UTCopyCommand.java | 64 +++++++++++++++++++ .../commands/seed/mixin/UTCopySeedMixin.java | 39 ----------- .../seed/mixin/UTFormatSeedMixin.java | 50 ++++++--------- .../universaltweaks/util/UTCommands.java | 12 ++++ .../mixins.tweaks.misc.commands.seed.json | 2 +- 8 files changed, 105 insertions(+), 74 deletions(-) create mode 100644 src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/UTCopyCommand.java delete mode 100644 src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTCopySeedMixin.java create mode 100644 src/main/java/mod/acgaming/universaltweaks/util/UTCommands.java diff --git a/src/main/java/mod/acgaming/universaltweaks/UniversalTweaks.java b/src/main/java/mod/acgaming/universaltweaks/UniversalTweaks.java index 99a5b84e..bd5116e1 100644 --- a/src/main/java/mod/acgaming/universaltweaks/UniversalTweaks.java +++ b/src/main/java/mod/acgaming/universaltweaks/UniversalTweaks.java @@ -35,6 +35,7 @@ import mod.acgaming.universaltweaks.tweaks.items.rarity.UTCustomRarity; import mod.acgaming.universaltweaks.tweaks.items.useduration.UTCustomUseDuration; import mod.acgaming.universaltweaks.tweaks.misc.armorcurve.UTArmorCurve; +import mod.acgaming.universaltweaks.util.UTCommands; import mod.acgaming.universaltweaks.tweaks.misc.endportal.UTEndPortalParallax; import mod.acgaming.universaltweaks.tweaks.misc.gui.lanserverproperties.UTLanServerProperties; import mod.acgaming.universaltweaks.tweaks.misc.incurablepotions.UTIncurablePotions; @@ -215,6 +216,7 @@ public void postInitClient(FMLPostInitializationEvent event) if (UTConfigTweaks.MISC.LOAD_SOUNDS.utLoadSoundMode != UTConfigTweaks.MiscCategory.LoadSoundsCategory.EnumSoundModes.NOTHING) UTLoadSound.initLists(); if (UTConfigTweaks.MISC.TOAST_CONTROL.utToastControlTutorialToggle) UTTutorialToast.utTutorialToast(); if (Loader.isModLoaded("botania")) UTBotaniaFancySkybox.initDimList(); + UTCommands.initClientCommands(); LOGGER.info(NAME + " client post-initialized"); } diff --git a/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java b/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java index 89a58e93..4d1b9559 100644 --- a/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java +++ b/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java @@ -1401,8 +1401,12 @@ public static class MiscCategory @Config.RequiresMcRestart @Config.Name("Copy World Seed") - @Config.Comment("Enables clicking of `/seed` world seed in chat to copy to clipboard") - public boolean utCopyWorldSeedToggle = false; + @Config.Comment + ({ + "Enables clicking of `/seed` world seed in chat to copy to clipboard", + "Required on server AND client" + }) + public boolean utCopyWorldSeedToggle = true; @Config.Name("Damage Tilt") @Config.Comment("Restores feature to tilt the camera when damaged") diff --git a/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java b/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java index f81d10de..1cdc04e8 100644 --- a/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java +++ b/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java @@ -128,6 +128,7 @@ public class UTLoadingPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader put("mixins.tweaks.misc.advancements.json", () -> UTConfigTweaks.MISC.utDisableAdvancementsToggle); put("mixins.tweaks.misc.armorcurve.json", () -> UTConfigTweaks.MISC.ARMOR_CURVE.utArmorCurveToggle); put("mixins.tweaks.misc.bannerlayers.json", () -> UTConfigTweaks.MISC.utBannerLayers != 6); + put("mixins.tweaks.misc.commands.seed.json", () -> UTConfigTweaks.MISC.utCopyWorldSeedToggle); put("mixins.tweaks.misc.console.addpacket.json", () -> UTConfigTweaks.MISC.utImprovedEntityTrackerToggle); put("mixins.tweaks.misc.incurablepotions.json", () -> UTConfigTweaks.MISC.INCURABLE_POTIONS.utIncurablePotionsToggle); put("mixins.tweaks.misc.lightning.damage.json", () -> UTConfigTweaks.MISC.LIGHTNING.utLightningDamage != 5.0D || UTConfigTweaks.MISC.LIGHTNING.utLightningFireTicks != 8); @@ -184,7 +185,6 @@ public class UTLoadingPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader put("mixins.tweaks.misc.chat.compactmessage.json", () -> UTConfigTweaks.MISC.CHAT.utCompactMessagesToggle); put("mixins.tweaks.misc.chat.keepsentmessages.json", () -> UTConfigTweaks.MISC.CHAT.utKeepSentMessageHistory); put("mixins.tweaks.misc.chat.maximumlines.json", () -> UTConfigTweaks.MISC.CHAT.utChatLines != 100); - put("mixins.tweaks.misc.commands.seed.json", () -> UTConfigTweaks.MISC.utCopyWorldSeedToggle); put("mixins.tweaks.misc.credits.json", () -> UTConfigTweaks.MISC.utSkipCreditsToggle); put("mixins.tweaks.misc.glint.enchantedbook.json", () -> UTConfigTweaks.MISC.utDisableEnchantmentBookGlint); put("mixins.tweaks.misc.glint.potion.json", () -> UTConfigTweaks.MISC.utDisablePotionGlint); diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/UTCopyCommand.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/UTCopyCommand.java new file mode 100644 index 00000000..fbf48eed --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/UTCopyCommand.java @@ -0,0 +1,64 @@ +package mod.acgaming.universaltweaks.tweaks.misc.commands.seed; + +import java.util.Arrays; +import javax.annotation.Nonnull; +import mod.acgaming.universaltweaks.UniversalTweaks; +import mod.acgaming.universaltweaks.config.UTConfigGeneral; +import mod.acgaming.universaltweaks.config.UTConfigTweaks; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.text.TextComponentString; +import net.minecraftforge.client.IClientCommand; + +public class UTCopyCommand extends CommandBase implements IClientCommand +{ + public static final String copyCommandBase = "/universalTweaksCopy "; + + @Nonnull + @Override + public String getName() + { + return copyCommandBase.substring(1).trim(); + } + + @Nonnull + @Override + public String getUsage(@Nonnull ICommandSender sender) + { + return copyCommandBase + ""; + } + + @Override + public boolean checkPermission(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender) + { + return true; + } + + @Override + public void execute(@Nonnull MinecraftServer server, ICommandSender sender, @Nonnull String[] args) throws CommandException + { + // Sanity check + if (!sender.getEntityWorld().isRemote) return; + if (!UTConfigTweaks.MISC.utCopyWorldSeedToggle) + { + sender.sendMessage(new TextComponentString("\nCopying the seed is disabled on client-side, please enable in 'Universal Tweaks - Tweaks' -> 'Misc' -> 'Copy World Seed'.")); + return; + } + if (args.length < 1) + { + UniversalTweaks.LOGGER.warn("UTCopyCommand :: Malformed input! " + Arrays.toString(args)); + } + if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTCopyCommand :: Copy seed"); + GuiScreen.setClipboardString(buildString(args, 0)); + sender.sendMessage(new TextComponentString("\nCopied seed to clipboard!")); + } + + @Override + public boolean allowUsageWithoutPrefix(ICommandSender sender, String message) + { + return false; + } +} diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTCopySeedMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTCopySeedMixin.java deleted file mode 100644 index 05172ea7..00000000 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTCopySeedMixin.java +++ /dev/null @@ -1,39 +0,0 @@ -package mod.acgaming.universaltweaks.tweaks.misc.commands.seed.mixin; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.util.text.ITextComponent; -import net.minecraft.util.text.TextComponentString; -import net.minecraft.util.text.event.ClickEvent; - -import mod.acgaming.universaltweaks.UniversalTweaks; -import mod.acgaming.universaltweaks.config.UTConfigGeneral; -import mod.acgaming.universaltweaks.config.UTConfigTweaks; -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; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -// Courtesy of jchung01 -@Mixin(GuiScreen.class) -public abstract class UTCopySeedMixin -{ - @Shadow - public Minecraft mc; - - @Inject(method = "handleComponentClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiScreen;sendChatMessage(Ljava/lang/String;Z)V"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD) - private void utCopySeed(ITextComponent component, CallbackInfoReturnable cir, ClickEvent clickevent) - { - if (!UTConfigTweaks.MISC.utCopyWorldSeedToggle) return; - - String[] splitCommand = clickevent.getValue().split(" "); - if (!splitCommand[0].equals("/utCopySeed")) return; - if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTCopySeedMixin :: Copy seed"); - - GuiScreen.setClipboardString(splitCommand[1]); - mc.player.sendMessage(new TextComponentString("Copied seed to clipboard!")); - cir.setReturnValue(Boolean.TRUE); - } -} \ No newline at end of file diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTFormatSeedMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTFormatSeedMixin.java index 6f4a26e4..4f56ac21 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTFormatSeedMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/commands/seed/mixin/UTFormatSeedMixin.java @@ -1,50 +1,38 @@ package mod.acgaming.universaltweaks.tweaks.misc.commands.seed.mixin; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import mod.acgaming.universaltweaks.UniversalTweaks; +import mod.acgaming.universaltweaks.config.UTConfigGeneral; +import mod.acgaming.universaltweaks.config.UTConfigTweaks; +import mod.acgaming.universaltweaks.tweaks.misc.commands.seed.UTCopyCommand; import net.minecraft.command.CommandShowSeed; -import net.minecraft.command.ICommandSender; -import net.minecraft.server.MinecraftServer; import net.minecraft.util.text.Style; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.event.ClickEvent; import net.minecraft.util.text.event.HoverEvent; -import net.minecraft.world.World; - -import mod.acgaming.universaltweaks.UniversalTweaks; -import mod.acgaming.universaltweaks.config.UTConfigGeneral; -import mod.acgaming.universaltweaks.config.UTConfigTweaks; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; // Courtesy of jchung01 @Mixin(CommandShowSeed.class) public class UTFormatSeedMixin { - // Can probably also use @ModifyArg + MixinExtras' @Local, but in beta/subject to change - @Inject(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/command/ICommandSender;sendMessage(Lnet/minecraft/util/text/ITextComponent;)V"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD) - private void utFormatSeedCommand(MinecraftServer server, ICommandSender sender, String[] args, CallbackInfo ci, World world) + @ModifyExpressionValue(method = "execute", at = @At(value = "NEW", target = "(Ljava/lang/String;[Ljava/lang/Object;)Lnet/minecraft/util/text/TextComponentTranslation;")) + private TextComponentTranslation utFormatSeedMessage(TextComponentTranslation message) { - if (!UTConfigTweaks.MISC.utCopyWorldSeedToggle) return; - if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTFormatSeedMixin :: Format seed"); - - long seed = world.getSeed(); - TextComponentTranslation seedText = new TextComponentTranslation("commands.seed.success", seed); - - // format seed - Style style = new Style(); - style.setColor(TextFormatting.GREEN).setUnderlined(true); - - // make & set events - style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("Click to copy seed to clipboard"))); - // use a dummy command that UTCopySeedMixin will handle - style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/utCopySeed " + seed)); - seedText.setStyle(style); - - sender.sendMessage(seedText); - ci.cancel(); + if (UTConfigTweaks.MISC.utCopyWorldSeedToggle) + { + if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTFormatSeedMixin :: Format seed"); + // Format seed + Style style = new Style(); + style.setColor(TextFormatting.GREEN).setUnderlined(true); + // Make & set events + style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("Click to copy seed to clipboard"))); + style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, UTCopyCommand.copyCommandBase + message.getFormatArgs()[0])); + message.setStyle(style); + } + return message; } } \ No newline at end of file diff --git a/src/main/java/mod/acgaming/universaltweaks/util/UTCommands.java b/src/main/java/mod/acgaming/universaltweaks/util/UTCommands.java new file mode 100644 index 00000000..cd6c854a --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/util/UTCommands.java @@ -0,0 +1,12 @@ +package mod.acgaming.universaltweaks.util; + +import mod.acgaming.universaltweaks.tweaks.misc.commands.seed.UTCopyCommand; +import net.minecraftforge.client.ClientCommandHandler; + +public class UTCommands +{ + public static void initClientCommands() + { + ClientCommandHandler.instance.registerCommand(new UTCopyCommand()); + } +} diff --git a/src/main/resources/mixins.tweaks.misc.commands.seed.json b/src/main/resources/mixins.tweaks.misc.commands.seed.json index 50ebd9d4..c32aade6 100644 --- a/src/main/resources/mixins.tweaks.misc.commands.seed.json +++ b/src/main/resources/mixins.tweaks.misc.commands.seed.json @@ -3,5 +3,5 @@ "refmap": "universaltweaks.refmap.json", "minVersion": "0.8", "compatibilityLevel": "JAVA_8", - "client": ["UTCopySeedMixin", "UTFormatSeedMixin"] + "mixins": ["UTFormatSeedMixin"] } \ No newline at end of file