Skip to content

Commit

Permalink
refactor(fabric): vanish 全版本适配
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlimiter committed Mar 1, 2024
1 parent 89eeabe commit 9ba5a1f
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 188 deletions.
2 changes: 1 addition & 1 deletion fabric/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
implementation include ("cn.evole.bot:OneBot-Client:${project.onebot_client_version}"){transitive = false}
implementation include ("cn.evole.config:AtomConfig-Toml:${project.toml_version}"){transitive = false}
if (mcVersion == 11605) include ("org.slf4j:slf4j-api:2.0.7")
if (mcVersion == 12001) modCompileOnly ("maven.modrinth:vanish:1.5.2-1.20.1")
modCompileOnly ("maven.modrinth:vanish:1.5.2-1.20.1")
compileOnly ("org.projectlombok:lombok:1.18.24")
annotationProcessor ("org.projectlombok:lombok:1.18.24")

Expand Down
2 changes: 1 addition & 1 deletion fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ archives_base_name=McBot-fabric
# Wrapper Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
loader_version=0.14.21
loader_version=0.15.7
fabric_version=0.83.1+1.20.1
onebot_client_version=0.3.9.1
toml_version=0.1.5
27 changes: 8 additions & 19 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
//#endif

//兼容1.20.1版本vanish
//#if MC == 12001
//$$ import cn.evole.mods.mcbot.init.compat.VanishCompat;
//#endif
//兼容vanish
import cn.evole.mods.mcbot.init.compat.vanish.VanishCompat;



Expand All @@ -45,7 +43,6 @@ public class McBot implements ModInitializer {
public static ConnectFactory service;
public static ListenerFactory listenerFactory;
public static Bot bot;
public static Thread app;

public static McBot INSTANCE = new McBot();

Expand All @@ -72,14 +69,9 @@ public void onInitialize() {
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);


//#if MC == 12001
//$$ VanishCompat.init();
//#endif

VanishCompat.init();
}


Expand All @@ -90,7 +82,7 @@ public void init() {
I18n.init();
UserBindApi.load(CONFIG_FOLDER);
ChatRecordApi.load(CONFIG_FOLDER);
Runtime.getRuntime().addShutdownHook(new Thread(McBot::killOutThreads));
//Runtime.getRuntime().addShutdownHook(new Thread(McBot::killOutThreads));
}

public void onServerStarting(MinecraftServer server) {
Expand All @@ -101,11 +93,8 @@ 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();
service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), blockingQueue);//创建websocket连接
bot = service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("▌ §c机器人服务端未配置或未打开");
}
Expand All @@ -126,7 +115,6 @@ public void onServerStopping(MinecraftServer server) {
CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空
listenerFactory.stop();//分发器关闭
service.stop();
app.interrupt();
messageThread.stop();
CQUtilsExecutor.shutdownNow();
}
Expand All @@ -139,7 +127,8 @@ private static void killOutThreads() {
try {
listenerFactory.stop();//分发器关闭
service.stop();
app.interrupt();
messageThread.stop();
CQUtilsExecutor.shutdownNow();
} catch (Exception ignored) {
}
}
Expand Down
46 changes: 18 additions & 28 deletions fabric/src/main/java/cn/evole/mods/mcbot/cmds/CmdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.GuildMessageEvent;

//兼容1.20.1版本vanish
//#if MC == 12001
//$$ import cn.evole.mods.mcbot.init.compat.VanishAPI;
//#endif
//兼容vanish
import cn.evole.mods.mcbot.init.compat.vanish.VanishAPI;

