Reduce checkpoint exceptions, use "new" Reflection utils in CommandRemover #256
@ -19,41 +19,22 @@
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.Reflection;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@UtilityClass
|
||||
public class CommandRemover {
|
||||
private CommandRemover(){}
|
||||
|
||||
private static String packageName = Bukkit.getServer().getClass().getPackage().getName();
|
||||
private static String version = packageName.substring(packageName.lastIndexOf('.') + 1);
|
||||
|
||||
public static void removeAll(String... cmds){
|
||||
for (String cmd : cmds) removeCommand(cmd);
|
||||
}
|
||||
|
||||
|
||||
private static void removeCommand(String command) {
|
||||
try {
|
||||
Class<?> serverClass = Class.forName("org.bukkit.craftbukkit." + version + ".CraftServer");
|
||||
|
||||
|
||||
Field f1 = serverClass.getDeclaredField("commandMap");
|
||||
f1.setAccessible(true);
|
||||
SimpleCommandMap commandMap = (SimpleCommandMap) f1.get(Bukkit.getServer());
|
||||
|
||||
Field f2 = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
f2.setAccessible(true);
|
||||
Map<String, Command> knownCommands = (Map<String, Command>) f2.get(commandMap);
|
||||
|
||||
knownCommands.remove(command.toLowerCase());
|
||||
} catch (Exception e) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Could not remove command", e);
|
||||
private static final Reflection.FieldAccessor<SimpleCommandMap> commandMap = Reflection.getField("{obc}.CraftServer", "commandMap", SimpleCommandMap.class);
|
||||
private static final Reflection.FieldAccessor<Map> knownCommands = Reflection.getField(SimpleCommandMap.class, "knownCommands", Map.class);
|
||||
public static void removeAll(String... cmds) {
|
||||
Lixfel markierte diese Unterhaltung als gelöst
|
||||
Map<String, Command> knownCmds = knownCommands.get(commandMap.get(Bukkit.getServer()));
|
||||
for (String cmd : cmds) {
|
||||
knownCmds.remove(cmd.toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren
Ist zwar irgendwie etwas unrelated aber ok
Nach PR-Titel nichtmehr (ja, sind halt 2 kleine Sachen)
Ich habe ja schon gesagt, dass es ok ist.