Make everything work (#1)

This commit is contained in:
Luna 2022-05-17 15:12:07 -03:00 committed by GitHub
parent 35a5ffe71c
commit 1207990b77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 16 deletions

View File

@ -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);
}

View File

@ -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 = "/<command> [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) {

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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);
}
}