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

162 lines
5.2 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.CommandMixtape;
2018-10-19 23:43:36 +00:00
import xyz.etztech.mixtape.commands.CommandSlashAlias;
2018-09-12 15:56:08 +00:00
import xyz.etztech.mixtape.listeners.CommandPreprocessListener;
2018-10-19 23:43:36 +00:00
import xyz.etztech.mixtape.listeners.PlayerJoinListener;
2018-09-12 15:56:08 +00:00
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;
2018-10-19 23:43:36 +00:00
private static Database database;
2018-09-12 15:56:08 +00:00
private static int loops;
2018-09-12 15:56:08 +00:00
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();
2018-10-19 23:43:36 +00:00
database = new Database(this);
2018-09-12 15:56:08 +00:00
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);
2018-10-19 23:43:36 +00:00
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
2018-09-12 15:56:08 +00:00
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
@Override
public void reloadConfig() {
super.reloadConfig();
config = Bukkit.getPluginManager().getPlugin("Mixtape").getConfig();
loops = config.getInt("loop");
2018-09-12 15:56:08 +00:00
validate();
}
2018-10-19 23:43:36 +00:00
2018-09-12 15:56:08 +00:00
public void log(String message) {
log.info( "[Mixtape]: " + message );
}
public static Mixtape getInstance() {
return instance;
}
2018-10-19 23:43:36 +00:00
public static Database getDatabase() {
return database;
}
public static void loadAliases(String uuid) {
Map<String, String> chats = new HashMap<>();
Map<String, String> commands = new HashMap<>();
List<Alias> 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));
}
2018-09-12 15:56:08 +00:00
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);
2018-10-19 23:43:36 +00:00
database.insert(uuid, alias, command, Database.AliasType.COMMAND);
2018-09-12 15:56:08 +00:00
}
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);
2018-10-19 23:43:36 +00:00
database.insert(uuid, alias, command, Database.AliasType.CHAT);
2018-09-12 15:56:08 +00:00
}
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;
}
public static int getLoops() {
return loops;
}
2018-09-12 15:56:08 +00:00
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);
}
}