From 35a5ffe71c649d1fe202a813a84aa0811684a953 Mon Sep 17 00:00:00 2001 From: Allink Date: Tue, 10 May 2022 15:37:13 +0100 Subject: [PATCH] Moar, not redy for production --- src/main/java/dev/plex/nush/Message.java | 14 ++++ src/main/java/dev/plex/nush/NushAction.java | 30 ++++++++ src/main/java/dev/plex/nush/NushModule.java | 50 +++++++----- .../plex/nush/command/impl/NUSHCommand.java | 73 ++++++++++++++---- .../java/dev/plex/nush/handler/Handler.java | 1 + .../plex/nush/handler/impl/ActionHandler.java | 77 +++++++++++++++++++ .../plex/nush/listener/impl/ChatListener.java | 54 ++++++++----- .../plex/nush/listener/impl/JoinListener.java | 28 ++++--- .../dev/plex/nush/util/ReflectionsUtil.java | 32 +++----- 9 files changed, 268 insertions(+), 91 deletions(-) create mode 100644 src/main/java/dev/plex/nush/Message.java create mode 100644 src/main/java/dev/plex/nush/NushAction.java create mode 100644 src/main/java/dev/plex/nush/handler/impl/ActionHandler.java diff --git a/src/main/java/dev/plex/nush/Message.java b/src/main/java/dev/plex/nush/Message.java new file mode 100644 index 0000000..5aaf8c7 --- /dev/null +++ b/src/main/java/dev/plex/nush/Message.java @@ -0,0 +1,14 @@ +package dev.plex.nush; + +import java.util.UUID; +import lombok.AllArgsConstructor; +import lombok.Data; +import net.kyori.adventure.text.Component; + +@Data +@AllArgsConstructor +public class Message { + + UUID sender; + Component message; +} diff --git a/src/main/java/dev/plex/nush/NushAction.java b/src/main/java/dev/plex/nush/NushAction.java new file mode 100644 index 0000000..54bc1b9 --- /dev/null +++ b/src/main/java/dev/plex/nush/NushAction.java @@ -0,0 +1,30 @@ +package dev.plex.nush; + +import javax.annotation.Nullable; + +public enum NushAction { + MUTE("Mute Player", 0), + CANCEL("Cancel", 1), + SMITE("Smite", 2), + BAN("Ban Player", 3), + ACCEPT("Accept", 4); + + public final String humanReadable; + public final int ordinal; + + NushAction(String humanReadable, int ordinal) { + this.humanReadable = humanReadable; + this.ordinal = ordinal; + } + + @Nullable + public static NushAction fromOrdinal(int ordinal) { + for (NushAction value : NushAction.values()) { + if (value.ordinal == ordinal) { + return value; + } + } + + return null; + } +} diff --git a/src/main/java/dev/plex/nush/NushModule.java b/src/main/java/dev/plex/nush/NushModule.java index ddb1fcc..c6ed434 100644 --- a/src/main/java/dev/plex/nush/NushModule.java +++ b/src/main/java/dev/plex/nush/NushModule.java @@ -1,30 +1,38 @@ package dev.plex.nush; +import dev.plex.Plex; +import dev.plex.module.PlexModule; import dev.plex.nush.handler.impl.CommandHandler; import dev.plex.nush.handler.impl.ListenerHandler; -import dev.plex.module.PlexModule; +import java.util.Map; +import java.util.Map.Entry; -public class NushModule extends PlexModule -{ - public static boolean enabled = false; - private static NushModule INSTANCE; +public class NushModule extends PlexModule { - @Override - public void enable() - { - INSTANCE = this; - getPlex().messages.addDefault("nushToggled", "{0} - {1} NUSH."); - new CommandHandler().init(this); - new ListenerHandler().init(this); - } + public static final Map messages = Map.ofEntries( + Map.entry("nushToggled", "{0} - {1} NUSH."), + Map.entry("nushApply", "Applying {0} to {1}!")); + public static boolean enabled = false; + private static NushModule INSTANCE; - @Override - public void disable() - { - // Unregistering listeners / commands is handled by Plex - } + public static NushModule getInstance() { + return INSTANCE; + } - public static NushModule getInstance() { - return INSTANCE; - } + @Override + public void enable() { + INSTANCE = this; + Plex plex = getPlex(); + for (Entry entry : messages.entrySet()) { + plex.messages.addDefault(entry.getKey(), entry.getValue()); + } + + new CommandHandler().init(this); + new ListenerHandler().init(this); + } + + @Override + public void disable() { + // Unregistering listeners / commands is handled by Plex + } } diff --git a/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java b/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java index 7fc62c4..6e32bca 100644 --- a/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java +++ b/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java @@ -3,11 +3,16 @@ package dev.plex.nush.command.impl; import dev.plex.command.PlexCommand; import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandPermissions; +import dev.plex.nush.Message; +import dev.plex.nush.NushAction; import dev.plex.nush.NushModule; +import dev.plex.nush.handler.impl.ActionHandler; import dev.plex.rank.enums.Rank; import dev.plex.util.PlexUtils; +import java.util.UUID; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -15,22 +20,56 @@ import org.jetbrains.annotations.Nullable; @CommandParameters(name = "nush", aliases = "raidmode", description = "Toggle NUSH on or off.", usage = "/ [on | enable | off | disable | toggle]") @CommandPermissions(level = Rank.ADMIN, permission = "plex.nush.command") -public class NUSHCommand extends PlexCommand -{ - @Override - protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args) - { - if(args.length == 0) { - NushModule.enabled = !NushModule.enabled; - } else if (args.length > 1) { - switch (args[0].toLowerCase()) { - case "on", "enable" -> NushModule.enabled = true; - case "off", "disable" -> NushModule.enabled = false; - case "toggle" -> NushModule.enabled = !NushModule.enabled; - } - } +public class NUSHCommand extends PlexCommand { - PlexUtils.broadcastToAdmins(messageComponent("nushToggled", commandSender.getName(), NushModule.enabled ? "Enabling" : "Disabling")); - return null; - } + @Override + protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, + @NotNull String[] args) { + if (args.length == 0) { + NushModule.enabled = !NushModule.enabled; + } else if (args.length == 1) { + switch (args[0].toLowerCase()) { + case "on", "enable" -> NushModule.enabled = true; + case "off", "disable" -> NushModule.enabled = false; + case "toggle" -> NushModule.enabled = !NushModule.enabled; + } + } else { + if (args[0].equalsIgnoreCase("work")) { + try { + UUID nushIdentifier = UUID.fromString(args[0]); + Message nushMessage = ActionHandler.MAP.get(nushIdentifier); + + NushAction action = NushAction.fromOrdinal(Integer.parseInt(args[2])); + if (action == null) { + return null; + } + StringBuilder command = new StringBuilder(); + + switch (action) { + case ACCEPT: + case CANCEL: + break; + default: + command.append(action.name().toLowerCase()); + break; + } + + command.append(" ").append(nushMessage.getSender()); + if (!command.toString().trim().isEmpty()) { + Bukkit.dispatchCommand(commandSender, command.toString()); + } + ActionHandler.resolve(nushIdentifier, action); + return Component.text(action.humanReadable, NamedTextColor.YELLOW); + } catch (Exception ignored) { + return null; + } + } + + return null; + } + + PlexUtils.broadcastToAdmins(messageComponent("nushToggled", commandSender.getName(), + NushModule.enabled ? "Enabling" : "Disabling")); + return null; + } } diff --git a/src/main/java/dev/plex/nush/handler/Handler.java b/src/main/java/dev/plex/nush/handler/Handler.java index 3e937b9..e4325c3 100644 --- a/src/main/java/dev/plex/nush/handler/Handler.java +++ b/src/main/java/dev/plex/nush/handler/Handler.java @@ -3,5 +3,6 @@ package dev.plex.nush.handler; import dev.plex.nush.NushModule; public interface Handler { + void init(NushModule module); } diff --git a/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java b/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java new file mode 100644 index 0000000..22a547e --- /dev/null +++ b/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java @@ -0,0 +1,77 @@ +package dev.plex.nush.handler.impl; + +import dev.plex.Plex; +import dev.plex.cache.DataUtils; +import dev.plex.nush.Message; +import dev.plex.nush.NushAction; +import dev.plex.nush.NushModule; +import dev.plex.nush.handler.Handler; +import dev.plex.player.PlexPlayer; +import dev.plex.util.PlexUtils; +import dev.plex.util.minimessage.SafeMiniMessage; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; +import org.bukkit.Bukkit; + +public class ActionHandler implements Handler { + + public static final Map MAP = new HashMap<>(); + private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder() + .match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]") + .replacement( + (matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent( + ClickEvent.openUrl(matchResult.group()))).build(); + private static NushModule MODULE; + + public static void resolve(UUID uuid, NushAction action) { + Message message = MAP.get(uuid); + if (message == null) { + return; + } + if (action == NushAction.ACCEPT) { + Audience.audience(Bukkit.getServer()) + .sendMessage(Identity.identity(message.getSender()), getMessage(message)); + } + MAP.remove(uuid); + } + + public static Component getMessage(Message message) { + String text = PlexUtils.getTextFromComponent(message.getMessage()); + Plex plex = MODULE.getPlex(); + PlexPlayer plexPlayer = DataUtils.getPlayer(message.getSender()); + Component prefix = plex.getRankManager().getPrefix(plexPlayer); + Component component = Component.empty(); + + if (prefix != null) { + component = component.append(prefix); + } + + return component.append(Component.space()).append( + PlexUtils.mmDeserialize(plex.config.getString("chat.name-color", "") + + MiniMessage.builder().tags( + TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), + StandardTags.decorations(), StandardTags.gradient(), + StandardTags.transition() + )).build().serialize(plexPlayer.getPlayer().displayName()))) + .append(Component.space()) + .append(Component.text("ยป").color(NamedTextColor.GRAY)).append(Component.space()) + .append( + SafeMiniMessage.mmDeserializeWithoutEvents(text)) + .replaceText(URL_REPLACEMENT_CONFIG); + } + + @Override + public void init(NushModule module) { + MODULE = NushModule.getInstance(); + } +} diff --git a/src/main/java/dev/plex/nush/listener/impl/ChatListener.java b/src/main/java/dev/plex/nush/listener/impl/ChatListener.java index d3e7713..f5d7e04 100644 --- a/src/main/java/dev/plex/nush/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/nush/listener/impl/ChatListener.java @@ -1,24 +1,28 @@ package dev.plex.nush.listener.impl; import dev.plex.Plex; -import dev.plex.admin.Admin; import dev.plex.cache.DataUtils; import dev.plex.listener.PlexListener; +import dev.plex.nush.Message; +import dev.plex.nush.NushAction; import dev.plex.nush.NushModule; +import dev.plex.nush.handler.impl.ActionHandler; import dev.plex.player.PlexPlayer; import dev.plex.rank.RankManager; +import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; import io.papermc.paper.event.player.AsyncChatEvent; import java.time.Duration; import java.time.Instant; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; +import java.util.UUID; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; public class ChatListener extends PlexListener { - public static final Map work = new HashMap<>(); @EventHandler(priority = EventPriority.HIGHEST) public void onChat(AsyncChatEvent event) { @@ -26,25 +30,39 @@ public class ChatListener extends PlexListener { Instant firstJoined = Instant.ofEpochMilli(player.getFirstPlayed()); Instant rightNow = Instant.now(); long difference = (Duration.between(firstJoined, rightNow).getSeconds() / 60); - if(difference >= 15) return; + if (difference >= 15) { + PlexLog.debug(player.getName(), "has been on the server for", difference, + "minutes, so Nush will skip them."); + return; + } NushModule module = NushModule.getInstance(); Plex plex = module.getPlex(); PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); RankManager rankManager = plex.getRankManager(); - if(rankManager.isAdmin(plexPlayer)) return; // we needn't process the chat message if they're an admin - Entry leastWork = null; - - for (Entry adminIntegerEntry : work.entrySet()) { - if(leastWork == null) { - leastWork = adminIntegerEntry; - return; - } else { - if(leastWork.getValue() > adminIntegerEntry.getValue()) { - leastWork = adminIntegerEntry; - } - } + if (rankManager.isAdmin(plexPlayer)) { + PlexLog.debug(player.getName(), "is an admin so Nush will skip them."); + return; // we needn't process the chat message if they're an admin } + + event.setCancelled(true); + UUID key = UUID.randomUUID(); + Message message = new Message(event.getPlayer().getUniqueId(), event.originalMessage()); + ActionHandler.MAP.put(key, message); + Component component = ActionHandler.getMessage(message); + + for (NushAction value : NushAction.values()) { + String command = String.format("/nush work %s %d", key, value.ordinal); + component = component.append( + Component.text(String.format("[%s] ", value.humanReadable)) + .clickEvent(ClickEvent.runCommand(command)) + .hoverEvent( + Component.text(command, NamedTextColor.YELLOW) + ) + ); + } + + PlexUtils.broadcast(component); } } diff --git a/src/main/java/dev/plex/nush/listener/impl/JoinListener.java b/src/main/java/dev/plex/nush/listener/impl/JoinListener.java index ebb594e..334d783 100644 --- a/src/main/java/dev/plex/nush/listener/impl/JoinListener.java +++ b/src/main/java/dev/plex/nush/listener/impl/JoinListener.java @@ -10,24 +10,22 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerJoinEvent; -public class JoinListener extends PlexListener -{ - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) - { - Player player = event.getPlayer(); - NushModule module = NushModule.getInstance(); - Plex plex = module.getPlex(); - PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); - RankManager rankManager = plex.getRankManager(); +public class JoinListener extends PlexListener { - if (!rankManager.isAdmin(plexPlayer)) - { - return; // we only want to add admins - } + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + NushModule module = NushModule.getInstance(); + Plex plex = module.getPlex(); + PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); + RankManager rankManager = plex.getRankManager(); + + if (!rankManager.isAdmin(plexPlayer)) { + return; // we only want to add admins + } /*if (ChatListener.work.containsKey()) { }*/ - } + } } diff --git a/src/main/java/dev/plex/nush/util/ReflectionsUtil.java b/src/main/java/dev/plex/nush/util/ReflectionsUtil.java index 57cb46a..1e8f941 100644 --- a/src/main/java/dev/plex/nush/util/ReflectionsUtil.java +++ b/src/main/java/dev/plex/nush/util/ReflectionsUtil.java @@ -3,7 +3,6 @@ package dev.plex.nush.util; import com.google.common.collect.ImmutableSet; import com.google.common.reflect.ClassPath; import dev.plex.Plex; - import dev.plex.util.PlexLog; import java.io.IOException; import java.util.Arrays; @@ -11,31 +10,24 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -public class ReflectionsUtil -{ +public class ReflectionsUtil { + @SuppressWarnings("UnstableApiUsage") - public static Set> getClassesFrom(String packageName) - { + public static Set> getClassesFrom(String packageName) { Set> classes = new HashSet<>(); - try - { + try { ClassPath path = ClassPath.from(Plex.class.getClassLoader()); ImmutableSet infoSet = path.getTopLevelClasses(packageName); infoSet.forEach(info -> { - try - { + try { Class clazz = Class.forName(info.getName()); classes.add(clazz); - } - catch (ClassNotFoundException ex) - { + } catch (ClassNotFoundException ex) { PlexLog.error("Unable to find class " + info.getName() + " in " + packageName); } }); - } - catch (IOException ex) - { + } catch (IOException ex) { PlexLog.error("Something went wrong while fetching classes from " + packageName); throw new RuntimeException(ex); } @@ -43,15 +35,15 @@ public class ReflectionsUtil } @SuppressWarnings("unchecked") - public static Set> getClassesBySubType(String packageName, Class subType) - { + public static Set> getClassesBySubType(String packageName, + Class subType) { Set> loadedClasses = getClassesFrom(packageName); Set> classes = new HashSet<>(); loadedClasses.forEach(clazz -> { - if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType)) - { - classes.add((Class)clazz); + if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()) + .contains(subType)) { + classes.add((Class) clazz); } }); return Collections.unmodifiableSet(classes);