/**
* Description:
Expand All @@ -19,55 +17,47 @@
* Version: 1.0
*/
public class CmdApi {
private static StringBuilder CmdMain(String cmd, boolean isOp, boolean vanishSupport) {
private static String CmdMain(String cmd, boolean isOp, boolean vanishSupport) {
StringBuilder result = new StringBuilder();
//#if MC == 12001
//$$ if (!vanishSupport) {
//$$ VanishAPI.performPrefixedCommand(McBot.SERVER.getCommands(), isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);
//$$ }
//#else
//#if MC >= 11900
//$$ McBot.SERVER.getCommands().performPrefixedCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);//优雅
//#else
McBot.SERVER.getCommands().performCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);
//#endif
//#endif
if (!vanishSupport) {
VanishAPI.performPrefixedCommand(McBot.SERVER.getCommands(), isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);
}
for (String s : (isOp ? BotCmdRun.OP.outPut : BotCmdRun.CUSTOM.outPut)) {
result.append(s.replaceAll(\\S", "")).append("\n");
}
if (isOp) BotCmdRun.OP.outPut.clear();
else BotCmdRun.CUSTOM.outPut.clear();
return result;
return result.toString();
}

private static void GroupCmd(long groupId, String cmd, boolean isOp, boolean OPEscape) {
Const.groupMsg(groupId, CmdMain(cmd, isOp, OPEscape).toString());
Const.groupMsg(groupId, CmdMain(cmd, isOp, OPEscape));
}

private static void GuildCmd(String guildId, String channelId, String cmd, boolean isOp, boolean OPEscape) {
Const.guildMsg(guildId, channelId, CmdMain(cmd, isOp, OPEscape).toString());
Const.guildMsg(guildId, channelId, CmdMain(cmd, isOp, OPEscape));
}

public static void invokeCommandGroup(GroupMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀
String origincommand = command;
String originCommand = command;
command = BotUtils.cmdParse(command);
String performedcommand = command;
String performedCommand = command;

if (performedcommand.equals("list")) {
if (performedCommand.equals("list")) {
// 如果指令包含list,则强行以非管理员身份执行
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), false, customCmd.isVanishSupport()));
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedCommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, originCommand), false, customCmd.isVanishSupport()));
}else{
if (BotUtils.groupAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), true, customCmd.isVanishSupport()));//admin
.filter(customCmd -> performedCommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, originCommand), true, customCmd.isVanishSupport()));//admin
} else
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), false, customCmd.isVanishSupport()));
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedCommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, originCommand), false, customCmd.isVanishSupport()));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public static int cqhttpExecute(CommandContext<CommandSourceStack> context) thro
ModConfig.INSTANCE.getBotConfig().setMiraiHttp(false);

