Adjusting some stuff :)

This commit is contained in:
Paul Reilly 2023-04-07 16:06:11 -05:00
parent 8262e81e69
commit e2d7f6ebcb
21 changed files with 718 additions and 416 deletions

View File

@ -38,7 +38,7 @@ public class Admin
{ {
this.uuid = UUID.fromString(resultSet.getString("uuid")); this.uuid = UUID.fromString(resultSet.getString("uuid"));
this.active = resultSet.getBoolean("active"); this.active = resultSet.getBoolean("active");
this.rank = GroupProvider.fromArgument(resultSet.getString("rank")).getGroup(); this.rank = GroupProvider.fromString(resultSet.getString("rank")).getGroup();
this.ips.clear(); this.ips.clear();
this.ips.addAll(FUtil.stringToList(resultSet.getString("ips"))); this.ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
this.lastLogin = new Date(resultSet.getLong("last_login")); this.lastLogin = new Date(resultSet.getLong("last_login"));
@ -70,15 +70,15 @@ public class Admin
public Map<String, Object> toSQLStorable() public Map<String, Object> toSQLStorable()
{ {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("uuid", uuid.toString()); map.put("uuid", uuid.toString());
map.put("active", active); map.put("active", active);
map.put("rank", rank.toString()); map.put("rank", rank.toString());
map.put("ips", FUtil.listToString(ips)); map.put("ips", FUtil.listToString(ips));
map.put("last_login", lastLogin.getTime()); map.put("last_login", lastLogin.getTime());
map.put("command_spy", commandSpy); map.put("command_spy", commandSpy);
map.put("potion_spy", potionSpy); map.put("potion_spy", potionSpy);
map.put("ac_format", acFormat); map.put("ac_format", acFormat);
return map; return map;
} }

View File

@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -106,9 +107,10 @@ public class AdminList extends FreedomService
return admin != null && admin.isActive(); return admin != null && admin.isActive();
} }
// Cast to OfflinePlayer
public boolean isAdmin(Player player) public boolean isAdmin(Player player)
{ {
if (Hierarchy.getHierarchy().isUserOnAdminTrack(player)) return true;
return isAdmin((OfflinePlayer) player); return isAdmin((OfflinePlayer) player);
} }
@ -124,19 +126,6 @@ public class AdminList extends FreedomService
return admin != null && admin.isActive(); return admin != null && admin.isActive();
} }
public boolean isSeniorAdmin(CommandSender sender)
{
Admin admin = getAdmin(sender);
if (admin == null)
{
return false;
}
return admin.getRank().getLuckPermsGroup().getWeight().orElse(0)
>= GroupProvider.SENIOR_ADMIN.getGroup().getLuckPermsGroup().getWeight().orElse(1);
// We don't want this to return true if there's no group weight available.
}
public Admin getAdmin(CommandSender sender) public Admin getAdmin(CommandSender sender)
{ {
if (sender instanceof Player player) if (sender instanceof Player player)

View File

@ -1,10 +1,9 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -39,12 +38,18 @@ public class Command_deop extends FreedomCommand
} }
AtomicBoolean atomicBoolean = new AtomicBoolean(silent); AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
User user = GroupProvider.getUser(player); Hierarchy.getHierarchy().demoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, throwable) ->
HierarchyProvider.OP.getHierarchy().demoteUser(user).whenComplete((result, throwable) ->
{ {
if (throwable != null) if (throwable != null)
{ {
FLog.severe("Error while demoting " + player.getName() + " to OP: " + throwable.getMessage()); 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;
}
if (!result.wasSuccessful()) {
msgNew("<red><player> is already non-op.", Placeholder.unparsed("player", player.getName()));
return; return;
} }

View File

@ -1,10 +1,9 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -20,17 +19,24 @@ public class Command_deopall extends FreedomCommand
FUtil.adminAction(sender.getName(), "De-opping all players on the server", true); FUtil.adminAction(sender.getName(), "De-opping all players on the server", true);
server.getOnlinePlayers().forEach(player -> server.getOnlinePlayers().forEach(player ->
{ Hierarchy.getHierarchy().demoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, ex) ->
User user = GroupProvider.getUser(player); {
HierarchyProvider.OP.getHierarchy().demoteUser(user).whenComplete((result, ex) -> { if (ex != null)
if (ex != null) { {
FLog.severe("Failed to demote " + player.getName() + " to default rank"); 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;
}
msg(player, YOU_ARE_NOT_OP); if (!result.wasSuccessful()) {
plugin.rm.updateDisplay(player); msgNew("<red><player> is already non-OP.", Placeholder.unparsed("player", player.getName()));
}); return;
}); }
msg(player, YOU_ARE_NOT_OP);
plugin.rm.updateDisplay(player);
}));
return true; return true;
} }

View File

