SteamWar/SpigotCore
Archiviert
13
0

Add SWCommand.unregister

Dieser Commit ist enthalten in:
yoyosource 2021-03-30 09:08:04 +02:00
Ursprung 35758f1000
Commit 50b039e6dc
2 geänderte Dateien mit 24 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -33,9 +33,9 @@ import java.util.logging.Level;
public abstract class SWCommand {
private boolean enabled = true;
private final Set<SubCommand> commandSet = new HashSet<>();
private final Set<SubCommand> commandHelpSet = new HashSet<>();
private final Command command;
private final LinkedList<SubCommand> commandSet = new LinkedList<>();
private final LinkedList<SubCommand> commandHelpSet = new LinkedList<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
protected SWCommand(String command) {
@ -43,10 +43,9 @@ public abstract class SWCommand {
}
protected SWCommand(String command, String... aliases) {
SWCommandUtils.commandMap.register("steamwar", new Command(command, "", "/" + command, Arrays.asList(aliases)) {
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
@Override
public boolean execute(CommandSender sender, String alias, String[] args) {
if (!enabled) return false;
for (SubCommand subCommand : commandSet) {
if (subCommand.invoke(sender, args)) {
return false;
@ -62,7 +61,6 @@ public abstract class SWCommand {
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (!enabled) return Collections.emptyList();
List<String> strings = new ArrayList<>();
for (SubCommand subCommand : commandSet) {
List<String> tabCompletes = subCommand.tabComplete(sender, args);
@ -78,7 +76,8 @@ public abstract class SWCommand {
}
return strings;
}
});
};
SWCommandUtils.commandMap.register("steamwar", this.command);
for (Method method : getClass().getDeclaredMethods()) {
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
@ -103,7 +102,7 @@ public abstract class SWCommand {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return;
}
commandHelpSet.add(new SubCommand(this, method, anno.value()));
commandHelpSet.addLast(new SubCommand(this, method, anno.value()));
});
}
for (Method method : getClass().getDeclaredMethods()) {
@ -130,7 +129,7 @@ public abstract class SWCommand {
return;
}
}
commandSet.add(new SubCommand(this, method, anno.value()));
commandSet.addLast(new SubCommand(this, method, anno.value()));
});
}
}
@ -169,12 +168,12 @@ public abstract class SWCommand {
});
}
protected boolean isEnabled() {
return enabled;
protected void unregister() {
SWCommandUtils.knownCommandMap.remove(command.getName());
for (String alias : command.getAliases()) {
SWCommandUtils.knownCommandMap.remove(alias);
}
protected void setEnabled(boolean enabled) {
this.enabled = enabled;
command.unregister(SWCommandUtils.commandMap);
}
@Retention(RetentionPolicy.RUNTIME)

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -77,6 +78,7 @@ public class SWCommandUtils {
}
static final CommandMap commandMap;
static final Map<String, Command> knownCommandMap;
static {
try {
@ -87,6 +89,14 @@ public class SWCommandUtils {
Bukkit.shutdown();
throw new SecurityException("Oh shit. Commands cannot be registered.", exception);
}
try {
final Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
knownCommandMap = (Map<String, Command>) knownCommandsField.get(commandMap);
} catch (NoSuchFieldException | IllegalAccessException exception) {
Bukkit.shutdown();
throw new SecurityException("Oh shit. Commands cannot be registered.", exception);
}
}
static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, boolean varArgs, String[] subCommand) {