diff --git a/src/main/java/com/redstoner/modules/datamanager/DataManager.java b/src/main/java/com/redstoner/modules/datamanager/DataManager.java index 63ce1ce..0129a53 100644 --- a/src/main/java/com/redstoner/modules/datamanager/DataManager.java +++ b/src/main/java/com/redstoner/modules/datamanager/DataManager.java @@ -2,7 +2,6 @@ package com.redstoner.modules.datamanager; import java.io.File; import java.io.FilenameFilter; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -37,58 +36,59 @@ import com.redstoner.modules.Module; @Commands(CommandHolderType.Stream) @AutoRegisterListener @Version(major = 4, minor = 1, revision = 10, compatible = 4) -public final class DataManager implements CoreModule, Listener -{ +public final class DataManager implements CoreModule, Listener { protected final File dataFolder = new File(Main.plugin.getDataFolder(), "data"); + protected JSONObject data = new JSONObject(); protected JSONObject config_data; + protected ArrayList module_index; - int old_hash = 0; - protected HashMap> states = new HashMap<>(); - private static DataManager previous_instance = null; protected ArrayList subcommands; + protected List scheduled_saves = new ArrayList<>(); + + protected HashMap> states = new HashMap<>(); + + private static DataManager previous_instance = null; + + int old_hash = 0; int task_id; - + @Override - public void postEnable() - { - if (!dataFolder.exists()) - dataFolder.mkdirs(); - for (Player p : Bukkit.getOnlinePlayers()) - { + public void postEnable() { + if (!dataFolder.exists()) dataFolder.mkdirs(); + + for (Player p : Bukkit.getOnlinePlayers()) { loadData_(p.getUniqueId().toString()); } + subcommands = new ArrayList<>(); subcommands.add("list"); subcommands.add("get"); subcommands.add("set"); subcommands.add("remove"); - if (previous_instance == null) - states.put(getID(Bukkit.getConsoleSender()), new HashMap()); - else - { + + if (previous_instance == null) states.put(getID(Bukkit.getConsoleSender()), new HashMap()); + else { this.states = previous_instance.states; previous_instance = null; } + config_data = JsonManager.getObject(new File(dataFolder, "configs.json")); - if (config_data == null) - config_data = new JSONObject(); + if (config_data == null) config_data = new JSONObject(); + fixJson(); updateIndex(); + CommandManager.registerCommand(getClass().getResourceAsStream("DataManager.cmd"), this, Main.plugin); - + // Schedule save every ten seconds - task_id = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.plugin, new Runnable() - { + task_id = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.plugin, new Runnable() { @Override - public void run() - { - for (String id : scheduled_saves) - { + public void run() { + for (String id : scheduled_saves) { Object raw = data.get(id); - if (raw == null || ((JSONObject) raw).size() == 0) - continue; + if (raw == null || ((JSONObject) raw).size() == 0) continue; JSONObject json = (JSONObject) raw; JsonManager.save(json, new File(dataFolder, id + ".json")); } @@ -96,934 +96,793 @@ public final class DataManager implements CoreModule, Listener } }, 0, 20).getTaskId(); } - + @Override - public void onDisable() - { + public void onDisable() { previous_instance = this; - for (Player p : Bukkit.getOnlinePlayers()) - { + + for (Player p : Bukkit.getOnlinePlayers()) { saveAndUnload(p); } + JsonManager.save(config_data, new File(dataFolder, "configs.json")); Bukkit.getScheduler().cancelTask(task_id); } - + @Command(hook = "import_file") - public boolean importFile(CommandSender sender, String file, String module) - { - try - { + public boolean importFile(CommandSender sender, String file, String module) { + try { JSONObject object = JsonManager.getObject(new File(file)); importObject_(module, object); - } - catch (Exception e) - { + } catch (Exception e) { getLogger().error("Could not import data!"); } + return true; } - + @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) - { + public void onPlayerJoin(PlayerJoinEvent event) { loadData_(event.getPlayer().getUniqueId().toString()); } - + @EventHandler - public void onPlayerQuit(PlayerQuitEvent event) - { + public void onPlayerQuit(PlayerQuitEvent event) { saveAndUnload(event.getPlayer()); } - - public static void loadData(CommandSender sender) - { + + public static void loadData(CommandSender sender) { loadData(getID(sender)); } - - public static void loadData(String id) - { - try - { + + public static void loadData(String id) { + try { Module mod = ModuleLoader.getModule("DataManager"); Method m = mod.getClass().getDeclaredMethod("loadData_", String.class); + m.setAccessible(true); m.invoke(mod, id); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected synchronized void loadData_(String id) - { + protected synchronized void loadData_(String id) { JSONObject playerData = JsonManager.getObject(new File(dataFolder, id + ".json")); - if (playerData == null) - playerData = new JSONObject(); + if (playerData == null) playerData = new JSONObject(); + data.put(id.toString(), playerData); states.put(id.toString(), new HashMap()); } - - public static Object getOrDefault(CommandSender sender, String key, Object fallback) - { + + public static Object getOrDefault(CommandSender sender, String key, Object fallback) { return getOrDefault(getID(sender), Utils.getCaller("DataManager"), key, fallback); } - - public static Object getOrDefault(String id, String key, Object fallback) - { + + public static Object getOrDefault(String id, String key, Object fallback) { return getOrDefault(id, Utils.getCaller("DataManager"), key, fallback); } - - public static Object getOrDefault(String id, String module, String key, Object fallback) - { - try - { + + public static Object getOrDefault(String id, String module, String key, Object fallback) { + try { Module mod = ModuleLoader.getModule("DataManager"); - Method m = mod.getClass().getDeclaredMethod("getOrDefault_", String.class, String.class, String.class, - Object.class); + + Method m = mod.getClass().getDeclaredMethod("getOrDefault_", String.class, String.class, String.class, Object.class); m.setAccessible(true); + return m.invoke(mod, id, module, key, fallback); + } catch (Exception e) { + return fallback; } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return fallback; } - - protected Object getOrDefault_(String id, String module, String key, Object fallback) - { + + protected Object getOrDefault_(String id, String module, String key, Object fallback) { Object o = getData_(id, module, key); return o == null ? fallback : o; } - - public static Object getData(CommandSender sender, String key) - { + + public static Object getData(CommandSender sender, String key) { return getData(getID(sender), Utils.getCaller("DataManager"), key); } - - public static Object getData(String id, String key) - { + + public static Object getData(String id, String key) { return getData(id, Utils.getCaller("DataManager"), key); } - - public static Object getData(String id, String module, String key) - { - try - { + + public static Object getData(String id, String module, String key) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("getData_", String.class, String.class, String.class); m.setAccessible(true); + return m.invoke(mod, id, module, key); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return null; - } - - protected Object getData_(String id, String module, String key) - { - if (data.containsKey(id)) - { - JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); - if (moduleData == null) - return null; - if (key == null) - return moduleData; - else - return moduleData.get(key); - } - else - return loadAndGet(id, module, key); - } - - protected synchronized Object loadAndGet(String id, String module, String key) - { - JSONObject playerData = JsonManager.getObject(new File(dataFolder, id + ".json")); - if (playerData == null) + } catch (Exception e) { return null; - if (key == null) - return playerData.get(module); - else - return ((JSONObject) playerData.get(module)).get(key); + } } - - public static void setData(CommandSender sender, String key, Object value) - { + + protected Object getData_(String id, String module, String key) { + if (data.containsKey(id)) { + JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); + + if (moduleData == null) return null; + if (key == null) return moduleData; + else return moduleData.get(key); + } else { + return loadAndGet(id, module, key); + } + } + + protected synchronized Object loadAndGet(String id, String module, String key) { + JSONObject playerData = JsonManager.getObject(new File(dataFolder, id + ".json")); + + if (playerData == null) return null; + if (key == null) return playerData.get(module); + else return ((JSONObject) playerData.get(module)).get(key); + } + + public static void setData(CommandSender sender, String key, Object value) { setData(getID(sender), Utils.getCaller("DataManager"), key, value); } - - public static void setData(String id, String key, Object value) - { + + public static void setData(String id, String key, Object value) { setData(id, Utils.getCaller("DataManager"), key, value); } - - public static void setData(String id, String module, String key, Object value) - { - try - { + + public static void setData(String id, String module, String key, Object value) { + try { Module mod = ModuleLoader.getModule("DataManager"); - Method m = mod.getClass().getDeclaredMethod("setData_", String.class, String.class, String.class, - Object.class); + + Method m = mod.getClass().getDeclaredMethod("setData_", String.class, String.class, String.class, Object.class); m.setAccessible(true); + m.invoke(mod, id, module, key, value); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected void setData_(String id, String module, String key, Object value) - { - if (data.containsKey(id)) - { + protected void setData_(String id, String module, String key, Object value) { + if (data.containsKey(id)) { JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); - if (moduleData == null) - { + + if (moduleData == null) { moduleData = new JSONObject(); ((JSONObject) data.get(id)).put(module, moduleData); } - if (key == null) - setDirectly_(id, module, value); - else - moduleData.put(key, value); + + if (key == null) setDirectly_(id, module, value); + else moduleData.put(key, value); + save_(id); - } - else + } else { loadAndSet(id, module, key, value); + } } - - public static void setDirectly(CommandSender sender, Object value) - { + + public static void setDirectly(CommandSender sender, Object value) { setDirectly(getID(sender), Utils.getCaller("DataManager"), value); } - - public static void setDirectly(String id, Object value) - { + + public static void setDirectly(String id, Object value) { setDirectly(id, Utils.getCaller("DataManager"), value); } - - public static void setDirectly(String id, String module, Object value) - { - try - { + + public static void setDirectly(String id, String module, Object value) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("setDirectly_", String.class, String.class, Object.class); m.setAccessible(true); + m.invoke(mod, id, module, value); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected void setDirectly_(String id, String module, Object value) - { - if (data.containsKey(id)) - { + protected void setDirectly_(String id, String module, Object value) { + if (data.containsKey(id)) { JSONObject playerdata = (JSONObject) data.get(id); playerdata.put(module, value); + save_(id); - } - else + } else { loadAndSetDirectly(id, module, value); + } } - + @SuppressWarnings("unchecked") - protected synchronized void loadAndSet(String id, String module, String key, Object value) - { + protected synchronized void loadAndSet(String id, String module, String key, Object value) { File dataFile = new File(dataFolder, id + ".json"); + JSONObject playerData = JsonManager.getObject(dataFile); - if (playerData == null) - playerData = new JSONObject(); + if (playerData == null) playerData = new JSONObject(); + JSONObject moduleData = ((JSONObject) playerData.get(module)); - if (moduleData == null) - { + if (moduleData == null) { moduleData = new JSONObject(); playerData.put(module, moduleData); } + moduleData.put(key, value); + JsonManager.save(playerData, dataFile); } - + @SuppressWarnings("unchecked") - protected void loadAndSetDirectly(String id, String module, Object value) - { + protected void loadAndSetDirectly(String id, String module, Object value) { File dataFile = new File(dataFolder, id + ".json"); + JSONObject playerData = JsonManager.getObject(dataFile); - if (playerData == null) - playerData = new JSONObject(); + if (playerData == null) playerData = new JSONObject(); + playerData.put(module, value); + JsonManager.save(playerData, dataFile); } - - public static void removeData(CommandSender sender, String key) - { + + public static void removeData(CommandSender sender, String key) { removeData(getID(sender), Utils.getCaller("DataManager"), key); } - - public static void removeData(String id, String key) - { + + public static void removeData(String id, String key) { removeData(id, Utils.getCaller("DataManager"), key); } - - public static void removeData(String id, String module, String key) - { - try - { + + public static void removeData(String id, String module, String key) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("removeData_", String.class, String.class, String.class); m.setAccessible(true); + m.invoke(mod, id, module, key); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected void removeData_(String id, String module, String key) - { - if (data.containsKey(id)) - { + protected void removeData_(String id, String module, String key) { + if (data.containsKey(id)) { JSONObject moduleData = ((JSONObject) ((JSONObject) data.get(id)).get(module)); - if (moduleData == null) - return; + if (moduleData == null) return; + moduleData.remove(key); data.put(module, data); + save_(id); - } - else + } else { loadAndRemove(id, module, key); + } } - - protected synchronized void loadAndRemove(String id, String module, String key) - { + + protected synchronized void loadAndRemove(String id, String module, String key) { File dataFile = new File(dataFolder, id + ".json"); + JSONObject playerData = JsonManager.getObject(dataFile); - if (playerData == null) - return; + if (playerData == null) return; + JSONObject moduleData = ((JSONObject) playerData.get(module)); - if (moduleData == null) - return; + if (moduleData == null) return; + moduleData.remove(key); + JsonManager.save(playerData, dataFile); } - - public static void importObject(JSONObject object) - { + + public static void importObject(JSONObject object) { importObject(object, Utils.getCaller("DataManager")); } - - public static void importObject(JSONObject object, String module) - { - try - { + + public static void importObject(JSONObject object, String module) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("importObject_", String.class, String.class, String.class); m.setAccessible(true); + m.invoke(mod, module, object); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - - protected void importObject_(String module, JSONObject object) - { - for (Object o : object.keySet()) - { + + protected void importObject_(String module, JSONObject object) { + for (Object o : object.keySet()) { String uid = null; - if (o instanceof String) - uid = (String) o; - else if (o instanceof UUID) - uid = ((UUID) o).toString(); - if (uid == null) - continue; + if (o instanceof String) uid = (String) o; + else if (o instanceof UUID) uid = ((UUID) o).toString(); + + if (uid == null) continue; + setDirectly_(uid, module, object.get(o)); } } - - public static void migrateAll(String oldName) - { + + public static void migrateAll(String oldName) { migrateAll(oldName, Utils.getCaller("DataManager")); } - - public static void migrateAll(String oldName, String newName) - { - try - { + + public static void migrateAll(String oldName, String newName) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("migrateAll_", String.class, String.class); m.setAccessible(true); + m.invoke(mod, oldName, newName); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - - protected void migrateAll_(String oldName, String newName) - { - for (String s : dataFolder.list(new FilenameFilter() - { + + protected void migrateAll_(String oldName, String newName) { + for (String s : dataFolder.list(new FilenameFilter() { @Override - public boolean accept(File dir, String name) - { + public boolean accept(File dir, String name) { return name.endsWith(".json"); } - })) - { + })) { migrate_(s.replace(".json", ""), oldName, newName); } } - - public static void migrate(String id, String oldName) - { + + public static void migrate(String id, String oldName) { migrate(id, oldName, Utils.getCaller("DataManager")); } - - public static void migrate(String id, String oldName, String newName) - { - try - { + + public static void migrate(String id, String oldName, String newName) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("migrate_", String.class, String.class, String.class); m.setAccessible(true); + m.invoke(mod, id, oldName, newName); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected void migrate_(String id, String oldName, String newName) - { - if (data.containsKey(id)) - { + protected void migrate_(String id, String oldName, String newName) { + if (data.containsKey(id)) { data.put(newName, data.get(oldName)); data.remove(oldName); + save_(id); - } - else + } else { loadAndMigrate(id, oldName, newName); + } } - + @SuppressWarnings("unchecked") - protected void loadAndMigrate(String id, String oldName, String newName) - { + protected void loadAndMigrate(String id, String oldName, String newName) { File dataFile = new File(dataFolder, id + ".json"); JSONObject data = JsonManager.getObject(dataFile); + data.put(newName, data.get(oldName)); data.remove(oldName); + JsonManager.save(data, dataFile); } - - public static void save(CommandSender sender) - { - try - { + + public static void save(CommandSender sender) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("save_", CommandSender.class); m.setAccessible(true); + m.invoke(mod, sender); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - - protected void save_(CommandSender sender) - { + + protected void save_(CommandSender sender) { save_(getID(sender)); } - - public static void save(String id) - { - try - { + + public static void save(String id) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("save_", String.class); m.setAccessible(true); + m.invoke(mod, id); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - - protected void save_(String id) - { + + protected void save_(String id) { scheduled_saves.add(id); } - - protected void saveAndUnload(CommandSender sender) - { + + protected void saveAndUnload(CommandSender sender) { saveAndUnload(getID(sender)); states.remove(getID(sender)); } - - protected void saveAndUnload(String id) - { + + protected void saveAndUnload(String id) { save_(id); data.remove(id); } - - private static String getID(CommandSender sender) - { - String id; - if (sender instanceof Player) - id = ((Player) sender).getUniqueId().toString(); - else - id = "CONSOLE"; - return id; + + private static String getID(CommandSender sender) { + if (sender instanceof Player) return ((Player) sender).getUniqueId().toString(); + else return "CONSOLE"; } - - public static void setState(CommandSender sender, String key, boolean value) - { - try - { + + public static void setState(CommandSender sender, String key, boolean value) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("setState_", CommandSender.class, String.class, boolean.class); m.setAccessible(true); + m.invoke(mod, sender, key, value); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - - protected void setState_(CommandSender sender, String key, boolean value) - { + + protected void setState_(CommandSender sender, String key, boolean value) { String id = getID(sender); + HashMap lstates = states.get(id); lstates.put(key, value); + states.put(id, lstates); } - - public static boolean getState(CommandSender sender, String key) - { - try - { + + public static boolean getState(CommandSender sender, String key) { + try { Module mod = ModuleLoader.getModule("DataManager"); + Method m = mod.getClass().getDeclaredMethod("getState_", CommandSender.class, String.class); m.setAccessible(true); + return (boolean) m.invoke(mod, sender, key); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return false; - } - - protected boolean getState_(CommandSender sender, String key) - { - String id = getID(sender); - HashMap lstates = states.get(id); - if (lstates == null) + } catch (Exception e) { return false; + } + } + + protected boolean getState_(CommandSender sender, String key) { + String id = getID(sender); + + HashMap lstates = states.get(id); + if (lstates == null) return false; + return lstates.containsKey(key) ? lstates.get(key) : false; } - - protected boolean hasConfigChanged() - { + + protected boolean hasConfigChanged() { return old_hash != config_data.hashCode(); } - - protected void updateIndex() - { - if (!hasConfigChanged()) - return; + + protected void updateIndex() { + if (!hasConfigChanged()) return; + old_hash = config_data.hashCode(); module_index = new ArrayList<>(); - if (config_data.size() > 0) - { - for (Object key : config_data.keySet()) + + if (config_data.size() > 0) { + for (Object key : config_data.keySet()) { module_index.add(key.toString()); + } } } - + @SuppressWarnings("unchecked") - protected void fixJson() - { - for (Object key : config_data.keySet()) - { + protected void fixJson() { + for (Object key : config_data.keySet()) { JSONObject json = (JSONObject) config_data.get(key); - for (Object key2 : json.keySet()) - { + + for (Object key2 : json.keySet()) { Object o = json.get(key2); - if (!(o instanceof ConfigEntry)) - json.put(key2, new ConfigEntry((JSONObject) o)); + + if (!(o instanceof ConfigEntry)) json.put(key2, new ConfigEntry((JSONObject) o)); } + config_data.put(key, json); } } - - private List subsetWhereStartsWith(List list, String prefix) - { + + private List subsetWhereStartsWith(List list, String prefix) { ArrayList subset = new ArrayList<>(); - if (prefix == null || prefix.equals("")) - return list; - for (String s : list) - if (s.toLowerCase().startsWith(prefix.toLowerCase())) + + if (prefix == null || prefix.equals("")) return list; + + for (String s : list) { + if (s.toLowerCase().startsWith(prefix.toLowerCase())) { subset.add(s); + } + } + return subset; } - + @SuppressWarnings("unchecked") @EventHandler - public void onTabComplete(TabCompleteEvent event) - { - if (event.getBuffer().toLowerCase().matches("^/?settings? .*") - || event.getBuffer().toLowerCase().matches("^/?configs? .*")) - { + public void onTabComplete(TabCompleteEvent event) { + if (event.getBuffer().toLowerCase().matches("^/?settings? .*") || event.getBuffer().toLowerCase().matches("^/?configs? .*")) { boolean argument_complete = event.getBuffer().endsWith(" "); String[] arguments = event.getBuffer().split(" "); + event.setCompletions(new ArrayList()); - if (arguments.length == 1 || (arguments.length == 2 && !argument_complete)) + + if (arguments.length == 1 || (arguments.length == 2 && !argument_complete)) { event.setCompletions(subsetWhereStartsWith(subcommands, arguments.length >= 2 ? arguments[1] : "")); - else if (arguments.length == 2 || (arguments.length == 3 && !argument_complete)) - { - switch (arguments[1].toLowerCase()) - { + } else if (arguments.length == 2 || (arguments.length == 3 && !argument_complete)) { + switch (arguments[1].toLowerCase()) { case "list": case "get": - case "set": - { - event.setCompletions( - subsetWhereStartsWith(module_index, arguments.length == 3 ? arguments[2] : "")); + case "set": { + event.setCompletions(subsetWhereStartsWith(module_index, arguments.length == 3 ? arguments[2] : "")); break; } } - } - else if ((arguments.length == 3 && argument_complete) || (arguments.length == 4 && !argument_complete)) - { - switch (arguments[1].toLowerCase()) - { + } else if ((arguments.length == 3 && argument_complete) || (arguments.length == 4 && !argument_complete)) { + switch (arguments[1].toLowerCase()) { case "get": - case "set": - { + case "set": { Object o = config_data.get(arguments[2]); - if (o == null) - break; - event.setCompletions(subsetWhereStartsWith(new ArrayList(((JSONObject) o).keySet()), - arguments.length == 4 ? arguments[3] : "")); + if (o == null) break; + event.setCompletions( + subsetWhereStartsWith(new ArrayList(((JSONObject) o).keySet()), arguments.length == 4 ? arguments[3] : "")); break; } } - } - else - { - if (arguments[1].toLowerCase().equals("set")) - { + } else { + if (arguments[1].toLowerCase().equals("set")) { Object o = config_data.get(arguments[2]); - if (o == null) - return; + if (o == null) return; + Object o2 = ((JSONObject) o).get(arguments[3]); - if (o2 == null) - return; + if (o2 == null) return; + event.setCompletions(subsetWhereStartsWith(Arrays.asList(((ConfigEntry) o2).getCompleteOptions()), - arguments.length > 4 ? String.join(" ", Arrays.copyOfRange(arguments, 4, arguments.length)) - : "")); + arguments.length > 4 ? String.join(" ", Arrays.copyOfRange(arguments, 4, arguments.length)) : "")); } } } } - + @Command(hook = "config_list") - public boolean list(CommandSender sender) - { + public boolean list(CommandSender sender) { getLogger().message(sender, Arrays.toString(module_index.toArray(new String[] {}))); return true; } - + @Command(hook = "config_list2") - public boolean list(CommandSender sender, String module) - { + public boolean list(CommandSender sender, String module) { Object o = config_data.get(module); - if (o == null) - { + + if (o == null) { getLogger().message(sender, "This module has not registered any settings."); - } - else - { + } else { ArrayList entries = new ArrayList<>(); JSONObject json = (JSONObject) o; - for (Object key : json.keySet()) - { + + for (Object key : json.keySet()) { String entry = key.toString(); entries.add("§e" + entry + "§7"); } - getLogger().message(sender, "The module §e" + module + "§7 has the following config settings: ", - Arrays.toString(entries.toArray(new String[] {}))); + + getLogger().message(sender, "The module §e" + module + "§7 has the following config settings: ", Arrays.toString(entries.toArray(new String[] {}))); } + return true; } - + @Command(hook = "config_get") - public boolean get(CommandSender sender, String module, String key) - { - getLogger().message(sender, new String[] {"§e" + module + "." + key + "§7 currently holds the value:", - getConfigOrDefault_(module, key, "").toString()}); + public boolean get(CommandSender sender, String module, String key) { + getLogger().message(sender, + new String[] { "§e" + module + "." + key + "§7 currently holds the value:", getConfigOrDefault_(module, key, "").toString() }); return true; } - + @Command(hook = "config_set") - public boolean set(CommandSender sender, String module, String key, String value) - { - if (setConfig_(module, key, value)) - { + public boolean set(CommandSender sender, String module, String key, String value) { + if (setConfig_(module, key, value)) { getLogger().message(sender, "Successfully changed the value for §e" + module + "." + key); + } else { + getLogger().message(sender, true, "§7\"§e" + value + "§7\" is not a valid value for setting §e" + module + "." + key); } - else - { - getLogger().message(sender, true, - "§7\"§e" + value + "§7\" is not a valid value for setting §e" + module + "." + key); - } + return true; } - + @Command(hook = "config_remove_all") - public boolean remove_all(CommandSender sender, String module) - { - if (removeAllConfig_(module)) - getLogger().message(sender, "Successfully deleted all config entries for module §e" + module + "§7!"); - else - getLogger().message(sender, true, "Could not delete all config entries for module §e" + module + "§7!"); + public boolean remove_all(CommandSender sender, String module) { + if (removeAllConfig_(module)) getLogger().message(sender, "Successfully deleted all config entries for module §e" + module + "§7!"); + else getLogger().message(sender, true, "Could not delete all config entries for module §e" + module + "§7!"); + return true; } - - public static Object getConfigOrDefault(String key, Object fallback) - { + + public static Object getConfigOrDefault(String key, Object fallback) { return getConfigOrDefault(Utils.getCaller("DataManager"), key, fallback); } - - public static Object getConfigOrDefault(String module, String key, Object fallback) - { - try - { + + public static Object getConfigOrDefault(String module, String key, Object fallback) { + try { Module mod = ModuleLoader.getModule("DataManager"); - Method m = mod.getClass().getDeclaredMethod("getConfigOrDefault_", String.class, String.class, - Object.class); + + Method m = mod.getClass().getDeclaredMethod("getConfigOrDefault_", String.class, String.class, Object.class); m.setAccessible(true); + return m.invoke(mod, module, key, fallback); + } catch (Exception e) { + return fallback; } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return fallback; } - - protected Object getConfigOrDefault_(String module, String key, Object fallback) - { + + protected Object getConfigOrDefault_(String module, String key, Object fallback) { Object o = getConfigData_(module, key); return o == null ? fallback : o; } - - protected Object getConfigData_(String module, String key) - { + + protected Object getConfigData_(String module, String key) { module = module.toLowerCase(); Object o = config_data.get(module); - if (o == null) - return null; - else - { - JSONObject json = (JSONObject) o; - Object o2 = json.get(key); - if (o2 == null) - return null; - return ((ConfigEntry) o2).getValue(); - } + + if (o == null) return null; + + JSONObject json = (JSONObject) o; + Object o2 = json.get(key); + + if (o2 == null) return null; + + return ((ConfigEntry) o2).getValue(); } - - protected ConfigEntry getConfigEntry_(String module, String key) - { + + protected ConfigEntry getConfigEntry_(String module, String key) { module = module.toLowerCase(); Object o = config_data.get(module); - if (o == null) - return null; - else - { - JSONObject json = (JSONObject) o; - return (ConfigEntry) json.get(key); - } + + if (o == null) return null; + + JSONObject json = (JSONObject) o; + return (ConfigEntry) json.get(key); } - - public static void setConfig(String key, String value) - { + + public static void setConfig(String key, String value) { setConfig(Utils.getCaller("DataManager"), key, value, null); } - - public static void setConfig(String key, String value, String[] complete_options) - { + + public static void setConfig(String key, String value, String[] complete_options) { setConfig(Utils.getCaller("DataManager"), key, value, complete_options); } - - public static void setConfig(String module, String key, String value, String[] complete_options) - { - try - { + + public static void setConfig(String module, String key, String value, String[] complete_options) { + try { Module mod = ModuleLoader.getModule("DataManager"); - Method m = mod.getClass().getDeclaredMethod("setConfig_", String.class, String.class, String.class, - String[].class); + + Method m = mod.getClass().getDeclaredMethod("setConfig_", String.class, String.class, String.class, String[].class); m.setAccessible(true); + m.invoke(mod, module, key, value, complete_options); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} + } catch (Exception e) {} } - + @SuppressWarnings("unchecked") - protected boolean setConfig_(String module, String key, String value) - { + protected boolean setConfig_(String module, String key, String value) { module = module.toLowerCase(); ConfigEntry entry = getConfigEntry_(module, key); - if (entry == null) - entry = new ConfigEntry(value, null); - if (entry.attemptSet(value)) - { - Object o = config_data.get(module); - JSONObject json; - if (o == null) - json = new JSONObject(); - else - json = (JSONObject) o; - json.put(key, entry); - config_data.put(module, json); - updateIndex(); - return true; - } - else - return false; - } - - @SuppressWarnings("unchecked") - protected void setConfig_(String module, String key, String value, String[] complete_options) - { - module = module.toLowerCase(); - ConfigEntry entry = new ConfigEntry(value, complete_options); + + if (entry == null) entry = new ConfigEntry(value, null); + if (!entry.attemptSet(value)) return false; + Object o = config_data.get(module); JSONObject json; - if (o == null) - json = new JSONObject(); - else - json = (JSONObject) o; + + if (o == null) json = new JSONObject(); + else json = (JSONObject) o; + json.put(key, entry); config_data.put(module, json); - updateIndex(); - } - - public static boolean removeConfig(String key) - { - return removeConfig(Utils.getCaller("DataManager"), key); - } - - public static boolean removeConfig(String module, String key) - { - try - { - Module mod = ModuleLoader.getModule("DataManager"); - Method m = mod.getClass().getDeclaredMethod("removeConfig_", String.class, String.class); - m.setAccessible(true); - return (boolean) m.invoke(mod, module, key); - } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return false; - } - - @SuppressWarnings("unchecked") - protected boolean removeConfig_(String module, String key) - { - module = module.toLowerCase(); - if (key == null) - return removeAllConfig_(module); - Object o = config_data.get(module); - JSONObject json; - if (o == null) - return false; - else - json = (JSONObject) o; - json.remove(key); - if (json.size() == 0) - config_data.remove(module); - else - config_data.put(module, json); + updateIndex(); return true; } - - protected boolean removeAllConfig_(String module) - { + + @SuppressWarnings("unchecked") + protected void setConfig_(String module, String key, String value, String[] complete_options) { module = module.toLowerCase(); - if (config_data.remove(module) == null) + ConfigEntry entry = new ConfigEntry(value, complete_options); + + Object o = config_data.get(module); + JSONObject json; + + if (o == null) json = new JSONObject(); + else json = (JSONObject) o; + + json.put(key, entry); + config_data.put(module, json); + + updateIndex(); + } + + public static boolean removeConfig(String key) { + return removeConfig(Utils.getCaller("DataManager"), key); + } + + public static boolean removeConfig(String module, String key) { + try { + Module mod = ModuleLoader.getModule("DataManager"); + + Method m = mod.getClass().getDeclaredMethod("removeConfig_", String.class, String.class); + m.setAccessible(true); + + return (boolean) m.invoke(mod, module, key); + } catch (Exception e) { return false; + } + } + + @SuppressWarnings("unchecked") + protected boolean removeConfig_(String module, String key) { + module = module.toLowerCase(); + + if (key == null) return removeAllConfig_(module); + + Object o = config_data.get(module); + JSONObject json; + + if (o == null) return false; + else json = (JSONObject) o; + + json.remove(key); + + if (json.size() == 0) config_data.remove(module); + else config_data.put(module, json); + + updateIndex(); + return true; + } + + protected boolean removeAllConfig_(String module) { + module = module.toLowerCase(); + + if (config_data.remove(module) == null) return false; + updateIndex(); return true; } } -class ConfigEntry -{ +class ConfigEntry { private String value; private String[] complete_options; - - public ConfigEntry(String value, String[] complete_options) - { + + public ConfigEntry(String value, String[] complete_options) { this.value = value; this.complete_options = complete_options; } - + @SuppressWarnings("unchecked") - public ConfigEntry(JSONObject json) - { - this(json.get("value").toString(), - (String[]) ((JSONArray) json.get("complete_options")).toArray(new String[] {})); + public ConfigEntry(JSONObject json) { + this(json.get("value").toString(), (String[]) ((JSONArray) json.get("complete_options")).toArray(new String[] {})); } - - protected boolean attemptSet(String value) - { - if (complete_options == null || complete_options.length == 0) - { + + protected boolean attemptSet(String value) { + if (complete_options == null || complete_options.length == 0) { this.value = value; return true; - } - else - { - for (String s : complete_options) - { - if (s.equals(value)) - { + } else { + for (String s : complete_options) { + if (s.equals(value)) { this.value = value; return true; } } + return false; } } - - protected String[] getCompleteOptions() - { + + protected String[] getCompleteOptions() { return complete_options; } - - protected String getValue() - { + + protected String getValue() { return value; } - + @Override - public String toString() - { + public String toString() { return "{\"value\":\"" + value + "\",\"complete_options\":" - + (complete_options == null || complete_options.length == 0 ? "[]" - : "[\"" + String.join("\",\"", complete_options) + "\"]") - + "}"; + + (complete_options == null || complete_options.length == 0 ? "[]" : "[\"" + String.join("\",\"", complete_options) + "\"]") + "}"; } } diff --git a/src/main/java/com/redstoner/modules/discord/Discord.java b/src/main/java/com/redstoner/modules/discord/Discord.java index 8a7e265..4a1bf04 100644 --- a/src/main/java/com/redstoner/modules/discord/Discord.java +++ b/src/main/java/com/redstoner/modules/discord/Discord.java @@ -35,6 +35,7 @@ public class Discord implements Module { @Override public boolean onEnable() { Config config; + try { config = Config.getConfig("Discord.json"); } catch (IOException | org.json.simple.parser.ParseException e1) { @@ -42,20 +43,26 @@ public class Discord implements Module { return false; } - if (config == null || !config.containsKey("database") || !config.containsKey("table") - || !config.containsKey("inviteLink")) { + if (config == null || !config.containsKey("database") || !config.containsKey("table") || !config.containsKey("inviteLink")) { getLogger().error("Could not load the Discord config file, disabling!"); + config.put("database", "redstoner"); config.put("table", "discord"); config.put("inviteLink", "https://discord.gg/example"); + return false; } + inviteLink = config.get("inviteLink"); + try { MysqlDatabase database = MysqlHandler.INSTANCE.getDatabase(config.get("database") + "?autoReconnect=true"); + MysqlField uuid = new MysqlField("uuid", new VarChar(36), false); MysqlField pass = new MysqlField("token", new VarChar(8), false); + database.createTableIfNotExists((String) config.get("table"), uuid, pass); + table = database.getTable(config.get("table")); } catch (NullPointerException e) { getLogger().error("Could not use the Discord config, aborting!"); @@ -82,8 +89,7 @@ public class Discord implements Module { tries++; } - if (tries > 10) - break; + if (tries > 10) break; } if (token == null) { @@ -97,11 +103,8 @@ public class Discord implements Module { table.insert(pUUID, token); new Message(sender, null).appendText("\n&cRedstoner&7 has a &2Discord&7 Now! \nClick ") - .appendLinkHover("&e" + inviteLink, inviteLink, "&aClick to Join") - .appendText("&7 to join. \n\nTo sync you rank, copy ") - .appendSuggestHover("&e" + token, token, "&aClick to Copy") - .appendText("&7 into &3#rank-sync&7.\n") - .send(); + .appendLinkHover("&e" + inviteLink, inviteLink, "&aClick to Join").appendText("&7 to join. \n\nTo sync you rank, copy ") + .appendSuggestHover("&e" + token, token, "&aClick to Copy").appendText("&7 into &3#rank-sync&7.\n").send(); } private String randomToken(int length) { @@ -113,4 +116,4 @@ public class Discord implements Module { return sb.toString(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/redstoner/modules/friends/Friends.java b/src/main/java/com/redstoner/modules/friends/Friends.java index 2eb9728..b55770f 100644 --- a/src/main/java/com/redstoner/modules/friends/Friends.java +++ b/src/main/java/com/redstoner/modules/friends/Friends.java @@ -1,6 +1,5 @@ package com.redstoner.modules.friends; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Set; import java.util.UUID; @@ -31,330 +30,316 @@ import com.redstoner.modules.datamanager.DataManager; @AutoRegisterListener @Commands(CommandHolderType.File) @Version(major = 4, minor = 0, revision = 0, compatible = 4) -public class Friends implements CoreModule -{ +public class Friends implements CoreModule { @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerJoin(PlayerJoinEvent e) - { + public void onPlayerJoin(PlayerJoinEvent e) { JSONArray friended_by = (JSONArray) DataManager.getOrDefault(e.getPlayer(), "friended_by", new JSONArray()); - for (Object obj : friended_by) - { + + for (Object obj : friended_by) { UUID uuid = UUID.fromString((String) obj); Player p = Bukkit.getPlayer(uuid); - if (p != null && p.canSee(e.getPlayer())) - { + + if (p != null && p.canSee(e.getPlayer())) { getLogger().message(p, "Your friend &e" + e.getPlayer().getDisplayName() + "&7 just joined!"); p.playSound(p.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1, 1); } } - JSONArray notifications = (JSONArray) DataManager.getOrDefault(e.getPlayer(), "scheduled_notifications", - new JSONArray()); - for (Object obj : notifications) + + JSONArray notifications = (JSONArray) DataManager.getOrDefault(e.getPlayer(), "scheduled_notifications", new JSONArray()); + + for (Object obj : notifications) { getLogger().message(e.getPlayer(), (String) obj); + } } - + @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerLeave(PlayerQuitEvent e) - { + public void onPlayerLeave(PlayerQuitEvent e) { JSONArray friended_by = (JSONArray) DataManager.getOrDefault(e.getPlayer(), "friended_by", new JSONArray()); - for (Object obj : friended_by) - { + + for (Object obj : friended_by) { UUID uuid = UUID.fromString((String) obj); Player p = Bukkit.getPlayer(uuid); - if (p != null && p.canSee(e.getPlayer())) - { + + if (p != null && p.canSee(e.getPlayer())) { getLogger().message(p, "Your friend &e" + e.getPlayer().getDisplayName() + "&7 just left!"); p.playSound(p.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1, 1); } } } - - @SuppressWarnings({"unchecked", "deprecation"}) + + @SuppressWarnings({ "unchecked", "deprecation" }) @Command(hook = "add") - public boolean add(CommandSender sender, String target) - { - if (target.equalsIgnoreCase("CONSOLE")) - { + public boolean add(CommandSender sender, String target) { + if (target.equalsIgnoreCase("CONSOLE")) { getLogger().message(sender, true, "You can't add console to your friends!"); return true; } + OfflinePlayer p = Bukkit.getPlayer(target); - if (p == null) - p = Bukkit.getOfflinePlayer(target); - if (p == null) - { + + if (p == null) p = Bukkit.getOfflinePlayer(target); + if (p == null) { getLogger().message(sender, true, "That player couldn't be found!"); return true; } + JSONArray friends = ((JSONArray) DataManager.getOrDefault(sender, "friends", new JSONArray())); - if (friends.contains(p.getUniqueId().toString())) - { + + if (friends.contains(p.getUniqueId().toString())) { getLogger().message(sender, true, "You are already friends with this person!"); return true; } + friends.add(p.getUniqueId().toString()); DataManager.save(sender); - JSONArray friended_by = ((JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "friended_by", - new JSONArray())); + + JSONArray friended_by = ((JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "friended_by", new JSONArray())); friended_by.add(getID(sender)); + DataManager.save(p.getUniqueId().toString()); + getLogger().message(sender, "You are now friends with &e" + p.getName() + "&7!"); - if (p instanceof Player) - { + + if (p instanceof Player) { getLogger().message((Player) p, "&e" + Utils.getName(sender) + "&7 added you as a friend!"); - } - else - { - JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), - "scheduled_notifications", new JSONArray()); + } else { + JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "scheduled_notifications", new JSONArray()); + notifications.add("&e" + Utils.getName(sender) + "&7 added you as a friend!"); notifications.remove("&e" + Utils.getName(sender) + "&7 removed you as a friend!"); + DataManager.setData(p.getUniqueId().toString(), "scheduled_notifications", notifications); } + return true; } - - @SuppressWarnings({"deprecation", "unchecked"}) + + @SuppressWarnings({ "deprecation", "unchecked" }) @Command(hook = "add_grouped") - public boolean add_grouped(CommandSender sender, String target, String group) - { - if (target.equalsIgnoreCase("CONSOLE")) - { + public boolean add_grouped(CommandSender sender, String target, String group) { + if (target.equalsIgnoreCase("CONSOLE")) { getLogger().message(sender, true, "You can't add console to your friends!"); return true; } + OfflinePlayer p = Bukkit.getPlayer(target); - if (p == null) - p = Bukkit.getOfflinePlayer(target); - if (p == null) - { + + if (p == null) p = Bukkit.getOfflinePlayer(target); + if (p == null) { getLogger().message(sender, true, "That player couldn't be found!"); return true; } + JSONArray friends = ((JSONArray) DataManager.getOrDefault(sender, "groups." + group, new JSONArray())); - if (friends.contains(p.getUniqueId().toString())) - { + + if (friends.contains(p.getUniqueId().toString())) { getLogger().message(sender, true, "This person already is part of that friendsgroup!"); return true; } + friends.add(p.getUniqueId().toString()); DataManager.save(sender); + getLogger().message(sender, "&e" + p.getName() + "&7 is now part of the group &e" + group + "&7!"); - if (p instanceof Player) - { - getLogger().message((Player) p, - "&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); - } - else - { - JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), - "scheduled_notifications", new JSONArray()); + + if (p instanceof Player) { + getLogger().message((Player) p, "&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); + } else { + JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "scheduled_notifications", new JSONArray()); + notifications.add("&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); - notifications - .remove("&e" + Utils.getName(sender) + " &7removed you from their friendsgroup &e" + group + "&7!"); + notifications.remove("&e" + Utils.getName(sender) + " &7removed you from their friendsgroup &e" + group + "&7!"); + DataManager.setData(p.getUniqueId().toString(), "scheduled_notifications", notifications); } + return true; } - - @SuppressWarnings({"deprecation", "unchecked"}) + + @SuppressWarnings({ "deprecation", "unchecked" }) @Command(hook = "del") - public boolean del(CommandSender sender, String target) - { - if (target.equalsIgnoreCase("CONSOLE")) - { + public boolean del(CommandSender sender, String target) { + if (target.equalsIgnoreCase("CONSOLE")) { getLogger().message(sender, true, "You can't add console to your friends!"); return true; } + OfflinePlayer p = Bukkit.getPlayer(target); - if (p == null) - p = Bukkit.getOfflinePlayer(target); - if (p == null) - { + + if (p == null) p = Bukkit.getOfflinePlayer(target); + if (p == null) { getLogger().message(sender, true, "That player couldn't be found!"); return true; } + JSONArray friends = ((JSONArray) DataManager.getOrDefault(sender, "friends", new JSONArray())); - if (friends.contains(p.getUniqueId().toString())) - { + + if (friends.contains(p.getUniqueId().toString())) { getLogger().message(sender, true, "You are already friends with this person!"); return true; } + friends.remove(p.getUniqueId().toString()); DataManager.save(sender); - JSONArray friended_by = ((JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "friended_by", - new JSONArray())); + + JSONArray friended_by = ((JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "friended_by", new JSONArray())); friended_by.remove(getID(sender)); + DataManager.save(p.getUniqueId().toString()); + getLogger().message(sender, "You are now friends with &e" + p.getName() + "&7!"); - if (p instanceof Player) - { + + if (p instanceof Player) { getLogger().message((Player) p, "&e" + Utils.getName(sender) + "&7 added you as a friend!"); - } - else - { - JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), - "scheduled_notifications", new JSONArray()); + } else { + JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "scheduled_notifications", new JSONArray()); + notifications.add("&e" + Utils.getName(sender) + "&7 removed you as a friend!"); notifications.remove("&e" + Utils.getName(sender) + "&7 added you as a friend!"); + DataManager.setData(p.getUniqueId().toString(), "scheduled_notifications", notifications); } + return true; } - - @SuppressWarnings({"deprecation", "unchecked"}) + + @SuppressWarnings({ "deprecation", "unchecked" }) @Command(hook = "del_grouped") - public boolean del_grouped(CommandSender sender, String target, String group) - { - if (target.equalsIgnoreCase("CONSOLE")) - { + public boolean del_grouped(CommandSender sender, String target, String group) { + if (target.equalsIgnoreCase("CONSOLE")) { getLogger().message(sender, true, "You can't add console to your friends!"); return true; } + OfflinePlayer p = Bukkit.getPlayer(target); - if (p == null) - p = Bukkit.getOfflinePlayer(target); - if (p == null) - { + + if (p == null) p = Bukkit.getOfflinePlayer(target); + if (p == null) { getLogger().message(sender, true, "That player couldn't be found!"); return true; } + JSONArray friends = ((JSONArray) DataManager.getOrDefault(sender, "groups." + group, new JSONArray())); - if (friends.contains(p.getUniqueId().toString())) - { + + if (friends.contains(p.getUniqueId().toString())) { getLogger().message(sender, true, "This person already is part of that friendsgroup!"); return true; } + friends.add(p.getUniqueId().toString()); DataManager.save(sender); + getLogger().message(sender, "&e" + p.getName() + "&7 is now part of the group &e" + group + "&7!"); - if (p instanceof Player) - { - getLogger().message((Player) p, - "&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); - } - else - { - JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), - "scheduled_notifications", new JSONArray()); - notifications - .add("&e" + Utils.getName(sender) + " &7removed you from their friendsgroup &e" + group + "&7!"); - notifications - .remove("&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); + + if (p instanceof Player) { + getLogger().message((Player) p, "&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); + } else { + JSONArray notifications = (JSONArray) DataManager.getOrDefault(p.getUniqueId().toString(), "scheduled_notifications", new JSONArray()); + + notifications.add("&e" + Utils.getName(sender) + " &7removed you from their friendsgroup &e" + group + "&7!"); + notifications.remove("&e" + Utils.getName(sender) + " &7added you to their friendsgroup &e" + group + "&7!"); + DataManager.setData(p.getUniqueId().toString(), "scheduled_notifications", notifications); } + return true; } - + @Command(hook = "list") - public boolean list(CommandSender sender) - { + public boolean list(CommandSender sender) { JSONArray friends = (JSONArray) DataManager.getOrDefault(sender, "friends", new JSONArray()); - if (friends.size() == 0) - { + + if (friends.size() == 0) { getLogger().message(sender, true, "You didn't add anyone to your friendslist yet."); - } - else - { + } else { StringBuilder sb = new StringBuilder(); - for (Object o : friends.toArray()) - { + + for (Object o : friends.toArray()) { UUID id = UUID.fromString((String) o); Player p = Bukkit.getPlayer(id); - if (p != null) - sb.append(p.getDisplayName() + "&7, "); - else - sb.append("&9" + Bukkit.getOfflinePlayer(id).getName() + "&7, "); + + if (p != null) sb.append(p.getDisplayName() + "&7, "); + else sb.append("&9" + Bukkit.getOfflinePlayer(id).getName() + "&7, "); } + String out = sb.toString().replaceAll(", $", ""); getLogger().message(sender, "You have a total of &e" + friends.size() + "&7 friends:", out); } + return true; } - + @Command(hook = "list_group") - public boolean list_group(CommandSender sender, String group) - { + public boolean list_group(CommandSender sender, String group) { JSONArray friends = (JSONArray) DataManager.getOrDefault(sender, "group." + group, new JSONArray()); - if (friends.size() == 0) - { + + if (friends.size() == 0) { getLogger().message(sender, true, "You didn't add anyone to this group yet."); - } - else - { + } else { StringBuilder sb = new StringBuilder(); - for (Object o : friends.toArray()) - { + + for (Object o : friends.toArray()) { UUID id = UUID.fromString((String) o); Player p = Bukkit.getPlayer(id); - if (p != null) - sb.append(p.getDisplayName() + "&7, "); - else - sb.append("&9" + Bukkit.getOfflinePlayer(id).getName() + "&7, "); + + if (p != null) sb.append(p.getDisplayName() + "&7, "); + else sb.append("&9" + Bukkit.getOfflinePlayer(id).getName() + "&7, "); } + String out = sb.toString().replaceAll(", $", ""); getLogger().message(sender, "You have a total of &e" + friends.size() + "&7 friends added to this group:", out); } + return true; } - + @Command(hook = "list_groups") - public boolean list_groups(CommandSender sender) - { + public boolean list_groups(CommandSender sender) { JSONObject raw = (JSONObject) DataManager.getOrDefault(sender, null, new JSONObject()); Set keys = raw.keySet(); - if (keys.size() == 0 || (keys.contains("friends") && keys.size() == 1)) - { + + if (keys.size() == 0 || (keys.contains("friends") && keys.size() == 1)) { getLogger().message(sender, true, "You don't have any custom groups made yet."); return true; - } - else - { + } else { StringBuilder sb = new StringBuilder(); - for (Object o : keys) - { + + for (Object o : keys) { sb.append("&e" + (String) o + "&7, "); } + String out = sb.toString().replaceAll(", $", ""); getLogger().message(sender, "", out); } + return true; } - - public static boolean isFriend(CommandSender player, CommandSender friend) - { + + public static boolean isFriend(CommandSender player, CommandSender friend) { return isFriend(player, friend, null); } - - public static boolean isFriend(CommandSender player, CommandSender friend, String condition) - { - try - { + + public static boolean isFriend(CommandSender player, CommandSender friend, String condition) { + try { Module mod = ModuleLoader.getModule("Friends"); Method m = mod.getClass().getDeclaredMethod("isFriend_", String.class); + return (boolean) m.invoke(mod, player, friend, condition); + } catch (Exception e) { + return false; } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return false; } - - protected boolean isFriend_(CommandSender player, CommandSender friend, String group) - { - if (group == null) - group = "friends"; - else if (!group.startsWith("group.")) - group = "group." + group; + + protected boolean isFriend_(CommandSender player, CommandSender friend, String group) { + if (group == null) group = "friends"; + else if (!group.startsWith("group.")) group = "group." + group; + JSONArray array = (JSONArray) DataManager.getOrDefault(player, group, new JSONArray()); return array.contains(getID(friend)); } - - private final String getID(CommandSender sender) - { - if (sender instanceof Player) - return ((Player) sender).getUniqueId().toString(); - else - return sender.getName(); + + private final String getID(CommandSender sender) { + if (sender instanceof Player) return ((Player) sender).getUniqueId().toString(); + else return sender.getName(); } } diff --git a/src/main/java/com/redstoner/modules/ignore/Ignore.java b/src/main/java/com/redstoner/modules/ignore/Ignore.java index c830700..c7ef372 100644 --- a/src/main/java/com/redstoner/modules/ignore/Ignore.java +++ b/src/main/java/com/redstoner/modules/ignore/Ignore.java @@ -1,6 +1,5 @@ package com.redstoner.modules.ignore; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.UUID; @@ -27,133 +26,109 @@ import net.nemez.chatapi.click.Message; @Commands(CommandHolderType.File) @AutoRegisterListener @Version(major = 4, minor = 0, revision = 0, compatible = 4) -public class Ignore implements Module -{ - +public class Ignore implements Module { @Command(hook = "unignore", async = AsyncType.ALWAYS) - public boolean unignore(CommandSender sender, String player) - { + public boolean unignore(CommandSender sender, String player) { return ignore(sender, player, false); } - + @Command(hook = "ignore", async = AsyncType.ALWAYS) - public boolean ignore(CommandSender sender, String player) - { + public boolean ignore(CommandSender sender, String player) { return ignore(sender, player, true); } - + @Command(hook = "list", async = AsyncType.ALWAYS) - public boolean list(CommandSender sender) - { + public boolean list(CommandSender sender) { getLogger().message(sender, "§7You are currently ignoring:"); - + JSONArray ignores = (JSONArray) DataManager.getOrDefault(sender, "ignores", new JSONArray()); - - if (ignores.isEmpty()) - { + + if (ignores.isEmpty()) { new Message(sender, null).appendText(" §7Nobody \\o/").send(); return true; } - + String players; OfflinePlayer pi = Bukkit.getOfflinePlayer(UUID.fromString((String) ignores.get(0))); players = " §3" + pi.getName() + "§7"; - - for (int i = 1; i < ignores.size(); i++) - { + + for (int i = 1; i < ignores.size(); i++) { OfflinePlayer p = Bukkit.getOfflinePlayer(UUID.fromString((String) ignores.get(i))); players += ", §3" + p.getName() + "§7"; } - + Message m = new Message(sender, null); m.appendText(players); m.send(); + return true; } - - @SuppressWarnings({"unchecked", "deprecation"}) - public boolean ignore(CommandSender sender, String player, boolean allowIgnore) - { + + @SuppressWarnings({ "unchecked", "deprecation" }) + public boolean ignore(CommandSender sender, String player, boolean allowIgnore) { JSONArray ignores = (JSONArray) DataManager.getOrDefault(sender, "ignores", new JSONArray()); - + Player p = Utils.isUUID(player) ? Bukkit.getPlayer(UUID.fromString(player)) : Bukkit.getPlayer(player); - - OfflinePlayer op = Utils.isUUID(player) ? Bukkit.getOfflinePlayer(UUID.fromString(player)) - : Bukkit.getOfflinePlayer(player); - + + OfflinePlayer op = Utils.isUUID(player) ? Bukkit.getOfflinePlayer(UUID.fromString(player)) : Bukkit.getOfflinePlayer(player); + String pName = p != null ? p.getDisplayName() : op.getName(); String pUUID = p != null ? p.getUniqueId().toString() : op.getUniqueId().toString(); String sUUID = ((Player) sender).getUniqueId().toString(); - - if (pUUID.equals(sUUID)) - { + + if (pUUID.equals(sUUID)) { getLogger().message(sender, true, "§7You can't ignore yourself :P"); return true; } - - if (ignores.contains(pUUID)) - { + + if (ignores.contains(pUUID)) { ignores.remove(pUUID); getLogger().message(sender, "§7You are no longer ignoring §3" + pName + "§7."); - } - else if (!allowIgnore) - { + } else if (!allowIgnore) { getLogger().message(sender, "§7You weren't ignoring §3" + pName + "§7."); - } - else - { + } else { ignores.add(pUUID); getLogger().message(sender, "§7You are now ignoring §3" + pName + "§7."); } + DataManager.setData(sender, "ignores", ignores); + return true; - + } - - public static BroadcastFilter getIgnoredBy(CommandSender sender) - { - try - { + + public static BroadcastFilter getIgnoredBy(CommandSender sender) { + try { Module mod = ModuleLoader.getModule("Ignore"); + Method m = mod.getClass().getDeclaredMethod("_getIgnoredBy", CommandSender.class); m.setAccessible(true); + return (BroadcastFilter) m.invoke(mod, sender); + } catch (Exception e) { + return null; } - catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) - {} - return null; } - + @SuppressWarnings("unused") - private BroadcastFilter _getIgnoredBy(CommandSender sender) - { - return new BroadcastFilter() - { - - private final String sUUID = sender instanceof Player ? ((Player) sender).getUniqueId().toString() - : "CONSOLE"; - + private BroadcastFilter _getIgnoredBy(CommandSender sender) { + return new BroadcastFilter() { + private final String sUUID = sender instanceof Player ? ((Player) sender).getUniqueId().toString() : "CONSOLE"; + @Override - public boolean sendTo(CommandSender recipient) - { - if (sUUID.equals("CONSOLE")) - return true; - - if (recipient instanceof Player) - { - Player player = (Player) recipient; - - if (sender.hasPermission("utils.ignore.override")) - return true; - - JSONArray ignores = (JSONArray) DataManager.getOrDefault(recipient, "ignores", new JSONArray()); - return !ignores.contains(sUUID); - } - else - return true; + public boolean sendTo(CommandSender recipient) { + if (sUUID.equals("CONSOLE")) return true; + + if ((recipient instanceof Player)) return true; + + Player player = (Player) recipient; + + if (sender.hasPermission("utils.ignore.override")) return true; + + JSONArray ignores = (JSONArray) DataManager.getOrDefault(recipient, "ignores", new JSONArray()); + return !ignores.contains(sUUID); + } }; } - }