use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.
the class SqlDao method getGroupsWithPermission.
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) throws SQLException {
List<HeldPermission<String>> held = new ArrayList<>();
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_SELECT_PERMISSION))) {
ps.setString(1, permission);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String holder = rs.getString("name");
boolean value = rs.getBoolean("value");
String server = rs.getString("server");
String world = rs.getString("world");
long expiry = rs.getLong("expiry");
String contexts = rs.getString("contexts");
NodeModel data = deserializeNode(permission, value, server, world, expiry, contexts);
held.add(NodeHeldPermission.of(holder, data));
}
}
}
}
return held;
}
use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.
the class SqlDao method saveGroup.
@Override
public void saveGroup(Group group) throws SQLException {
group.getIoLock().lock();
try {
// Empty data, just delete.
if (group.getEnduringNodes().isEmpty()) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_DELETE))) {
ps.setString(1, group.getName());
ps.execute();
}
}
return;
}
// Get a snapshot of current data
Set<NodeModel> remote = new HashSet<>();
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_SELECT))) {
ps.setString(1, group.getName());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String permission = rs.getString("permission");
boolean value = rs.getBoolean("value");
String server = rs.getString("server");
String world = rs.getString("world");
long expiry = rs.getLong("expiry");
String contexts = rs.getString("contexts");
remote.add(deserializeNode(permission, value, server, world, expiry, contexts));
}
}
}
}
Set<NodeModel> local = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
Map.Entry<Set<NodeModel>, Set<NodeModel>> diff = compareSets(local, remote);
Set<NodeModel> toAdd = diff.getKey();
Set<NodeModel> toRemove = diff.getValue();
if (!toRemove.isEmpty()) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_DELETE_SPECIFIC))) {
for (NodeModel nd : toRemove) {
ps.setString(1, group.getName());
ps.setString(2, nd.getPermission());
ps.setBoolean(3, nd.getValue());
ps.setString(4, nd.getServer());
ps.setString(5, nd.getWorld());
ps.setLong(6, nd.getExpiry());
ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
ps.addBatch();
}
ps.executeBatch();
}
}
}
if (!toAdd.isEmpty()) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(GROUP_PERMISSIONS_INSERT))) {
for (NodeModel nd : toAdd) {
ps.setString(1, group.getName());
ps.setString(2, nd.getPermission());
ps.setBoolean(3, nd.getValue());
ps.setString(4, nd.getServer());
ps.setString(5, nd.getWorld());
ps.setLong(6, nd.getExpiry());
ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
ps.addBatch();
}
ps.executeBatch();
}
}
}
} finally {
group.getIoLock().unlock();
}
}
use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.
the class SqlDao method saveUser.
@Override
public void saveUser(User user) throws SQLException {
user.getIoLock().lock();
try {
// Empty data - just delete from the DB.
if (!this.plugin.getUserManager().shouldSave(user)) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_DELETE))) {
ps.setString(1, user.getUuid().toString());
ps.execute();
}
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
ps.setString(1, NodeFactory.DEFAULT_GROUP_NAME);
ps.setString(2, user.getUuid().toString());
ps.execute();
}
}
return;
}
// Get a snapshot of current data.
Set<NodeModel> remote = new HashSet<>();
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_SELECT))) {
ps.setString(1, user.getUuid().toString());
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String permission = rs.getString("permission");
boolean value = rs.getBoolean("value");
String server = rs.getString("server");
String world = rs.getString("world");
long expiry = rs.getLong("expiry");
String contexts = rs.getString("contexts");
remote.add(deserializeNode(permission, value, server, world, expiry, contexts));
}
}
}
}
Set<NodeModel> local = user.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toSet());
Map.Entry<Set<NodeModel>, Set<NodeModel>> diff = compareSets(local, remote);
Set<NodeModel> toAdd = diff.getKey();
Set<NodeModel> toRemove = diff.getValue();
if (!toRemove.isEmpty()) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_DELETE_SPECIFIC))) {
for (NodeModel nd : toRemove) {
ps.setString(1, user.getUuid().toString());
ps.setString(2, nd.getPermission());
ps.setBoolean(3, nd.getValue());
ps.setString(4, nd.getServer());
ps.setString(5, nd.getWorld());
ps.setLong(6, nd.getExpiry());
ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
ps.addBatch();
}
ps.executeBatch();
}
}
}
if (!toAdd.isEmpty()) {
try (Connection c = this.provider.getConnection()) {
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_INSERT))) {
for (NodeModel nd : toAdd) {
ps.setString(1, user.getUuid().toString());
ps.setString(2, nd.getPermission());
ps.setBoolean(3, nd.getValue());
ps.setString(4, nd.getServer());
ps.setString(5, nd.getWorld());
ps.setLong(6, nd.getExpiry());
ps.setString(7, this.gson.toJson(ContextSetJsonSerializer.serializeContextSet(nd.getContexts())));
ps.addBatch();
}
ps.executeBatch();
}
}
}
try (Connection c = this.provider.getConnection()) {
boolean hasPrimaryGroupSaved;
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_SELECT_PRIMARY_GROUP_BY_UUID))) {
ps.setString(1, user.getUuid().toString());
try (ResultSet rs = ps.executeQuery()) {
hasPrimaryGroupSaved = rs.next();
}
}
if (hasPrimaryGroupSaved) {
// update
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_UPDATE_PRIMARY_GROUP_BY_UUID))) {
ps.setString(1, user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME));
ps.setString(2, user.getUuid().toString());
ps.execute();
}
} else {
// insert
try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_INSERT))) {
ps.setString(1, user.getUuid().toString());
ps.setString(2, user.getName().orElse("null"));
ps.setString(3, user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME));
ps.execute();
}
}
}
} finally {
user.getIoLock().unlock();
}
}
use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.
the class ConfigurateDao method readAttributes.
private static Collection<NodeModel> readAttributes(ConfigurationNode attributes, String permission) {
boolean value = attributes.getNode("value").getBoolean(true);
String server = attributes.getNode("server").getString("global");
String world = attributes.getNode("world").getString("global");
long expiry = attributes.getNode("expiry").getLong(0L);
ImmutableContextSet context = ImmutableContextSet.empty();
ConfigurationNode contextMap = attributes.getNode("context");
if (!contextMap.isVirtual() && contextMap.hasMapChildren()) {
context = ContextSetConfigurateSerializer.deserializeContextSet(contextMap).makeImmutable();
}
ConfigurationNode batchAttribute = attributes.getNode("permissions");
if (permission.startsWith("luckperms.batch") && !batchAttribute.isVirtual() && batchAttribute.hasListChildren()) {
List<NodeModel> nodes = new ArrayList<>();
for (ConfigurationNode element : batchAttribute.getChildrenList()) {
nodes.add(NodeModel.of(element.getString(), value, server, world, expiry, context));
}
return nodes;
} else {
return Collections.singleton(NodeModel.of(permission, value, server, world, expiry, context));
}
}
use of me.lucko.luckperms.common.node.NodeModel in project LuckPerms by lucko.
the class ConfigurateDao method createAndLoadGroup.
@Override
public Group createAndLoadGroup(String name) throws Exception {
Group group = this.plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock();
try {
ConfigurationNode object = readFile(StorageLocation.GROUP, name);
if (object != null) {
Set<Node> nodes = readNodes(object).stream().map(NodeModel::toNode).collect(Collectors.toSet());
group.setEnduringNodes(nodes);
} else {
ConfigurationNode data = SimpleConfigurationNode.root();
data.getNode("name").setValue(group.getName());
Set<NodeModel> nodes = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
writeNodes(data, nodes);
saveFile(StorageLocation.GROUP, name, data);
}
} catch (Exception e) {
throw reportException(name, e);
} finally {
group.getIoLock().unlock();
}
group.getRefreshBuffer().requestDirectly();
return group;
}
Aggregations