@ -1,9 +1,13 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import com.earth2me.essentials.commands.PlayerNotFoundException;
import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -120,6 +124,7 @@ public class Command_mbconfig extends FreedomCommand
{ {
FUtil.adminAction(sender.getName(), "Adding " + data.getName() + " to the Master Builder list", true); FUtil.adminAction(sender.getName(), "Adding " + data.getName() + " to the Master Builder list", true);
data.setMasterBuilder(true); data.setMasterBuilder(true);
Hierarchy.getHierarchy().addUserToGroup(GroupProvider.MASTER_BUILDER.getGroup(), player);
plugin.pl.save(data); plugin.pl.save(data);
if (player != null) if (player != null)
{ {
@ -143,23 +148,34 @@ public class Command_mbconfig extends FreedomCommand
return noPerms(); return noPerms();
} }
Player player = getPlayer(args[1]); try
PlayerData data = player != null ? plugin.pl.getData(player) : plugin.pl.getData(args[1]);
if (data == null || !data.isMasterBuilder())
{ {
msg("Master Builder not found: " + args[1]); OfflinePlayer player = getOfflinePlayer(args[1]);
PlayerData data = plugin.pl.getData(args[1]);
if (data == null || !data.isMasterBuilder())
{
msgNew("Master Builder not found: <arg>", arg(args[1]));
return true;
}
FUtil.adminAction(sender.getName(), "Removing " + data.getName() + " from the Master Builder list", true);
Hierarchy.getHierarchy().dropUserFromAll(Hierarchy.getHierarchy().builder(), player);
data.setMasterBuilder(false);
plugin.pl.save(data);
if (player.isOnline())
{
// This is not null because the player is online, so we can safely call this method.
plugin.rm.updateDisplay(player.getPlayer());
}
return true;
} catch (PlayerNotFoundException e)
{
msgNew("Player not found: <arg>", arg(args[1]));
return true; return true;
} }
FUtil.adminAction(sender.getName(), "Removing " + data.getName() + " from the Master Builder list", true);
data.setMasterBuilder(false);
plugin.pl.save(data);
if (player != null)
{
plugin.rm.updateDisplay(player);
}
return true;
} }
default: default:
{ {

View File

@ -1,10 +1,8 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -40,15 +38,21 @@ public class Command_op extends FreedomCommand
} }
AtomicBoolean atomicBoolean = new AtomicBoolean(silent); AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
User user = GroupProvider.getUser(player); Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((result, throwable) ->
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((ignored, throwable) ->
{ {
if (throwable != null) if (throwable != null)
{ {
FLog.severe("Failed to promote user " + player.getName()); 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; return;
} }
if (!result.wasSuccessful())
{
msgNew("<red><player> is already OP!", player(player));
}
msg(player, YOU_ARE_OP); msg(player, YOU_ARE_OP);
plugin.rm.updateDisplay(player); plugin.rm.updateDisplay(player);
if (!atomicBoolean.get()) if (!atomicBoolean.get())

View File

@ -1,10 +1,8 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -19,20 +17,19 @@ public class Command_opall extends FreedomCommand
FUtil.adminAction(sender.getName(), "Opping all players on the server", true); FUtil.adminAction(sender.getName(), "Opping all players on the server", true);
server.getOnlinePlayers().forEach(player -> server.getOnlinePlayers().forEach(player ->
{ Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), player).whenComplete((ignored, throwable) ->
User user = GroupProvider.getUser(player);
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{ {
FLog.severe("Failed to promote " + player.getName()); if (throwable != null)
return; {
} 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;
}
msg(player, YOU_ARE_OP); msg(player, YOU_ARE_OP);
plugin.rm.updateDisplay(player); plugin.rm.updateDisplay(player);
}); }));
});
return true; return true;
} }

View File

