fix: 修复模块加载并调整构建以兼容 Paper 26.1.2

This commit is contained in:
2026-04-22 03:27:19 +08:00
parent 1863d04c65
commit 33ca8abd2a
10 changed files with 724 additions and 420 deletions

View File

@@ -1,6 +1,8 @@
package cn.infstar.essentialsC;
import cn.infstar.essentialsC.commands.*;
import cn.infstar.essentialsC.commands.BaseCommand;
import cn.infstar.essentialsC.commands.CommandRegistry;
import cn.infstar.essentialsC.commands.HelpCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -18,200 +20,112 @@ public final class EssentialsC extends JavaPlugin {
registerPluginChannels();
registerListeners();
registerCommands();
getLogger().info("插件已启用!版本: " + getDescription().getVersion());
getLogger().info("EssentialsC enabled. Version: " + getDescription().getVersion());
}
@Override
public void onDisable() {
getLogger().info("EssentialsC 插件已禁用!");
getLogger().info("EssentialsC disabled.");
}
public static LangManager getLangManager() {
return langManager;
}
/**
* 注册 JEI 配方同步所需的插件频道
*/
private void registerPluginChannels() {
org.bukkit.plugin.messaging.Messenger messenger = getServer().getMessenger();
// 注册 Fabric 和 NeoForge 的配方同步频道
messenger.registerOutgoingPluginChannel(this, "fabric:recipe_sync");
messenger.registerOutgoingPluginChannel(this, "neoforge:recipe_content");
}
private void registerListeners() {
if (registerListener("cn.infstar.essentialsC.listeners.ShulkerBoxListener")) {
getLogger().info("- 潜影盒模块");
getLogger().info("- Shulker box module");
}
if (registerListener("cn.infstar.essentialsC.listeners.JeiRecipeSyncListener")) {
getLogger().info("- JEI 配方同步");
getLogger().info("- JEI recipe sync");
}
if (registerListener("cn.infstar.essentialsC.listeners.MobDropListener")) {
try {
Class.forName("cn.infstar.essentialsC.listeners.MobDropMenuListener");
new cn.infstar.essentialsC.listeners.MobDropMenuListener(this);
} catch (ClassNotFoundException e) {
}
getLogger().info("- 生物掉落控制");
createOptionalInstance("cn.infstar.essentialsC.listeners.MobDropMenuListener");
getLogger().info("- Mob drop control");
}
}
private boolean registerListener(String className) {
try {
Class<?> listenerClass = Class.forName(className);
Object listenerInstance = listenerClass.getConstructor(EssentialsC.class).newInstance(this);
getServer().getPluginManager().registerEvents((org.bukkit.event.Listener) listenerInstance, this);
return true;
} catch (Exception e) {
} catch (Exception ignored) {
return false;
}
}
private void createOptionalInstance(String className) {
try {
Class<?> targetClass = Class.forName(className);
targetClass.getConstructor(EssentialsC.class).newInstance(this);
} catch (Exception ignored) {
}
}
private void registerCommands() {
try {
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true);
org.bukkit.command.CommandMap commandMap = (org.bukkit.command.CommandMap) bukkitCommandMap.get(Bukkit.getServer());
int commandCount = 0;
if (classExists("cn.infstar.essentialsC.commands.WorkbenchCommand")) {
registerCommandWithAliases(commandMap, "workbench", new WorkbenchCommand(), "wb");
commandCount++;
for (CommandRegistry.CommandSpec spec : CommandRegistry.getCommandSpecs()) {
BaseCommand executor = CommandRegistry.getCommand(spec.name());
if (executor == null) {
continue;
}
registerCommandWithAliases(commandMap, spec.name(), executor, spec.aliases().toArray(String[]::new));
}
if (classExists("cn.infstar.essentialsC.commands.AnvilCommand")) {
registerCommandWithAliases(commandMap, "anvil", new AnvilCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.CartographyTableCommand")) {
registerCommandWithAliases(commandMap, "cartographytable", new CartographyTableCommand(), "ct", "cartography");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.GrindstoneCommand")) {
registerCommandWithAliases(commandMap, "grindstone", new GrindstoneCommand(), "gs");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.LoomCommand")) {
registerCommandWithAliases(commandMap, "loom", new LoomCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SmithingTableCommand")) {
registerCommandWithAliases(commandMap, "smithingtable", new SmithingTableCommand(), "st", "smithing");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.StonecutterCommand")) {
registerCommandWithAliases(commandMap, "stonecutter", new StonecutterCommand(), "sc");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.EnderChestCommand")) {
registerCommandWithAliases(commandMap, "enderchest", new EnderChestCommand(), "ec");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.BlocksMenuCommand")) {
registerCommandWithAliases(commandMap, "blocks", new BlocksMenuCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.FlyCommand")) {
registerCommandWithAliases(commandMap, "fly", new FlyCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.HealCommand")) {
registerCommandWithAliases(commandMap, "heal", new HealCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.FeedCommand")) {
registerCommandWithAliases(commandMap, "feed", new FeedCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.VanishCommand")) {
registerCommandWithAliases(commandMap, "vanish", new VanishCommand(), "v");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SeenCommand")) {
registerCommandWithAliases(commandMap, "seen", new SeenCommand(), "info");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.HatCommand")) {
registerCommandWithAliases(commandMap, "hat", new HatCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SuicideCommand")) {
registerCommandWithAliases(commandMap, "suicide", new SuicideCommand(), "die");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.RepairCommand")) {
registerCommandWithAliases(commandMap, "repair", new RepairCommand(), "rep");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.MobDropCommand")) {
registerCommandWithAliases(commandMap, "mobdrops", new MobDropCommand());
commandCount++;
}
registerCommandWithAliases(commandMap, "essentialsc", new HelpCommand(), "essc");
commandCount++;
} catch (Exception e) {
getLogger().severe("无法注册命令: " + e.getMessage());
getLogger().severe("Failed to register commands: " + e.getMessage());
e.printStackTrace();
}
}
private boolean classExists(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
/**
* 注册命令并支持别名
* @param commandMap Bukkit CommandMap
* @param name 主命令名
* @param executor 命令执行器
* @param aliases 别名列表(可选)
*/
private void registerCommandWithAliases(org.bukkit.command.CommandMap commandMap, String name, cn.infstar.essentialsC.commands.BaseCommand executor, String... aliases) {
private void registerCommandWithAliases(org.bukkit.command.CommandMap commandMap, String name, BaseCommand executor, String... aliases) {
Command command = new Command(name) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
return executor.onCommand(sender, this, commandLabel, args);
}
@Override
public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter) {
return ((org.bukkit.command.TabCompleter) executor).onTabComplete(sender, this, alias, args);
if (executor instanceof org.bukkit.command.TabCompleter completer) {
return completer.onTabComplete(sender, this, alias, args);
}
return super.tabComplete(sender, alias, args);
}
};
command.setPermission(executor.getPermission());
// 注册到默认命名空间,使玩家可以直接使用 /workbench 而不是 /essentialsc:workbench
commandMap.register("", command);
// 注册别名
for (String alias : aliases) {
Command aliasCmd = new Command(alias) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
return executor.onCommand(sender, this, commandLabel, args);
}
@Override
public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter) {
return ((org.bukkit.command.TabCompleter) executor).onTabComplete(sender, this, alias, args);
public java.util.List<String> tabComplete(CommandSender sender, String label, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter completer) {
return completer.onTabComplete(sender, this, label, args);
}
return super.tabComplete(sender, alias, args);
return super.tabComplete(sender, label, args);
}
};
aliasCmd.setPermission(executor.getPermission());