Fixes part 1

There's definitely more to do here, I am dead inside
This commit is contained in:
Paul Reilly 2023-04-05 20:46:24 -05:00
parent a48596de8a
commit 8262e81e69
16 changed files with 361 additions and 89 deletions

View File

@ -1,14 +1,17 @@
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
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.model.user.User;
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;
@CommandPermissions(permission = "deop", source = SourceType.BOTH)
@CommandParameters(description = "Deop a player", usage = "/<command> <partialname>")
public class Command_deop extends FreedomCommand
@ -35,20 +38,24 @@ public class Command_deop extends FreedomCommand
return true;
}
AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
User user = GroupProvider.getUser(player);
if (user.getPrimaryGroup().equalsIgnoreCase(GroupProvider.NON_OP.getGroup().getLuckPermsGroup().getName()))
HierarchyProvider.OP.getHierarchy().demoteUser(user).whenComplete((result, throwable) ->
{
msgNew(sender, "<red><player> is already de-opped!", Placeholder.unparsed("<player>", player.getName()));
return true;
if (throwable != null)
{
FLog.severe("Error while demoting " + player.getName() + " to OP: " + throwable.getMessage());
return;
}
user.setPrimaryGroup(GroupProvider.NON_OP.getGroup().getLuckPermsGroup().getName());
msg(player, YOU_ARE_NOT_OP);
plugin.rm.updateDisplay(player);
if (!silent)
if (!atomicBoolean.get())
{
FUtil.adminAction(sender.getName(), "De-opping " + player.getName(), false);
}
});
return true;
}

View File

@ -1,6 +1,8 @@
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.command.Command;
@ -20,10 +22,15 @@ public class Command_deopall extends FreedomCommand
server.getOnlinePlayers().forEach(player ->
{
User user = GroupProvider.getUser(player);
user.setPrimaryGroup(GroupProvider.NON_OP.getGroup().getLuckPermsGroup().getName());
HierarchyProvider.OP.getHierarchy().demoteUser(user).whenComplete((result, ex) -> {
if (ex != null) {
FLog.severe("Failed to demote " + player.getName() + " to default rank");
}
msg(player, YOU_ARE_NOT_OP);
plugin.rm.updateDisplay(player);
});
});
return true;
}

View File

@ -35,7 +35,7 @@ public class Command_nickmm extends FreedomCommand
return true;
}
Component parsed = FUtil.miniMessage(args[0], Placeholder.unparsed("<name>", sender.getName()));
Component parsed = FUtil.miniMessage(args[0], Placeholder.unparsed("name", sender.getName()));
plugin.esb.setNickname(sender.getName(), FUtil.miniMessage(parsed));
msgNew("Your nickname is now: " + FUtil.miniMessage(parsed));

View File

@ -1,14 +1,17 @@
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
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.model.user.User;
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;
@CommandPermissions(permission = "op", source = SourceType.BOTH, cooldown = 5)
@CommandParameters(description = "OP a player", usage = "/<command> <partialname>")
public class Command_op extends FreedomCommand
@ -36,20 +39,24 @@ public class Command_op extends FreedomCommand
return true;
}
AtomicBoolean atomicBoolean = new AtomicBoolean(silent);
User user = GroupProvider.getUser(player);
if (user.getPrimaryGroup().equalsIgnoreCase(GroupProvider.OP.getGroup().getLuckPermsGroup().getName()))
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((ignored, throwable) ->
{
msgNew(sender, "<red><player> is already OP!", Placeholder.unparsed("<player>", player.getName()));
return true;
if (throwable != null)
{
FLog.severe("Failed to promote user " + player.getName());
return;
}
user.setPrimaryGroup(GroupProvider.OP.getGroup().getLuckPermsGroup().getName());
msg(player, YOU_ARE_OP);
plugin.rm.updateDisplay(player);
if (!silent)
if (!atomicBoolean.get())
{
FUtil.adminAction(sender.getName(), "Opping " + player.getName(), false);
}
});
return true;
}

View File