@ -1,10 +1,8 @@
package me.totalfreedom.totalfreedommod.command; package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -18,9 +16,10 @@ public class Command_opme extends FreedomCommand
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
FUtil.adminAction(sender.getName(), "Opping " + sender.getName(), false); FUtil.adminAction(sender.getName(), "Opping " + sender.getName(), false);
User user = GroupProvider.getUser(playerSender); Hierarchy.getHierarchy().promoteUser(Hierarchy.getHierarchy().op(), playerSender).whenComplete((result, error) ->
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((result, error) -> { {
if (error != null) { if (error != null)
{
FLog.severe("Error while promoting " + playerSender.getName() + " to OP: " + error.getMessage()); FLog.severe("Error while promoting " + playerSender.getName() + " to OP: " + error.getMessage());
return; return;
} }

View File

@ -5,14 +5,13 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup; import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
import me.totalfreedom.totalfreedommod.rank.GroupProvider; import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider; import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.util.PermissibleCompletion; import me.totalfreedom.totalfreedommod.util.PermissibleCompletion;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.group.Group;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -73,72 +72,77 @@ public class Command_saconfig extends FreedomCommand
Player player = getPlayer(args[1]); Player player = getPlayer(args[1]);
FUtil.adminAction(sender.getName(), "Promoting " + player.getName() + " to the next rank", true); if (player == null)
HierarchyProvider.ADMIN.getHierarchy()
.promoteUser(GroupProvider.getUser(player))
.whenComplete((result, ex) -> {
if (ex != null) {
FLog.severe("Failed to promote "
+ player.getName()
+ " to the next rank");
return;
}
result.getGroupTo()
.ifPresent(group ->
msgNew("<gold>Admin <player> has been promoted to <rank>",
Placeholder.unparsed("player", player.getName()),
Placeholder.unparsed("rank", group)));
});
}
case "setrank" ->
{
checkConsole();
checkPermission("tfm.saconfig.setrank");
if (args.length < 3)
{ {
return false; msgNew("<red>Player not found.");
}
DisplayableGroup rank = GroupProvider.fromArgument(args[2]).getGroup();
if (rank == null)
{
msgNew("<red>Unknown rank: <rank>", Placeholder.unparsed("rank", args[2]));
return true; 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;
}
if (!rank.isAtLeast(GroupProvider.ADMIN.getGroup())) if (!result.wasSuccessful() || result.getGroupTo().isEmpty())
{ {
msgNew("<red>Rank must be Admin or higher."); msgNew("<red>Failed to promote <player> to the next rank.", player(player));
return true; return;
} }
Admin admin = plugin.al.getEntryByName(args[1]); result.getGroupTo().ifPresentOrElse(group ->
if (admin == null) {
{ Admin admin = plugin.al.getAdmin(player);
msgNew("Unknown admin: <player>", Placeholder.unparsed("player", args[1]));
return true;
}
FUtil.adminAction(sender.getName(), "Setting " + admin.getName() + "'s rank to " + rank.getName(), true); if (admin == null)
{
admin = new Admin(player);
plugin.al.addAdmin(admin);
}
admin.setRank(rank); Group actual = Hierarchy.getHierarchy().getGroup(group);
plugin.al.save(admin); DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup();
Player player = getPlayer(admin.getName()); if (rank == null)
if (player != null) {
{ msgNew("<red>Unknown rank: <rank>", Placeholder.unparsed("rank", args[2]));
plugin.rm.updateDisplay(player); return;
} }
if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) admin.setRank(rank);
{ admin.addIp(FUtil.getIp(player));
plugin.dc.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); admin.setActive(true);
} admin.setLastLogin(new Date());
msgNew("Set <admin>'s rank to <rank>.", Placeholder.unparsed("admin", admin.getName()), Placeholder.component("rank", rank.getName())); 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 <admin>'s rank to <rank>.", 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("<gold>Admin <player> has been promoted to <rank>",
player(player),
Placeholder.unparsed("rank", group));
}, () -> msgNew("<red>Failed to promote <player> to the next rank.", player(player)));
});
return true; return true;
} }
@ -173,78 +177,87 @@ public class Command_saconfig extends FreedomCommand
return true; return true;
} }
case "add" -> case "demote" ->
{ {
checkConsole();
checkPermission("tfm.saconfig.demote");
if (args.length < 2) if (args.length < 2)
{ {
return false; return false;
} }
checkConsole(); Player player = getPlayer(args[1]);
checkPermission("tfm.saconfig.add");
// Player already admin?
final Player player = getPlayer(args[1]);
if (player == null) if (player == null)
{ {
msg(PLAYER_NOT_FOUND); msgNew("<red>Player not found.");
return true; return true;
} }
if (plugin.al.isAdmin(player)) Hierarchy.getHierarchy()
{ .demoteUser(Hierarchy.getHierarchy().admin(), player)
msgNew("<red>That player is already an admin."); .whenComplete((result, ex) ->
return true; {
} if (ex != null)
{
FLog.severe("Failed to demote "
+ player.getName()
+ " to the next rank");
return;
}
// Find the old admin entry if (!result.wasSuccessful() || result.getGroupTo().isEmpty())
Admin admin = null; {
for (Admin loopAdmin : plugin.al.getAllAdmins()) msgNew("<red>Failed to demote <player> to the next rank.", player(player));
{ return;
if (loopAdmin.getUuid().equals(player.getUniqueId())) }
{
admin = loopAdmin;
break;
}
}
if (admin == null) // New admin result.getGroupTo().ifPresentOrElse(group ->
{ {
FUtil.adminAction(sender.getName(), "Adding " + player.getName() + " to the admin list", true); Admin admin = plugin.al.getAdmin(player);
admin = new Admin(player);
User user = GroupProvider.getUser(player);
HierarchyProvider.ADMIN.getHierarchy().promoteUser(user);
plugin.al.addAdmin(admin); if (admin == null)
plugin.rm.updateDisplay(player); {
} else // Existing admin admin = new Admin(player);
{ plugin.al.addAdmin(admin);
User user = GroupProvider.getUser(player); }
HierarchyProvider.ADMIN.getHierarchy().promoteUser(user);
FUtil.adminAction(sender.getName(), "Re-adding " + player.getName() + " to the admin list", true);
admin.addIp(FUtil.getIp(player));
admin.setActive(true);
admin.setLastLogin(new Date());
plugin.al.save(admin); Group actual = Hierarchy.getHierarchy().getGroup(group);
plugin.al.updateTables(); DisplayableGroup rank = GroupProvider.fromLuckPermsGroup(actual).getGroup();
plugin.rm.updateDisplay(player);
if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) if (rank == null)
{ {
plugin.dc.syncRoles(admin, plugin.pl.getData(player).getDiscordID()); msgNew("<red>Unknown rank: <rank>", Placeholder.unparsed("rank", args[2]));
} return;
} }
final FPlayer fPlayer = plugin.pl.getPlayer(player); admin.setRank(rank);
if (fPlayer.getFreezeData().isFrozen()) admin.setLastLogin(new Date());
{
fPlayer.getFreezeData().setFrozen(false);
msgNew(player, "You have been unfrozen.");
}
return true; 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 <admin>'s rank to <rank>.", 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("<gold>Admin <player> has been demoted to <rank>",
Placeholder.unparsed("player", player.getName()),
Placeholder.unparsed("rank", group));
}, () -> msgNew("<red>Failed to demote <player> to the next rank.", Placeholder.unparsed("player", player.getName())));
});
} }
case "remove" -> case "remove" ->
{ {
@ -260,8 +273,7 @@ public class Command_saconfig extends FreedomCommand
Admin admin = plugin.al.getAdmin(player); Admin admin = plugin.al.getAdmin(player);
plugin.al.removeAdmin(admin); plugin.al.removeAdmin(admin);
User user = GroupProvider.getUser(player); Hierarchy.getHierarchy().dropUserFromAll(Hierarchy.getHierarchy().admin(), player);
HierarchyProvider.ADMIN.getHierarchy().dropUserFromAll(user);
FUtil.adminAction(sender.getName(), "Removing " + admin.getName() + " from the admin list", true); FUtil.adminAction(sender.getName(), "Removing " + admin.getName() + " from the admin list", true);
admin.setActive(false); admin.setActive(false);
@ -297,11 +309,11 @@ public class Command_saconfig extends FreedomCommand
{ {
return argumentCompletions(args[0], return argumentCompletions(args[0],
PermissibleCompletion.of("tfm.saconfig.info", "info"), PermissibleCompletion.of("tfm.saconfig.info", "info"),
PermissibleCompletion.of("tfm.saconfig.add", "add"), PermissibleCompletion.of("tfm.saconfig.demote", "demote"),
PermissibleCompletion.of("tfm.saconfig.remove", "remove"), PermissibleCompletion.of("tfm.saconfig.remove", "remove"),
PermissibleCompletion.of("tfm.saconfig.reload", "reload"), PermissibleCompletion.of("tfm.saconfig.reload", "reload"),
PermissibleCompletion.of("tfm.saconfig.clean", "clean"), PermissibleCompletion.of("tfm.saconfig.clean", "clean"),
PermissibleCompletion.of("tfm.saconfig.setrank", "setrank")); 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("add") || args[0].equals("remove") || args[0].equals("setrank") || args[0].equals("info")))
{ {

View File

@ -10,6 +10,7 @@ import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.util.PermissibleCompletion; import me.totalfreedom.totalfreedommod.util.PermissibleCompletion;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -22,7 +23,6 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.annotation.AnnotationFormatError; import java.lang.annotation.AnnotationFormatError;
import java.util.*; import java.util.*;
@ -160,7 +160,8 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
.toList(); .toList();
} }
protected List<String> playerCompletions(String arg) { protected List<String> playerCompletions(String arg)
{
return Bukkit.getOnlinePlayers() return Bukkit.getOnlinePlayers()
.stream() .stream()
.map(Player::getName) .map(Player::getName)
@ -178,6 +179,24 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
return plugin.al.isAdmin(sender); return plugin.al.isAdmin(sender);
} }
@NotNull
public TagResolver.Single player(Player player)
{
return Placeholder.unparsed("player", player.getName());
}
@NotNull
public TagResolver.Single sender(CommandSender sender)
{
return Placeholder.unparsed("sender", sender.getName());
}
@NotNull
public TagResolver.Single usage()
{
return Placeholder.unparsed("usage", this.getUsage().replace("<command>", this.getName().toLowerCase()));
}
protected void checkConsole() protected void checkConsole()
{ {
if (!isConsole()) if (!isConsole())
@ -232,6 +251,11 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
public abstract boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole); public abstract boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole);
protected TagResolver.Single arg(String arg)
{
return Placeholder.unparsed("arg", arg);
}
protected List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args) protected List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{ {
return FUtil.getPlayerList(); return FUtil.getPlayerList();
@ -347,6 +371,7 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
{ {
if (cmd == null) if (cmd == null)
{ {
msgNew(sender, "<gray><usage>", usage());
return false; return false;
} // Reduce complexity! Exit immediately if the command is null. } // Reduce complexity! Exit immediately if the command is null.

View File

@ -0,0 +1,48 @@
package me.totalfreedom.totalfreedommod.perms;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.group.GroupManager;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class GroupWrapper
{
private final GroupManager manager;
private final Set<Group> groupSet;
public GroupWrapper(GroupManager manager)
{
this.manager = manager;
this.groupSet = manager.getLoadedGroups();
}
public CompletableFuture<Group> getGroup(String name)
{
if (!manager.isLoaded(name) || !groupSet.contains(manager.getGroup(name)))
{
return manager.createAndLoadGroup(name);
}
return CompletableFuture.supplyAsync(() -> manager.getGroup(name));
}
public CompletableFuture<Void> removeGroup(String name)
{
return getGroup(name).thenAccept(a ->
{
manager.deleteGroup(a);
groupSet.remove(a);
});
}
public CompletableFuture<Void> addGroup(String name)
{
return getGroup(name).thenAccept(groupSet::add);
}
public CompletableFuture<Void> saveGroup(String name)
{
return getGroup(name).thenAccept(manager::saveGroup);
}
}

View File

@ -0,0 +1,37 @@
package me.totalfreedom.totalfreedommod.perms;
import net.luckperms.api.track.Track;
import net.luckperms.api.track.TrackManager;
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 = manager.getLoadedTracks();
}
public CompletableFuture<Track> getTrack(String name)
{
if (!manager.isLoaded(name) || !trackSet.contains(manager.getTrack(name)))
{
return manager.createAndLoadTrack(name).thenApplyAsync(a -> {
trackSet.add(a);
return a;
});
}
return CompletableFuture.supplyAsync(() -> manager.getTrack(name));
}
public CompletableFuture<Void> saveTrack(String name)
{
return getTrack(name).thenAccept(manager::saveTrack);
}
}

View File

@ -0,0 +1,80 @@
package me.totalfreedom.totalfreedommod.perms;
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.query.QueryOptions;
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 User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public CachedMetaData getMetaData()
{
return metaData;
}
public void setMetaData(CachedMetaData metaData)
{
this.metaData = metaData;
}
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 clone()
{
return new UserData(user, metaData, permissionData, contextSet, queryOptions);
}
}

View File

@ -0,0 +1,107 @@
package me.totalfreedom.totalfreedommod.perms;
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.model.user.UserManager;
import net.luckperms.api.platform.PlayerAdapter;
import net.luckperms.api.query.QueryOptions;
import org.bukkit.OfflinePlayer;
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<OfflinePlayer> playerAdapter;
private final Set<User> userSet;
private final Set<UserData> userDataSet;
public UserWrapper(UserManager manager, PlayerAdapter<OfflinePlayer> adapter)
{
this.manager = manager;
this.userSet = manager.getLoadedUsers();
this.playerAdapter = adapter;
this.userDataSet = new HashSet<>();
}
public CompletableFuture<Void> addUserData(OfflinePlayer 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(OfflinePlayer player)
{
if (!manager.isLoaded(player.getUniqueId()) || !userSet.contains(manager.getUser(player.getUniqueId())))
{
return manager.loadUser(player.getUniqueId());
}
return CompletableFuture.supplyAsync(() -> manager.getUser(player.getUniqueId()));
}
public CompletableFuture<Void> removeUser(OfflinePlayer player)
{
return getUser(player).thenAccept(a ->
{
manager.cleanupUser(a);
userSet.remove(a);
});
}
public CompletableFuture<UserData> getUserData(OfflinePlayer 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(OfflinePlayer player)
{
return getUserData(player).thenAccept(userDataSet::remove);
}
public CompletableFuture<Void> updateUserData(OfflinePlayer player)
{
return getUserData(player).thenApply(userData ->
{
UserData newData = userData.clone();
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);
}
public CompletableFuture<Void> saveUser(User user)
{
return CompletableFuture.runAsync(() -> manager.saveUser(user));
}
}

View File

@ -104,7 +104,7 @@ public class PlayerList extends FreedomService
} }
// May not return null // May not return null
public FPlayer getPlayer(Player player) public FPlayer getPlayer(OfflinePlayer player)
{ {
FPlayer tPlayer = playerMap.get(player.getUniqueId()); FPlayer tPlayer = playerMap.get(player.getUniqueId());
if (tPlayer != null) if (tPlayer != null)

View File

@ -39,7 +39,6 @@ public class DisplayableGroup implements Displayable
private final boolean hasDefaultLoginMessage; private final boolean hasDefaultLoginMessage;
public DisplayableGroup(String group, public DisplayableGroup(String group,
String inheritance,
Component plural, Component plural,
Component tag, Component tag,
int weight, int weight,
@ -65,11 +64,6 @@ public class DisplayableGroup implements Displayable
.prefix(FUtil.miniMessage(GroupProvider.OPEN.append(tag.color(color)).append(GroupProvider.CLOSE))) .prefix(FUtil.miniMessage(GroupProvider.OPEN.append(tag.color(color)).append(GroupProvider.CLOSE)))
.priority(1) .priority(1)
.build(); .build();
if (inheritance != null)
{
InheritanceNode inheritanceNode = InheritanceNode.builder(inheritance).build();
g.getData(DataType.NORMAL).add(inheritanceNode);
}
g.getData(DataType.NORMAL).add(prefixNode); g.getData(DataType.NORMAL).add(prefixNode);
g.getData(DataType.NORMAL).add(weightNode); g.getData(DataType.NORMAL).add(weightNode);
}).join(); // Block until the group is created and loaded. }).join(); // Block until the group is created and loaded.

View File

@ -3,8 +3,8 @@ package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.user.User;
import net.luckperms.api.track.TrackManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -16,11 +16,11 @@ public interface GroupProvider<T extends DisplayableGroup>
Component OPEN = Component.text("[", NamedTextColor.DARK_GRAY); Component OPEN = Component.text("[", NamedTextColor.DARK_GRAY);
Component CLOSE = Component.text("]", NamedTextColor.DARK_GRAY); Component CLOSE = Component.text("]", NamedTextColor.DARK_GRAY);
GroupProvider<DisplayableGroup> NON_OP = () -> new DisplayableGroup("default", null, Component.text("Non-Ops"), Component.empty(), 0, NamedTextColor.WHITE, null, false, false); GroupProvider<DisplayableGroup> NON_OP = () -> new DisplayableGroup("default", Component.text("Non-Ops"), Component.empty(), 0, NamedTextColor.WHITE, null, false, false);
GroupProvider<DisplayableGroup> OP = () -> new DisplayableGroup("op", "default", Component.text("Operators"), Component.text("Op"), 1, NamedTextColor.GREEN, null, false, false); GroupProvider<DisplayableGroup> OP = () -> new DisplayableGroup("op", Component.text("Operators"), Component.text("Op"), 1, NamedTextColor.GREEN, null, false, false);
GroupProvider<DisplayableGroup> MASTER_BUILDER = () -> new DisplayableGroup("builder", "op", Component.text("Master Builders"), Component.text("MB"), 2, NamedTextColor.DARK_AQUA, ChatColor.DARK_AQUA, true, true); GroupProvider<DisplayableGroup> MASTER_BUILDER = () -> new DisplayableGroup("builder", Component.text("Master Builders"), Component.text("MB"), 2, NamedTextColor.DARK_AQUA, ChatColor.DARK_AQUA, true, true);
GroupProvider<DisplayableGroup> ADMIN = () -> new DisplayableGroup("admin", "builder", Component.text("Administrators"), Component.text("Admin"), 3, NamedTextColor.DARK_GREEN, ChatColor.DARK_GREEN, true, true); GroupProvider<DisplayableGroup> ADMIN = () -> new DisplayableGroup("admin", Component.text("Administrators"), Component.text("Admin"), 3, NamedTextColor.DARK_GREEN, ChatColor.DARK_GREEN, true, true);
GroupProvider<DisplayableGroup> SENIOR_ADMIN = () -> new DisplayableGroup("senior", "admin", Component.text("Senior Administrators"), Component.text("SrA"), 4, NamedTextColor.GOLD, ChatColor.GOLD, true, true); GroupProvider<DisplayableGroup> SENIOR_ADMIN = () -> new DisplayableGroup("senior", Component.text("Senior Administrators"), Component.text("SrA"), 4, NamedTextColor.GOLD, ChatColor.GOLD, true, true);
static User getUser(Player player) static User getUser(Player player)
{ {
@ -30,16 +30,26 @@ public interface GroupProvider<T extends DisplayableGroup>
.getUser(player); .getUser(player);
} }
static GroupProvider<DisplayableGroup> fromArgument(String arg) static GroupProvider<DisplayableGroup> fromString(String arg)
{ {
return switch (arg.toLowerCase()) return switch (arg.toLowerCase())
{ {
case "op" -> OP; case "op" -> OP;
case "builder" -> MASTER_BUILDER; case "builder" -> MASTER_BUILDER;
case "admin" -> ADMIN; case "admin" -> ADMIN;
case "senior" -> SENIOR_ADMIN; case "senior" -> SENIOR_ADMIN;
default -> NON_OP; default -> NON_OP;
}; };
}
static GroupProvider<DisplayableGroup> 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;
};
} }
static Set<GroupProvider<DisplayableGroup>> providerSet() static Set<GroupProvider<DisplayableGroup>> providerSet()

View File

@ -1,193 +1,179 @@
package me.totalfreedom.totalfreedommod.rank; package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.perms.GroupWrapper;
import me.totalfreedom.totalfreedommod.perms.TrackWrapper;
import me.totalfreedom.totalfreedommod.perms.UserWrapper;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.group.Group; import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User; import net.luckperms.api.node.NodeEqualityPredicate;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.InheritanceNode;
import net.luckperms.api.track.DemotionResult;
import net.luckperms.api.track.PromotionResult; import net.luckperms.api.track.PromotionResult;
import net.luckperms.api.track.Track; import net.luckperms.api.track.Track;
import org.bukkit.OfflinePlayer;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicBoolean;
public class Hierarchy public class Hierarchy
{ {
private final LuckPerms lp; private static final Hierarchy hierarchy = new Hierarchy(); // static singleton for global state.
private final Track track; private final GroupWrapper groupWrapper;
private final TrackWrapper trackWrapper;
private final UserWrapper userWrapper;
public Hierarchy(String name) private Hierarchy()
{ {
this.lp = TotalFreedomMod.getPlugin().lpb.getAPI(); 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(),
TotalFreedomMod.getPlugin().lpb.getAPI().getPlayerAdapter(OfflinePlayer.class));
}
if (lp == null) public static Hierarchy getHierarchy()
{ {
throw new IllegalStateException("LuckPerms not found!"); return hierarchy;
} }
if (!lp.getTrackManager().isLoaded(name)) public boolean isUserOnAdminTrack(OfflinePlayer player)
{
AtomicBoolean isOnAdminTrack = new AtomicBoolean(false);
userWrapper.getUser(player).thenAccept(user ->
user.data().toCollection().stream()
.filter(NodeType.INHERITANCE::matches)
.map(NodeType.INHERITANCE::cast)
.filter(NodeEqualityPredicate.ONLY_KEY
.equalTo(InheritanceNode
.builder(GroupProvider.ADMIN.getGroup().getLuckPermsGroup().getName())
.build()))
.forEach(node ->
{
if (node.getGroupName().startsWith("admin"))
{
isOnAdminTrack.set(true);
}
})).whenComplete((a, b) ->
{ {
this.track = lp.getTrackManager().createAndLoadTrack(name).whenComplete((ignored, throwable) -> if (b != null)
{ {
if (throwable != null) FLog.severe(b);
}
});
return isOnAdminTrack.get();
}
public Track op()
{
return trackWrapper.getTrack("OP").join();
}
public Track builder()
{
return trackWrapper.getTrack("BUILDER").join();
}
public Track admin()
{
return trackWrapper.getTrack("ADMIN").join();
}
public void addInheritance(DisplayableGroup previous, DisplayableGroup current)
{
CompletableFuture.runAsync(() ->
{
current.getLuckPermsGroup().data().add(InheritanceNode.builder(previous.getLuckPermsGroup()).build());
groupWrapper.saveGroup(current.getLuckPermsGroup().getName());
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
public Group getGroup(String name) {
return groupWrapper.getGroup(name).join();
}
public void addGroupToTrack(Track track, DisplayableGroup group)
{
trackWrapper.getTrack(track.getName()).thenAccept(t ->
{
t.appendGroup(group.getLuckPermsGroup());
trackWrapper.saveTrack(t.getName());
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
public CompletableFuture<PromotionResult> promoteUser(Track track, OfflinePlayer player)
{
return userWrapper.getUserData(player).thenApply(data ->
track.promote(data.getUser(), data.getContextSet()));
}
public CompletableFuture<DemotionResult> demoteUser(Track track, OfflinePlayer player)
{
return userWrapper.getUserData(player).thenApply(data ->
track.demote(data.getUser(), data.getContextSet()));
}
public void addUserToGroup(DisplayableGroup group, OfflinePlayer player)
{
userWrapper.getUserData(player).thenAccept(user ->
{
for (InheritanceNode node : group.getLuckPermsGroup().getNodes(NodeType.INHERITANCE))
{
user.getUser().data().add(node);
}
user.getUser().getInheritedGroups(user.getQueryOptions()).add(group.getLuckPermsGroup());
userWrapper.saveUser(user.getUser());
}).whenComplete((a, b) ->
{
if (b != null)
{
FLog.severe(b);
}
});
}
public void dropUserFromAll(Track track, OfflinePlayer player)
{
userWrapper.getUserData(player).thenAccept(data ->
{
for (String group : track.getGroups())
{
groupWrapper.getGroup(group).whenComplete((g, b) ->
{ {
throw new IllegalStateException("Failed to create track " + name, throwable); if (b != null)
} {
}).join(); // Block and wait for the track to load. FLog.severe(b);
} else return;
{ }
this.track = lp.getTrackManager().getTrack(name);
}
}
public void addGroup(DisplayableGroup group) for (InheritanceNode node : g.getNodes(NodeType.INHERITANCE))
{ {
updateAppend(group.getLuckPermsGroup()).join(); // wait for the group to be updated. data.getUser().data().remove(node);
} }
});
public void addGroup(Group group, int index)
{
updateInsert(group, index).join(); // wait for the group to be updated.
}
public void dropGroup(Group group) // This is non-blocking by default.
{
updateDrop(group).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
FLog.severe("Failed to update track " + track.getName());
} }
}); userWrapper.saveUser(data.getUser());
} }).whenComplete((a, b) ->
public void addGroupNonBlocking(Group group, int index)
{
updateInsert(group, index).whenComplete((ignored, throwable) ->
{ {
if (throwable != null) if (b != null)
{ {
FLog.severe("Failed to update track " + track.getName()); FLog.severe(b);
} }
}); });
} }
public CompletableFuture<Boolean> trackUser(User user)
{
Group group = fromName(track.getGroups().get(0)); // First group.
return CompletableFuture.supplyAsync(() ->
lp.getContextManager()
.getContext(user)
.map(context -> user
.getInheritedGroups(QueryOptions.defaultContextualOptions())
.add(group))
.orElse(false));
}
public CompletableFuture<PromotionResult> promoteUser(User user)
{
boolean empty = user.getInheritedGroups(QueryOptions.defaultContextualOptions())
.stream()
.filter(group -> track.getGroups().contains(group.getName()))
.findFirst()
.isEmpty();
if (empty) trackUser(user).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
FLog.severe("Failed to track user " + user.getFriendlyName());
}
});
return CompletableFuture.supplyAsync(() ->
{
AtomicReference<PromotionResult> result = new AtomicReference<>();
lp.getContextManager()
.getContext(user)
.ifPresent(context -> result.set(track.promote(user, context)));
return result.get();
});
}
public void dropUserFromAll(User user)
{
for (String group : track.getGroups())
{
dropUserFromGroup(user, fromName(group))
.whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
FLog.severe("Failed to drop user "
+ user.getFriendlyName()
+ " from group "
+ group);
return;
}
FLog.info("Successfully removed " + user.getFriendlyName() + " from group " + group);
});
}
}
public CompletableFuture<Void> demoteUser(User user)
{
return CompletableFuture.supplyAsync(() -> {
lp.getContextManager()
.getContext(user)
.ifPresent(context -> track.demote(user, context));
return null;
});
}
public CompletableFuture<Boolean> dropUserFromGroup(User user, Group group)
{
return CompletableFuture.supplyAsync(() -> user
.getInheritedGroups(QueryOptions.defaultContextualOptions())
.remove(group));
}
public Group fromName(String name)
{
return lp.getGroupManager().getGroup(name);
}
public CompletableFuture<Void> updateAppend(Group group)
{
return CompletableFuture.supplyAsync(() ->
{
if (!track.containsGroup(group.getName()))
{
track.appendGroup(group);
}
return null;
});
}
public CompletableFuture<Void> updateInsert(Group group, int index)
{
return CompletableFuture.supplyAsync(() ->
{
if (!track.containsGroup(group))
{
track.insertGroup(group, index);
}
return null;
});
}
public CompletableFuture<Void> updateDrop(Group group)
{
return CompletableFuture.supplyAsync(() ->
{
if (track.containsGroup(group))
{
track.removeGroup(group);
}
return null;
});
}
} }

