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;
|
package de.steamwar.core;
|
||||||
|
|
||||||
|
import com.comphenix.tinyprotocol.Reflection;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.SimpleCommandMap;
|
import org.bukkit.command.SimpleCommandMap;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
public class CommandRemover {
|
public class CommandRemover {
|
||||||
private CommandRemover(){}
|
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);
|
||||||
private static String packageName = Bukkit.getServer().getClass().getPackage().getName();
|
|
||||||
private static String version = packageName.substring(packageName.lastIndexOf('.') + 1);
|
|
||||||
|
|
||||||
public static void removeAll(String... cmds) {
|
public static void removeAll(String... cmds) {
|
||||||
for (String cmd : cmds) removeCommand(cmd);
|
Map<String, Command> knownCmds = knownCommands.get(commandMap.get(Bukkit.getServer()));
|
||||||
}
|
for (String cmd : cmds) {
|
||||||
|
knownCmds.remove(cmd.toLowerCase());
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren