diff --git a/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerAdvancements.java b/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerAdvancements.java index 65a354f..4c44258 100644 --- a/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerAdvancements.java +++ b/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerAdvancements.java @@ -10,8 +10,13 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +//#if MC >= 11202 +//$$ import net.minecraft.advancements.AdvancementHolder; +//#endif + //兼容vanish import cn.evole.mods.mcbot.init.compat.vanish.VanishAPI; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; /** * Author cnlimiter @@ -24,16 +29,23 @@ public abstract class MixinPlayerAdvancements { @Shadow private ServerPlayer player; - //#if MC < 11900 - @Inject(method = "award", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;Ljava/util/UUID;)V", shift = At.Shift.AFTER)) + @Inject(method = "award", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILEXCEPTION) + + //#if MC >= 11202 + //$$ public void mcbot$award(AdvancementHolder advancement, String string, CallbackInfoReturnable cir) { //#else - //$$ @Inject(method = "award", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Z)V", shift = At.Shift.AFTER)) - //#endif public void mcbot$award(Advancement advancement, String string, CallbackInfoReturnable cir) { + //#endif + + ServerPlayer player = this.player; if (VanishAPI.isVanished(player)) return; + //#if MC >= 11202 + //$$ IEvents.PLAYER_ADVANCEMENT.invoker().onAdvancement(player, advancement.value()); + //#else IEvents.PLAYER_ADVANCEMENT.invoker().onAdvancement(player, advancement); + //#endif } } diff --git a/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java b/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java index 18d4467..49b78b3 100644 --- a/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java +++ b/fabric/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java @@ -1,25 +1,19 @@ -//#if MC < 11800 package cn.evole.mods.mcbot.init.mixins; import cn.evole.mods.mcbot.Const; -import cn.evole.mods.mcbot.McBot; import cn.evole.mods.mcbot.init.config.ModConfig; -import com.mojang.brigadier.CommandDispatcher; +import cn.evole.mods.mcbot.util.MCVersion; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import lombok.val; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.Util; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; import net.minecraft.commands.arguments.MessageArgument; -import net.minecraft.network.chat.ChatType; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.TextComponent; -import net.minecraft.server.players.PlayerList; -import net.minecraft.world.entity.Entity; +import net.minecraft.server.commands.SayCommand; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.nio.charset.StandardCharsets; @@ -30,42 +24,24 @@ * Description */ -@Mixin(value = Commands.class) +@Mixin(value = SayCommand.class) public abstract class MixinSystemCmd { - @Unique - private static void say_register(CommandDispatcher dispatcher) { - dispatcher.register((Commands.literal("say").requires((CommandSourceStack) -> CommandSourceStack.hasPermission(2))).then(Commands.argument("message", MessageArgument.message()).executes((commandContext) -> { + @Inject(method = "method_13563", at = @At(value = "RETURN")) + private static void mcbot$say(CommandContext commandContext, CallbackInfoReturnable cir) throws CommandSyntaxException { + val version = MCVersion.getMcVersion().replace(".", "_"); + if (FabricLoader.getInstance().isModLoaded("mcbot_" + version) + && ModConfig.INSTANCE != null + && ModConfig.INSTANCE.getStatus().isSChatEnable() + && ModConfig.INSTANCE.getStatus().isSEnable() + && ModConfig.INSTANCE.getCmd().isMcPrefixOn()) { Component component = MessageArgument.getMessage(commandContext, "message"); - Component component2 = new TextComponent(String.format("[%s] %s", commandContext.getSource().getDisplayName().getString(), component.getString())); - Entity entity = commandContext.getSource().getEntity(); - - - if (FabricLoader.getInstance().isModLoaded("mcbot") - && ModConfig.INSTANCE != null - && ModConfig.INSTANCE.getStatus().isSChatEnable() - && ModConfig.INSTANCE.getStatus().isSEnable() - && ModConfig.INSTANCE.getCmd().isMcPrefixOn()) { - if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGuildMsg(msg); - } else { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGroupMsg(msg); - } - } - if (entity != null) { - commandContext.getSource().getServer().getPlayerList().broadcastMessage(component2, ChatType.SYSTEM, Util.NIL_UUID); + if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) { + val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); + Const.sendGuildMsg(msg); } else { - commandContext.getSource().getServer().getPlayerList().broadcastMessage(component2, ChatType.SYSTEM, Util.NIL_UUID); + val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); + Const.sendGroupMsg(msg); } - - return 1; - }))); - } - - @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/commands/SayCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V")) - private void mcbot$sayRedirect(CommandDispatcher dispatcher) { - say_register(dispatcher); + } } } -//#endif diff --git a/fabric/src/main/java/cn/evole/mods/mcbot/util/MCVersion.java b/fabric/src/main/java/cn/evole/mods/mcbot/util/MCVersion.java new file mode 100644 index 0000000..fc44b68 --- /dev/null +++ b/fabric/src/main/java/cn/evole/mods/mcbot/util/MCVersion.java @@ -0,0 +1,37 @@ +package cn.evole.mods.mcbot.util; + +import org.jetbrains.annotations.NotNull; + +/** + * MCVersion + * + * @author cnlimiter + * @version 1.0 + * @description 获取minecraft的版本 + * @date 2024/3/2 23:23 + */ +public class MCVersion { + + @NotNull + public static String getMcVersion() { + try { + // Fabric + return (String) Reflection.clazz("net.fabricmc.loader.impl.FabricLoaderImpl") + .get("INSTANCE") + .get("getGameProvider()") + .get("getNormalizedGameVersion()").get(); + } catch (Exception ignored) { + + } + try { + // Quilt + return (String) Reflection.clazz("org.quiltmc.loader.impl.QuiltLoaderImpl") + .get("INSTANCE") + .get("getGameProvider()") + .get("getNormalizedGameVersion()").get(); + } catch (Exception ignored) { + + } + return "1.16.5"; + } +} diff --git a/fabric/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java b/fabric/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java index f5b2b22..3e56395 100644 --- a/fabric/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java +++ b/fabric/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java @@ -2,6 +2,7 @@ import cn.evole.mods.mcbot.Const; import cn.evole.mods.mcbot.init.config.ModConfig; +import cn.evole.mods.mcbot.util.MCVersion; import cn.evole.mods.mcbot.util.Reflection; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -26,7 +27,7 @@ public class I18n { public static void init() { translations = new HashMap<>(); - val version = getMcVersion().replace(".", "_"); + val version = MCVersion.getMcVersion().replace(".", "_"); Optional optional = FabricLoader.getInstance().getModContainer("mcbot_" + version).orElseThrow(null) .findPath("/lang/" + ModConfig.INSTANCE.getCommon().getLanguageSelect() + ".json"); @@ -85,25 +86,5 @@ public static String get(String key) { } } - private static String getMcVersion() { - try { - // Fabric - return (String) Reflection.clazz("net.fabricmc.loader.impl.FabricLoaderImpl") - .get("INSTANCE") - .get("getGameProvider()") - .get("getNormalizedGameVersion()").get(); - } catch (Exception ignored) { - - } - try { - // Quilt - return (String) Reflection.clazz("org.quiltmc.loader.impl.QuiltLoaderImpl") - .get("INSTANCE") - .get("getGameProvider()") - .get("getNormalizedGameVersion()").get(); - } catch (Exception ignored) { - } - return null; - } } diff --git a/fabric/versions/1.18.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java b/fabric/versions/1.18.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java deleted file mode 100644 index c701dd9..0000000 --- a/fabric/versions/1.18.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java +++ /dev/null @@ -1,70 +0,0 @@ -//#if MC >= 11600 -package cn.evole.mods.mcbot.init.mixins; - -import cn.evole.mods.mcbot.Const; -import cn.evole.mods.mcbot.McBot; -import cn.evole.mods.mcbot.init.config.ModConfig; -import com.mojang.brigadier.CommandDispatcher; -import lombok.val; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.Util; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.commands.arguments.MessageArgument; -import net.minecraft.network.chat.ChatType; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.TextComponent; -import net.minecraft.server.players.PlayerList; -import net.minecraft.world.entity.Entity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import java.nio.charset.StandardCharsets; - -/** - * Author cnlimiter - * CreateTime 2023/5/19 0:54 - * Name MixinSystemCmd - * Description - */ -@Mixin(value = Commands.class) -public abstract class MixinSystemCmd { - @Unique - private static void say_register(CommandDispatcher dispatcher) { - dispatcher.register((Commands.literal("say").requires((CommandSourceStack) -> CommandSourceStack.hasPermission(2))).then(Commands.argument("message", MessageArgument.message()).executes((commandContext) -> { - Component component = MessageArgument.getMessage(commandContext, "message"); - Component component2 = new TextComponent(String.format("[%s] %s", commandContext.getSource().getDisplayName().getString(), component.getString())); - Entity entity = commandContext.getSource().getEntity(); - - - if (FabricLoader.getInstance().isModLoaded("mcbot") - && ModConfig.INSTANCE != null - && ModConfig.INSTANCE.getStatus().isSChatEnable() - && ModConfig.INSTANCE.getStatus().isSEnable() - && ModConfig.INSTANCE.getCmd().isMcPrefixOn()) { - if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGuildMsg(msg); - } else { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(component.getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGroupMsg(msg); - } - } - if (entity != null) { - commandContext.getSource().getServer().getPlayerList().broadcastMessage(component2, ChatType.CHAT, entity.getUUID()); - } else { - commandContext.getSource().getServer().getPlayerList().broadcastMessage(component2, ChatType.SYSTEM, Util.NIL_UUID); - } - - return 1; - }))); - } - - @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/commands/SayCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V")) - private void mcbot$sayRedirect(CommandDispatcher dispatcher) { - say_register(dispatcher); - } -} -//#endif diff --git a/fabric/versions/1.19.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java b/fabric/versions/1.19.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java deleted file mode 100644 index a0cf920..0000000 --- a/fabric/versions/1.19.2/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java +++ /dev/null @@ -1,64 +0,0 @@ -//#if MC >= 11900 -package cn.evole.mods.mcbot.init.mixins; - -import cn.evole.mods.mcbot.Const; -import cn.evole.mods.mcbot.McBot; -import cn.evole.mods.mcbot.init.config.ModConfig; -import com.mojang.brigadier.CommandDispatcher; -import lombok.val; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.commands.arguments.MessageArgument; -import net.minecraft.network.chat.ChatType; -import net.minecraft.server.players.PlayerList; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import java.nio.charset.StandardCharsets; - -/** - * Author cnlimiter - * CreateTime 2023/5/19 0:54 - * Name MixinSystemCmd - * Description - */ -@Mixin(value = Commands.class) -public abstract class MixinSystemCmd { - @Unique - private static void say_register(CommandDispatcher dispatcher) { - dispatcher.register((Commands.literal("say").requires((CommandSourceStack) -> CommandSourceStack.hasPermission(2))).then(Commands.argument("message", MessageArgument.message()).executes((commandContext) -> { - MessageArgument.ChatMessage chatMessage = MessageArgument.getChatMessage(commandContext, "message"); - CommandSourceStack commandSourceStack = commandContext.getSource(); - PlayerList playerList = commandSourceStack.getServer().getPlayerList(); - chatMessage.resolve(commandSourceStack, (playerChatMessage) -> { - - if (FabricLoader.getInstance().isModLoaded("mcbot") - && ModConfig.INSTANCE != null - && ModConfig.INSTANCE.getStatus().isSChatEnable() - && ModConfig.INSTANCE.getStatus().isSEnable() - && ModConfig.INSTANCE.getCmd().isMcPrefixOn()) { - if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(playerChatMessage.serverContent().getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGuildMsg(msg); - } else { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(playerChatMessage.serverContent().getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGroupMsg(msg); - } - } - playerList.broadcastChatMessage(playerChatMessage, commandSourceStack, ChatType.bind(ChatType.SAY_COMMAND, commandSourceStack)); - }); - - - return 1; - }))); - } - - @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/commands/SayCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V")) - private void mcbot$sayRedirect(CommandDispatcher dispatcher) { - say_register(dispatcher); - } -} -//#endif diff --git a/fabric/versions/1.19.3/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java b/fabric/versions/1.19.3/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java deleted file mode 100644 index 6599c0d..0000000 --- a/fabric/versions/1.19.3/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinSystemCmd.java +++ /dev/null @@ -1,61 +0,0 @@ -//#if MC >= 11903 -package cn.evole.mods.mcbot.init.mixins; - -import cn.evole.mods.mcbot.Const; -import cn.evole.mods.mcbot.init.config.ModConfig; -import com.mojang.brigadier.CommandDispatcher; -import lombok.val; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.commands.arguments.MessageArgument; -import net.minecraft.network.chat.ChatType; -import net.minecraft.server.players.PlayerList; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import java.nio.charset.StandardCharsets; - -/** - * Author cnlimiter - * CreateTime 2023/5/19 0:54 - * Name MixinSystemCmd - * Description - */ -@Mixin(value = Commands.class) -public abstract class MixinSystemCmd { - @Unique - private static void say_register(CommandDispatcher dispatcher) { - dispatcher.register((Commands.literal("say").requires((CommandSourceStack) -> CommandSourceStack.hasPermission(2))).then(Commands.argument("message", MessageArgument.message()).executes((commandContext) -> { - MessageArgument.resolveChatMessage(commandContext, "message", (playerChatMessage) -> { - CommandSourceStack CommandSourceStack = commandContext.getSource(); - PlayerList playerList = CommandSourceStack.getServer().getPlayerList(); - ///////////////////////// - if (FabricLoader.getInstance().isModLoaded("mcbot") - && ModConfig.INSTANCE != null - && ModConfig.INSTANCE.getStatus().isSChatEnable() - && ModConfig.INSTANCE.getStatus().isSEnable() - && ModConfig.INSTANCE.getCmd().isMcPrefixOn()) { - if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(playerChatMessage.decoratedContent().getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGuildMsg(msg); - } else { - val msg = String.format("[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "] %s", new String(playerChatMessage.decoratedContent().getString().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); - Const.sendGroupMsg(msg); - } - } - ///////////////////////// - playerList.broadcastChatMessage(playerChatMessage, CommandSourceStack, ChatType.bind(ChatType.SAY_COMMAND, CommandSourceStack)); - }); - return 1; - }))); - } - - @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/commands/SayCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V")) - private void mcbot$sayRedirect(CommandDispatcher dispatcher) { - say_register(dispatcher); - } -} -//#endif