From 2f0f38b20692794ae539d8169a9d098a03c4b434 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Sat, 20 Oct 2018 01:36:57 -0500 Subject: [PATCH] H2 Support Added H2 Database Added conversion for old plugin.yml Updated docs Fixes #1 Fixes #2 --- docs/source/changelog/index.rst | 3 +- docs/source/changelog/v1.2.rst | 2 +- docs/source/changelog/v2.0.rst | 14 ++ pom.xml | 7 +- .../xyz/etztech/deluxegroups/Database.java | 228 ++++++++++++++++++ .../xyz/etztech/deluxegroups/DeluxeGroup.java | 12 +- .../etztech/deluxegroups/DeluxeGroups.java | 135 +++-------- .../deluxegroups/command/CommandGroup.java | 97 ++++---- .../deluxegroups/command/CommandMain.java | 12 +- .../listeners/AsyncPlayerChatListener.java | 22 +- src/main/resources/groups.yml | 2 - 11 files changed, 341 insertions(+), 193 deletions(-) create mode 100644 docs/source/changelog/v2.0.rst create mode 100644 src/main/java/xyz/etztech/deluxegroups/Database.java delete mode 100644 src/main/resources/groups.yml diff --git a/docs/source/changelog/index.rst b/docs/source/changelog/index.rst index 1812d56..755c760 100644 --- a/docs/source/changelog/index.rst +++ b/docs/source/changelog/index.rst @@ -8,4 +8,5 @@ Changelogs .. toctree:: :maxdepth: 1 - v1.2 \ No newline at end of file + v1.2 + v2.0 \ No newline at end of file diff --git a/docs/source/changelog/v1.2.rst b/docs/source/changelog/v1.2.rst index 89ae71c..8095383 100644 --- a/docs/source/changelog/v1.2.rst +++ b/docs/source/changelog/v1.2.rst @@ -3,7 +3,7 @@ .. _deluxegroups_v1.2: DeluxeGroups v1.2 -======== +================= Additions --------- diff --git a/docs/source/changelog/v2.0.rst b/docs/source/changelog/v2.0.rst new file mode 100644 index 0000000..61824ef --- /dev/null +++ b/docs/source/changelog/v2.0.rst @@ -0,0 +1,14 @@ +.. include:: ../common.rst + +.. _deluxegroups_v2.0: + +DeluxeGroups v2.0 +================= + +Additions +--------- +* Implemented H2 support! (Includes conversion for old groups.yml) + +Bug Fixes +--------- +None \ No newline at end of file diff --git a/pom.xml b/pom.xml index f0c542a..0889be6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xyz.etztech DeluxeGroups - 1.2 + 2.0 jar @@ -38,19 +38,16 @@ us.dynmap dynmap-api 1.9.4 - provided xyz.etztech EtzCore - 1.0.4 - compile + 1.0.5 commons-lang commons-lang 2.6 - provided diff --git a/src/main/java/xyz/etztech/deluxegroups/Database.java b/src/main/java/xyz/etztech/deluxegroups/Database.java new file mode 100644 index 0000000..4cb6e8d --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/Database.java @@ -0,0 +1,228 @@ +package xyz.etztech.deluxegroups; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.plugin.java.JavaPlugin; +import xyz.etztech.core.db.DataSource; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Database extends DataSource { + + public Database(JavaPlugin plugin) { + super(plugin); + create(); + } + + private void create() { + try { + Connection connection = getConnection(); + final String CREATE1 = "CREATE TABLE IF NOT EXISTS `groups` (`id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(50) NOT NULL, `password` VARCHAR(50), `permanent` BOOLEAN NOT NULL);"; + PreparedStatement pst = connection.prepareStatement(CREATE1); + pst.execute(); + final String CREATE2 = "CREATE TABLE IF NOT EXISTS `players` (`id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `group_id` INT NOT NULL);"; + pst = connection.prepareStatement(CREATE2); + pst.execute(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public int addGroup(DeluxeGroup group) { + int id = 0; + try { + Connection connection = getConnection(); + final String GROUP = "INSERT INTO `groups` (`name`, `password`, `permanent`) VALUES (?, ?, FALSE);"; + PreparedStatement pst = connection.prepareStatement(GROUP, Statement.RETURN_GENERATED_KEYS); + pst.setString(1, group.getName()); + pst.setString(2, group.getPassword()); + pst.executeUpdate(); + ResultSet keys = pst.getGeneratedKeys(); + if (keys.next()) { + id = keys.getInt("id"); + } + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return id; + } + + public void removeGroup(int groupId) { + try { + Connection connection = getConnection(); + final String REMOVE = "DELETE FROM `groups` WHERE `id` = ?;"; + PreparedStatement pst = connection.prepareStatement(REMOVE); + pst.setInt(1, groupId); + pst.executeUpdate(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public void updateGroup(DeluxeGroup group) { + try { + Connection connection = getConnection(); + final String UPDATE = "UPDATE `groups` SET `name` = ?, `password` = ?, `permanent` = ? WHERE `id` = ?;"; + PreparedStatement pst = connection.prepareStatement(UPDATE); + pst.setString(1, group.getName()); + pst.setString(2, group.getPassword()); + pst.setBoolean(3, group.getPermanent()); + pst.setInt(4, group.getId()); + pst.executeUpdate(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public void addPlayer(String uuid, int groupId) { + try { + Connection connection = getConnection(); + final String PLAYER = "INSERT INTO `players` (`uuid`, `group_id`) VALUES (?, ?);"; + PreparedStatement pst = connection.prepareStatement(PLAYER); + pst.setString(1, uuid); + pst.setInt(2, groupId); + pst.executeUpdate(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public void removePlayer(String uuid) { + try { + Connection connection = getConnection(); + final String REMOVE = "DELETE FROM `players` WHERE `uuid` = ?;"; + PreparedStatement pst = connection.prepareStatement(REMOVE); + pst.setString(1, uuid); + pst.executeUpdate(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public DeluxeGroup getGroup(String groupName) { + try { + Connection connection = getConnection(); + final String GROUP = "SELECT `id`, `name`, `password`, `permanent` FROM `groups` WHERE LOWER(`name`) = LOWER(?);"; + PreparedStatement pst = connection.prepareStatement(GROUP); + pst.setString(1, groupName); + ResultSet rs = pst.executeQuery(); + DeluxeGroup group = getDeluxeGroup(rs); + connection.close(); + return group; + } catch (Exception ex) { + ex.printStackTrace(); + } + return new DeluxeGroup(); + } + + public DeluxeGroup getPlayerGroup(String uuid) { + try { + Connection connection = getConnection(); + final String GROUP = "SELECT `id`, `name`, `password`, `permanent` FROM `groups` WHERE `id` = (SELECT `group_id` FROM `players` WHERE `uuid` = ?);"; + PreparedStatement pst = connection.prepareStatement(GROUP); + pst.setString(1, uuid); + ResultSet rs = pst.executeQuery(); + DeluxeGroup group = getDeluxeGroup(rs); + connection.close(); + return group; + } catch (Exception ex) { + ex.printStackTrace(); + } + return new DeluxeGroup(); + } + + public List getGroups() { + List groups = new ArrayList<>(); + try { + Connection connection = getConnection(); + final String GROUPS = "SELECT `id`, `name`, `password`, `permanent` FROM `groups`;"; + PreparedStatement pst = connection.prepareStatement(GROUPS); + ResultSet rs = pst.executeQuery(); + while (rs.next()) { + DeluxeGroup group = new DeluxeGroup(); + group.setId(rs.getInt("id")); + group.setName(rs.getString("name")); + group.setPassword(rs.getString("password")); + group.setPermanent(rs.getBoolean("permanent")); + groups.add(group); + } + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return groups; + } + + public List getPlayers(int groupId) { + List players = new ArrayList<>(); + try { + Connection connection = getConnection(); + final String PLAYERS = "SELECT `uuid` FROM `players` WHERE `group_id` = ?;"; + PreparedStatement pst = connection.prepareStatement(PLAYERS); + pst.setInt(1, groupId); + ResultSet rs = pst.executeQuery(); + while (rs.next()) { + OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("uuid"))); + players.add(player); + } + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return players; + } + + public boolean inGroup(String uuid) { + boolean inGroup = false; + try { + Connection connection = getConnection(); + final String INGROUP = "SELECT `uuid` FROM `players` WHERE `uuid` = ?;"; + PreparedStatement pst = connection.prepareStatement(INGROUP); + pst.setString(1, uuid); + ResultSet rs = pst.executeQuery(); + inGroup = rs.next(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return inGroup; + } + + public boolean groupExists(String groupName) { + boolean exists = false; + try { + Connection connection = getConnection(); + final String EXISTS = "SELECT `id` FROM `groups` WHERE LOWER(`name`) = LOWER(?);"; + PreparedStatement pst = connection.prepareStatement(EXISTS); + pst.setString(1, groupName); + ResultSet rs = pst.executeQuery(); + exists = rs.next(); + connection.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return exists; + } + + private DeluxeGroup getDeluxeGroup(ResultSet rs) throws SQLException { + if (rs.next()) { + DeluxeGroup group = new DeluxeGroup(); + group.setId(rs.getInt("id")); + group.setName(rs.getString("name")); + group.setPassword(rs.getString("password")); + group.setPermanent(rs.getBoolean("permanent")); + group.setGroupList(new ArrayList<>(getPlayers(group.getId()))); + return group; + } + return new DeluxeGroup(); + } +} diff --git a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java index d17ad81..a2bc070 100644 --- a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java +++ b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java @@ -14,6 +14,7 @@ import java.util.List; public class DeluxeGroup { + private int id; private String name; private String password; private boolean permanent; @@ -22,6 +23,7 @@ public class DeluxeGroup { private PrintWriter log; public DeluxeGroup() { + this.id = 0; this.name = ""; this.password = ""; this.permanent = false; @@ -29,6 +31,14 @@ public class DeluxeGroup { } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public String getName() { return name; } @@ -134,6 +144,4 @@ public class DeluxeGroup { String message = player + ": " + chat; DeluxeUtil.log(this.log, message, false); } - - } diff --git a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java index 5dd79c4..9ce1d5f 100644 --- a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java +++ b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java @@ -6,9 +6,9 @@ import org.bukkit.OfflinePlayer; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.dynmap.DynmapAPI; import xyz.etztech.core.api.IMinecraftManager; -import xyz.etztech.core.maven.MavenLibrary; -import xyz.etztech.core.maven.MavenPlugin; import xyz.etztech.deluxegroups.command.CommandGroup; import xyz.etztech.deluxegroups.command.CommandMain; import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener; @@ -21,34 +21,29 @@ import java.util.*; import java.util.logging.Logger; -@MavenLibrary(group = "us.dynmap", artifact = "dynmap-api", version = "1.9.4", repository = "http://repo.mikeprimm.com/") -@MavenLibrary(group = "commons-lang", artifact = "commons-lang", version = "2.6") -public class DeluxeGroups extends MavenPlugin { +public class DeluxeGroups extends JavaPlugin { private static DeluxeGroups instance; public static FileConfiguration config; - public static FileConfiguration groupConfig; private Logger log = Logger.getLogger( "Minecraft" ); - - protected static Map inGroup = new HashMap(); - protected static Map groups = new HashMap(); + private static Database database; // Objects that can be reloaded - AsyncPlayerChatListener chatListener; + private AsyncPlayerChatListener chatListener; // Dynmap API - private static Object dynmap = null; + private static DynmapAPI dynmap = null; // MinecraftManager API private static IMinecraftManager minecraftManager = null; - @Override - public void enable() { + public void onEnable() { instance = this; + database = new Database(this); saveDefaultConfig(); reloadConfig(); @@ -62,7 +57,7 @@ public class DeluxeGroups extends MavenPlugin { // Dynmap integration if (Bukkit.getPluginManager().isPluginEnabled("dynmap")) { - dynmap = Bukkit.getPluginManager().getPlugin("dynmap"); + dynmap = (DynmapAPI) Bukkit.getPluginManager().getPlugin("dynmap"); DeluxeUtil.setupDynmap(dynmap); getServer().getPluginManager().registerEvents(new SessionListener(), this); } @@ -88,32 +83,7 @@ public class DeluxeGroups extends MavenPlugin { } } - @Override - public void disable() { - log("Saving groups."); - String root; - for (DeluxeGroup group : groups.values()) { - root = group.getName().toLowerCase(); - groupConfig.createSection(root); - groupConfig.set(root + ".name", group.getName()); - groupConfig.set(root + ".password", group.getPassword()); - groupConfig.set(root + ".permanent", group.getPermanent()); - groupConfig.set(root + ".players", group.getSize() > 0 ? group.getUUIDList() : null); - } - File groupf = new File(getDataFolder(), "groups.yml"); - try { - PrintWriter pw = new PrintWriter(groupf); - pw.write("# This is a file for saving all your groups\n" + - "# Do not manually edit this file!\n\n"); - pw.close(); - } catch (Exception ex) {} - try { - groupf.createNewFile(); - groupConfig.save(groupf); - } catch (IOException e) { - e.printStackTrace(); - } - } + public void onDisable() {} public void loadConfig() { config = Bukkit.getPluginManager().getPlugin("DeluxeGroups").getConfig(); @@ -123,46 +93,47 @@ public class DeluxeGroups extends MavenPlugin { public void reloadConfig() { super.reloadConfig(); loadConfig(); - loadGroupConfig(); + convertConfig(); if (chatListener != null) { chatListener.reload(); } } - public void loadGroupConfig() { + + @Deprecated + public boolean convertConfig() { File groupf = new File(getDataFolder(), "groups.yml"); if (!groupf.exists()) { - groupf.getParentFile().mkdirs(); - saveResource("groups.yml", false); + return false; } - groupConfig = new YamlConfiguration(); + log("Converting old groups.yml to H2."); + FileConfiguration groupConfig = new YamlConfiguration(); try { groupConfig.load(groupf); int loaded = 0; - OfflinePlayer offlinePlayer; DeluxeGroup group; for (String key : groupConfig.getKeys(false)) { group = new DeluxeGroup(); group.setName(groupConfig.getString(key + ".name")); group.setPassword(groupConfig.getString(key + ".password")); group.setPermanent(groupConfig.getBoolean(key + ".permanent")); - ArrayList playerList = new ArrayList<>(); + int id = database.addGroup(group); for (String uuid : groupConfig.getStringList(key + ".players")) { - offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(uuid)); - playerList.add(offlinePlayer); - setInGroup(uuid, group.getName()); + database.addPlayer(uuid, id); } - group.setGroupList(playerList); - addGroup(group); loaded++; } - log("Loaded " + loaded + " groups!"); + log("Converted " + loaded + " groups to H2! The groups.yml will be deleted next time the server is stopped."); + groupf.deleteOnExit(); + return true; } catch (Exception e) { e.printStackTrace(); } + return false; } + public void log(String message) { log.info( "[DeluxeGroups]: " + message ); } @@ -171,63 +142,11 @@ public class DeluxeGroups extends MavenPlugin { return instance; } - public static Collection getGroups() { - return groups.values(); + public static Database getDatabase() { + return database; } - public static void addGroup(DeluxeGroup group) { - groups.put(group.getName().toLowerCase(), group); - } - - public static void removeGroup(DeluxeGroup group) { - groups.remove(group.getName().toLowerCase()); - } - - public static DeluxeGroup getGroup(String name) { - name = name.toLowerCase(); - if (groups.containsKey(name)) { - return groups.get(name); - } - return null; - } - - public static void setInGroup(String uuid, String group) { - inGroup.put(uuid, group.toLowerCase()); - } - - public static void removeFromInGroup(String uuid) { - inGroup.remove(uuid); - } - - public static String getInGroup(String uuid) { - if (inGroup.containsKey(uuid)) { - return inGroup.get(uuid); - } - return null; - } - - public static void addToGroup(Player uuid, String group) { - group = group.toLowerCase(); - if (groups.containsKey(group)) { - groups.get(group).add(uuid); - } - } - - public static void removeFromGroup(Player player, String group) { - group = group.toLowerCase(); - if (groups.containsKey(group)) { - groups.get(group).remove(player); - } - } - - public static void removeFromGroup(OfflinePlayer player, String group) { - group = group.toLowerCase(); - if (groups.containsKey(group)) { - groups.get(group).remove(player.getUniqueId().toString()); - } - } - - public static Object getDynmap() { + public static DynmapAPI getDynmap() { return dynmap; } diff --git a/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java b/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java index b0f6bbc..5d1935c 100644 --- a/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java +++ b/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java @@ -31,8 +31,8 @@ public class CommandGroup Player localPlayer1 = (Player)sender; if (args.length == 0) { - if (DeluxeGroups.getInGroup(localPlayer1.getUniqueId().toString()) != null) { - DeluxeGroup deluxeGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(localPlayer1.getUniqueId().toString())); + if (DeluxeGroups.getDatabase().inGroup(localPlayer1.getUniqueId().toString())) { + DeluxeGroup deluxeGroup = DeluxeGroups.getDatabase().getPlayerGroup(localPlayer1.getUniqueId().toString()); DeluxeUtil.sms(localPlayer1, ChatColor.AQUA + "You are in group " + deluxeGroup.getName()); } else { DeluxeUtil.sms(localPlayer1, ChatColor.RED + "You are not in a group!"); @@ -44,7 +44,7 @@ public class CommandGroup } else if ("join".equals(subcommand.toLowerCase())) { joinGroup(localPlayer1, args); } else if ("leave".equals(subcommand.toLowerCase())) { - leaveGroup(localPlayer1, args, false); + leaveGroup(localPlayer1, false); } else if ("list".equals(subcommand.toLowerCase())) { listGroup(localPlayer1, args); } else if ("password".equals(subcommand.toLowerCase())) { @@ -61,26 +61,18 @@ public class CommandGroup return true; } - private boolean groupExists(String groupName) { - for (DeluxeGroup group : DeluxeGroups.getGroups()) { - if (group.getName().toLowerCase().equals(groupName.toLowerCase())) { - return true; - } - } - return false; - } private void createGroup(Player player, String[] args) { if (args.length < 2) { DeluxeUtil.sms(player, ChatColor.RED + Usage.CREATE.getFullUsage(player)); return; } - if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { + if (DeluxeGroups.getDatabase().inGroup(player.getUniqueId().toString())) { DeluxeUtil.sms(player, ChatColor.RED + "You are already in a group! Leave it first to create a new one."); return; } DeluxeGroup cg = new DeluxeGroup(); - if (!groupExists(args[1])) { + if (!DeluxeGroups.getDatabase().groupExists(args[1])) { cg.setName(args[1]); if (args.length == 2) { DeluxeUtil.sms(player, ChatColor.AQUA + "Creating " + args[1] + " with no password."); @@ -102,8 +94,8 @@ public class CommandGroup return; } cg.add(player); - DeluxeGroups.setInGroup(player.getUniqueId().toString(), args[1]); - DeluxeGroups.addGroup(cg); + int id = DeluxeGroups.getDatabase().addGroup(cg); + DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), id); } else { DeluxeUtil.sms(player, ChatColor.RED + "A group with that name already exists!"); } @@ -114,22 +106,22 @@ public class CommandGroup DeluxeUtil.sms(player, ChatColor.RED + Usage.JOIN.getFullUsage(player)); return; } - if (groupExists(args[1])) { - if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { - if (args[1].toLowerCase().equals(DeluxeGroups.getInGroup(player.getUniqueId().toString()).toLowerCase())) { + if (DeluxeGroups.getDatabase().groupExists(args[1])) { + if (DeluxeGroups.getDatabase().inGroup(player.getUniqueId().toString())) { + DeluxeGroup group = DeluxeGroups.getDatabase().getPlayerGroup(player.getUniqueId().toString()); + if (args[1].toLowerCase().equals(group.getName().toLowerCase())) { DeluxeUtil.sms(player, ChatColor.RED + "You are already in " - + DeluxeGroups.getInGroup(player.getUniqueId().toString())); + + group.getName()); return; } - leaveGroup(player, args, true); + leaveGroup(player, true); } - DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(args[1]); if (args.length == 2) { if (!group.hasPassword()) { group.sendMessage(player.getName() + " has joined the group!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); - DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); - DeluxeGroups.addToGroup(player, group.getName()); + DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); } else { DeluxeUtil.sms(player, ChatColor.RED + group.getName() + " is password protected."); } @@ -137,13 +129,11 @@ public class CommandGroup if (group.hasPassword() && args[2].equals(group.getPassword())) { group.sendMessage(player.getName() + " has joined the group!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); - DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); - DeluxeGroups.addToGroup(player, group.getName()); + DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); } else if (!group.hasPassword()) { group.sendMessage(player.getName() + " has joined the group!"); DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); - DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); - DeluxeGroups.addToGroup(player, group.getName()); + DeluxeGroups.getDatabase().addPlayer(player.getUniqueId().toString(), group.getId()); } else { DeluxeUtil.sms(player, ChatColor.RED + "Incorrect password."); } @@ -157,8 +147,8 @@ public class CommandGroup private void listGroup(Player player, String[] args) { if (args.length == 1) { - if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { - DeluxeGroup deluxeGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(player.getUniqueId().toString())); + if (DeluxeGroups.getDatabase().inGroup(player.getUniqueId().toString())) { + DeluxeGroup deluxeGroup = DeluxeGroups.getDatabase().getPlayerGroup(player.getUniqueId().toString()); StringBuilder message = new StringBuilder(ChatColor.DARK_AQUA + "===== " + deluxeGroup.getName() + " ====="); for (OfflinePlayer groupPlayer : deluxeGroup.getGroupList()) { if (groupPlayer.isOnline()) { @@ -173,7 +163,7 @@ public class CommandGroup } } else if (args.length == 2) { if (player.hasPermission("deluxegroups.admin")) { - DeluxeGroup carrotGroup = DeluxeGroups.getGroup(args[1]); + DeluxeGroup carrotGroup = DeluxeGroups.getDatabase().getGroup(args[1]); if (carrotGroup != null) { StringBuilder message = new StringBuilder(ChatColor.DARK_AQUA + "===== " + carrotGroup.getName() + " ====="); for (OfflinePlayer groupPlayer : carrotGroup.getGroupList()) { @@ -195,16 +185,16 @@ public class CommandGroup } } - private void leaveGroup(Player player, String[] args, boolean fromJoin) { - if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { - DeluxeGroups.removeFromGroup(player, DeluxeGroups.getInGroup(player.getUniqueId().toString())); - DeluxeGroup leaveGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(player.getUniqueId().toString())); - DeluxeGroups.removeFromInGroup(player.getUniqueId().toString()); + private void leaveGroup(Player player, boolean fromJoin) { + if (DeluxeGroups.getDatabase().inGroup(player.getUniqueId().toString())) { + DeluxeGroup leaveGroup = DeluxeGroups.getDatabase().getPlayerGroup(player.getUniqueId().toString()); + DeluxeGroups.getDatabase().removePlayer(player.getUniqueId().toString()); DeluxeUtil.sms(player, ChatColor.AQUA + "You have left " + leaveGroup.getName()); + leaveGroup.remove(player); leaveGroup.sendMessage(ChatColor.AQUA + player.getName() + " has left the group."); if (leaveGroup.getSize() < 1 && !leaveGroup.getPermanent()) { // Disband - DeluxeGroups.removeGroup(leaveGroup); + DeluxeGroups.getDatabase().removeGroup(leaveGroup.getId()); } } else if (!fromJoin){ DeluxeUtil.sms(player, ChatColor.RED + "You are not in a group!"); @@ -220,10 +210,11 @@ public class CommandGroup DeluxeUtil.sms(player, ChatColor.RED + Usage.PASSWORD.getFullUsage(player)); return; } - DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(args[1]); if (group != null) { if (args[2].equals(args[3])) { - DeluxeGroups.getGroup(args[1]).setPassword(args[2]); + group.setPassword(args[2]); + DeluxeGroups.getDatabase().updateGroup(group); DeluxeUtil.sms(player, ChatColor.GREEN + "Password for " + group.getName() + " changed to '" + group.getPassword() + "'."); } else { DeluxeUtil.sms(player, ChatColor.RED + "Passwords did not match."); @@ -242,15 +233,16 @@ public class CommandGroup DeluxeUtil.sms(player, ChatColor.RED + Usage.PERMANENT.getFullUsage(player)); return; } - DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + DeluxeGroup group = DeluxeGroups.getDatabase().getGroup(args[1]); if (group != null) { Boolean perm = DeluxeUtil.resolveBoolean(args[2]); if (perm != null) { - DeluxeGroups.getGroup(args[1]).setPermanent(perm); + group.setPermanent(perm); + DeluxeGroups.getDatabase().updateGroup(group); DeluxeUtil.sms(player, ChatColor.GREEN + group.getName() + " is " + (group.getPermanent() ? "now" : "no longer") + " permanent."); if (!group.getPermanent() && group.getSize() < 1) { // Disband - DeluxeGroups.removeGroup(group); + DeluxeGroups.getDatabase().removeGroup(group.getId()); } } else { DeluxeUtil.sms(player, ChatColor.RED + Usage.PERMANENT.getFullUsage(player)); @@ -271,13 +263,15 @@ public class CommandGroup } Player argPlayer = Bukkit.getPlayer(args[1]); if (argPlayer != null) { - if (DeluxeGroups.getInGroup(argPlayer.getUniqueId().toString()) != null) { - DeluxeGroup group = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(argPlayer.getUniqueId().toString())); - DeluxeGroups.removeFromGroup(argPlayer, group.getName()); - //group.remove(argPlayer.getUniqueId().toString()); - DeluxeGroups.removeFromInGroup(argPlayer.getUniqueId().toString()); + if (DeluxeGroups.getDatabase().inGroup(argPlayer.getUniqueId().toString())) { + DeluxeGroup group = DeluxeGroups.getDatabase().getPlayerGroup(argPlayer.getUniqueId().toString()); + DeluxeGroups.getDatabase().removePlayer(argPlayer.getUniqueId().toString()); DeluxeUtil.sms(player, ChatColor.GREEN + argPlayer.getName() + " was kicked from " + group.getName() + "."); DeluxeUtil.sms(argPlayer, ChatColor.RED + "You were kicked from " + group.getName() + "."); + if (!group.getPermanent() && group.getSize() < 1) { + // Disband + DeluxeGroups.getDatabase().removeGroup(group.getId()); + } } else { DeluxeUtil.sms(player, ChatColor.RED + argPlayer.getName() + " is not in a group."); } @@ -289,10 +283,9 @@ public class CommandGroup offPlayer = null; } if (offPlayer != null) { - if (DeluxeGroups.getInGroup(offPlayer.getUniqueId().toString()) != null) { - DeluxeGroup group = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(offPlayer.getUniqueId().toString())); - DeluxeGroups.removeFromGroup(offPlayer, group.getName()); - DeluxeGroups.removeFromInGroup(offPlayer.getUniqueId().toString()); + if (DeluxeGroups.getDatabase().inGroup(argPlayer.getUniqueId().toString())) { + DeluxeGroup group = DeluxeGroups.getDatabase().getPlayerGroup(argPlayer.getUniqueId().toString()); + DeluxeGroups.getDatabase().removePlayer(argPlayer.getUniqueId().toString()); DeluxeUtil.sms(player, ChatColor.GREEN + offPlayer.getName() + " was kicked from " + group.getName() + "."); if (offPlayer.isOnline()) { Player onPlayer = Bukkit.getPlayer(offPlayer.getUniqueId()); @@ -307,7 +300,7 @@ public class CommandGroup } } - private void help(Player player) { + public static void help(Player player) { StringBuilder message = new StringBuilder(ChatColor.GOLD + "===== Group Help ====="); for (Usage usage : Usage.values()) { if (usage.canUse(player)) { @@ -362,7 +355,7 @@ public class CommandGroup } public boolean canUse(Player player) { - return (permission != null && player.hasPermission(permission)) || alternate != null; + return permission == null || player.hasPermission(permission); } public String getFullUsage(Player player) { diff --git a/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java b/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java index 2c0bd2a..59dbf70 100644 --- a/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java +++ b/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java @@ -5,6 +5,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import xyz.etztech.deluxegroups.DeluxeGroups; public class CommandMain implements CommandExecutor { @@ -25,14 +26,9 @@ public class CommandMain implements CommandExecutor { case "help": sender.sendMessage(ChatColor.GOLD + "----- DeluxeGroups Commands -----"); sender.sendMessage(ChatColor.YELLOW + "/dg reload - Reload the config"); - sender.sendMessage(ChatColor.GOLD + "----- DeluxeGroups Group Commands -----"); - sender.sendMessage(ChatColor.YELLOW + "/group - Display your group"); - sender.sendMessage(ChatColor.YELLOW + "/group create - Create a new group."); - sender.sendMessage(ChatColor.YELLOW + "/group join - Join a group"); - sender.sendMessage(ChatColor.YELLOW + "/group leave - Leave a group"); - sender.sendMessage(ChatColor.YELLOW + "/group list - List the players in a group"); - sender.sendMessage(ChatColor.YELLOW + "/group password - Modify the password for your group"); - sender.sendMessage(ChatColor.YELLOW + "/group kick - Kick a player from the group"); + if (sender instanceof Player) { + CommandGroup.help((Player) sender); + } break; case "reload": reload(sender); diff --git a/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java index 183bc35..65cac93 100644 --- a/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java +++ b/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java @@ -42,11 +42,11 @@ public class AsyncPlayerChatListener implements Listener { return; } - DynmapAPI dynmap = (DynmapAPI) DeluxeGroups.getDynmap(); + DynmapAPI dynmap = DeluxeGroups.getDynmap(); IMinecraftManager minecraftManager = DeluxeGroups.getMinecraftManager(); - if (DeluxeGroups.getInGroup(sender.getUniqueId().toString()) != null) { + if (DeluxeGroups.getDatabase().inGroup(sender.getUniqueId().toString())) { boolean groupChat = false; for (String prefix : this.groupPrefixes) { // If chat starts with 1 prefix, it is group chat. @@ -58,15 +58,7 @@ public class AsyncPlayerChatListener implements Listener { event.setMessage(chat); // Normal chat - if (!event.isCancelled()) { - if (dynmap != null) { - dynmap.postPlayerMessageToWeb(sender.getName(), sender.getName(), chat); - //dynmap.sendBroadcastToWeb(sender.getName(), chat); - } - if (minecraftManager != null) { - minecraftManager.globalLog(sender, chat); - } - } + normalChat(event, sender, chat, dynmap, minecraftManager); return; } break; @@ -74,8 +66,7 @@ public class AsyncPlayerChatListener implements Listener { } if (groupChat) { - String groupName = DeluxeGroups.getInGroup(sender.getUniqueId().toString()); - DeluxeGroup group = DeluxeGroups.getGroup(groupName); + DeluxeGroup group = DeluxeGroups.getDatabase().getPlayerGroup(sender.getUniqueId().toString()); group.log(sender.getPlayer().getName(), chat); String format = plugin.getConfig().getString("format.custom"); @@ -102,10 +93,13 @@ public class AsyncPlayerChatListener implements Listener { } // Normal chat + normalChat(event, sender, chat, dynmap, minecraftManager); + } + + private void normalChat(AsyncPlayerChatEvent event, Player sender, String chat, DynmapAPI dynmap, IMinecraftManager minecraftManager) { if (!event.isCancelled()) { if (dynmap != null) { dynmap.postPlayerMessageToWeb(sender.getName(), sender.getName(), chat); - //dynmap.sendBroadcastToWeb(sender.getName(), chat); } if (minecraftManager != null) { minecraftManager.globalLog(sender, chat); diff --git a/src/main/resources/groups.yml b/src/main/resources/groups.yml deleted file mode 100644 index 4cd966a..0000000 --- a/src/main/resources/groups.yml +++ /dev/null @@ -1,2 +0,0 @@ -# This is a file for saving all your groups -# Do not manually edit this file! \ No newline at end of file