Utilize "new" Reflection utils in CommandRemover
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Dieser Commit ist enthalten in:
Ursprung
da01fbb447
Commit
65f83eb0aa
@ -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);
|
||||
|
||||
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) {
|
||||
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);
|
||||
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