Halfway fixed... need to figure out why the group save is getting spammed...

This commit is contained in:
Paul Reilly 2023-05-03 15:57:45 -05:00
parent 9138d1856b
commit 618eb669b2
16 changed files with 284 additions and 607 deletions

View File

@ -1,12 +1,5 @@
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;

View File

@ -2,16 +2,14 @@ package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.command.handling.*;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.luckperms.api.track.DemotionResult;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.concurrent.atomic.AtomicBoolean;
@Intercept("deop")
@CommandPermissions(permission = "deop", source = SourceType.BOTH)
@CommandParameters(description = "Deop a player", usage = "/<command> <partialname>")
@ -39,30 +37,22 @@ public class Command_deop extends FreedomCommand
return true;
}
AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
Hierarchy.getHierarchy().demoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, throwable) ->
{
if (throwable != null)
{
msgNew("<red>Could not demote <player> to non-OP. Check the logs for more details.", player(player));
FLog.severe("Error while demoting " + player.getName() + " to non-OP:");
FLog.severe(throwable);
return;
}
boolean b = silent;
DemotionResult result = Hierarchy.getHierarchy()
.demoteUser(Hierarchy.getHierarchy().op(), player);
if (!result.wasSuccessful()) {
if (!result.wasSuccessful())
{
msgNew("<red><player> is already non-op.", Placeholder.unparsed("player", player.getName()));
return;
return true;
}
msg(player, YOU_ARE_NOT_OP);
if (!atomicBoolean.get())
if (!b)
{
FUtil.adminAction(sender.getName(), "De-opping " + player.getName(), false);
}
});
return true;
}
}

View File

@ -5,9 +5,9 @@ import me.totalfreedom.totalfreedommod.command.handling.CommandPermissions;
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
import me.totalfreedom.totalfreedommod.command.handling.SourceType;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.luckperms.api.track.DemotionResult;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -23,23 +23,18 @@ public class Command_deopall extends FreedomCommand
FUtil.adminAction(sender.getName(), "De-opping all players on the server", true);
server.getOnlinePlayers().forEach(player ->
Hierarchy.getHierarchy().demoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, ex) ->
{
if (ex != null)
{
msgNew("<red>Could not demote <player> to non-OP. Check the logs for more details.", player(player));
FLog.severe("Failed to demote " + player.getName() + " to non-OP:");
FLog.severe(ex);
return;
}
DemotionResult result = Hierarchy.getHierarchy()
.demoteUser(Hierarchy.getHierarchy().op(), player);
if (!result.wasSuccessful()) {
if (!result.wasSuccessful())
{
msgNew("<red><player> is already non-OP.", Placeholder.unparsed("player", player.getName()));
return;
}
msg(player, YOU_ARE_NOT_OP);
}));
});
return true;
}

View File

@ -158,7 +158,7 @@ public class Command_mbconfig extends FreedomCommand
FUtil.adminAction(sender.getName(), "Removing " + data.getName() + " from the Master Builder list", true);
Hierarchy.getHierarchy()
.dropUserFromAll(Hierarchy.getHierarchy().builder(), player);
.dropUser(Hierarchy.getHierarchy().builder(), player);
data.setMasterBuilder(false);
plugin.pl.save(data);

View File

@ -2,15 +2,13 @@ package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.command.handling.*;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.track.PromotionResult;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.concurrent.atomic.AtomicBoolean;
@Intercept("op")
@CommandPermissions(permission = "op", source = SourceType.BOTH, cooldown = 5)
@CommandParameters(description = "OP a player", usage = "/<command> <partialname>")
@ -39,33 +37,22 @@ public class Command_op extends FreedomCommand
return true;
}
AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, throwable) ->
{
if (throwable != null)
{
msgNew("<red>Failed to promote <player> to OP. Check logs for more details.", player(player));
FLog.severe("Failed to promote " + player.getName() + " to OP:");
FLog.severe(throwable);
return;
}
boolean b = silent;
PromotionResult result = Hierarchy.getHierarchy()
.promoteUser(Hierarchy.getHierarchy().op(), player);
if (result == null) {
msgNew("<red><player> was not on the track! Added.", player(player));
}
else if (!result.wasSuccessful())
if (!result.wasSuccessful())
{
msgNew("<red><player> is already OP!", player(player));
return;
return true;
}
msg(player, YOU_ARE_OP);
if (!atomicBoolean.get())
if (!b)
{
FUtil.adminAction(sender.getName(), "Opping " + player.getName(), false);
}
});
return true;

