From bc89f1a425a8b14da214854a870db470f51f6f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Fri, 9 Nov 2018 17:00:28 +0800 Subject: [PATCH] + update SimpleCommandBuilder --- .../builder/SimpleCommandBuilder.java | 11 +++++++++- .../commands/internal/TCommandHandler.java | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/skymc/taboolib/commands/builder/SimpleCommandBuilder.java b/src/main/java/me/skymc/taboolib/commands/builder/SimpleCommandBuilder.java index 209d1eb..b698177 100644 --- a/src/main/java/me/skymc/taboolib/commands/builder/SimpleCommandBuilder.java +++ b/src/main/java/me/skymc/taboolib/commands/builder/SimpleCommandBuilder.java @@ -32,6 +32,7 @@ public class SimpleCommandBuilder { private CompleterTab completerTab = EMPTY_COMPLETER_TAB; private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND; private boolean silence; + private boolean forceRegister; SimpleCommandBuilder(String command, Plugin plugin) { this.command = command; @@ -43,7 +44,7 @@ public class SimpleCommandBuilder { } public static SimpleCommandBuilder create(String command, Plugin plugin) { - return new SimpleCommandBuilder(command, plugin); + return new SimpleCommandBuilder(command.toLowerCase(), plugin); } public SimpleCommandBuilder description(String description) { @@ -86,9 +87,17 @@ public class SimpleCommandBuilder { return this; } + public SimpleCommandBuilder forceRegister() { + this.forceRegister = true; + return this; + } + public SimpleCommandBuilder build() { Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分"); Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分"); + if (forceRegister) { + TCommandHandler.getKnownCommands().remove(command); + } TCommandHandler.registerPluginCommand( plugin, command, diff --git a/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java b/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java index ee7f365..d6e80e9 100644 --- a/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java +++ b/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java @@ -1,7 +1,10 @@ package me.skymc.taboolib.commands.internal; +import com.ilummc.tlib.inject.TPluginManager; import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.commands.builder.SimpleCommandBuilder; +import me.skymc.taboolib.common.util.SimpleReflection; import me.skymc.taboolib.fileutils.FileUtils; import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.methods.ReflectionUtils; @@ -12,12 +15,14 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.SimplePluginManager; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -28,15 +33,13 @@ import java.util.stream.Collectors; public class TCommandHandler implements Listener { private static SimpleCommandMap commandMap; + private static Map knownCommands; public TCommandHandler() { - try { - Field commandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); - commandMap.setAccessible(true); - TCommandHandler.commandMap = (SimpleCommandMap) commandMap.get(Bukkit.getPluginManager()); - } catch (Exception e) { - e.printStackTrace(); - } + SimpleReflection.saveFiled(Bukkit.getPluginManager() instanceof TPluginManager ? TPluginManager.class : SimplePluginManager.class, "commandMap"); + SimpleReflection.saveFiled(SimpleCommandMap.class, "knownCommands"); + commandMap = (SimpleCommandMap) SimpleReflection.getFieldValue(Bukkit.getPluginManager() instanceof TPluginManager ? TPluginManager.class : SimplePluginManager.class, Bukkit.getPluginManager(), "commandMap"); + knownCommands = (Map) SimpleReflection.getFieldValue(SimpleCommandMap.class, commandMap, "knownCommands"); try { registerCommands(); } catch (Exception e) { @@ -192,4 +195,8 @@ public class TCommandHandler implements Listener { public static SimpleCommandMap getCommandMap() { return commandMap; } + + public static Map getKnownCommands() { + return knownCommands; + } }