Skip to content

Commit

Permalink
Merge pull request #138 from A0000Xz/fabric
Browse files Browse the repository at this point in the history
添加了1.20.1版本对vanish模组的兼容性
  • Loading branch information
cnlimiter authored Feb 21, 2024
2 parents 3d0e64a + 007ac00 commit 773aad7
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 0 deletions.
130 changes: 130 additions & 0 deletions fabric/versions/1.20.1/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
@@ -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<String> 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) {
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
});
}
}



Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 773aad7

Please sign in to comment.