View File

@ -5,8 +5,8 @@ import me.totalfreedom.totalfreedommod.command.handling.CommandPermissions;
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
import me.totalfreedom.totalfreedommod.command.handling.SourceType;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.track.PromotionResult;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -21,25 +21,18 @@ 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((result, throwable) ->
{
if (throwable != null)
{
msgNew("<red>Failed to promote <player> to OP. Check the logs for more details.", player(player));
FLog.severe("Failed to promote " + player.getName());
FLog.severe(throwable);
return;
}
PromotionResult result = Hierarchy.getHierarchy()
.promoteUser(Hierarchy.getHierarchy().op(), player);
if (result == null) {
msgNew("<red><player> was not present on the track! Added.", player(player));
} else if (!result.wasSuccessful()) {
if (!result.wasSuccessful())
{
msgNew("<red>Player is already OP!", player(player));
return;
}
msg(player, YOU_ARE_OP);
}));
});
return true;
}

View File

@ -5,8 +5,8 @@ import me.totalfreedom.totalfreedommod.command.handling.CommandPermissions;
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
import me.totalfreedom.totalfreedommod.command.handling.SourceType;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.track.PromotionResult;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -20,22 +20,17 @@ public class Command_opme extends FreedomCommand
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
FUtil.adminAction(sender.getName(), "Opping " + sender.getName(), false);
Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), playerSender)
.whenComplete((result, error) ->
{
if (error != null)
{
FLog.severe("Error while promoting " + playerSender.getName() + " to OP: " + error.getMessage());
return;
}
PromotionResult result = Hierarchy.getHierarchy()
.promoteUser(Hierarchy.getHierarchy().op(), playerSender);
if (result != null && !result.wasSuccessful()) {
if (!result.wasSuccessful())
{
msgNew("<red>You are already op!");
return;
return true;
}
msg(YOU_ARE_OP);
});
return true;
}
}

View File