try {
McBot.app = new Thread(() -> {
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.ws.createBot();//创建机器人实例
}, "BotServer");
McBot.app.start();
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand Down Expand Up @@ -77,11 +74,8 @@ public static int miraiExecute(CommandContext<CommandSourceStack> context) throw
//#endif
ModConfig.INSTANCE.getBotConfig().setMiraiHttp(true);
try {
McBot.app = new Thread(() -> {
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.ws.createBot();//创建机器人实例
}, "BotServer");
McBot.app.start();
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand Down Expand Up @@ -112,11 +106,8 @@ public static int cqhttpCommonExecute(CommandContext<CommandSourceStack> context
//#endif
ModConfig.INSTANCE.getBotConfig().setMiraiHttp(false);
try {
McBot.app = new Thread(() -> {
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.ws.createBot();//创建机器人实例
}, "BotServer");
McBot.app.start();
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand All @@ -137,11 +128,8 @@ public static int miraiCommonExecute(CommandContext<CommandSourceStack> context)
//#endif
ModConfig.INSTANCE.getBotConfig().setMiraiHttp(true);
try {
McBot.app = new Thread(() -> {
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.ws.createBot();//创建机器人实例
}, "BotServer");
McBot.app.start();
McBot.service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), McBot.blockingQueue);//创建websocket连接
McBot.bot = McBot.service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//#if MC == 12001
package cn.evole.mods.mcbot.init.compat;
package cn.evole.mods.mcbot.init.compat.vanish;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -55,4 +54,3 @@ public static String getSelfPlayer() {
return "";
}
}
//#endif
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
//#if MC == 12001
package cn.evole.mods.mcbot.init.compat;
package cn.evole.mods.mcbot.init.compat.vanish;

import me.drex.vanish.util.VanishManager;
import com.mojang.authlib.GameProfile;
import me.drex.vanish.util.VanishManager;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

import java.util.HashMap;
import java.util.UUID;

public class VanishAPI {
public static boolean isVanished(ServerPlayer player) {
public static boolean isVanished(ServerPlayer player){
if(!VanishCompat.VANISH) return false;
// 获取玩家的 GameProfile
GameProfile gameProfile = player.getGameProfile();
// 从 GameProfile 中获取 UUID
UUID uuid = gameProfile.getId();
// 使用 VanishManager.isVanished 方法检查玩家是否处于隐身状态
return VanishManager.isVanished(player.getServer(), uuid);
}

public static boolean isVanished(Player player) {
if(!VanishCompat.VANISH) return false;
// 获取玩家的 GameProfile
GameProfile gameProfile = player.getGameProfile();
// 从 GameProfile 中获取 UUID
UUID uuid = gameProfile.getId();
// 使用 VanishManager.isVanished 方法检查玩家是否处于隐身状态
return VanishManager.isVanished(player.getServer(), uuid);
}

Expand All @@ -38,13 +26,16 @@ public static boolean isVanished(Player player) {
* @param commands MinecraftServer.getCommands()
* @param sender BotCmdRun.CUSTOM / BotCmdRun.OP
* @param cmd 指令
* @return 同原版指令执行返回的值
*/
public static int performPrefixedCommand(Commands commands, CommandSourceStack sender, String cmd) {
public static void performPrefixedCommand(Commands commands, CommandSourceStack sender, String cmd) {
String targetCmd = cmd.length() > Short.MAX_VALUE ? cmd.substring(0, Short.MAX_VALUE) : cmd; // 截断字符串以防管理不当人(?

if (targetCmd.indexOf('@') == -1)
return commands.performPrefixedCommand(sender, cmd);
//#if MC < 11900
commands.performCommand(sender, cmd);
//#else
//$$ commands.performPrefixedCommand(sender, cmd);
//#endif

HashMap<String, String> cache = new HashMap<>();

Expand All @@ -63,7 +54,10 @@ public static int performPrefixedCommand(Commands commands, CommandSourceStack s
}
}
}
return commands.performPrefixedCommand(sender, targetCmd);
//#if MC < 11900
commands.performCommand(sender, targetCmd);
//#else
//$$ commands.performPrefixedCommand(sender, targetCmd);
//#endif
}
}
//#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cn.evole.mods.mcbot.init.compat.vanish;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.init.event.IPlayerEvent;
import lombok.Getter;
import me.drex.vanish.api.VanishEvents;
import net.minecraft.network.chat.Component;
//#if MC <11900
import net.minecraft.network.chat.TextComponent;
//#endif
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;

import java.util.ArrayList;
import java.util.List;

public class VanishCompat {
public static final boolean VANISH = Const.isLoad("melius-vanish");

@Getter
public static List<ServerPlayer> vanishedPlayers = new ArrayList<>(); // 维护一个列表,包含全部隐身的玩家

public static void init() {
if (VANISH) {
initVanishEvents();
}
}

// 初始化 Vanish 事件监听器
private static void initVanishEvents() {
// 注册事件监听器,当玩家解除隐身时调用 loggedIn 方法
VanishEvents.UN_VANISH_MESSAGE_EVENT.register((serverPlayer) -> {
// 获取玩家所在的世界信息
Level world = serverPlayer.getCommandSenderWorld();
// 维护列表
vanishedPlayers.remove(serverPlayer);
// 调用 loggedIn 方法
IPlayerEvent.loggedIn(world, serverPlayer);
// 返回一个空的 Component
//#if MC < 11900
return new TextComponent("");
//#else
//$$ return Component.empty();
//#endif
});

// 注册事件监听器,当玩家隐身时调用 loggedOut 方法
VanishEvents.VANISH_MESSAGE_EVENT.register((serverPlayer) -> {
// 获取玩家所在的世界信息
Level world = serverPlayer.getCommandSenderWorld();
// 维护列表
vanishedPlayers.add(serverPlayer);
// 调用 loggedOut 方法
IPlayerEvent.loggedOut(world, serverPlayer);
// 返回一个空的 Component
//#if MC < 11900
return new TextComponent("");
//#else
//$$ return Component.empty();
//#endif
});

}
}




Loading

0 comments on commit 9ba5a1f

Please sign in to comment.