Skip to content

Commit

Permalink
feat(fabric): #143
Browse files Browse the repository at this point in the history
修复mixin错误
say mixin精简
  • Loading branch information
cnlimiter committed Mar 2, 2024
1 parent 8a8c859 commit 3026614
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Boolean> 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<Boolean> 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
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -30,42 +24,24 @@
* Description
*/

@Mixin(value = Commands.class)
@Mixin(value = SayCommand.class)
public abstract class MixinSystemCmd {
@Unique
private static void say_register(CommandDispatcher<CommandSourceStack> 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<Integer> 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 = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/commands/SayCommand;register(Lcom/mojang/brigadier/CommandDispatcher;)V"))
private void mcbot$sayRedirect(CommandDispatcher<CommandSourceStack> dispatcher) {
say_register(dispatcher);
}
}
}
//#endif
37 changes: 37 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/util/MCVersion.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
23 changes: 2 additions & 21 deletions fabric/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,7 @@ public class I18n {
public static void init() {
translations = new HashMap<>();

val version = getMcVersion().replace(".", "_");
val version = MCVersion.getMcVersion().replace(".", "_");

Optional<Path> optional = FabricLoader.getInstance().getModContainer("mcbot_" + version).orElseThrow(null)
.findPath("/lang/" + ModConfig.INSTANCE.getCommon().getLanguageSelect() + ".json");
Expand Down Expand Up @@ -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;
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3026614

Please sign in to comment.