1.1.6 | New alias for commands & alias registration for plugin reload

Changes:
- For commands the placeholder '[alias]' has been added.
- If you add a new alias, it will now be registred on reload of the plugin, so no restart is needed anymore (If a command is renamed / removed, the old one will still be registred, but will not work. I am looking for a solution in the next update).
This commit is contained in:
JaTiTV 2022-11-17 23:39:38 +01:00
parent e96447d4fa
commit f00ac77743
8 changed files with 394 additions and 53 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.t2code</groupId> <groupId>net.t2code</groupId>
<artifactId>Alias</artifactId> <artifactId>Alias</artifactId>
<version>1.1.5</version> <version>1.1.6</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>T2C-Alias</name> <name>T2C-Alias</name>

View File

@ -2,6 +2,8 @@ package net.t2code.alias.Spigot;
import net.t2code.alias.Spigot.objects.AliasObject; import net.t2code.alias.Spigot.objects.AliasObject;
import net.t2code.alias.Spigot.objects.SubAliasObject; import net.t2code.alias.Spigot.objects.SubAliasObject;
import net.t2code.alias.Spigot.system.BukkitCommandWrap;
import net.t2code.alias.Spigot.system.BukkitCommandWrap_Useless;
import net.t2code.alias.Spigot.system.Load; import net.t2code.alias.Spigot.system.Load;
import net.t2code.alias.Util; import net.t2code.alias.Util;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate; import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
@ -23,6 +25,7 @@ public final class Main extends JavaPlugin {
private static List<String> autor; private static List<String> autor;
private static Main plugin; private static Main plugin;
public static HashMap<String, AliasObject> aliasHashMap = new HashMap<>(); public static HashMap<String, AliasObject> aliasHashMap = new HashMap<>();
public static HashMap<String, Boolean> loadAliasHashMap = new HashMap<>();
public static HashMap<String, SubAliasObject> subAliasHashMap = new HashMap<>(); public static HashMap<String, SubAliasObject> subAliasHashMap = new HashMap<>();
public static ArrayList<String> allAliases = new ArrayList<>(); public static ArrayList<String> allAliases = new ArrayList<>();
public static ArrayList<String> allSubAliases = new ArrayList<>(); public static ArrayList<String> allSubAliases = new ArrayList<>();
@ -35,6 +38,12 @@ public final class Main extends JavaPlugin {
autor = plugin.getDescription().getAuthors(); autor = plugin.getDescription().getAuthors();
version = plugin.getDescription().getVersion(); version = plugin.getDescription().getVersion();
if (pluginNotFound("T2CodeLib", 96388, Util.getRequiredT2CodeLibVersion())) return; if (pluginNotFound("T2CodeLib", 96388, Util.getRequiredT2CodeLibVersion())) return;
try {
Class.forName("com.mojang.brigadier.CommandDispatcher");
this.bukkitCommandWrap = new BukkitCommandWrap();
} catch (ClassNotFoundException | NoClassDefFoundError e) {
this.bukkitCommandWrap = new BukkitCommandWrap_Useless();
}
Load.onLoad(Util.getPrefix(), autor, version, Util.getSpigot(), Util.getDiscord(), Util.getBstatsID()); Load.onLoad(Util.getPrefix(), autor, version, Util.getSpigot(), Util.getDiscord(), Util.getBstatsID());
} }
@ -91,4 +100,10 @@ public final class Main extends JavaPlugin {
public static List<String> getAutor() { public static List<String> getAutor() {
return autor; return autor;
} }
private BukkitCommandWrap bukkitCommandWrap = null;
public BukkitCommandWrap getBukkitCommandWrap() {
return this.bukkitCommandWrap;
}
} }

View File