View File

@ -1,11 +0,0 @@
package me.totalfreedom.totalfreedommod.rank;
@FunctionalInterface
public interface HierarchyProvider<T extends Hierarchy>
{
HierarchyProvider<Hierarchy> OP = () -> new Hierarchy("op-track");
HierarchyProvider<Hierarchy> BUILDER = () -> new Hierarchy("builder-track");
HierarchyProvider<Hierarchy> ADMIN = () -> new Hierarchy("admin-track");
T getHierarchy();
}

View File

@ -25,11 +25,17 @@ public class RankManager extends FreedomService
@Override @Override
public void onStart() public void onStart()
{ {
HierarchyProvider.OP.getHierarchy().addGroup(GroupProvider.NON_OP.getGroup());
HierarchyProvider.OP.getHierarchy().addGroup(GroupProvider.OP.getGroup()); // Here, we are automatically creating new tracks if they don't exist, and then adding groups to them.
HierarchyProvider.BUILDER.getHierarchy().addGroup(GroupProvider.MASTER_BUILDER.getGroup()); Hierarchy hierarchy = Hierarchy.getHierarchy();
HierarchyProvider.ADMIN.getHierarchy().addGroup(GroupProvider.ADMIN.getGroup()); hierarchy.addGroupToTrack(hierarchy.op(), GroupProvider.NON_OP.getGroup());
HierarchyProvider.ADMIN.getHierarchy().addGroup(GroupProvider.SENIOR_ADMIN.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.addInheritance(GroupProvider.NON_OP.getGroup(), GroupProvider.OP.getGroup());
hierarchy.addInheritance(GroupProvider.ADMIN.getGroup(), GroupProvider.SENIOR_ADMIN.getGroup());
} }
@Override @Override
@ -71,7 +77,7 @@ public class RankManager extends FreedomService
// Master builders show up if they are not an admin // Master builders show up if they are not an admin
if (plugin.pl.getData(player).isMasterBuilder() && !plugin.al.isAdmin(player)) if (plugin.pl.getData(player).isMasterBuilder() && !plugin.al.isAdmin(player))
{ {
return Title.MASTER_BUILDER; return GroupProvider.MASTER_BUILDER.getGroup();
} }
return getRank(player); return getRank(player);
@ -137,13 +143,11 @@ public class RankManager extends FreedomService
return entry.getRank(); return entry.getRank();
} }
return plugin.lpb.getAPI() String primary = plugin.lpb.getAPI()
.getPlayerAdapter(Player.class) .getPlayerAdapter(Player.class)
.getUser(player) .getUser(player)
.getPrimaryGroup() .getPrimaryGroup();
.equalsIgnoreCase("op") return GroupProvider.fromString(primary).getGroup();
? GroupProvider.OP.getGroup()
: GroupProvider.NON_OP.getGroup();
} }
public Component getTag(Player player, Component defaultTag) public Component getTag(Player player, Component defaultTag)

View File

@ -36,11 +36,5 @@
<version>3.5.4</version> <version>3.5.4</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>me.totalfreedom</groupId>
<artifactId>commons</artifactId>
<version>2023.03</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>