mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-12-26 11:08:13 +01:00
Use gson instead of internal serialization method
This commit is contained in:
parent
484bc44288
commit
f6d8fb5286
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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<String> 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<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;
|
||||
public void loadNodes(String json) {
|
||||
nodes.putAll(plugin.getGson().fromJson(json, new TypeToken<Map<String, Boolean>>(){}.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);
|
||||
}
|
||||
}
|
||||
|
6
pom.xml
6
pom.xml
@ -51,6 +51,12 @@
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.5</version>
|
||||
</dependency>
|
||||
<!-- gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
Loading…
Reference in New Issue
Block a user