package xyz.etztech.mixtape; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import xyz.etztech.mixtape.commands.CommandAlias; import xyz.etztech.mixtape.commands.CommandSlashAlias; import xyz.etztech.mixtape.commands.CommandMixtape; import xyz.etztech.mixtape.listeners.CommandPreprocessListener; import java.util.*; import java.util.logging.Logger; public final class Mixtape extends JavaPlugin { private Logger log = Logger.getLogger( "Minecraft" ); private static Mixtape instance; public static FileConfiguration config; private static Map> chatAliases = new HashMap<>(); private static Map> commandAliases = new HashMap<>(); private static boolean global; @Override public void onEnable() { // Plugin startup logic instance = this; saveDefaultConfig(); reloadConfig(); this.getCommand("mixtape").setExecutor(new CommandMixtape(this)); this.getCommand("alias").setExecutor(new CommandAlias(this)); this.getCommand("/alias").setExecutor(new CommandSlashAlias(this)); getServer().getPluginManager().registerEvents(new CommandPreprocessListener(this), this); } @Override public void onDisable() { // Plugin shutdown logic } @Override public void reloadConfig() { super.reloadConfig(); config = Bukkit.getPluginManager().getPlugin("Mixtape").getConfig(); validate(); } public void log(String message) { log.info( "[Mixtape]: " + message ); } public static Mixtape getInstance() { return instance; } public static void setCommandAlias(String uuid, String alias, String command) { Map map = new HashMap<>(); if (commandAliases.containsKey(uuid)) { map = commandAliases.get(uuid); } map.put(alias, command); commandAliases.put(uuid, map); } public static Map getCommandAliases(String uuid) { return commandAliases.containsKey(uuid) ? commandAliases.get(uuid) : new HashMap<>(); } public static void setChatAlias(String uuid, String alias, String command) { Map map = new HashMap<>(); if (chatAliases.containsKey(uuid)) { map = chatAliases.get(uuid); } map.put(alias, command); chatAliases.put(uuid, map); } public static Map getChatAliases(String uuid) { return chatAliases.containsKey(uuid) ? chatAliases.get(uuid) : new HashMap<>(); } public static boolean getGlobal() { return global; } public static void setGlobal(boolean bool) { global = bool; } private void validate() { List errors = new ArrayList<>(); try { List aliases = getConfig().getList("aliases"); for (int i = 0; i < aliases.size(); i++) { try { LinkedHashMap alias = (LinkedHashMap) aliases.get(i); if (!alias.containsKey("alias")) { errors.add("Could not load alias #" + (i+1) + "'s alias"); } if (!alias.containsKey("command")) { errors.add("Could not load alias #" + (i+1) + "'s command"); } } catch (Exception ex) { errors.add("Could not load alias #" + (i+1)); } } } catch (Exception ex) { errors.add("Could not load config. Is it up-to-date?"); } for (String message : errors) { log(message); } if (errors.size() > 0) { log("Disabling global aliases..."); } Mixtape.setGlobal(errors.size() == 0); } }