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.CommandMixtape; import xyz.etztech.mixtape.commands.CommandSlashAlias; import xyz.etztech.mixtape.listeners.CommandPreprocessListener; import xyz.etztech.mixtape.listeners.PlayerJoinListener; 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 Database database; private static int loops; 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(); database = new Database(this); 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); getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this); } @Override public void onDisable() { // Plugin shutdown logic } @Override public void reloadConfig() { super.reloadConfig(); config = Bukkit.getPluginManager().getPlugin("Mixtape").getConfig(); loops = config.getInt("loop"); validate(); } public void log(String message) { log.info( "[Mixtape]: " + message ); } public static Mixtape getInstance() { return instance; } public static Database getDatabase() { return database; } public static void loadAliases(String uuid) { Map chats = new HashMap<>(); Map commands = new HashMap<>(); List aliases = database.getAliases(uuid); for (Alias alias : aliases) { if (alias.getType().equals(Database.AliasType.CHAT)) { chats.put(alias.getFrom(), alias.getTo()); } else if (alias.getType().equals(Database.AliasType.COMMAND)) { commands.put(alias.getFrom(), alias.getTo()); } } chatAliases.put(uuid, chats); commandAliases.put(uuid, commands); Bukkit.getConsoleSender().sendMessage(String.format("Loaded %s aliases for %s", aliases.size(), uuid)); } 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); database.insert(uuid, alias, command, Database.AliasType.COMMAND); } 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); database.insert(uuid, alias, command, Database.AliasType.CHAT); } 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; } public static int getLoops() { return loops; } 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); } }