Mixtape/src/main/java/xyz/etztech/mixtape/Mixtape.java

128 lines
3.9 KiB
Java
Raw Normal View History

2018-09-12 15:56:08 +00:00
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<String, Map<String, String>> chatAliases = new HashMap<>();
private static Map<String, Map<String, String>> 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<String, String> map = new HashMap<>();
if (commandAliases.containsKey(uuid)) {
map = commandAliases.get(uuid);
}
map.put(alias, command);
commandAliases.put(uuid, map);
}
public static Map<String, String> getCommandAliases(String uuid) {
return commandAliases.containsKey(uuid) ? commandAliases.get(uuid) : new HashMap<>();
}
public static void setChatAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
if (chatAliases.containsKey(uuid)) {
map = chatAliases.get(uuid);
}
map.put(alias, command);
chatAliases.put(uuid, map);
}
public static Map<String, String> 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<String> errors = new ArrayList<>();
try {
List<?> aliases = getConfig().getList("aliases");
for (int i = 0; i < aliases.size(); i++) {
try {
LinkedHashMap<String, String> alias = (LinkedHashMap<String, String>) 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);
}
}