diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index 19debc343..a149d9baa 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -1,5 +1,6 @@ package me.lucko.luckperms; +import com.google.gson.Gson; import lombok.Getter; import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.data.DatastoreConfiguration; @@ -28,9 +29,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private UserManager userManager; private GroupManager groupManager; private Datastore datastore; + private Gson gson; @Override public void onEnable() { + gson = new Gson(); configuration = new BukkitConfig(this); // register events @@ -93,6 +96,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { Bukkit.getScheduler().runTask(this, r); } + @Override + public Gson getGson() { + return gson; + } + @Override public String getVersion() { return VERSION; diff --git a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java index 722601714..94f1b4302 100644 --- a/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/LPBungeePlugin.java @@ -1,5 +1,6 @@ package me.lucko.luckperms; +import com.google.gson.Gson; import lombok.Getter; import me.lucko.luckperms.commands.CommandManager; import me.lucko.luckperms.data.Datastore; @@ -24,9 +25,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { private UserManager userManager; private GroupManager groupManager; private Datastore datastore; + private Gson gson; @Override public void onEnable() { + gson = new Gson(); configuration = new BungeeConfig(this); // register events @@ -87,4 +90,9 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { public void doSync(Runnable r) { r.run(); } + + @Override + public Gson getGson() { + return gson; + } } diff --git a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java index 87c96003e..ba9464e18 100644 --- a/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/LuckPermsPlugin.java @@ -1,5 +1,6 @@ package me.lucko.luckperms; +import com.google.gson.Gson; import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.users.UserManager; @@ -74,4 +75,6 @@ public interface LuckPermsPlugin { * @param r the task to run */ void doSync(Runnable r); + + Gson getGson(); } diff --git a/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java b/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java index 61658253a..98266f9a6 100644 --- a/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/HikariDatastore.java @@ -2,6 +2,7 @@ package me.lucko.luckperms.data; import com.zaxxer.hikari.HikariDataSource; import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.users.User; @@ -11,7 +12,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.UUID; import java.util.logging.Level; @@ -121,9 +121,7 @@ public class HikariDatastore extends Datastore { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { - if (!resultSet.getString("perms").equals("#")) { - user.loadNodes((Arrays.asList(resultSet.getString("perms").split(":")))); - } + user.loadNodes(resultSet.getString("perms")); user.setName(resultSet.getString("name")); preparedStatement.close(); @@ -143,11 +141,14 @@ public class HikariDatastore extends Datastore { @Override public boolean loadOrCreateUser(UUID uuid, String username) { User user = plugin.getUserManager().makeUser(uuid, username); + try { + user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true); + } catch (ObjectAlreadyHasException ignored) {} boolean success = runQuery(connection -> { PreparedStatement preparedStatement = connection.prepareStatement(USER_INSERT); - preparedStatement.setString(1, uuid.toString()); - preparedStatement.setString(2, username); - preparedStatement.setString(3, plugin.getConfiguration().getDefaultGroupNode()); + preparedStatement.setString(1, user.getUuid().toString()); + preparedStatement.setString(2, user.getName()); + preparedStatement.setString(3, user.serializeNodes()); preparedStatement.setString(4, username); preparedStatement.execute(); preparedStatement.close(); @@ -158,9 +159,7 @@ public class HikariDatastore extends Datastore { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { - if (!resultSet.getString("perms").equals("#")) { - user.loadNodes(Arrays.asList(resultSet.getString("perms").split(":"))); - } + user.loadNodes(resultSet.getString("perms")); preparedStatement.close(); resultSet.close(); @@ -196,9 +195,9 @@ public class HikariDatastore extends Datastore { Group group = plugin.getGroupManager().makeGroup(name); boolean success = runQuery(connection -> { PreparedStatement preparedStatement = connection.prepareStatement(GROUP_INSERT); - preparedStatement.setString(1, name); - preparedStatement.setString(2, "#"); - preparedStatement.setString(3, "#"); + preparedStatement.setString(1, group.getName()); + preparedStatement.setString(2, group.serializeNodes()); + preparedStatement.setString(3, group.serializeNodes()); preparedStatement.execute(); preparedStatement.close(); @@ -208,9 +207,7 @@ public class HikariDatastore extends Datastore { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { - if (!resultSet.getString("perms").equals("#")) { - group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":"))); - } + group.loadNodes(resultSet.getString("perms")); } preparedStatement.close(); @@ -231,9 +228,7 @@ public class HikariDatastore extends Datastore { ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { - if (!resultSet.getString("perms").equals("#")) { - group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":"))); - } + group.loadNodes(resultSet.getString("perms")); preparedStatement.close(); resultSet.close(); @@ -258,9 +253,7 @@ public class HikariDatastore extends Datastore { while (resultSet.next()) { Group group = plugin.getGroupManager().makeGroup(resultSet.getString("name")); - if (!resultSet.getString("perms").equals("#")) { - group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":"))); - } + group.loadNodes(resultSet.getString("perms")); groups.add(group); } preparedStatement.close(); diff --git a/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java b/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java index 444825430..744f5372e 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java +++ b/common/src/main/java/me/lucko/luckperms/utils/PermissionObject.java @@ -1,5 +1,6 @@ package me.lucko.luckperms.utils; +import com.google.gson.reflect.TypeToken; import lombok.Getter; import lombok.Setter; import me.lucko.luckperms.LuckPermsPlugin; @@ -11,7 +12,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * Represents an object that can hold permissions @@ -262,48 +262,18 @@ public abstract class PermissionObject { } /** - * Loads a list of semi-serialised nodes into the object - * @param data The data to be loaded + * Loads serialised nodes into the object + * @param json The json data to be loaded */ - public void loadNodes(List data) { - // String is the node in format "server/plugin.command-false" or "plugin.command-false" or "server/plugin.command" - // or just "plugin.command" - - for (String s : data) { - String[] parts = s.split("-", 2); - - if (parts.length == 2) { - nodes.put(parts[0], Boolean.valueOf(parts[1])); - } else { - nodes.put(parts[0], true); - } - } - } - - /** - * Convert the permission nodes map to a list of strings - * @return a {@link List} of nodes - */ - public List getNodesAsString() { - List data = new ArrayList<>(); - - for (String node : nodes.keySet()) { - if (nodes.get(node)) { - data.add(node); - } else { - data.add(node + "-false"); - } - } - - return data; + public void loadNodes(String json) { + nodes.putAll(plugin.getGson().fromJson(json, new TypeToken>(){}.getType())); } /** * Serialize the nodes in the object to be saved in the datastore - * @return A serialized string + * @return A json string */ public String serializeNodes() { - if (nodes.isEmpty()) return "#"; - return getNodesAsString().stream().collect(Collectors.joining(":")); + return plugin.getGson().toJson(nodes); } } diff --git a/pom.xml b/pom.xml index 65efe13a4..7501071fa 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,12 @@ slf4j-simple 1.7.5 + + + com.google.code.gson + gson + 2.7 + org.projectlombok