+ update SimpleCommandBuilder

This commit is contained in:
坏黑 2018-11-09 17:00:28 +08:00
parent da4ddf4493
commit bc89f1a425
2 changed files with 24 additions and 8 deletions

View File

@ -32,6 +32,7 @@ public class SimpleCommandBuilder {
private CompleterTab completerTab = EMPTY_COMPLETER_TAB; private CompleterTab completerTab = EMPTY_COMPLETER_TAB;
private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND; private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND;
private boolean silence; private boolean silence;
private boolean forceRegister;
SimpleCommandBuilder(String command, Plugin plugin) { SimpleCommandBuilder(String command, Plugin plugin) {
this.command = command; this.command = command;
@ -43,7 +44,7 @@ public class SimpleCommandBuilder {
} }
public static SimpleCommandBuilder create(String command, Plugin plugin) { public static SimpleCommandBuilder create(String command, Plugin plugin) {
return new SimpleCommandBuilder(command, plugin); return new SimpleCommandBuilder(command.toLowerCase(), plugin);
} }
public SimpleCommandBuilder description(String description) { public SimpleCommandBuilder description(String description) {
@ -86,9 +87,17 @@ public class SimpleCommandBuilder {
return this; return this;
} }
public SimpleCommandBuilder forceRegister() {
this.forceRegister = true;
return this;
}
public SimpleCommandBuilder build() { public SimpleCommandBuilder build() {
Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分"); Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分");
Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分"); Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分");
if (forceRegister) {
TCommandHandler.getKnownCommands().remove(command);
}
TCommandHandler.registerPluginCommand( TCommandHandler.registerPluginCommand(
plugin, plugin,
command, command,

View File

@ -1,7 +1,10 @@
package me.skymc.taboolib.commands.internal; package me.skymc.taboolib.commands.internal;
import com.ilummc.tlib.inject.TPluginManager;
import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.TabooLib; 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.fileutils.FileUtils;
import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.listener.TListener;
import me.skymc.taboolib.methods.ReflectionUtils; import me.skymc.taboolib.methods.ReflectionUtils;
@ -12,12 +15,14 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.SimplePluginManager;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -28,15 +33,13 @@ import java.util.stream.Collectors;
public class TCommandHandler implements Listener { public class TCommandHandler implements Listener {
private static SimpleCommandMap commandMap; private static SimpleCommandMap commandMap;
private static Map<String, Command> knownCommands;
public TCommandHandler() { public TCommandHandler() {
try { SimpleReflection.saveFiled(Bukkit.getPluginManager() instanceof TPluginManager ? TPluginManager.class : SimplePluginManager.class, "commandMap");
Field commandMap = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap"); SimpleReflection.saveFiled(SimpleCommandMap.class, "knownCommands");
commandMap.setAccessible(true); commandMap = (SimpleCommandMap) SimpleReflection.getFieldValue(Bukkit.getPluginManager() instanceof TPluginManager ? TPluginManager.class : SimplePluginManager.class, Bukkit.getPluginManager(), "commandMap");
TCommandHandler.commandMap = (SimpleCommandMap) commandMap.get(Bukkit.getPluginManager()); knownCommands = (Map<String, Command>) SimpleReflection.getFieldValue(SimpleCommandMap.class, commandMap, "knownCommands");
} catch (Exception e) {
e.printStackTrace();
}
try { try {
registerCommands(); registerCommands();
} catch (Exception e) { } catch (Exception e) {
@ -192,4 +195,8 @@ public class TCommandHandler implements Listener {
public static SimpleCommandMap getCommandMap() { public static SimpleCommandMap getCommandMap() {
return commandMap; return commandMap;
} }
public static Map<String, Command> getKnownCommands() {
return knownCommands;
}
} }