@ -2,6 +2,7 @@ package net.t2code.alias.Spigot.cmdManagement;
import net.t2code.alias.Spigot.Main; import net.t2code.alias.Spigot.Main;
import net.t2code.alias.Spigot.config.languages.SelectMessages; import net.t2code.alias.Spigot.config.languages.SelectMessages;
import net.t2code.alias.Spigot.system.Load;
import net.t2code.alias.Util; import net.t2code.alias.Util;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate; import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
@ -10,16 +11,16 @@ import org.bukkit.entity.Player;
public class Commands { public class Commands {
public static void info(CommandSender sender) { public static void info(CommandSender sender) {
T2Ctemplate.sendInfo(sender,Main.getPlugin(),Util.getSpigotID(),Util.getDiscord(),Util.getInfoText()); T2Ctemplate.sendInfo(sender, Main.getPlugin(), Util.getSpigotID(), Util.getDiscord(), Util.getInfoText());
} }
public static void reload(CommandSender sender) { public static void reload(CommandSender sender) {
if (sender instanceof Player) sender.sendMessage(SelectMessages.reloadStart); if (sender instanceof Player) T2Csend.player((Player) sender, SelectMessages.reloadStart);
T2Csend.console(Util.getPrefix() + "§8-------------------------------"); T2Csend.console(Util.getPrefix() + "§8-------------------------------");
T2Csend.console(Util.getPrefix() + " §6Plugin reload..."); T2Csend.console(Util.getPrefix() + " §6Plugin reload...");
T2Csend.console(Util.getPrefix() + "§8-------------------------------"); T2Csend.console(Util.getPrefix() + "§8-------------------------------");
Main.getPlugin().onEnable(); Load.loadReload();
if (sender instanceof Player) sender.sendMessage(SelectMessages.reloadEnd); if (sender instanceof Player) T2Csend.player((Player) sender, SelectMessages.reloadEnd);
T2Csend.console(Util.getPrefix() + "§8-------------------------------"); T2Csend.console(Util.getPrefix() + "§8-------------------------------");
T2Csend.console(Util.getPrefix() + " §2Plugin successfully reloaded."); T2Csend.console(Util.getPrefix() + " §2Plugin successfully reloaded.");
T2Csend.console(Util.getPrefix() + "§8-------------------------------"); T2Csend.console(Util.getPrefix() + "§8-------------------------------");

View File

@ -27,7 +27,7 @@ public class ExecuteAlias {
if (aliasObject.adminEnable) { if (aliasObject.adminEnable) {
if (player.hasPermission(aliasObject.adminPermission)) { if (player.hasPermission(aliasObject.adminPermission)) {
if (aliasObject.adminCommandEnable) { if (aliasObject.adminCommandEnable) {
aliasAdminCommand(aliasObject, player, args); aliasAdminCommand(aliasObject, alias, player, args);
} }
if (aliasObject.adminMessageEnable) { if (aliasObject.adminMessageEnable) {
aliasAdminMessage(aliasObject, alias, player, args); aliasAdminMessage(aliasObject, alias, player, args);
@ -57,7 +57,7 @@ public class ExecuteAlias {
} }
} }
if (aliasObject.commandEnable) { if (aliasObject.commandEnable) {
aliasCommand(aliasObject, player, args); aliasCommand(aliasObject, alias, player, args);
} }
if (aliasObject.messageEnable) { if (aliasObject.messageEnable) {
aliasMessage(aliasObject, alias, player, args); aliasMessage(aliasObject, alias, player, args);
@ -76,7 +76,7 @@ public class ExecuteAlias {
if (aliasObject.adminEnable) { if (aliasObject.adminEnable) {
if (player.hasPermission(aliasObject.adminPermission)) { if (player.hasPermission(aliasObject.adminPermission)) {
if (aliasObject.adminCommandEnable) { if (aliasObject.adminCommandEnable) {
subAliasAdminCommand(aliasObject, player, args); subAliasAdminCommand(aliasObject,alias, player, args);
} }
if (aliasObject.adminMessageEnable) { if (aliasObject.adminMessageEnable) {
subAliasAdminMessage(aliasObject, alias, player, args); subAliasAdminMessage(aliasObject, alias, player, args);
@ -113,7 +113,7 @@ public class ExecuteAlias {
} }
} else { } else {
if (aliasObject.consoleEnable) { if (aliasObject.consoleEnable) {
subAliasConsole(aliasObject, sender, args); subAliasConsole(aliasObject,alias, sender, args);
} else T2Csend.sender(sender, SelectMessages.onlyForPlayer); } else T2Csend.sender(sender, SelectMessages.onlyForPlayer);
} }
} }
@ -140,7 +140,7 @@ public class ExecuteAlias {
} }
} }
private static void subAliasConsole(SubAliasObject alias, CommandSender sender, String[] args) { private static void subAliasConsole(SubAliasObject aliasObject, String alias, CommandSender sender, String[] args) {
String targetSt = "[target]"; String targetSt = "[target]";
try { try {
List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList()); List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList());
@ -148,28 +148,29 @@ public class ExecuteAlias {
} catch (Exception ignored) { } catch (Exception ignored) {
} }
if (alias.consoleCommandEnable) { if (aliasObject.consoleCommandEnable) {
for (String cmd : alias.consoleCommands) { for (String cmd : aliasObject.consoleCommands) {
if (alias.consoleBungeeCommand) { String replace = cmd.replace("[target]", targetSt).replace("[alias]", alias);
if (aliasObject.consoleBungeeCommand) {
if (SelectConfig.getBungee()) { if (SelectConfig.getBungee()) {
BCommandSenderReciver.sendToBungee(sender, cmd.replace("[target]", targetSt), true); BCommandSenderReciver.sendToBungee(sender, replace, true);
} else { } else {
T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
T2Csend.sender(sender, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.sender(sender, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
} }
} else { } else {
T2Ccmd.console(cmd.replace("[target]", targetSt)); T2Ccmd.console(replace);
} }
} }
} }
if (alias.consoleMessageEnable) { if (aliasObject.consoleMessageEnable) {
for (String msg : alias.consoleMessages) { for (String msg : aliasObject.consoleMessages) {
T2Csend.console(T2Creplace.replace(prefix, msg)); T2Csend.console(T2Creplace.replace(prefix, msg));
} }
} }
} }
private static void aliasAdminCommand(AliasObject alias, Player player, String[] args) { private static void aliasAdminCommand(AliasObject aliasObject, String alias, Player player, String[] args) {
String targetSt = "[target]"; String targetSt = "[target]";
try { try {
List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList()); List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList());
@ -177,26 +178,26 @@ public class ExecuteAlias {
} catch (Exception ignored) { } catch (Exception ignored) {
} }
for (String cmd : alias.adminCommands) { for (String cmd : aliasObject.adminCommands) {
if (alias.adminBungeeCommand) { if (aliasObject.adminBungeeCommand) {
if (SelectConfig.getBungee()) { if (SelectConfig.getBungee()) {
BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt), alias.adminCommandAsConsole); BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias), aliasObject.adminCommandAsConsole);
} else { } else {
T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
} }
} else { } else {
if (alias.adminCommandAsConsole) { if (aliasObject.adminCommandAsConsole) {
T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} else { } else {
T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} }
} }
} }
} }
private static void subAliasAdminCommand(SubAliasObject alias, Player player, String[] args) { private static void subAliasAdminCommand(SubAliasObject aliasObject, String alias, Player player, String[] args) {
String targetSt = "[target]"; String targetSt = "[target]";
try { try {
List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList()); List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList());
@ -204,20 +205,20 @@ public class ExecuteAlias {
} catch (Exception ignored) { } catch (Exception ignored) {
} }
for (String cmd : alias.adminCommands) { for (String cmd : aliasObject.adminCommands) {
if (alias.adminBungeeCommand) { if (aliasObject.adminBungeeCommand) {
if (SelectConfig.getBungee()) { if (SelectConfig.getBungee()) {
BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt), alias.adminCommandAsConsole); BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias), aliasObject.adminCommandAsConsole);
} else { } else {
T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
} }
} else { } else {
if (alias.adminCommandAsConsole) { if (aliasObject.adminCommandAsConsole) {
T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} else { } else {
T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} }
} }
} }
@ -261,7 +262,7 @@ public class ExecuteAlias {
} }
} }
private static void aliasCommand(AliasObject alias, Player player, String[] args) { private static void aliasCommand(AliasObject aliasObject, String alias, Player player, String[] args) {
String targetSt = "[target]"; String targetSt = "[target]";
try { try {
List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList()); List<String> results = Arrays.stream(args).filter(a -> a.contains("-p:")).collect(Collectors.toList());
@ -269,20 +270,19 @@ public class ExecuteAlias {
} catch (Exception ignored) { } catch (Exception ignored) {
} }
for (String cmd : alias.command) { for (String cmd : aliasObject.command) {
if (alias.bungeeCommand) { if (aliasObject.bungeeCommand) {
if (SelectConfig.getBungee()) { if (SelectConfig.getBungee()) {
BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt), alias.commandAsConsole); BCommandSenderReciver.sendToBungee(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias), aliasObject.commandAsConsole);
} else { } else {
T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config."); T2Csend.player(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
} }
} else { } else {
if (alias.commandAsConsole) { if (aliasObject.commandAsConsole) {
T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.console(cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} else { } else {
T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt)); T2Ccmd.player(player, cmd.replace("[player]", player.getName()).replace("[target]", targetSt).replace("[alias]", alias));
} }
} }
} }

