Use gson instead of internal serialization method

This commit is contained in:
Luck 2016-06-22 12:51:37 +01:00
parent 484bc44288
commit f6d8fb5286
6 changed files with 47 additions and 59 deletions

View File

@ -1,5 +1,6 @@
package me.lucko.luckperms; package me.lucko.luckperms;
import com.google.gson.Gson;
import lombok.Getter; import lombok.Getter;
import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.data.Datastore;
import me.lucko.luckperms.data.DatastoreConfiguration; import me.lucko.luckperms.data.DatastoreConfiguration;
@ -28,9 +29,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
private UserManager userManager; private UserManager userManager;
private GroupManager groupManager; private GroupManager groupManager;
private Datastore datastore; private Datastore datastore;
private Gson gson;
@Override @Override
public void onEnable() { public void onEnable() {
gson = new Gson();
configuration = new BukkitConfig(this); configuration = new BukkitConfig(this);
// register events // register events
@ -93,6 +96,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
Bukkit.getScheduler().runTask(this, r); Bukkit.getScheduler().runTask(this, r);
} }
@Override
public Gson getGson() {
return gson;
}
@Override @Override
public String getVersion() { public String getVersion() {
return VERSION; return VERSION;

View File

@ -1,5 +1,6 @@
package me.lucko.luckperms; package me.lucko.luckperms;
import com.google.gson.Gson;
import lombok.Getter; import lombok.Getter;
import me.lucko.luckperms.commands.CommandManager; import me.lucko.luckperms.commands.CommandManager;
import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.data.Datastore;
@ -24,9 +25,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
private UserManager userManager; private UserManager userManager;
private GroupManager groupManager; private GroupManager groupManager;
private Datastore datastore; private Datastore datastore;
private Gson gson;
@Override @Override
public void onEnable() { public void onEnable() {
gson = new Gson();
configuration = new BungeeConfig(this); configuration = new BungeeConfig(this);
// register events // register events
@ -87,4 +90,9 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
public void doSync(Runnable r) { public void doSync(Runnable r) {
r.run(); r.run();
} }
@Override
public Gson getGson() {
return gson;
}
} }

View File

@ -1,5 +1,6 @@
package me.lucko.luckperms; package me.lucko.luckperms;
import com.google.gson.Gson;
import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.data.Datastore;
import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.groups.GroupManager;
import me.lucko.luckperms.users.UserManager; import me.lucko.luckperms.users.UserManager;
@ -74,4 +75,6 @@ public interface LuckPermsPlugin {
* @param r the task to run * @param r the task to run
*/ */
void doSync(Runnable r); void doSync(Runnable r);
Gson getGson();
} }

View File

@ -2,6 +2,7 @@ package me.lucko.luckperms.data;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.groups.GroupManager; import me.lucko.luckperms.groups.GroupManager;
import me.lucko.luckperms.users.User; import me.lucko.luckperms.users.User;
@ -11,7 +12,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -121,9 +121,7 @@ public class HikariDatastore extends Datastore {
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
if (!resultSet.getString("perms").equals("#")) { user.loadNodes(resultSet.getString("perms"));
user.loadNodes((Arrays.asList(resultSet.getString("perms").split(":"))));
}
user.setName(resultSet.getString("name")); user.setName(resultSet.getString("name"));
preparedStatement.close(); preparedStatement.close();
@ -143,11 +141,14 @@ public class HikariDatastore extends Datastore {
@Override @Override
public boolean loadOrCreateUser(UUID uuid, String username) { public boolean loadOrCreateUser(UUID uuid, String username) {
User user = plugin.getUserManager().makeUser(uuid, username); User user = plugin.getUserManager().makeUser(uuid, username);
try {
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
} catch (ObjectAlreadyHasException ignored) {}
boolean success = runQuery(connection -> { boolean success = runQuery(connection -> {
PreparedStatement preparedStatement = connection.prepareStatement(USER_INSERT); PreparedStatement preparedStatement = connection.prepareStatement(USER_INSERT);
preparedStatement.setString(1, uuid.toString()); preparedStatement.setString(1, user.getUuid().toString());
preparedStatement.setString(2, username); preparedStatement.setString(2, user.getName());
preparedStatement.setString(3, plugin.getConfiguration().getDefaultGroupNode()); preparedStatement.setString(3, user.serializeNodes());
preparedStatement.setString(4, username); preparedStatement.setString(4, username);
preparedStatement.execute(); preparedStatement.execute();
preparedStatement.close(); preparedStatement.close();
@ -158,9 +159,7 @@ public class HikariDatastore extends Datastore {
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
if (!resultSet.getString("perms").equals("#")) { user.loadNodes(resultSet.getString("perms"));
user.loadNodes(Arrays.asList(resultSet.getString("perms").split(":")));
}
preparedStatement.close(); preparedStatement.close();
resultSet.close(); resultSet.close();
@ -196,9 +195,9 @@ public class HikariDatastore extends Datastore {
Group group = plugin.getGroupManager().makeGroup(name); Group group = plugin.getGroupManager().makeGroup(name);
boolean success = runQuery(connection -> { boolean success = runQuery(connection -> {
PreparedStatement preparedStatement = connection.prepareStatement(GROUP_INSERT); PreparedStatement preparedStatement = connection.prepareStatement(GROUP_INSERT);
preparedStatement.setString(1, name); preparedStatement.setString(1, group.getName());
preparedStatement.setString(2, "#"); preparedStatement.setString(2, group.serializeNodes());
preparedStatement.setString(3, "#"); preparedStatement.setString(3, group.serializeNodes());
preparedStatement.execute(); preparedStatement.execute();
preparedStatement.close(); preparedStatement.close();
@ -208,9 +207,7 @@ public class HikariDatastore extends Datastore {
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
if (!resultSet.getString("perms").equals("#")) { group.loadNodes(resultSet.getString("perms"));
group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":")));
}
} }
preparedStatement.close(); preparedStatement.close();
@ -231,9 +228,7 @@ public class HikariDatastore extends Datastore {
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
if (!resultSet.getString("perms").equals("#")) { group.loadNodes(resultSet.getString("perms"));
group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":")));
}
preparedStatement.close(); preparedStatement.close();
resultSet.close(); resultSet.close();
@ -258,9 +253,7 @@ public class HikariDatastore extends Datastore {
while (resultSet.next()) { while (resultSet.next()) {
Group group = plugin.getGroupManager().makeGroup(resultSet.getString("name")); Group group = plugin.getGroupManager().makeGroup(resultSet.getString("name"));
if (!resultSet.getString("perms").equals("#")) { group.loadNodes(resultSet.getString("perms"));
group.loadNodes(Arrays.asList(resultSet.getString("perms").split(":")));
}
groups.add(group); groups.add(group);
} }
preparedStatement.close(); preparedStatement.close();

View File

@ -1,5 +1,6 @@
package me.lucko.luckperms.utils; package me.lucko.luckperms.utils;
import com.google.gson.reflect.TypeToken;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.LuckPermsPlugin;
@ -11,7 +12,6 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* Represents an object that can hold permissions * 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 * Loads serialised nodes into the object
* @param data The data to be loaded * @param json The json data to be loaded
*/ */
public void loadNodes(List<String> data) { public void loadNodes(String json) {
// String is the node in format "server/plugin.command-false" or "plugin.command-false" or "server/plugin.command" nodes.putAll(plugin.getGson().fromJson(json, new TypeToken<Map<String, Boolean>>(){}.getType()));
// 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<String> getNodesAsString() {
List<String> data = new ArrayList<>();
for (String node : nodes.keySet()) {
if (nodes.get(node)) {
data.add(node);
} else {
data.add(node + "-false");
}
}
return data;
} }
/** /**
* Serialize the nodes in the object to be saved in the datastore * Serialize the nodes in the object to be saved in the datastore
* @return A serialized string * @return A json string
*/ */
public String serializeNodes() { public String serializeNodes() {
if (nodes.isEmpty()) return "#"; return plugin.getGson().toJson(nodes);
return getNodesAsString().stream().collect(Collectors.joining(":"));
} }
} }

View File

@ -51,6 +51,12 @@
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.7.5</version> <version>1.7.5</version>
</dependency> </dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<!-- Lombok --> <!-- Lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>