@ -10,11 +10,12 @@ import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.util.PermissibleCompletion;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.track.DemotionResult;
import net.luckperms.api.track.PromotionResult;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -26,7 +27,8 @@ import java.util.Date;
import java.util.List;
@CommandPermissions(permission = "saconfig", source = SourceType.BOTH)
@CommandParameters(description = "List, add, remove, or set the rank of admins, clean or reload the admin list, or view admin information.", usage = "/<command> <list | clean | reload | | setrank <username> <rank> | <add | remove | info> <username>>", aliases = "slconfig")
@CommandParameters(description = "List, add, remove, or set the rank of admins, clean or reload the admin list, or view admin information.",
usage = "/<command> <list | clean | reload | | setrank <username> <rank> | <promote | demote | info | remove> <username>>", aliases = "slconfig")
public class Command_saconfig extends FreedomCommand
{
@ -82,26 +84,14 @@ public class Command_saconfig extends FreedomCommand
return true;
}
Hierarchy.getHierarchy()
.promoteUser(Hierarchy.getHierarchy().admin(), player)
.whenComplete((result, ex) ->
{
if (ex != null)
{
FLog.severe("Failed to promote "
+ player.getName()
+ " to the next rank");
return;
}
PromotionResult result = Hierarchy.getHierarchy()
.promoteUser(Hierarchy.getHierarchy().admin(), player);
if (result == null) {
msgNew("<green>Added <player> to the ADMIN group.", player(player));
}
else if (!result.wasSuccessful() || result.getGroupTo().isEmpty())
if (!result.wasSuccessful() || result.getGroupTo().isEmpty())
{
msgNew("<red>Failed to promote <player> to the next rank.", player(player));
return;
} else {
} else
{
result.getGroupTo().ifPresentOrElse(group ->
{
Admin admin = plugin.al.getAdmin(player);
@ -113,7 +103,7 @@ public class Command_saconfig extends FreedomCommand
}
Group actual = Hierarchy.getHierarchy().getGroup(group);
DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup();
DisplayableGroup rank = GroupProvider.wrappedFromLPG(actual).getGroup();
if (rank == null)
{
@ -148,7 +138,6 @@ public class Command_saconfig extends FreedomCommand
Placeholder.unparsed("rank", group));
}, () -> msgNew("<red>Failed to promote <player> to the next rank.", player(player)));
}
});
return true;
}
@ -201,22 +190,13 @@ public class Command_saconfig extends FreedomCommand
return true;
}
Hierarchy.getHierarchy()
.demoteUser(Hierarchy.getHierarchy().admin(), player)
.whenComplete((result, ex) ->
{
if (ex != null)
{
FLog.severe("Failed to demote "
+ player.getName()
+ " to the next rank");
return;
}
DemotionResult result = Hierarchy.getHierarchy()
.demoteUser(Hierarchy.getHierarchy().admin(), player);
if (!result.wasSuccessful() || result.getGroupTo().isEmpty())
{
msgNew("<red>Failed to demote <player> to the next rank.", player(player));
return;
return true;
}
result.getGroupTo().ifPresentOrElse(group ->
@ -230,7 +210,7 @@ public class Command_saconfig extends FreedomCommand
}
Group actual = Hierarchy.getHierarchy().getGroup(group);
DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup();
DisplayableGroup rank = GroupProvider.wrappedFromLPG(actual).getGroup();
if (rank == null)
{
@ -262,7 +242,8 @@ public class Command_saconfig extends FreedomCommand
Placeholder.unparsed("player", player.getName()),
Placeholder.unparsed("rank", group));
}, () -> msgNew("<red>Failed to demote <player> to the next rank.", Placeholder.unparsed("player", player.getName())));
});
return true;
}
case "remove" ->
{
@ -278,7 +259,7 @@ public class Command_saconfig extends FreedomCommand
Admin admin = plugin.al.getAdmin(player);
plugin.al.removeAdmin(admin);
Hierarchy.getHierarchy().dropUserFromAll(Hierarchy.getHierarchy().admin(), player);
Hierarchy.getHierarchy().dropUser(Hierarchy.getHierarchy().admin(), player);
FUtil.adminAction(sender.getName(), "Removing " + admin.getName() + " from the admin list", true);
admin.setActive(false);
@ -299,7 +280,6 @@ public class Command_saconfig extends FreedomCommand
return false;
}
}
return false;
}
@Override
@ -315,11 +295,17 @@ public class Command_saconfig extends FreedomCommand
PermissibleCompletion.of("tfm.saconfig.clean", "clean"),
PermissibleCompletion.of("tfm.saconfig.promote", "promote"));
}
if (args.length == 2 && (args[0].equals("add") || args[0].equals("remove") || args[0].equals("setrank") || args[0].equals("info")))
if (args.length == 2 && (args[0].equals("promote")
|| args[0].equals("demote")
|| args[0].equals("remove")
|| args[0].equals("setrank")
|| args[0].equals("info")))
{
return playerCompletions(args[1]);
}
if (args.length == 3 && args[0].equals("setrank") && sender.hasPermission("tfm.saconfig.setrank"))
if (args.length == 3
&& args[0].equals("setrank")
&& sender.hasPermission("tfm.saconfig.setrank"))
{
return Arrays.asList("ADMIN", "SENIOR_ADMIN");
}

View File

@ -4,9 +4,7 @@ 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
{
@ -17,43 +15,21 @@ public class GroupWrapper
this.manager = manager;
}
public CompletableFuture<Group> getGroup(String name)
public Group getGroup(String name)
{
if (!manager.isLoaded(name) || !manager.getLoadedGroups().contains(manager.getGroup(name)))
if (!manager.isLoaded(name))
{
Optional<Group> group = manager.loadGroup(name).join();
return group
.map(CompletableFuture::completedFuture)
.orElseGet(() -> manager.createAndLoadGroup(name));
return manager.createAndLoadGroup(name).join();
}
return CompletableFuture.supplyAsync(() -> manager.getGroup(name));
return manager.getGroup(name);
}
public void modifyGroup(String name, Function<Group, ? extends Group> function)
public void saveGroup(String name)
{
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);
}
});
Group group = getGroup(name);
manager.saveGroup(group)
.thenRun(() ->
FLog.info("Saved group " + name + " to LuckPerms."));
}
}

View File