View File

@ -4,11 +4,14 @@ import com.google.common.base.Preconditions;
import net.t2code.alias.Spigot.Main; import net.t2code.alias.Spigot.Main;
import net.t2code.alias.Spigot.system.AliasRegister; import net.t2code.alias.Spigot.system.AliasRegister;
import net.t2code.alias.Spigot.objects.AliasObject; import net.t2code.alias.Spigot.objects.AliasObject;
import net.t2code.alias.Spigot.system.BukkitCommandWrap_Useless;
import net.t2code.alias.Util; import net.t2code.alias.Util;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -23,6 +26,7 @@ public class SelectAlias {
Main.aliasHashMap.clear(); Main.aliasHashMap.clear();
Main.allAliases.clear(); Main.allAliases.clear();
File f = new File(Main.getPath() + "/Alias/"); File f = new File(Main.getPath() + "/Alias/");
File[] fileArray = f.listFiles(); File[] fileArray = f.listFiles();
for (File config_gui : fileArray) { for (File config_gui : fileArray) {
@ -101,6 +105,7 @@ public class SelectAlias {
T2Csend.console(Prefix + " §aAlias file §e" + config_gui.getName() + " §awas loaded"); T2Csend.console(Prefix + " §aAlias file §e" + config_gui.getName() + " §awas loaded");
} }
} }
T2Ctemplate.onLoadSeparateStroke(Util.getPrefix());
AliasRegister.onRegister(); AliasRegister.onRegister();
} }

