diff --git a/src/main/java/mc/unraveled/reforged/command/GroupCMD.java b/src/main/java/mc/unraveled/reforged/command/GroupCMD.java index 64f3368..815039e 100644 --- a/src/main/java/mc/unraveled/reforged/command/GroupCMD.java +++ b/src/main/java/mc/unraveled/reforged/command/GroupCMD.java @@ -63,6 +63,8 @@ public class GroupCMD extends AbstractCommandBase { RankManager manager = getPlugin().getRankManager(); OfflinePlayer player; + manager.unbake(); + switch (target) { case "player": if (getPlugin().getServer().getPlayer(value) != null) { @@ -77,17 +79,25 @@ public class GroupCMD extends AbstractCommandBase { if (actionType) { manager.insertPlayer(rankContext.getContext(), player); + manager.bake(); + manager.save(); return Component.text("Added player " + player.getName() + " to group " + group + "!").color(NamedTextColor.GREEN); } else { manager.ejectPlayer(rankContext.getContext(), player); + manager.bake(); + manager.save(); return Component.text("Removed player " + player.getName() + " from group " + group + "!").color(NamedTextColor.GREEN); } case "permission": if (actionType) { manager.insertPermission(rankContext.getContext(), value); + manager.bake(); + manager.save(); return Component.text("Added permission " + value + " to group " + group + "!").color(NamedTextColor.GREEN); } else { manager.ejectPermission(rankContext.getContext(), value); + manager.bake(); + manager.save(); return Component.text("Removed permission " + value + " from group " + group + "!").color(NamedTextColor.GREEN); } default: diff --git a/src/main/java/mc/unraveled/reforged/permission/RankManager.java b/src/main/java/mc/unraveled/reforged/permission/RankManager.java index 8364456..1e2861e 100644 --- a/src/main/java/mc/unraveled/reforged/permission/RankManager.java +++ b/src/main/java/mc/unraveled/reforged/permission/RankManager.java @@ -7,6 +7,7 @@ import mc.unraveled.reforged.plugin.Traverse; import mc.unraveled.reforged.storage.DBGroup; import mc.unraveled.reforged.util.Tuple; import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -77,6 +78,22 @@ public class RankManager implements Baker, Locker { .forEach(tuple -> tuple.getSecond().remove(player)); } + public void save() { + if (!baked) throw new IllegalStateException("You cannot save a rank manager that has not been baked!"); + + DBGroup groupHandler = new DBGroup(plugin.getSQLManager().establish()); + bakedGroups.forEach(group -> { + for (OfflinePlayer p : group.getPlayers()) { + groupHandler.addPlayer(group.getRank(), p); + } + + for (String arg : group.getPermissions()) { + groupHandler.addPermission(group.getRank(), arg); + } + }); + groupHandler.close(); + } + @Override public void bake() { if (baked) return; diff --git a/src/main/java/mc/unraveled/reforged/storage/DBGroup.java b/src/main/java/mc/unraveled/reforged/storage/DBGroup.java index 5c0e417..fbb5929 100644 --- a/src/main/java/mc/unraveled/reforged/storage/DBGroup.java +++ b/src/main/java/mc/unraveled/reforged/storage/DBGroup.java @@ -56,14 +56,14 @@ public class DBGroup { } @SneakyThrows - public void addPlayer(Rank rank, Player player) { + public void addPlayer(Rank rank, OfflinePlayer player) { PreparedStatement statement = getConnection().prepareStatement("INSERT INTO " + rank.getAttachment().getName() + " (uuid) VALUES (?);"); statement.setString(1, player.getUniqueId().toString()); statement.executeUpdate(); } @SneakyThrows - public void removePlayer(Rank rank, Player player) { + public void removePlayer(Rank rank, OfflinePlayer player) { PreparedStatement statement = getConnection().prepareStatement("DELETE FROM " + rank.getAttachment().getName() + " WHERE uuid = ?;"); statement.setString(1, player.getUniqueId().toString()); statement.executeUpdate();