@ -1,6 +1,8 @@
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.command.Command;
@ -19,10 +21,18 @@ public class Command_opall extends FreedomCommand
server.getOnlinePlayers().forEach(player ->
{
User user = GroupProvider.getUser(player);
user.setPrimaryGroup(GroupProvider.OP.getGroup().getLuckPermsGroup().getName());
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
FLog.severe("Failed to promote " + player.getName());
return;
}
msg(player, YOU_ARE_OP);
plugin.rm.updateDisplay(player);
});
});
return true;
}

View File

@ -1,6 +1,8 @@
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.luckperms.api.model.user.User;
import org.bukkit.command.Command;
@ -17,9 +19,15 @@ public class Command_opme extends FreedomCommand
{
FUtil.adminAction(sender.getName(), "Opping " + sender.getName(), false);
User user = GroupProvider.getUser(playerSender);
user.setPrimaryGroup(GroupProvider.OP.getGroup().getLuckPermsGroup().getName());
HierarchyProvider.OP.getHierarchy().promoteUser(user).whenComplete((result, error) -> {
if (error != null) {
FLog.severe("Error while promoting " + playerSender.getName() + " to OP: " + error.getMessage());
return;
}
msg(YOU_ARE_OP);
plugin.rm.updateDisplay(playerSender);
});
return true;
}
}

View File

@ -5,10 +5,14 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.rank.HierarchyProvider;
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.user.User;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -33,13 +37,12 @@ public class Command_saconfig extends FreedomCommand
switch (args[0])
{
case "list":
case "list" ->
{
msgNew("<gold>Admins: <admins>", Placeholder.unparsed("admins", StringUtils.join(plugin.al.getAdminNames(), ", ")));
return true;
}
case "clean":
case "clean" ->
{
checkConsole();
checkPermission("tfm.saconfig.clean");
@ -49,8 +52,7 @@ public class Command_saconfig extends FreedomCommand
msgNew("<gold>Admins: <admins>", Placeholder.unparsed("admins", StringUtils.join(plugin.al.getAdminNames(), ", ")));
return true;
}
case "reload":
case "reload" ->
{
checkPermission("tfm.saconfig.reload");
@ -59,8 +61,37 @@ public class Command_saconfig extends FreedomCommand
msgNew("Admin list reloaded!");
return true;
}
case "promote" ->
{
checkConsole();
checkPermission("tfm.saconfig.promote");
case "setrank":
if (args.length < 2)
{
return false;
}
Player player = getPlayer(args[1]);
FUtil.adminAction(sender.getName(), "Promoting " + player.getName() + " to the next rank", true);
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");
@ -111,8 +142,7 @@ public class Command_saconfig extends FreedomCommand
return true;
}
case "info":
case "info" ->
{
if (args.length < 2)
{
@ -143,8 +173,7 @@ public class Command_saconfig extends FreedomCommand
return true;
}
case "add":
case "add" ->
{
if (args.length < 2)
{
@ -184,11 +213,15 @@ public class Command_saconfig extends FreedomCommand
{
FUtil.adminAction(sender.getName(), "Adding " + player.getName() + " to the admin list", true);
admin = new Admin(player);
User user = GroupProvider.getUser(player);
HierarchyProvider.ADMIN.getHierarchy().promoteUser(user);
plugin.al.addAdmin(admin);
plugin.rm.updateDisplay(player);
} else // Existing 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);
@ -213,8 +246,7 @@ public class Command_saconfig extends FreedomCommand
return true;
}
case "remove":
case "remove" ->
{
if (args.length < 2)
{
@ -225,16 +257,11 @@ public class Command_saconfig extends FreedomCommand
checkPermission("tfm.saconfig.remove");
Player player = getPlayer(args[1]);
Admin admin = plugin.al.getAdmin(player);
plugin.al.removeAdmin(admin);
Admin admin = player != null ? plugin.al.getAdmin(player) : plugin.al.getEntryByName(args[1]);
if (admin == null)
{
msgNew("Unknown admin: <player>", Placeholder.unparsed("player", args[1]));
return true;
}
String adminName = admin.getName();
User user = GroupProvider.getUser(player);
HierarchyProvider.ADMIN.getHierarchy().dropUserFromAll(user);
FUtil.adminAction(sender.getName(), "Removing " + admin.getName() + " from the admin list", true);
admin.setActive(false);
@ -249,17 +276,18 @@ public class Command_saconfig extends FreedomCommand
if (plugin.dc != null && plugin.dc.isEnabled() && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean())
{
plugin.dc.syncRoles(admin, plugin.pl.getData(adminName).getDiscordID());
plugin.dc.syncRoles(admin, plugin.pl.getData(admin.getName())
.getDiscordID());
}
return true;
}
default:
default ->
{
return false;
}
}
return false;
}
@Override

View File

@ -0,0 +1,193 @@
package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.query.QueryOptions;
import net.luckperms.api.track.PromotionResult;
import net.luckperms.api.track.Track;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
public class Hierarchy
{
private final LuckPerms lp;
private final Track track;
public Hierarchy(String name)
{
this.lp = TotalFreedomMod.getPlugin().lpb.getAPI();
if (lp == null)
{
throw new IllegalStateException("LuckPerms not found!");
}
if (!lp.getTrackManager().isLoaded(name))
{
this.track = lp.getTrackManager().createAndLoadTrack(name).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
throw new IllegalStateException("Failed to create track " + name, throwable);
}
}).join(); // Block and wait for the track to load.
} else
{
this.track = lp.getTrackManager().getTrack(name);
}
}
public void addGroup(DisplayableGroup group)
{
updateAppend(group.getLuckPermsGroup()).join(); // wait for the group to be updated.
}
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());
}
});
}
public void addGroupNonBlocking(Group group, int index)
{
updateInsert(group, index).whenComplete((ignored, throwable) ->
{
if (throwable != null)
{
FLog.severe("Failed to update track " + track.getName());
}
});
}
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