View File

@ -2,28 +2,84 @@ package net.t2code.alias.Spigot.system;
import net.t2code.alias.Spigot.Main; import net.t2code.alias.Spigot.Main;
import net.t2code.alias.Spigot.cmdManagement.RegisterCommands; import net.t2code.alias.Spigot.cmdManagement.RegisterCommands;
import net.t2code.alias.Util;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
import org.bukkit.plugin.SimplePluginManager;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Map;
import java.util.Objects;
public class AliasRegister { public class AliasRegister {
public static void onRegister() { public static void onRegister() {
for (String alias : Main.allAliases) {
if (Main.aliasHashMap.get(alias) != null) {
if (alias.equals(" ")) continue;
if (Main.aliasHashMap.get(alias).aliasEnable) {
try { try {
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true); bukkitCommandMap.setAccessible(true);
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
commandMap.register(alias, new RegisterCommands(alias)); // onUnRegister(bukkitCommandMap);
for (String alias : Main.allAliases) {
register(alias, commandMap);
// wrap(alias, commandMap);
}
Main.getPlugin().getBukkitCommandWrap().sync();
if (Bukkit.getOnlinePlayers().size() >= 1)
for (Player player : Bukkit.getOnlinePlayers()) player.updateCommands();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
//public static void onUnRegister(Field bukkitCommandMap) throws IllegalAccessException {
//
// // if (Main.allAliases != null && !Main.allAliases.isEmpty()) {
// // if (!(Main.getPlugin().getBukkitCommandWrap() instanceof BukkitCommandWrap_Useless)) {
// // for (String alias : Main.allAliases) {
// // T2Csend.debugmsg(Main.getPlugin(),"uload: " + alias);
// // Main.getPlugin().
// // Main.getPlugin().getBukkitCommandWrap().unwrap(alias);
// // }
// // Main.getPlugin().getBukkitCommandWrap().sync();
// // if (Bukkit.getOnlinePlayers().size() >= 1)
// // for (Player player : Bukkit.getOnlinePlayers()) player.updateCommands();
// // }
// // }
// // commandMap.clearCommands();
//
//
// for (Map.Entry<String, Boolean> entry : Main.loadAliasHashMap.entrySet()) {
// ((SimpleCommandMap) bukkitCommandMap.get(simplePluginManager)).getCommand(entry.getKey()).unregister(bukkitCommandMap.get(Bukkit.getServer()));
//
//
// if (entry.getValue())
// Main.getPlugin().getBukkitCommandWrap().unwrap(entry.getKey());
// }
//
// Main.getPlugin().getBukkitCommandWrap().sync();
// if (Bukkit.getOnlinePlayers().size() >= 1)
// for (Player player : Bukkit.getOnlinePlayers()) player.updateCommands();
// Main.loadAliasHashMap.clear();
//
//}
private static void register(String alias, CommandMap commandMap) {
if (Main.aliasHashMap.get(alias) != null) {
if (alias.equals(" ")) return;
if (Main.aliasHashMap.get(alias).aliasEnable) {
commandMap.register(alias, new RegisterCommands(alias));
T2Csend.console(Util.getPrefix() + " §aAlias §e" + alias + " §aregister");
Main.loadAliasHashMap.put(alias, true);
} else Main.loadAliasHashMap.put(alias, false);
} else T2Csend.warning(Main.getPlugin(), " §4AliasHashmap is null! - " + alias); } else T2Csend.warning(Main.getPlugin(), " §4AliasHashmap is null! - " + alias);
} }
private static void wrap(String alias, CommandMap commandMap) {
Command cmd = commandMap.getCommand(alias);
Main.getPlugin().getBukkitCommandWrap().wrap(cmd, alias);
} }
} }

View File

@ -0,0 +1,247 @@
package net.t2code.alias.Spigot.system;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BukkitCommandWrap {
private Field bField;
private Method removeCommandMethod;
private String nmsVersion;
private Class minecraftServerClass;
private Method aMethod;
private Method getServerMethod;
private Field vanillaCommandDispatcherField;
private Method getCommandDispatcherMethod;
private Method registerMethod;
private Method syncCommandsMethod;
private Constructor bukkitcommandWrapperConstructor;
public BukkitCommandWrap() {
try {
this.nmsVersion = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
this.nmsVersion = null;
}
}
public void wrap(Command command, String alias) {
if (this.nmsVersion == null) return;
if (this.minecraftServerClass == null) try {
this.minecraftServerClass = Class.forName("net.minecraft.server." + this.nmsVersion + ".MinecraftServer");
} catch (ClassNotFoundException e) {
try {
this.minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer");
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.addSuppressed(e);
classNotFoundException.printStackTrace();
return;
}
}
if (this.getServerMethod == null) try {
this.getServerMethod = this.minecraftServerClass.getMethod("getServer");
this.getServerMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
Object minecraftServer;
try {
minecraftServer = this.getServerMethod.invoke(this.minecraftServerClass);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return;
}
if (this.vanillaCommandDispatcherField == null) try {
this.vanillaCommandDispatcherField = this.minecraftServerClass.getDeclaredField("vanillaCommandDispatcher");
this.vanillaCommandDispatcherField.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
return;
}
Object commandDispatcher = null;
try {
commandDispatcher = this.vanillaCommandDispatcherField.get(minecraftServer);
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
if (this.bField == null) try {
this.bField = Class.forName("net.minecraft.commands.CommandDispatcher").getDeclaredField("g");
this.bField.setAccessible(true);
} catch (NoSuchFieldException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
com.mojang.brigadier.CommandDispatcher b;
try {
b = (com.mojang.brigadier.CommandDispatcher) this.bField.get(commandDispatcher);
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
if (this.aMethod == null) try {
this.aMethod = commandDispatcher.getClass().getDeclaredMethod("a");
this.aMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
if (this.bukkitcommandWrapperConstructor == null) try {
this.bukkitcommandWrapperConstructor = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".command.BukkitCommandWrapper").getDeclaredConstructor(Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".CraftServer"), Command.class);
this.bukkitcommandWrapperConstructor.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
Object commandWrapper;
try {
commandWrapper = this.bukkitcommandWrapperConstructor.newInstance(Bukkit.getServer(), command);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return;
}
Object a;
try {
a = this.aMethod.invoke(commandDispatcher);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return;
}
if (this.registerMethod == null) try {
this.registerMethod = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".command.BukkitCommandWrapper").getMethod("register", com.mojang.brigadier.CommandDispatcher.class, String.class);
this.registerMethod.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
this.registerMethod.invoke(commandWrapper, a, alias);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void sync() {
if (this.syncCommandsMethod == null) try {
this.syncCommandsMethod = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".CraftServer").getDeclaredMethod("syncCommands");
this.syncCommandsMethod.setAccessible(true);
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
this.syncCommandsMethod.invoke(Bukkit.getServer());
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void unwrap(String command) {
if (this.nmsVersion == null) return;
if (this.minecraftServerClass == null) try {
this.minecraftServerClass = Class.forName("net.minecraft.server." + this.nmsVersion + ".MinecraftServer");
} catch (ClassNotFoundException e) {
try {
this.minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer");
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
classNotFoundException.addSuppressed(e);
return;
}
}
if (this.getServerMethod == null) try {
this.getServerMethod = this.minecraftServerClass.getMethod("getServer");
this.getServerMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
Object server;
try {
server = this.getServerMethod.invoke(this.minecraftServerClass);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return;
}
if (this.vanillaCommandDispatcherField == null) try {
this.vanillaCommandDispatcherField = this.minecraftServerClass.getDeclaredField("vanillaCommandDispatcher");
this.vanillaCommandDispatcherField.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
return;
}
Object commandDispatcher = null;
try {
commandDispatcher = this.vanillaCommandDispatcherField.get(server);
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
if (this.bField == null) try {
this.bField = Class.forName("net.minecraft.server." + this.nmsVersion + ".CommandDispatcher").getDeclaredField("b");
this.bField.setAccessible(true);
} catch (NoSuchFieldException | ClassNotFoundException e) {
if (this.bField == null) try {
this.bField = Class.forName("net.minecraft.commands.CommandDispatcher").getDeclaredField("g");
this.bField.setAccessible(true);
} catch (NoSuchFieldException | ClassNotFoundException ex) {
ex.addSuppressed(e);
e.printStackTrace();
return;
}
}
com.mojang.brigadier.CommandDispatcher b;
try {
b = (com.mojang.brigadier.CommandDispatcher) this.bField.get(commandDispatcher);
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
}
if (this.removeCommandMethod == null) try {
try {
this.removeCommandMethod = RootCommandNode.class.getDeclaredMethod("removeCommand", String.class);
} catch (NoSuchMethodException | NoSuchMethodError ex) {
this.removeCommandMethod = CommandNode.class.getDeclaredMethod("removeCommand", String.class);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
try {
this.removeCommandMethod.invoke(b.getRoot(), command);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,17 @@
package net.t2code.alias.Spigot.system;
import org.bukkit.command.Command;
public class BukkitCommandWrap_Useless extends BukkitCommandWrap {
public BukkitCommandWrap_Useless() {
}
@Override
public void wrap(Command command, String alias) {
}
@Override
public void unwrap(String command) {
}
}