@ -1,39 +1,35 @@
package me.totalfreedom.totalfreedommod.perms;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.luckperms.api.track.Track;
import net.luckperms.api.track.TrackManager;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class TrackWrapper
{
private final TrackManager manager;
private final Set<Track> trackSet;
public TrackWrapper(TrackManager manager)
{
this.manager = manager;
this.trackSet = new HashSet<>();
trackSet.addAll(manager.getLoadedTracks());
}
public CompletableFuture<Track> getTrack(String name)
public Track getTrack(String name)
{
if (!manager.isLoaded(name) || !trackSet.contains(manager.getTrack(name)))
if (!manager.isLoaded(name))
{
return manager.createAndLoadTrack(name).thenApplyAsync(a -> {
trackSet.add(a);
return a;
});
return manager.createAndLoadTrack(name).join();
}
return CompletableFuture.supplyAsync(() -> manager.getTrack(name));
return manager.getTrack(name);
}
public void saveTrack(String name)
{
getTrack(name).thenAccept(manager::saveTrack);
Track t = getTrack(name);
manager.saveTrack(t)
.thenRun(() ->
FLog.info("Saved track " + name + " successfully."));
}
}

View File

@ -1,84 +1,46 @@
package me.totalfreedom.totalfreedommod.perms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.cacheddata.CachedData;
import net.luckperms.api.cacheddata.CachedDataManager;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.cacheddata.CachedPermissionData;
import net.luckperms.api.context.ImmutableContextSet;
import net.luckperms.api.model.user.User;
import net.luckperms.api.platform.PlayerAdapter;
import net.luckperms.api.query.QueryOptions;
import org.bukkit.entity.Player;
/**
* This class is a utility class that can be used to retrieve
* numerous contextual options related to LuckPerms users.
*/
public class UserData
{
private User user;
private CachedMetaData metaData;
private CachedPermissionData permissionData;
private ImmutableContextSet contextSet;
private QueryOptions queryOptions;
public UserData(User user, CachedMetaData metaData, CachedPermissionData permissionData, ImmutableContextSet contextSet, QueryOptions queryOptions)
{
this.user = user;
this.metaData = metaData;
this.permissionData = permissionData;
this.contextSet = contextSet;
this.queryOptions = queryOptions;
public static User fromPlayer(Player player) {
PlayerAdapter<Player> adapter = LuckPermsProvider
.get()
.getPlayerAdapter(Player.class);
return adapter.getUser(player);
}
public User getUser()
{
return user;
public static QueryOptions getQueryOptions(User user) {
return user.getQueryOptions();
}
public void setUser(User user)
{
this.user = user;
public static CachedDataManager getDataManager(User user) {
return user.getCachedData();
}
public CachedMetaData getMetaData()
{
return metaData;
public static ImmutableContextSet getContextSet(User user) {
return getQueryOptions(user).context();
}
public void setMetaData(CachedMetaData metaData)
{
this.metaData = metaData;
public static CachedMetaData getMetaData(User user) {
return getDataManager(user).getMetaData();
}
public CachedPermissionData getPermissionData()
{
return permissionData;
}
public void setPermissionData(CachedPermissionData permissionData)
{
this.permissionData = permissionData;
}
public ImmutableContextSet getContextSet()
{
return contextSet;
}
public void setContextSet(ImmutableContextSet contextSet)
{
this.contextSet = contextSet;
}
public QueryOptions getQueryOptions()
{
return queryOptions;
}
public void setQueryOptions(QueryOptions queryOptions)
{
this.queryOptions = queryOptions;
}
public UserData copy()
{
return new UserData(getUser(),
getMetaData(),
getPermissionData(),
getContextSet(),
getQueryOptions());
public static CachedPermissionData getPermissionData(User user) {
return getDataManager(user).getPermissionData();
}
}

View File

@ -1,122 +1,70 @@
package me.totalfreedom.totalfreedommod.perms;
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.cacheddata.CachedPermissionData;
import net.luckperms.api.context.ImmutableContextSet;
import net.luckperms.api.model.group.Group;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.InheritanceNode;
import net.luckperms.api.platform.PlayerAdapter;
import net.luckperms.api.query.QueryOptions;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class UserWrapper
{
private final UserManager manager;
private final PlayerAdapter<Player> playerAdapter;
private final Set<UserData> userDataSet;
public UserWrapper(UserManager manager, PlayerAdapter<Player> adapter)
{
this.manager = manager;
this.playerAdapter = adapter;
this.userDataSet = new HashSet<>();
}
public CompletableFuture<Void> addUserData(Player player)
{
User user = playerAdapter.getUser(player);
CachedPermissionData permissionData = playerAdapter.getPermissionData(player);
ImmutableContextSet contextSet = playerAdapter.getContext(player);
QueryOptions queryOptions = playerAdapter.getQueryOptions(player);
CachedMetaData metaData = playerAdapter.getMetaData(player);
UserData userData = new UserData(user, metaData, permissionData, contextSet, queryOptions);
return CompletableFuture.runAsync(() -> userDataSet.add(userData));
}
public CompletableFuture<User> getUser(Player player)
public User getUser(Player player)
{
if (!manager.isLoaded(player.getUniqueId()) || !manager.getLoadedUsers().contains(manager.getUser(player.getUniqueId())))
{
return manager.loadUser(player.getUniqueId());
return manager.loadUser(player.getUniqueId()).join();
}
return CompletableFuture.supplyAsync(() -> manager.getUser(player.getUniqueId()));
}
public CompletableFuture<UserData> getUserData(Player player)
{
return getUser(player).thenApply(user ->
{
for (UserData userData : userDataSet)
{
if (userData.getUser().getUniqueId().equals(user.getUniqueId()))
{
return userData;
}
}
return null;
});
}
public CompletableFuture<Void> removeUserData(Player player)
{
return getUserData(player).thenAccept(userDataSet::remove);
}
public CompletableFuture<Void> updateUserData(Player player)
{
return getUserData(player).thenApply(userData ->
{
UserData newData = userData.copy();
CachedPermissionData permissionData = playerAdapter.getPermissionData(player);
ImmutableContextSet contextSet = playerAdapter.getContext(player);
QueryOptions queryOptions = playerAdapter.getQueryOptions(player);
CachedMetaData metaData = playerAdapter.getMetaData(player);
newData.setPermissionData(permissionData);
newData.setContextSet(contextSet);
newData.setQueryOptions(queryOptions);
newData.setMetaData(metaData);
userDataSet.remove(userData);
return newData;
}).thenAccept(userDataSet::add);
return manager.getUser(player.getUniqueId());
}
public void addToGroup(User user, DisplayableGroup group)
{
CompletableFuture.runAsync(() ->
manager.modifyUser(user.getUniqueId(), c ->
{
if (!c.getNodes(NodeType.INHERITANCE)
.contains(group.getInheritance()))
c.data().add(group.getInheritance());
}));
})
.thenRun(() ->
FLog.info("Successfully saved user "
+ user.getUsername()
+ " to group "
+ group.getName()));
}
public void removeFromGroup(User user, DisplayableGroup group)
{
CompletableFuture.runAsync(() ->
manager.modifyUser(user.getUniqueId(), userConsumer ->
{
if (userConsumer.getNodes(NodeType.INHERITANCE)
.contains(group.getInheritance()))
userConsumer.data().remove(group.getInheritance());
}));
}).thenRun(() ->
FLog.info("Successfully removed user "
+ user.getUsername()
+ " from group "
+ group.getName()));
}
public void saveUser(User user)
{
CompletableFuture.runAsync(() -> manager.saveUser(user));
manager.saveUser(user).thenRun(() ->
FLog.info("Successfully saved user "
+ user.getUsername()));
}
}

View File

@ -84,7 +84,7 @@ public class DisplayableGroup implements Displayable
this.weight = tempWeight;
this.group = matched;
this.inheritance = InheritanceNode.builder(this.group).build();
Hierarchy.getHierarchy().gw().saveGroupAsync(this.group.getName());
this.save();
}
/**
@ -109,7 +109,7 @@ public class DisplayableGroup implements Displayable
group.data().add(node);
permissions.add(node);
Hierarchy.getHierarchy().gw().saveGroupAsync(group.getName());
save();
}
}
@ -123,7 +123,7 @@ public class DisplayableGroup implements Displayable
{
group.data().remove(node);
permissions.remove(node);
Hierarchy.getHierarchy().gw().saveGroupAsync(group.getName());
save();
}
}
}
@ -224,4 +224,9 @@ public class DisplayableGroup implements Displayable
{
return permissions;
}
public void save()
{
Hierarchy.getHierarchy().gw().saveGroup(group.getName());
}
}

View File

@ -36,7 +36,7 @@ public interface GroupProvider<T extends DisplayableGroup>
return NodeMatcher.key(inheritanceNode(group));
}
static DisplayableGroup LAMBDA$fromLuckPermsGroup(Group group)
static DisplayableGroup directFromLPG(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))
@ -64,7 +64,7 @@ public interface GroupProvider<T extends DisplayableGroup>
};
}
static GroupProvider<DisplayableGroup> fromLuckPermsGroup(Group group)
static GroupProvider<DisplayableGroup> wrappedFromLPG(Group group)
{
return Stream.of(NON_OP, OP, MASTER_BUILDER, ADMIN, SENIOR_ADMIN)
.filter(displayableGroup -> displayableGroup.getGroup().getLuckPermsGroup().equals(group))

View File

@ -3,12 +3,9 @@ package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.perms.GroupWrapper;
import me.totalfreedom.totalfreedommod.perms.TrackWrapper;
import me.totalfreedom.totalfreedommod.perms.UserData;
import me.totalfreedom.totalfreedommod.perms.UserWrapper;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.luckperms.api.event.EventBus;
import net.luckperms.api.event.EventSubscription;
import net.luckperms.api.event.node.NodeAddEvent;
import net.luckperms.api.event.node.NodeRemoveEvent;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.NodeType;
@ -17,14 +14,8 @@ import net.luckperms.api.platform.PlayerAdapter;
import net.luckperms.api.track.DemotionResult;
import net.luckperms.api.track.PromotionResult;
import net.luckperms.api.track.Track;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class Hierarchy
{
private static final Hierarchy hierarchy = new Hierarchy(); // static singleton for global state.
@ -38,9 +29,7 @@ public class Hierarchy
this.playerAdapter = TotalFreedomMod.getPlugin().lpb.getAPI().getPlayerAdapter(Player.class);
this.groupWrapper = new GroupWrapper(TotalFreedomMod.getPlugin().lpb.getAPI().getGroupManager());
this.trackWrapper = new TrackWrapper(TotalFreedomMod.getPlugin().lpb.getAPI().getTrackManager());
this.userWrapper = new UserWrapper(TotalFreedomMod.getPlugin().lpb.getAPI().getUserManager(),
getPlayerAdapter());
new LuckPermsUserDataListener().register();
this.userWrapper = new UserWrapper(TotalFreedomMod.getPlugin().lpb.getAPI().getUserManager(), getPlayerAdapter());
}
public static Hierarchy getHierarchy()
@ -70,219 +59,89 @@ public class Hierarchy
public boolean isUserOnAdminTrack(Player player)
{
return userWrapper.getUser(player)
.thenApply(user -> user.getNodes(NodeType.INHERITANCE)
.contains(GroupProvider.inheritanceNode(GroupProvider.ADMIN.getGroup())))
.join();
User user = uw().getUser(player);
return user
.getNodes(NodeType.INHERITANCE)
.contains(GroupProvider
.inheritanceNode(
GroupProvider
.ADMIN
.getGroup()));
}
public Track op()
{
return trackWrapper.getTrack("OP").join();
return tw().getTrack("OP");
}
public Track builder()
{
return trackWrapper.getTrack("BUILDER").join();
return tw().getTrack("BUILDER");
}
public Track admin()
{
return trackWrapper.getTrack("ADMIN").join();
}
@SuppressWarnings("unchecked")
public <T> T getWrapper(Class<T> 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");
}
return tw().getTrack("ADMIN");
}
public void asyncInheritFrom(DisplayableGroup inherited, DisplayableGroup inheritor)
{
CompletableFuture.runAsync(() ->
{
inheritor.getLuckPermsGroup().data().add(inherited.getInheritance());
groupWrapper.saveGroupAsync(inheritor.getLuckPermsGroup().getName());
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
inheritor
.getLuckPermsGroup()
.data()
.add(inherited.getInheritance());
gw().saveGroup(inheritor
.getLuckPermsGroup()
.getName());
FLog.info("Inherited " + inherited.getName() + " to " + inheritor.getName());
}
public Group getGroup(String name)
{
return groupWrapper.getGroup(name).join();
return gw().getGroup(name);
}
public void asyncTrackGroup(Track track, DisplayableGroup group)
{
trackWrapper.getTrack(track.getName()).thenAccept(t ->
{
Track t = tw().getTrack(track.getName());
t.appendGroup(group.getLuckPermsGroup());
trackWrapper.saveTrack(t.getName());
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
tw().saveTrack(t.getName());
gw().saveGroup(group.getLuckPermsGroup().getName());
FLog.info("Added " + group.getName() + " to track " + track.getName());
}
public void asyncAddUserData(Player player)
public PromotionResult promoteUser(Track track, Player player)
{
userWrapper.addUserData(player).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
User user = UserData.fromPlayer(player);
return track.promote(user, UserData.getContextSet(user));
}
public void asyncUpdateUserData(Player player)
public DemotionResult demoteUser(Track track, Player player)
{
userWrapper.updateUserData(player).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
public void asyncUserDrop(Player player)
{
userWrapper.removeUserData(player).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
public CompletableFuture<PromotionResult> promoteUser(Track track, Player player)
{
return userWrapper.getUserData(player)
.thenApply(data -> track.promote(data.getUser(), data.getContextSet()));
}
public CompletableFuture<DemotionResult> demoteUser(Track track, Player player)
{
return userWrapper.getUserData(player)
.thenApply(data -> track.demote(data.getUser(), data.getContextSet()));
User user = UserData.fromPlayer(player);
return track.demote(user, UserData.getContextSet(user));
}
public void addUserToGroup(DisplayableGroup group, Player player)
{
userWrapper.getUserData(player).thenAccept(user ->
userWrapper.addToGroup(user.getUser(), group))
.whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
User user = UserData.fromPlayer(player);
uw().addToGroup(user, group);
FLog.info("Successfully added " + player.getName() + " to " + group.getName());
}
public void dropUserFromAll(Track track, Player player)
{
userWrapper.getUserData(player).thenAccept(data ->
public void dropUser(Track track, Player player)
{
User user = UserData.fromPlayer(player);
for (String group : track.getGroups())
{
groupWrapper.getGroup(group).whenComplete((g, b) ->
{
if (b != null)
{
FLog.severe(b);
return;
}
Group g = gw().getGroup(group);
InheritanceNode node = GroupProvider.directFromLPG(g).getInheritance();
if (data.getUser().getNodes(NodeType.INHERITANCE)
.contains(InheritanceNode.builder(g).build()))
userWrapper.removeFromGroup(data.getUser(), GroupProvider.fromLuckPermsGroup(g).getGroup());
});
}
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
private class LuckPermsUserDataListener
{
private final Map<NamespacedKey, EventSubscription<?>> subscriptions = new HashMap<>();
public void register()
{
EventBus eventBus = TotalFreedomMod.getPlugin().lpb.getAPI().getEventBus();
subscriptions.put(new NamespacedKey(TotalFreedomMod.getPlugin(), "node_add"), eventBus.subscribe(TotalFreedomMod.getPlugin(), NodeAddEvent.class, this::onNodeAdd));
subscriptions.put(new NamespacedKey(TotalFreedomMod.getPlugin(), "node_remove"), eventBus.subscribe(TotalFreedomMod.getPlugin(), NodeRemoveEvent.class, this::onNodeRemove));
}
public void unregister()
{
subscriptions.forEach((key, subscription) -> subscription.close());
subscriptions.clear();
}
private void onNodeAdd(NodeAddEvent event)
{
if (event.getTarget() instanceof User user)
{
Player player = Bukkit.getPlayer(user.getUniqueId());
if (player != null)
{
TotalFreedomMod.getPlugin().rm.updateDisplay(player);
asyncUpdateUserData(player);
uw().saveUser(user);
}
}
if (event.getTarget() instanceof Group group)
{
gw().saveGroupAsync(group.getName());
}
}
private void onNodeRemove(NodeRemoveEvent event)
{
if (event.getTarget() instanceof User user)
{
Player player = Bukkit.getPlayer(user.getUniqueId());
if (player != null)
{
TotalFreedomMod.getPlugin().rm.updateDisplay(player);
asyncUpdateUserData(player);
uw().saveUser(user);
}
}
if (event.getTarget() instanceof Group group)
{
gw().saveGroupAsync(group.getName());
}
if (user.getNodes(NodeType.INHERITANCE).contains(node))
uw().removeFromGroup(user, GroupProvider.wrappedFromLPG(g).getGroup());
}
}
}

View File

@ -188,19 +188,11 @@ public class RankManager extends FreedomService
updatePlayerTeam(player);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLeave(PlayerQuitEvent event)
{
Hierarchy.getHierarchy().asyncUserDrop(event.getPlayer());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event)
{
final Player player = event.getPlayer();
Hierarchy.getHierarchy().asyncAddUserData(player);
PlayerData target = plugin.pl.getData(player);
boolean isAdmin = plugin.al.isAdmin(player);