refactor: 改为运行期模块开关并简化构建

This commit is contained in:
2026-06-10 00:39:42 +08:00
parent 0a45be832b
commit c0289c55d3
17 changed files with 271 additions and 247 deletions

View File

@@ -1,6 +1,7 @@
package cn.infstar.essentialsC.commands;
import cn.infstar.essentialsC.EssentialsC;
import cn.infstar.essentialsC.ModuleManager;
import cn.infstar.essentialsC.tpsbar.TpsBarService;
import java.lang.reflect.Constructor;
@@ -20,44 +21,44 @@ public final class CommandRegistry {
private static final Set<String> UNAVAILABLE_COMMANDS = new java.util.HashSet<>();
static {
register("workbench", "essentialsc.command.workbench", "cn.infstar.essentialsC.commands.WorkbenchCommand", "wb");
register("anvil", "essentialsc.command.anvil", "cn.infstar.essentialsC.commands.AnvilCommand");
register("cartographytable", "essentialsc.command.cartographytable", "cn.infstar.essentialsC.commands.CartographyTableCommand", "ct", "cartography");
register("grindstone", "essentialsc.command.grindstone", "cn.infstar.essentialsC.commands.GrindstoneCommand", "gs");
register("loom", "essentialsc.command.loom", "cn.infstar.essentialsC.commands.LoomCommand");
register("smithingtable", "essentialsc.command.smithingtable", "cn.infstar.essentialsC.commands.SmithingTableCommand", "st", "smithing");
register("stonecutter", "essentialsc.command.stonecutter", "cn.infstar.essentialsC.commands.StonecutterCommand", "sc");
register("enderchest", "essentialsc.command.enderchest", "cn.infstar.essentialsC.commands.EnderChestCommand", "ec");
register("blocks", "essentialsc.command.blocks", "cn.infstar.essentialsC.commands.BlocksMenuCommand");
register("hat", "essentialsc.command.hat", "cn.infstar.essentialsC.commands.HatCommand");
register("suicide", "essentialsc.command.suicide", "cn.infstar.essentialsC.commands.SuicideCommand", "die");
register("fly", "essentialsc.command.fly", "cn.infstar.essentialsC.commands.FlyCommand");
register("nightvision", "essentialsc.command.nightvision", "cn.infstar.essentialsC.commands.NightVisionCommand", "nv");
register("glow", "essentialsc.command.glow", "cn.infstar.essentialsC.commands.GlowCommand");
register("heal", "essentialsc.command.heal", "cn.infstar.essentialsC.commands.HealCommand");
register("vanish", "essentialsc.command.vanish", "cn.infstar.essentialsC.commands.VanishCommand", "v");
register("seen", "essentialsc.command.seen", "cn.infstar.essentialsC.commands.SeenCommand", "info");
register("feed", "essentialsc.command.feed", "cn.infstar.essentialsC.commands.FeedCommand");
register("repair", "essentialsc.command.repair", "cn.infstar.essentialsC.commands.RepairCommand", "rep");
register("tpsbar", "essentialsc.command.tpsbar", "cn.infstar.essentialsC.commands.TpsBarCommand");
register("mobdrops", "essentialsc.mobdrops.enderman", "cn.infstar.essentialsC.commands.MobDropCommand");
registerSubCommand("admin", "essentialsc.command.admin", "cn.infstar.essentialsC.commands.AdminCommand");
register("workbench", "essentialsc.command.workbench", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.WorkbenchCommand", "wb");
register("anvil", "essentialsc.command.anvil", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.AnvilCommand");
register("cartographytable", "essentialsc.command.cartographytable", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.CartographyTableCommand", "ct", "cartography");
register("grindstone", "essentialsc.command.grindstone", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.GrindstoneCommand", "gs");
register("loom", "essentialsc.command.loom", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.LoomCommand");
register("smithingtable", "essentialsc.command.smithingtable", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.SmithingTableCommand", "st", "smithing");
register("stonecutter", "essentialsc.command.stonecutter", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.StonecutterCommand", "sc");
register("enderchest", "essentialsc.command.enderchest", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.EnderChestCommand", "ec");
register("blocks", "essentialsc.command.blocks", ModuleManager.BLOCKS, "cn.infstar.essentialsC.commands.BlocksMenuCommand");
register("hat", "essentialsc.command.hat", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.HatCommand");
register("suicide", "essentialsc.command.suicide", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.SuicideCommand", "die");
register("fly", "essentialsc.command.fly", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.FlyCommand");
register("nightvision", "essentialsc.command.nightvision", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.NightVisionCommand", "nv");
register("glow", "essentialsc.command.glow", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.GlowCommand");
register("heal", "essentialsc.command.heal", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.HealCommand");
register("vanish", "essentialsc.command.vanish", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.VanishCommand", "v");
register("seen", "essentialsc.command.seen", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.SeenCommand", "info");
register("feed", "essentialsc.command.feed", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.FeedCommand");
register("repair", "essentialsc.command.repair", ModuleManager.PLAYER, "cn.infstar.essentialsC.commands.RepairCommand", "rep");
register("tpsbar", "essentialsc.command.tpsbar", ModuleManager.TPSBAR, "cn.infstar.essentialsC.commands.TpsBarCommand");
register("mobdrops", "essentialsc.mobdrops.enderman", ModuleManager.MOB_DROPS, "cn.infstar.essentialsC.commands.MobDropCommand");
registerSubCommand("admin", "essentialsc.command.admin", ModuleManager.ADMIN_MODE, "cn.infstar.essentialsC.commands.AdminCommand");
}
private CommandRegistry() {
}
private static void register(String name, String permission, String className, String... aliases) {
register(name, permission, className, true, aliases);
private static void register(String name, String permission, String moduleKey, String className, String... aliases) {
register(name, permission, moduleKey, className, true, aliases);
}
private static void registerSubCommand(String name, String permission, String className, String... aliases) {
register(name, permission, className, false, aliases);
private static void registerSubCommand(String name, String permission, String moduleKey, String className, String... aliases) {
register(name, permission, moduleKey, className, false, aliases);
}
private static void register(String name, String permission, String className, boolean standalone, String... aliases) {
private static void register(String name, String permission, String moduleKey, String className, boolean standalone, String... aliases) {
List<String> aliasList = List.of(aliases);
CommandSpec spec = new CommandSpec(name, permission, className, aliasList, standalone);
CommandSpec spec = new CommandSpec(name, permission, moduleKey, className, aliasList, standalone);
COMMANDS.put(name, spec);
ALIAS_TO_COMMAND.put(name, name);
for (String alias : aliasList) {
@@ -91,6 +92,10 @@ public final class CommandRegistry {
return null;
}
if (isRuntimeDisabled(resolvedName)) {
return null;
}
BaseCommand cached = COMMAND_CACHE.get(resolvedName);
if (cached != null) {
return cached;
@@ -98,9 +103,6 @@ public final class CommandRegistry {
if (UNAVAILABLE_COMMANDS.contains(resolvedName)) {
return null;
}
if (isRuntimeDisabled(resolvedName)) {
return null;
}
CommandSpec spec = COMMANDS.get(resolvedName);
if (spec == null) {
@@ -125,6 +127,21 @@ public final class CommandRegistry {
}
private static boolean isRuntimeDisabled(String resolvedName) {
CommandSpec spec = COMMANDS.get(resolvedName);
if (spec == null) {
return true;
}
try {
EssentialsC plugin = EssentialsC.getPlugin(EssentialsC.class);
ModuleManager moduleManager = plugin.getModuleManager();
if (moduleManager != null && !moduleManager.isEnabled(spec.moduleKey())) {
return true;
}
} catch (IllegalStateException ignored) {
return false;
}
if (!"tpsbar".equals(resolvedName)) {
return false;
}
@@ -138,6 +155,11 @@ public final class CommandRegistry {
}
}
public record CommandSpec(String name, String permission, String className, List<String> aliases, boolean standalone) {
public static void clearCache() {
COMMAND_CACHE.clear();
UNAVAILABLE_COMMANDS.clear();
}
public record CommandSpec(String name, String permission, String moduleKey, String className, List<String> aliases, boolean standalone) {
}
}