diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deop.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deop.java index c901c137..01ff2952 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deop.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deop.java @@ -48,7 +48,7 @@ public class Command_deop extends FreedomCommand return; } - if (!result.wasSuccessful()) { + if (result == null || !result.wasSuccessful()) { msgNew(" is already non-op.", Placeholder.unparsed("player", player.getName())); return; } @@ -60,6 +60,7 @@ public class Command_deop extends FreedomCommand { FUtil.adminAction(sender.getName(), "De-opping " + player.getName(), false); } + Hierarchy.getHierarchy().updateUserData(player); }); return true; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deopall.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deopall.java index 7c70cf8c..31114e85 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deopall.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_deopall.java @@ -29,13 +29,14 @@ public class Command_deopall extends FreedomCommand return; } - if (!result.wasSuccessful()) { + if (result == null || !result.wasSuccessful()) { msgNew(" is already non-OP.", Placeholder.unparsed("player", player.getName())); return; } msg(player, YOU_ARE_NOT_OP); plugin.rm.updateDisplay(player); + Hierarchy.getHierarchy().updateUserData(player); })); return true; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_op.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_op.java index 750a6e18..6861bbb2 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_op.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_op.java @@ -48,13 +48,18 @@ public class Command_op extends FreedomCommand return; } - if (!result.wasSuccessful()) + if (result == null) { + msgNew(" was not on the track! Added.", player(player)); + } + else if (!result.wasSuccessful()) { msgNew(" is already OP!", player(player)); + return; } msg(player, YOU_ARE_OP); plugin.rm.updateDisplay(player); + Hierarchy.getHierarchy().updateUserData(player); if (!atomicBoolean.get()) { FUtil.adminAction(sender.getName(), "Opping " + player.getName(), false); diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java index 6ecd3beb..ee1f0280 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java @@ -17,7 +17,7 @@ public class Command_opall extends FreedomCommand FUtil.adminAction(sender.getName(), "Opping all players on the server", true); server.getOnlinePlayers().forEach(player -> - Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((ignored, throwable) -> + Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, throwable) -> { if (throwable != null) { @@ -27,8 +27,16 @@ public class Command_opall extends FreedomCommand return; } + if (result == null) { + msgNew(" was not present on the track! Added.", player(player)); + } else if (!result.wasSuccessful()) { + msgNew("Player is already OP!", player(player)); + return; + } + msg(player, YOU_ARE_OP); plugin.rm.updateDisplay(player); + Hierarchy.getHierarchy().updateUserData(player); })); return true; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opme.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opme.java index 14b17427..c037366e 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opme.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opme.java @@ -24,8 +24,14 @@ public class Command_opme extends FreedomCommand return; } + if (result != null && !result.wasSuccessful()) { + msgNew("You are already op!"); + return; + } + msg(YOU_ARE_OP); plugin.rm.updateDisplay(playerSender); + Hierarchy.getHierarchy().updateUserData(playerSender); }); return true; } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java index 58f7e3cd..abe3923f 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java @@ -90,58 +90,63 @@ public class Command_saconfig extends FreedomCommand return; } - if (!result.wasSuccessful() || result.getGroupTo().isEmpty()) + if (result == null) { + msgNew("Added to the ADMIN group.", player(player)); + } + else if (!result.wasSuccessful() || result.getGroupTo().isEmpty()) { msgNew("Failed to promote to the next rank.", player(player)); return; + } else { + result.getGroupTo().ifPresentOrElse(group -> + { + Admin admin = plugin.al.getAdmin(player); + + if (admin == null) + { + admin = new Admin(player); + plugin.al.addAdmin(admin); + } + + Group actual = Hierarchy.getHierarchy().getGroup(group); + DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup(); + + if (rank == null) + { + msgNew("Unknown rank: ", Placeholder.unparsed("rank", args[2])); + return; + } + + admin.setRank(rank); + admin.addIp(FUtil.getIp(player)); + admin.setActive(true); + admin.setLastLogin(new Date()); + + plugin.al.save(admin); + plugin.al.updateTables(); + plugin.rm.updateDisplay(player); + + if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) + { + plugin.dc.syncRoles(admin, plugin.pl.getData(player).getDiscordID()); + } + + msgNew("Set 's rank to .", Placeholder.unparsed("admin", admin.getName()), Placeholder.component("rank", rank.getName())); + + final FPlayer fPlayer = plugin.pl.getPlayer(player); + if (fPlayer.getFreezeData().isFrozen()) + { + fPlayer.getFreezeData().setFrozen(false); + msgNew(player, "You have been unfrozen."); + } + + Hierarchy.getHierarchy().updateUserData(player); + + msgNew("Admin has been promoted to ", + player(player), + Placeholder.unparsed("rank", group)); + }, () -> msgNew("Failed to promote to the next rank.", player(player))); } - - result.getGroupTo().ifPresentOrElse(group -> - { - Admin admin = plugin.al.getAdmin(player); - - if (admin == null) - { - admin = new Admin(player); - plugin.al.addAdmin(admin); - } - - Group actual = Hierarchy.getHierarchy().getGroup(group); - DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup(); - - if (rank == null) - { - msgNew("Unknown rank: ", Placeholder.unparsed("rank", args[2])); - return; - } - - admin.setRank(rank); - admin.addIp(FUtil.getIp(player)); - admin.setActive(true); - admin.setLastLogin(new Date()); - - plugin.al.save(admin); - plugin.al.updateTables(); - plugin.rm.updateDisplay(player); - - if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) - { - plugin.dc.syncRoles(admin, plugin.pl.getData(player).getDiscordID()); - } - - msgNew("Set 's rank to .", Placeholder.unparsed("admin", admin.getName()), Placeholder.component("rank", rank.getName())); - - final FPlayer fPlayer = plugin.pl.getPlayer(player); - if (fPlayer.getFreezeData().isFrozen()) - { - fPlayer.getFreezeData().setFrozen(false); - msgNew(player, "You have been unfrozen."); - } - - msgNew("Admin has been promoted to ", - player(player), - Placeholder.unparsed("rank", group)); - }, () -> msgNew("Failed to promote to the next rank.", player(player))); }); return true; @@ -168,7 +173,7 @@ public class Command_saconfig extends FreedomCommand if (admin == null) { - msgNew("Unknown admin: ", Placeholder.unparsed("player", args[1])); + msgNew("Unknown admin: ", arg(args[1])); return true; } else { @@ -207,7 +212,7 @@ public class Command_saconfig extends FreedomCommand return; } - if (!result.wasSuccessful() || result.getGroupTo().isEmpty()) + if (result == null || !result.wasSuccessful() || result.getGroupTo().isEmpty()) { msgNew("Failed to demote to the next rank.", player(player)); return; @@ -253,6 +258,8 @@ public class Command_saconfig extends FreedomCommand msgNew(player, "You have been unfrozen."); } + Hierarchy.getHierarchy().updateUserData(player); + msgNew("Admin has been demoted to ", Placeholder.unparsed("player", player.getName()), Placeholder.unparsed("rank", group)); diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java index 9495dd89..2c1725e9 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java @@ -17,13 +17,11 @@ public class UserWrapper { private final UserManager manager; private final PlayerAdapter playerAdapter; - private final Set userSet; private final Set userDataSet; public UserWrapper(UserManager manager, PlayerAdapter adapter) { this.manager = manager; - this.userSet = manager.getLoadedUsers(); this.playerAdapter = adapter; this.userDataSet = new HashSet<>(); } @@ -42,7 +40,7 @@ public class UserWrapper public CompletableFuture getUser(Player player) { - if (!manager.isLoaded(player.getUniqueId()) || !userSet.contains(manager.getUser(player.getUniqueId()))) + if (!manager.isLoaded(player.getUniqueId()) || !manager.getLoadedUsers().contains(manager.getUser(player.getUniqueId()))) { return manager.loadUser(player.getUniqueId()); } @@ -50,15 +48,6 @@ public class UserWrapper return CompletableFuture.supplyAsync(() -> manager.getUser(player.getUniqueId())); } - public CompletableFuture removeUser(Player player) - { - return getUser(player).thenAccept(a -> - { - manager.cleanupUser(a); - userSet.remove(a); - }); - } - public CompletableFuture getUserData(Player player) { return getUser(player).thenApply(user -> diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java index d4ecf7c5..622acf71 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java @@ -7,8 +7,6 @@ import me.totalfreedom.totalfreedommod.util.FConverter; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java index 35564afc..997cb8c7 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java @@ -115,16 +115,87 @@ public class Hierarchy }); } + public void addUserData(Player player) { + userWrapper.addUserData(player).whenComplete((a, b) -> + { + if (b != null) + { + FLog.severe(b); + } + }); + } + + public void updateUserData(Player player) { + userWrapper.updateUserData(player).whenComplete((a, b) -> + { + if (b != null) + { + FLog.severe(b); + } + }); + } + + public void dropUserData(Player player) { + userWrapper.removeUserData(player).whenComplete((a, b) -> + { + if (b != null) + { + FLog.severe(b); + } + }); + } + public CompletableFuture promoteUser(Track track, Player player) { return userWrapper.getUserData(player).thenApply(data -> - track.promote(data.getUser(), data.getContextSet())); + { + boolean isOnTrack = false; + for (String trackGroup : track.getGroups()) + { + for (Group userGroup : data.getUser().getInheritedGroups(data.getQueryOptions())) + { + if (userGroup.getName().equals(trackGroup)) + { + isOnTrack = true; + } + } + } + + if (isOnTrack) + { + return track.promote(data.getUser(), data.getContextSet()); + } else + { + addUserToGroup(GroupProvider.fromString(track.getGroups().get(0)).getGroup(), player); + return null; + } + }); } public CompletableFuture demoteUser(Track track, Player player) { return userWrapper.getUserData(player).thenApply(data -> - track.demote(data.getUser(), data.getContextSet())); + { + boolean isOnTrack = false; + for (String trackGroup : track.getGroups()) + { + for (Group userGroup : data.getUser().getInheritedGroups(data.getQueryOptions())) + { + if (userGroup.getName().equals(trackGroup)) + { + isOnTrack = true; + } + } + } + + if (isOnTrack) + { + return track.demote(data.getUser(), data.getContextSet()); + } else + { + return null; + } + }); } public void addUserToGroup(DisplayableGroup group, Player player) @@ -145,6 +216,8 @@ public class Hierarchy { FLog.severe(b); } + + updateUserData(player); }); } @@ -175,6 +248,8 @@ public class Hierarchy { FLog.severe(b); } + + updateUserData(player); }); } } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java index f2d9c3d4..5fee4967 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java @@ -15,6 +15,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; @@ -187,10 +188,18 @@ public class RankManager extends FreedomService updatePlayerTeam(player); } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLeave(PlayerQuitEvent event) { + Hierarchy.getHierarchy().dropUserData(event.getPlayer()); + } + @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent event) { final Player player = event.getPlayer(); + + Hierarchy.getHierarchy().addUserData(player); + PlayerData target = plugin.pl.getData(player); boolean isAdmin = plugin.al.isAdmin(player);