From c88e5302ad29badc075fea05f48002caa9807431 Mon Sep 17 00:00:00 2001 From: A0000Xz <122650088+A0000Xz@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:03:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0vanish=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mcbot/init/mixins/MixinPlayerList.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerList.java diff --git a/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerList.java b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerList.java new file mode 100644 index 00000000..b46cad98 --- /dev/null +++ b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/init/mixins/MixinPlayerList.java @@ -0,0 +1,66 @@ +package cn.evole.mods.mcbot.init.mixins; + + +import cn.evole.mods.mcbot.init.callbacks.IEvents; +import com.mojang.authlib.GameProfile; +import me.drex.vanish.util.VanishManager; +import net.minecraft.network.Connection; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.players.PlayerList; +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 java.util.UUID; +//#if MC >= 12002 +//$$ import net.minecraft.server.network.CommonListenerCookie; +//#endif + +/** + * Author cnlimiter + * CreateTime 2023/5/19 0:51 + * Name MixinPlayerList + * Description + */ + +@Mixin(value = PlayerList.class, priority = 1001) +public abstract class MixinPlayerList { + //#if MC >= 12002 + //$$ @Inject(method = "placeNewPlayer", at = @At(value = "TAIL")) + //$$ public void PlayerList_placeNewPlayer(Connection connection, ServerPlayer player, CommonListenerCookie commonListenerCookie, CallbackInfo ci) { + //$$ IEvents.PLAYER_LOGGED_IN.invoker().onPlayerLoggedIn(player.getCommandSenderWorld(), player); + //$$ } + //$$ @Inject(method = "remove", at = @At(value = "HEAD")) + //$$ public void PlayerList_remove(ServerPlayer player, CallbackInfo ci) { + //$$ IEvents.PLAYER_LOGGED_OUT.invoker().onPlayerLoggedOut(player.getCommandSenderWorld(), player); + //$$ } + //#else + @Inject(method = "placeNewPlayer", at = @At(value = "TAIL")) + public void PlayerList_placeNewPlayer(Connection connection, ServerPlayer player, CallbackInfo ci) { + if (!isPlayerVanished(player)) { + // 如果玩家不是隐身状态,则执行逻辑 + IEvents.PLAYER_LOGGED_IN.invoker().onPlayerLoggedIn(player.getCommandSenderWorld(), player); + } else { + // 如果玩家是隐身状态,直接返回不执行任何操作 + } + } + @Inject(method = "remove", at = @At(value = "HEAD")) + public void PlayerList_remove(ServerPlayer player, CallbackInfo ci) { + if (!isPlayerVanished(player)) { + // 如果玩家不是隐身状态,则执行逻辑 + IEvents.PLAYER_LOGGED_OUT.invoker().onPlayerLoggedOut(player.getCommandSenderWorld(), player); + } else { + // 如果玩家是隐身状态,直接返回不执行任何操作 + } + } + //#endif + private boolean isPlayerVanished(ServerPlayer player) { + // 获取玩家的 GameProfile + GameProfile gameProfile = player.getGameProfile(); + // 从 GameProfile 中获取 UUID + UUID uuid = gameProfile.getId(); + // 使用 VanishManager.isVanished 方法检查玩家是否处于隐身状态 + return VanishManager.isVanished(player.getServer(), uuid); + } +} \ No newline at end of file From 007ac0012bb6beea992a1b654e56b7347bcead25 Mon Sep 17 00:00:00 2001 From: A0000Xz <122650088+A0000Xz@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:04:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0vanish=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/evole/mods/mcbot/McBot.java | 130 ++++++++++++++++++ .../cn/evole/mods/mcbot/compat/ModCompat.java | 43 ++++++ 2 files changed, 173 insertions(+) create mode 100644 fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/McBot.java create mode 100644 fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/compat/ModCompat.java diff --git a/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/McBot.java b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/McBot.java new file mode 100644 index 00000000..ce4ad876 --- /dev/null +++ b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/McBot.java @@ -0,0 +1,130 @@ +package cn.evole.mods.mcbot; + +import cn.evole.mods.mcbot.data.UserBindApi; +import cn.evole.mods.mcbot.init.callbacks.IEvents; +import cn.evole.mods.mcbot.init.event.*; +import cn.evole.mods.mcbot.init.config.ModConfig; +import cn.evole.mods.mcbot.init.handler.CustomCmdHandler; +import cn.evole.mods.mcbot.util.locale.I18n; +import cn.evole.onebot.client.connection.ConnectFactory; +import cn.evole.onebot.client.core.Bot; +import cn.evole.onebot.client.factory.ListenerFactory; +import cn.evole.onebot.sdk.util.FileUtils; +import net.fabricmc.api.ModInitializer; +//#if MC >= 11900 +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +//#else +//import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +//#endif +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.server.MinecraftServer; + +import java.nio.file.Path; +import java.util.concurrent.LinkedBlockingQueue; +import cn.evole.mods.mcbot.compat.ModCompat; + +public class McBot implements ModInitializer { + + public static MinecraftServer SERVER = null; + public static Path CONFIG_FOLDER; + public static Path CONFIG_FILE; + + public static LinkedBlockingQueue blockingQueue; + public static ConnectFactory service; + public static ListenerFactory listenerFactory; + public static Bot bot; + public static Thread app; + + public static McBot INSTANCE = new McBot(); + + static { + // 初始化 ModCompat 类 + ModCompat.init(); + } + + + public MinecraftServer getServer() { + return SERVER; + } + + @Override + public void onInitialize() { + init(); + //#if MC >= 11900 + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> ICmdEvent.register(dispatcher)); + //#else + //CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> ICmdEvent.register(dispatcher)); + //#endif + + ServerLifecycleEvents.SERVER_STARTING.register(this::onServerStarting); + ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStarted); + ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping); + ServerLifecycleEvents.SERVER_STOPPED.register(this::onServerStopped); + + ServerTickEvents.END_SERVER_TICK.register(ITickEvent::register); + + IEvents.PLAYER_LOGGED_IN.register(IPlayerEvent::loggedIn); + IEvents.PLAYER_LOGGED_OUT.register(IPlayerEvent::loggedOut); + IEvents.PLAYER_ADVANCEMENT.register(IPlayerEvent::advancement); + IEvents.PLAYER_DEATH.register(IPlayerEvent::death); + + IEvents.SERVER_CHAT.register(IChatEvent::register); + } + + + public void init() { + CONFIG_FOLDER = Const.configDir.resolve("mcbot"); + FileUtils.checkFolder(CONFIG_FOLDER); + CONFIG_FILE = CONFIG_FOLDER.resolve("config.toml"); + I18n.init(); + UserBindApi.load(CONFIG_FOLDER); + Runtime.getRuntime().addShutdownHook(new Thread(McBot::killOutThreads)); + } + + public void onServerStarting(MinecraftServer server) { + SERVER = server;//获取服务器实例 + } + + public void onServerStarted(MinecraftServer server) { + blockingQueue = new LinkedBlockingQueue<>();//使用队列传输数据 + if (ModConfig.INSTANCE.getCommon().isAutoOpen()) { + try { + app = new Thread(() -> { + service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), blockingQueue);//创建websocket连接 + bot = service.ws.createBot();//创建机器人实例 + }, "BotServer"); + app.start(); + } catch (Exception e) { + Const.LOGGER.error("▌ §c机器人服务端未配置或未打开"); + } + } + listenerFactory = new ListenerFactory(blockingQueue);//创建事件分发器 + listenerFactory.start(); + CustomCmdHandler.INSTANCE.load();//自定义命令加载 + IBotEvent.init(listenerFactory);//事件监听 + } + + public void onServerStopping(MinecraftServer server) { + Const.isShutdown = true; + Const.LOGGER.info("▌ §c正在关闭群服互联 §a┈━═☆"); + UserBindApi.save(CONFIG_FOLDER); + CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空 + listenerFactory.stop();//分发器关闭 + service.stop(); + app.interrupt(); + } + + public void onServerStopped(MinecraftServer server) { + killOutThreads(); + } + + private static void killOutThreads() { + try { + listenerFactory.stop();//分发器关闭 + service.stop(); + app.interrupt(); + } catch (Exception ignored) { + } + } +} \ No newline at end of file diff --git a/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/compat/ModCompat.java b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/compat/ModCompat.java new file mode 100644 index 00000000..08155149 --- /dev/null +++ b/fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/compat/ModCompat.java @@ -0,0 +1,43 @@ +package cn.evole.mods.mcbot.compat; + +import cn.evole.mods.mcbot.init.event.IPlayerEvent; +import me.drex.vanish.api.VanishEvents; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.network.chat.Component; +import net.minecraft.world.level.Level; + +public class ModCompat { + public static final boolean VANISH = FabricLoader.getInstance().isModLoaded("melius-vanish"); + + public static void init() { + if (VANISH) { + initVanishEvents(); + } + } + + // 初始化 Vanish 事件监听器 + private static void initVanishEvents() { + // 注册事件监听器,当玩家解除隐身时调用 loggedIn 方法 + VanishEvents.UN_VANISH_MESSAGE_EVENT.register((serverPlayer) -> { + // 获取玩家所在的世界信息 + Level world = serverPlayer.getCommandSenderWorld(); + // 调用 loggedIn 方法 + IPlayerEvent.loggedIn(world, serverPlayer); + // 返回一个空的 Component + return Component.empty(); + }); + + // 注册事件监听器,当玩家隐身时调用 loggedOut 方法 + VanishEvents.VANISH_MESSAGE_EVENT.register((serverPlayer) -> { + // 获取玩家所在的世界信息 + Level world = serverPlayer.getCommandSenderWorld(); + // 调用 loggedOut 方法 + IPlayerEvent.loggedOut(world, serverPlayer); + // 返回一个空的 Component + return Component.empty(); + }); + } +} + + +