From 1207990b7702810dacc679ed20cedff73aaa0e57 Mon Sep 17 00:00:00 2001 From: Luna <90072930+LunaWasFlaggedAgain@users.noreply.github.com> Date: Tue, 17 May 2022 15:12:07 -0300 Subject: [PATCH] Make everything work (#1) --- src/main/java/dev/plex/nush/NushModule.java | 2 ++ .../plex/nush/command/impl/NUSHCommand.java | 30 ++++++++++++------- .../plex/nush/handler/impl/ActionHandler.java | 8 +++-- .../nush/handler/impl/ListenerHandler.java | 2 ++ .../plex/nush/listener/impl/ChatListener.java | 13 +++++--- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/main/java/dev/plex/nush/NushModule.java b/src/main/java/dev/plex/nush/NushModule.java index c6ed434..8b0e6c1 100644 --- a/src/main/java/dev/plex/nush/NushModule.java +++ b/src/main/java/dev/plex/nush/NushModule.java @@ -2,6 +2,7 @@ package dev.plex.nush; import dev.plex.Plex; import dev.plex.module.PlexModule; +import dev.plex.nush.handler.impl.ActionHandler; import dev.plex.nush.handler.impl.CommandHandler; import dev.plex.nush.handler.impl.ListenerHandler; import java.util.Map; @@ -27,6 +28,7 @@ public class NushModule extends PlexModule { plex.messages.addDefault(entry.getKey(), entry.getValue()); } + new ActionHandler().init(this); new CommandHandler().init(this); new ListenerHandler().init(this); } 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 6e32bca..3a6f5f6 100644 --- a/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java +++ b/src/main/java/dev/plex/nush/command/impl/NUSHCommand.java @@ -8,6 +8,7 @@ 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.PlexLog; import dev.plex.util.PlexUtils; import java.util.UUID; import net.kyori.adventure.text.Component; @@ -18,6 +19,9 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static dev.plex.nush.NushAction.ACCEPT; +import static dev.plex.nush.NushAction.CANCEL; + @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 { @@ -36,28 +40,34 @@ public class NUSHCommand extends PlexCommand { } else { if (args[0].equalsIgnoreCase("work")) { try { - UUID nushIdentifier = UUID.fromString(args[0]); + UUID nushIdentifier = UUID.fromString(args[1]); Message nushMessage = ActionHandler.MAP.get(nushIdentifier); + if (nushMessage == null) { + return null; + } + NushAction action = NushAction.fromOrdinal(Integer.parseInt(args[2])); if (action == null) { return null; } + + if (action == ACCEPT || action == CANCEL) { + ActionHandler.resolve(nushIdentifier, action); + return Component.text(action.humanReadable, NamedTextColor.YELLOW); + } + StringBuilder command = new StringBuilder(); - switch (action) { - case ACCEPT: - case CANCEL: - break; - default: - command.append(action.name().toLowerCase()); - break; - } + command.append(action.name().toLowerCase()); + command.append(" "); + command.append(nushMessage.getSender()); - command.append(" ").append(nushMessage.getSender()); if (!command.toString().trim().isEmpty()) { + PlexLog.debug("Dispatching command: {0}", command.toString()); Bukkit.dispatchCommand(commandSender, command.toString()); } + ActionHandler.resolve(nushIdentifier, action); return Component.text(action.humanReadable, NamedTextColor.YELLOW); } catch (Exception ignored) { diff --git a/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java b/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java index 22a547e..d0cfefc 100644 --- a/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java +++ b/src/main/java/dev/plex/nush/handler/impl/ActionHandler.java @@ -22,6 +22,7 @@ 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; +import org.bukkit.entity.Player; public class ActionHandler implements Handler { @@ -39,8 +40,11 @@ public class ActionHandler implements Handler { return; } if (action == NushAction.ACCEPT) { - Audience.audience(Bukkit.getServer()) - .sendMessage(Identity.identity(message.getSender()), getMessage(message)); + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.getUniqueId() != message.getSender()) { + player.sendMessage(Identity.identity(message.getSender()), getMessage(message)); + } + } } MAP.remove(uuid); } diff --git a/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java b/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java index 04b5572..16976c4 100644 --- a/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java +++ b/src/main/java/dev/plex/nush/handler/impl/ListenerHandler.java @@ -2,12 +2,14 @@ package dev.plex.nush.handler.impl; import dev.plex.nush.NushModule; import dev.plex.nush.handler.Handler; +import dev.plex.nush.listener.impl.ChatListener; import dev.plex.nush.listener.impl.JoinListener; public class ListenerHandler implements Handler { @Override public void init(NushModule module) { + module.registerListener(new ChatListener()); module.registerListener(new JoinListener()); } } 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 f5d7e04..cfda211 100644 --- a/src/main/java/dev/plex/nush/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/nush/listener/impl/ChatListener.java @@ -26,13 +26,13 @@ public class ChatListener extends PlexListener { @EventHandler(priority = EventPriority.HIGHEST) public void onChat(AsyncChatEvent event) { + if(event.isCancelled()) return; Player player = event.getPlayer(); Instant firstJoined = Instant.ofEpochMilli(player.getFirstPlayed()); Instant rightNow = Instant.now(); long difference = (Duration.between(firstJoined, rightNow).getSeconds() / 60); if (difference >= 15) { - PlexLog.debug(player.getName(), "has been on the server for", difference, - "minutes, so Nush will skip them."); + PlexLog.debug("{0} has been on the server for {1} minutes, so Nush will skip them.", player.getName(), difference); return; } @@ -42,7 +42,7 @@ public class ChatListener extends PlexListener { RankManager rankManager = plex.getRankManager(); if (rankManager.isAdmin(plexPlayer)) { - PlexLog.debug(player.getName(), "is an admin so Nush will skip them."); + PlexLog.debug("{0} is an admin so Nush will skip them.", player.getName()); return; // we needn't process the chat message if they're an admin } @@ -52,6 +52,11 @@ public class ChatListener extends PlexListener { ActionHandler.MAP.put(key, message); Component component = ActionHandler.getMessage(message); + // Send the user the message so they think it got sent + player.sendMessage(component); + + component = component.append(Component.text("\n")); + for (NushAction value : NushAction.values()) { String command = String.format("/nush work %s %d", key, value.ordinal); component = component.append( @@ -63,6 +68,6 @@ public class ChatListener extends PlexListener { ); } - PlexUtils.broadcast(component); + PlexUtils.broadcastToAdmins(component); } }