From 9138d1856bc5ca1a0fe796821b2d76e7c5d8c6a5 Mon Sep 17 00:00:00 2001 From: Paul Reilly Date: Thu, 13 Apr 2023 14:37:55 -0500 Subject: [PATCH] attempt to fix once again --- .../FreedomServiceHandler.java | 13 +- .../totalfreedommod/admin/Admin.java | 2 +- .../totalfreedommod/admin/AdminList.java | 4 +- .../totalfreedommod/api/Context.java | 18 +-- .../totalfreedommod/api/ShopItem.java | 3 +- .../api/event/AdminChatEvent.java | 10 +- .../bridge/EssentialsBridge.java | 4 +- .../totalfreedommod/command/Command_deop.java | 2 +- .../command/Command_deopall.java | 2 +- .../command/Command_saconfig.java | 4 +- .../totalfreedommod/perms/GroupWrapper.java | 34 ++++- .../totalfreedommod/perms/UserWrapper.java | 12 +- .../totalfreedommod/player/PlayerList.java | 2 +- .../rank/DisplayableGroup.java | 123 ++++++++++++------ .../totalfreedommod/rank/GroupProvider.java | 22 ++-- .../totalfreedommod/rank/Hierarchy.java | 71 ++++++++-- .../totalfreedommod/rank/RankManager.java | 19 ++- 17 files changed, 237 insertions(+), 108 deletions(-) diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/FreedomServiceHandler.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/FreedomServiceHandler.java index ffd397b2..9e20a60d 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/FreedomServiceHandler.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/FreedomServiceHandler.java @@ -1,5 +1,12 @@ package me.totalfreedom.totalfreedommod; +import me.totalfreedom.totalfreedommod.api.Callback; +import me.totalfreedom.totalfreedommod.api.event.EventBus; +import me.totalfreedom.totalfreedommod.api.event.MethodEvent; +import me.totalfreedom.totalfreedommod.rank.DisplayableGroup; +import me.totalfreedom.totalfreedommod.rank.Hierarchy; +import me.totalfreedom.totalfreedommod.util.FLog; + import java.util.ArrayList; import java.util.List; @@ -29,8 +36,7 @@ public class FreedomServiceHandler try { service.onStart(); - } - catch (Exception e) + } catch (Exception e) { e.printStackTrace(); } @@ -44,8 +50,7 @@ public class FreedomServiceHandler try { service.onStop(); - } - catch (Exception e) + } catch (Exception e) { e.printStackTrace(); } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java index ce491f8b..da970cd1 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java @@ -153,7 +153,7 @@ public class Admin private void setActiveSplitWorkToReduceComplexity(TotalFreedomMod plugin) { - if (getRank().isAtLeast(GroupProvider.ADMIN.getGroup())) + if (getRank().weightCheckAgainst(GroupProvider.ADMIN.getGroup())) { if (plugin.btb != null) { diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java index 6ba49090..493ce879 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java @@ -192,7 +192,7 @@ public class AdminList extends FreedomService public boolean removeAdmin(Admin admin) { - if (admin.getRank().isAtLeast(GroupProvider.ADMIN.getGroup()) && (plugin.btb != null)) + if (admin.getRank().weightCheckAgainst(GroupProvider.ADMIN.getGroup()) && (plugin.btb != null)) { plugin.btb.killTelnetSessions(admin.getName()); } @@ -270,7 +270,7 @@ public class AdminList extends FreedomService public void deactivateOldEntries(boolean verbose) { allAdmins.stream() - .filter(admin -> admin.isActive() && !admin.getRank().isAtLeast(GroupProvider.SENIOR_ADMIN.getGroup())) + .filter(admin -> admin.isActive() && !admin.getRank().weightCheckAgainst(GroupProvider.SENIOR_ADMIN.getGroup())) .forEach(admin -> { final Date lastLogin = admin.getLastLogin(); diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java index 1c1e7c4e..0f80de92 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/Context.java @@ -2,15 +2,15 @@ package me.totalfreedom.totalfreedommod.api; public class Context { - private final T value; + private final T value; - public Context(T value) - { - this.value = value; - } + public Context(T value) + { + this.value = value; + } - public T getValue() - { - return value; - } + public T getValue() + { + return value; + } } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java index f3865ce2..7d3c2984 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/ShopItem.java @@ -66,8 +66,7 @@ public enum ShopItem try { return ShopItem.valueOf(string.toUpperCase()); - } - catch (Exception ignored) + } catch (Exception ignored) { } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java index 0426372f..1e97a0be 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/api/event/AdminChatEvent.java @@ -9,13 +9,13 @@ import org.jetbrains.annotations.NotNull; public class AdminChatEvent extends Event { - private static HandlerList handlerList = new HandlerList(); + private static final HandlerList handlerList = new HandlerList(); //-- - private Key identifier; + private final Key identifier; private Component prefix = Component.empty(); - private Component name; - private Displayable displayable; - private Component message; + private final Component name; + private final Displayable displayable; + private final Component message; public AdminChatEvent(Key identifier, Component prefix, Component name, Displayable rank, Component message, boolean async) { diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/bridge/EssentialsBridge.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/bridge/EssentialsBridge.java index 8175ce50..def57bdf 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/bridge/EssentialsBridge.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/bridge/EssentialsBridge.java @@ -7,6 +7,7 @@ import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.rank.DisplayableGroup; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; +import net.luckperms.api.context.ContextSatisfyMode; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -158,7 +159,8 @@ public class EssentialsBridge extends FreedomService Player invOwner = (Player)inventoryHolder; DisplayableGroup recieverRank = plugin.rm.getRank(player); DisplayableGroup playerRank = plugin.rm.getRank(invOwner); - if (playerRank.getWeight() >= recieverRank.getWeight() || !invOwner.isOnline()) + if (playerRank.getWeight().getWeight() >= recieverRank.getWeight().getWeight() + || !invOwner.isOnline()) { event.setCancelled(true); refreshPlayer = player; 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 f86aab32..7659bff2 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 @@ -50,7 +50,7 @@ public class Command_deop extends FreedomCommand return; } - if (result == null || !result.wasSuccessful()) { + if (!result.wasSuccessful()) { msgNew(" is already non-op.", Placeholder.unparsed("player", player.getName())); return; } 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 b903520d..47de6242 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 @@ -33,7 +33,7 @@ public class Command_deopall extends FreedomCommand return; } - if (result == null || !result.wasSuccessful()) { + if (!result.wasSuccessful()) { msgNew(" is already non-OP.", Placeholder.unparsed("player", player.getName())); return; } 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 bead9b55..a125a0d5 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 @@ -213,7 +213,7 @@ public class Command_saconfig extends FreedomCommand return; } - if (result == null || !result.wasSuccessful() || result.getGroupTo().isEmpty()) + if (!result.wasSuccessful() || result.getGroupTo().isEmpty()) { msgNew("Failed to demote to the next rank.", player(player)); return; @@ -244,7 +244,7 @@ public class Command_saconfig extends FreedomCommand plugin.al.save(admin); plugin.al.updateTables(); - if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) + if (plugin.dc != null && plugin.dc.isEnabled() && Boolean.TRUE.equals(ConfigEntry.DISCORD_ROLE_SYNC.getBoolean())) { plugin.dc.syncRoles(admin, plugin.pl.getData(player).getDiscordID()); } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/GroupWrapper.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/GroupWrapper.java index 017b4755..ca292266 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/GroupWrapper.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/GroupWrapper.java @@ -1,9 +1,12 @@ package me.totalfreedom.totalfreedommod.perms; +import me.totalfreedom.totalfreedommod.util.FLog; import net.luckperms.api.model.group.Group; import net.luckperms.api.model.group.GroupManager; +import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.function.Function; public class GroupWrapper { @@ -18,14 +21,39 @@ public class GroupWrapper { if (!manager.isLoaded(name) || !manager.getLoadedGroups().contains(manager.getGroup(name))) { - return manager.createAndLoadGroup(name); + Optional group = manager.loadGroup(name).join(); + return group + .map(CompletableFuture::completedFuture) + .orElseGet(() -> manager.createAndLoadGroup(name)); } return CompletableFuture.supplyAsync(() -> manager.getGroup(name)); } - public void saveGroup(String name) + public void modifyGroup(String name, Function function) { - getGroup(name).thenAccept(manager::saveGroup); + getGroup(name) + .thenApplyAsync(function) + .thenAcceptAsync(manager::saveGroup) + .whenCompleteAsync((aVoid, throwable) -> + { + if (throwable != null) + { + FLog.severe(throwable); + } + }); + } + + public void saveGroupAsync(String name) + { + getGroup(name) + .thenAcceptAsync(manager::saveGroup) + .whenCompleteAsync((aVoid, throwable) -> + { + if (throwable != null) + { + FLog.severe(throwable); + } + }); } } 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 ba5d4d41..bd33302a 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/perms/UserWrapper.java @@ -99,21 +99,19 @@ public class UserWrapper manager.modifyUser(user.getUniqueId(), c -> { if (!c.getNodes(NodeType.INHERITANCE) - .contains(GroupProvider.inheritanceNode(group))) - c.data().add(GroupProvider.inheritanceNode(group)); + .contains(group.getInheritance())) + c.data().add(group.getInheritance()); })); } - public void removeFromGroup(User user, Group group) + public void removeFromGroup(User user, DisplayableGroup group) { - InheritanceNode groupNode = InheritanceNode.builder(group).build(); - CompletableFuture.runAsync(() -> manager.modifyUser(user.getUniqueId(), userConsumer -> { if (userConsumer.getNodes(NodeType.INHERITANCE) - .contains(groupNode)) - userConsumer.data().remove(groupNode); + .contains(group.getInheritance())) + userConsumer.data().remove(group.getInheritance()); })); } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java index d0487111..849887da 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java @@ -100,7 +100,7 @@ public class PlayerList extends FreedomService public boolean isTelnetMasterBuilder(PlayerData playerData) { Admin admin = plugin.al.getEntryByUuid(playerData.getUuid()); - return admin != null && admin.getRank().isAtLeast(GroupProvider.ADMIN.getGroup()) && playerData.isMasterBuilder(); + return admin != null && admin.getRank().weightCheckAgainst(GroupProvider.ADMIN.getGroup()) && playerData.isMasterBuilder(); } // May not return null diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/DisplayableGroup.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/DisplayableGroup.java index a2f6bfeb..acf80066 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/DisplayableGroup.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/DisplayableGroup.java @@ -1,19 +1,21 @@ package me.totalfreedom.totalfreedommod.rank; -import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.util.FUtil; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; -import net.luckperms.api.model.data.DataType; +import net.luckperms.api.context.ContextSatisfyMode; import net.luckperms.api.model.group.Group; +import net.luckperms.api.node.NodeType; import net.luckperms.api.node.types.InheritanceNode; +import net.luckperms.api.node.types.PermissionNode; import net.luckperms.api.node.types.PrefixNode; import net.luckperms.api.node.types.WeightNode; import org.apache.commons.lang.StringUtils; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; -import java.util.concurrent.CompletableFuture; public class DisplayableGroup implements Displayable { @@ -24,7 +26,7 @@ public class DisplayableGroup implements Displayable private final Component abbr; private final Component plural; private final Component article; - private final int weight; + private final WeightNode weight; private final Component tag; @@ -38,6 +40,12 @@ public class DisplayableGroup implements Displayable private final boolean hasDefaultLoginMessage; + + private final InheritanceNode inheritance; + private final PrefixNode prefix; + private final List permissions = new ArrayList<>(); + + public DisplayableGroup(String group, Component plural, Component tag, @@ -47,54 +55,80 @@ public class DisplayableGroup implements Displayable boolean hasTeam, boolean hasDefaultLoginMessage) { - Group matched = TotalFreedomMod.getPlugin().lpb.getAPI().getGroupManager().getGroup(group); + WeightNode tempWeight; + PrefixNode tempPrefix; + Group matched = Hierarchy.getHierarchy().getGroup(group); // we don't need to null check because if there is no group this method will also create one. - if (matched == null) - { - CompletableFuture cfg = TotalFreedomMod.getPlugin() - .lpb - .getAPI() - .getGroupManager() - .createAndLoadGroup(group); - - cfg.thenAcceptAsync(g -> - { - WeightNode weightNode = WeightNode.builder(weight).build(); - PrefixNode prefixNode = PrefixNode.builder() - .prefix(FUtil.miniMessage(GroupProvider.OPEN.append(tag.color(color)).append(GroupProvider.CLOSE))) - .priority(1) - .build(); - g.getData(DataType.NORMAL).add(prefixNode); - g.getData(DataType.NORMAL).add(weightNode); - }).join(); // Block until the group is created and loaded. - - matched = TotalFreedomMod.getPlugin().lpb.getAPI().getGroupManager().getGroup(group); - - if (matched == null) - throw new IllegalArgumentException("Group " + group + " does not exist and could not be created."); - } - - this.group = matched; this.name = (matched.getDisplayName() != null) ? FUtil.miniMessage(matched.getDisplayName()) : FUtil.miniMessage(matched.getName()); this.plural = plural; this.article = StringUtils.startsWithAny(this.name.toString().toLowerCase(Locale.ROOT), new String[]{"a", "e", "i", "o", "u"}) ? Component.text("an") : Component.text("a"); this.abbr = tag; - this.weight = weight; - this.tag = GroupProvider.OPEN.append(tag).append(GroupProvider.CLOSE); this.color = color; this.teamColor = teamColor; this.hasTeam = hasTeam; this.coloredTag = tag.color(color); + this.tag = GroupProvider.OPEN.append(coloredTag).append(GroupProvider.CLOSE); this.hasDefaultLoginMessage = hasDefaultLoginMessage; + tempWeight = WeightNode.builder(weight).build(); + tempPrefix = PrefixNode.builder(FUtil.miniMessage(getTag()), weight).build(); + + permissions.addAll(matched.getNodes(NodeType.PERMISSION)); + + if (!matched.getNodes(NodeType.PREFIX).contains(tempPrefix)) matched.data().add(tempPrefix); + else tempPrefix = matched.getNodes(NodeType.PREFIX).iterator().next(); + + if (!matched.getNodes(NodeType.WEIGHT).contains(tempWeight)) matched.data().add(tempWeight); + else tempWeight = matched.getNodes(NodeType.WEIGHT).iterator().next(); + + this.prefix = tempPrefix; + this.weight = tempWeight; + this.group = matched; + this.inheritance = InheritanceNode.builder(this.group).build(); + Hierarchy.getHierarchy().gw().saveGroupAsync(this.group.getName()); } - public boolean isAtLeast(@NotNull DisplayableGroup rank) + /** + * Checks the weight of this rank instance against the given rank. + * + * @param rank The rank to check against. + * @return True if the given rank is less than or equal to the weight of this specific rank instance. + */ + public boolean weightCheckAgainst(@NotNull DisplayableGroup rank) { - return rank.getLuckPermsGroup().getWeight().orElse(0) - <= getLuckPermsGroup().getWeight().orElse(0); + return rank.getWeight().getWeight() <= getWeight().getWeight(); } - public int getWeight() + public void addPermission(String permission) + { + PermissionNode node = PermissionNode.builder(permission).build(); + for (PermissionNode it : group.getNodes(NodeType.PERMISSION)) + { + if (it.getContexts().isSatisfiedBy(node.getContexts()) + || it.getKey().equals(node.getKey())) + continue; + + group.data().add(node); + permissions.add(node); + Hierarchy.getHierarchy().gw().saveGroupAsync(group.getName()); + } + } + + public void removePermission(String permission) + { + PermissionNode node = PermissionNode.builder(permission).build(); + for (PermissionNode it : group.getNodes(NodeType.PERMISSION)) + { + if (it.getContexts().isSatisfiedBy(node.getContexts(), ContextSatisfyMode.AT_LEAST_ONE_VALUE_PER_KEY) + || it.getKey().equals(node.getKey())) + { + group.data().remove(node); + permissions.remove(node); + Hierarchy.getHierarchy().gw().saveGroupAsync(group.getName()); + } + } + } + + public WeightNode getWeight() { return weight; } @@ -175,4 +209,19 @@ public class DisplayableGroup implements Displayable { return hasDefaultLoginMessage; } + + public InheritanceNode getInheritance() + { + return inheritance; + } + + public PrefixNode getPrefix() + { + return prefix; + } + + public List getPermissions() + { + return permissions; + } } diff --git a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/GroupProvider.java b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/GroupProvider.java index 9a77d025..2a385a10 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/GroupProvider.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/GroupProvider.java @@ -10,7 +10,9 @@ import net.luckperms.api.node.types.InheritanceNode; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import java.util.List; import java.util.Set; +import java.util.stream.Stream; @FunctionalInterface public interface GroupProvider @@ -34,6 +36,14 @@ public interface GroupProvider return NodeMatcher.key(inheritanceNode(group)); } + static DisplayableGroup LAMBDA$fromLuckPermsGroup(Group group) + { + return List.of(NON_OP.getGroup(), OP.getGroup(), MASTER_BUILDER.getGroup(), ADMIN.getGroup(), SENIOR_ADMIN.getGroup()).stream() + .filter(displayableGroup -> displayableGroup.getLuckPermsGroup().equals(group)) + .findFirst() + .orElse(NON_OP.getGroup()); + } + static User getUser(Player player) { return TotalFreedomMod.getPlugin() @@ -56,14 +66,10 @@ public interface GroupProvider static GroupProvider fromLuckPermsGroup(Group group) { - return switch (group.getName().toLowerCase()) - { - case "op" -> OP; - case "builder" -> MASTER_BUILDER; - case "admin" -> ADMIN; - case "senior" -> SENIOR_ADMIN; - default -> NON_OP; - }; + return Stream.of(NON_OP, OP, MASTER_BUILDER, ADMIN, SENIOR_ADMIN) + .filter(displayableGroup -> displayableGroup.getGroup().getLuckPermsGroup().equals(group)) + .findFirst() + .orElse(NON_OP); } static Set> providerSet() 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 e3546cd4..628f105a 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/Hierarchy.java @@ -48,6 +48,21 @@ public class Hierarchy return hierarchy; } + public TrackWrapper tw() + { + return trackWrapper; + } + + public GroupWrapper gw() + { + return groupWrapper; + } + + public UserWrapper uw() + { + return userWrapper; + } + public PlayerAdapter getPlayerAdapter() { return playerAdapter; @@ -76,12 +91,30 @@ public class Hierarchy return trackWrapper.getTrack("ADMIN").join(); } - public void addInheritance(DisplayableGroup previous, DisplayableGroup current) + @SuppressWarnings("unchecked") + public T getWrapper(Class clazz) + { + if (clazz == GroupWrapper.class) + { + return (T) groupWrapper; + } else if (clazz == TrackWrapper.class) + { + return clazz.cast(trackWrapper); + } else if (clazz == UserWrapper.class) + { + return clazz.cast(userWrapper); + } else + { + throw new IllegalArgumentException("Invalid class type"); + } + } + + public void asyncInheritFrom(DisplayableGroup inherited, DisplayableGroup inheritor) { CompletableFuture.runAsync(() -> { - current.getLuckPermsGroup().data().add(InheritanceNode.builder(previous.getLuckPermsGroup()).build()); - groupWrapper.saveGroup(current.getLuckPermsGroup().getName()); + inheritor.getLuckPermsGroup().data().add(inherited.getInheritance()); + groupWrapper.saveGroupAsync(inheritor.getLuckPermsGroup().getName()); }).whenComplete((a, b) -> { if (b != null) @@ -96,7 +129,7 @@ public class Hierarchy return groupWrapper.getGroup(name).join(); } - public void addGroupToTrack(Track track, DisplayableGroup group) + public void asyncTrackGroup(Track track, DisplayableGroup group) { trackWrapper.getTrack(track.getName()).thenAccept(t -> { @@ -111,7 +144,7 @@ public class Hierarchy }); } - public void addUserData(Player player) + public void asyncAddUserData(Player player) { userWrapper.addUserData(player).whenComplete((a, b) -> { @@ -122,7 +155,7 @@ public class Hierarchy }); } - public void updateUserData(Player player) + public void asyncUpdateUserData(Player player) { userWrapper.updateUserData(player).whenComplete((a, b) -> { @@ -133,7 +166,7 @@ public class Hierarchy }); } - public void dropUserData(Player player) + public void asyncUserDrop(Player player) { userWrapper.removeUserData(player).whenComplete((a, b) -> { @@ -185,7 +218,7 @@ public class Hierarchy if (data.getUser().getNodes(NodeType.INHERITANCE) .contains(InheritanceNode.builder(g).build())) - userWrapper.removeFromGroup(data.getUser(), g); + userWrapper.removeFromGroup(data.getUser(), GroupProvider.fromLuckPermsGroup(g).getGroup()); }); } }).whenComplete((a, b) -> @@ -216,30 +249,40 @@ public class Hierarchy private void onNodeAdd(NodeAddEvent event) { - if (event.isUser()) + if (event.getTarget() instanceof User user) { - User user = (User) event.getTarget(); Player player = Bukkit.getPlayer(user.getUniqueId()); if (player != null) { TotalFreedomMod.getPlugin().rm.updateDisplay(player); - updateUserData(player); + asyncUpdateUserData(player); + uw().saveUser(user); } } + + if (event.getTarget() instanceof Group group) + { + gw().saveGroupAsync(group.getName()); + } } private void onNodeRemove(NodeRemoveEvent event) { - if (event.isUser()) + if (event.getTarget() instanceof User user) { - User user = (User) event.getTarget(); Player player = Bukkit.getPlayer(user.getUniqueId()); if (player != null) { TotalFreedomMod.getPlugin().rm.updateDisplay(player); - updateUserData(player); + asyncUpdateUserData(player); + uw().saveUser(user); } } + + if (event.getTarget() instanceof Group group) + { + gw().saveGroupAsync(group.getName()); + } } } } 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 fa0fbee8..715f7e24 100644 --- a/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java +++ b/commons/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java @@ -7,7 +7,6 @@ import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.util.FUtil; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; @@ -30,14 +29,14 @@ public class RankManager extends FreedomService // Here, we are automatically creating new tracks if they don't exist, and then adding groups to them. Hierarchy hierarchy = Hierarchy.getHierarchy(); - hierarchy.addGroupToTrack(hierarchy.op(), GroupProvider.NON_OP.getGroup()); - hierarchy.addGroupToTrack(hierarchy.op(), GroupProvider.OP.getGroup()); - hierarchy.addGroupToTrack(hierarchy.builder(), GroupProvider.MASTER_BUILDER.getGroup()); - hierarchy.addGroupToTrack(hierarchy.admin(), GroupProvider.ADMIN.getGroup()); - hierarchy.addGroupToTrack(hierarchy.admin(), GroupProvider.SENIOR_ADMIN.getGroup()); + hierarchy.asyncTrackGroup(hierarchy.op(), GroupProvider.NON_OP.getGroup()); + hierarchy.asyncTrackGroup(hierarchy.op(), GroupProvider.OP.getGroup()); + hierarchy.asyncTrackGroup(hierarchy.builder(), GroupProvider.MASTER_BUILDER.getGroup()); + hierarchy.asyncTrackGroup(hierarchy.admin(), GroupProvider.ADMIN.getGroup()); + hierarchy.asyncTrackGroup(hierarchy.admin(), GroupProvider.SENIOR_ADMIN.getGroup()); - hierarchy.addInheritance(GroupProvider.NON_OP.getGroup(), GroupProvider.OP.getGroup()); - hierarchy.addInheritance(GroupProvider.ADMIN.getGroup(), GroupProvider.SENIOR_ADMIN.getGroup()); + hierarchy.asyncInheritFrom(GroupProvider.NON_OP.getGroup(), GroupProvider.OP.getGroup()); + hierarchy.asyncInheritFrom(GroupProvider.ADMIN.getGroup(), GroupProvider.SENIOR_ADMIN.getGroup()); } @Override @@ -192,7 +191,7 @@ public class RankManager extends FreedomService @EventHandler(priority = EventPriority.MONITOR) public void onPlayerLeave(PlayerQuitEvent event) { - Hierarchy.getHierarchy().dropUserData(event.getPlayer()); + Hierarchy.getHierarchy().asyncUserDrop(event.getPlayer()); } @EventHandler(priority = EventPriority.MONITOR) @@ -200,7 +199,7 @@ public class RankManager extends FreedomService { final Player player = event.getPlayer(); - Hierarchy.getHierarchy().addUserData(player); + Hierarchy.getHierarchy().asyncAddUserData(player); PlayerData target = plugin.pl.getData(player);