@ -0,0 +1,11 @@
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,7 +25,11 @@ public class RankManager extends FreedomService
@Override
public void onStart()
{
// We don't need to do anything here.
HierarchyProvider.OP.getHierarchy().addGroup(GroupProvider.NON_OP.getGroup());
HierarchyProvider.OP.getHierarchy().addGroup(GroupProvider.OP.getGroup());
HierarchyProvider.BUILDER.getHierarchy().addGroup(GroupProvider.MASTER_BUILDER.getGroup());
HierarchyProvider.ADMIN.getHierarchy().addGroup(GroupProvider.ADMIN.getGroup());
HierarchyProvider.ADMIN.getHierarchy().addGroup(GroupProvider.SENIOR_ADMIN.getGroup());
}
@Override

View File

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

View File

@ -5,16 +5,12 @@ import me.totalfreedom.discord.command.HelpCommand;
import me.totalfreedom.discord.command.ListCommand;
import me.totalfreedom.discord.command.TPSCommand;
import me.totalfreedom.discord.handling.CommandHandler;
import me.totalfreedom.discord.listener.AdminChatListener;
import me.totalfreedom.discord.listener.BukkitNative;
import me.totalfreedom.discord.listener.MinecraftListener;
import me.totalfreedom.discord.listener.*;
import me.totalfreedom.discord.react.ReactiveBukkitScheduler;
import me.totalfreedom.totalfreedommod.api.Context;
import me.totalfreedom.totalfreedommod.api.TFD4JCommons;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.StringUtil;
import org.slf4j.Logger;
/**
@ -28,6 +24,8 @@ public class TFD4J extends JavaPlugin
private Bot bot;
private MinecraftListener mc;
private AdminChatListener ac;
private PrivateMessageListener pm;
private ReactionListener rl;
private TFD4JCommons tfd4jcommons;
private BukkitNative bn;
private CommandHandler ch;
@ -55,6 +53,10 @@ public class TFD4J extends JavaPlugin
slf4j().info("Bukkit Native listener successfully registered! Registering the Discord4J Listeners...");
this.mc = new MinecraftListener(this);
this.ac = new AdminChatListener(this);
this.pm = new PrivateMessageListener(this);
this.rl = new ReactionListener(this);
pm.privateMessageReceived();
rl.onReactionAdd();
mc.minecraftChatBound();
ac.adminChatBound();

View File

@ -48,16 +48,14 @@ public class AdminChatListener
.getEventDispatcher()
.on(MessageCreateEvent.class)
.filter(m -> m.getMessage()
.getChannel()
.blockOptional()
.orElseThrow()
.getId()
.getChannelId()
.equals(SnowflakeEntry.ADMIN_CHAT_CHANNEL_ID.getSnowflake()))
.filter(m -> !m.getMessage()
.getAuthor()
.orElseThrow()
.getId()
.equals(tfd4j.getBot().getClient().getSelfId()))
.doOnError(FLog::severe)
.subscribe(this::createMessageSpec);
}

View File

@ -10,6 +10,7 @@ import me.totalfreedom.discord.TFD4J;
import me.totalfreedom.discord.util.SnowflakeEntry;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
@ -37,26 +38,17 @@ public class MinecraftListener
tfd4j.getBot().getClient()
.getEventDispatcher()
.on(MessageCreateEvent.class)
.filter(m -> m.getMessage()
.getChannelId()
.equals(SnowflakeEntry.CHAT_CHANNEL_ID.getSnowflake()))
.filter(m -> m.getMember().orElse(null) != null)
.filter(m -> !m.getMessage()
.getAuthor()
.orElseThrow(IllegalAccessError::new)
.getId()
.equals(tfd4j.getBot().getClient().getSelfId()))
.filter(m -> m.getMessage()
.getChannel()
.blockOptional()
.orElseThrow(IllegalAccessError::new)
.getId()
.equals(SnowflakeEntry.CHAT_CHANNEL_ID.getSnowflake()))
.filter(m ->
{
Boolean b = m.getMessage()
.getChannel()
.map(TextChannel.class::isInstance)
.block();
return b != null && b;
}).subscribe(this::doMessageBodyDetails);
.doOnError(FLog::severe)
.subscribe(this::doMessageBodyDetails);
}
private void doMessageBodyDetails(MessageCreateEvent m)

View File

@ -1,7 +1,6 @@
package me.totalfreedom.discord.listener;
import discord4j.core.event.domain.message.MessageCreateEvent;
import me.totalfreedom.discord.Bot;
import me.totalfreedom.discord.TFD4J;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.admin.Admin;
@ -43,8 +42,7 @@ public class PrivateMessageListener
TotalFreedomMod.getPlugin().pl.save(player);
tfd4j.getBot().getLinkCodes().remove(code);
}
else
} else
{
return;
}

View File

@ -8,6 +8,7 @@ import discord4j.core.object.entity.channel.TextChannel;
import discord4j.discordjson.json.MessageCreateRequest;
import me.totalfreedom.discord.TFD4J;
import me.totalfreedom.discord.util.SnowflakeEntry;
import me.totalfreedom.totalfreedommod.util.FLog;
public class ReactionListener
{
@ -30,18 +31,18 @@ public class ReactionListener
.orElseThrow()
.getId()
.equals(tfd4j.getBot().getClient().getSelfId()))
.filter(r -> !r.getChannel()
.blockOptional()
.orElseThrow().getId().equals(SnowflakeEntry.REPORT_CHANNEL_ID.getSnowflake()))
.filter(r -> !r.getChannelId()
.equals(SnowflakeEntry.REPORT_CHANNEL_ID.getSnowflake()))
.filter(r -> r.getEmoji()
.asUnicodeEmoji()
.orElseThrow(UnsupportedOperationException::new)
.getRaw()
.equals("\uD83D\uDCCB"))
.doOnError(FLog::severe)
.subscribe(this::reactionWork);
}
public void reactionWork(ReactionAddEvent event)
private void reactionWork(ReactionAddEvent event)
{
final TextChannel archiveChannel = tfd4j.getBot()
.getClient()