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:
@@ -2,28 +2,84 @@ package net.t2code.alias.Spigot.system;
|
||||
|
||||
import net.t2code.alias.Spigot.Main;
|
||||
import net.t2code.alias.Spigot.cmdManagement.RegisterCommands;
|
||||
import net.t2code.alias.Util;
|
||||
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
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.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AliasRegister {
|
||||
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 {
|
||||
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
bukkitCommandMap.setAccessible(true);
|
||||
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
|
||||
commandMap.register(alias, new RegisterCommands(alias));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else T2Csend.warning(Main.getPlugin(), " §4AliasHashmap is null! - " + alias);
|
||||
try {
|
||||
final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
bukkitCommandMap.setAccessible(true);
|
||||
CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
|
||||
// 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) {
|
||||
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);
|
||||
}
|
||||
|
||||
private static void wrap(String alias, CommandMap commandMap) {
|
||||
Command cmd = commandMap.getCommand(alias);
|
||||
Main.getPlugin().getBukkitCommandWrap().wrap(cmd, alias);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user