From a3a71afcef57f0ba9a3a7e32ac6f454c054ae62a Mon Sep 17 00:00:00 2001 From: YanisBft Date: Tue, 29 Aug 2023 00:41:40 +0200 Subject: [PATCH] Add HelpopMessageSentEvent (#5490) Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com> --- .../essentials/commands/Commandhelpop.java | 34 ++++++++--- .../api/v2/events/HelpopMessageSendEvent.java | 58 +++++++++++++++++++ 2 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 Essentials/src/main/java/net/essentialsx/api/v2/events/HelpopMessageSendEvent.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhelpop.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhelpop.java index b0324e188..b2673de21 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhelpop.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhelpop.java @@ -3,9 +3,13 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.Console; import com.earth2me.essentials.User; +import com.earth2me.essentials.messaging.IMessageRecipient; import com.earth2me.essentials.utils.FormatUtil; +import net.ess3.api.IUser; +import net.essentialsx.api.v2.events.HelpopMessageSendEvent; import org.bukkit.Server; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -20,7 +24,7 @@ public class Commandhelpop extends EssentialsCommand { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { user.setDisplayNick(); - final String message = sendMessage(server, user.getDisplayName(), args); + final String message = sendMessage(server, user, args); if (!user.isAuthorized("essentials.helpop.receive")) { user.sendMessage(message); } @@ -28,17 +32,33 @@ public class Commandhelpop extends EssentialsCommand { @Override public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { - sendMessage(server, Console.DISPLAY_NAME, args); + sendMessage(server, Console.getInstance(), args); } - private String sendMessage(final Server server, final String from, final String[] args) throws Exception { + private String sendMessage(final Server server, final IMessageRecipient from, final String[] args) throws Exception { if (args.length < 1) { throw new NotEnoughArgumentsException(); } - final String message = tl("helpOp", from, FormatUtil.stripFormat(getFinalArg(args, 0))); - ess.getLogger().log(Level.INFO, message); - ess.broadcastMessage("essentials.helpop.receive", message); - return message; + + final String message = FormatUtil.stripFormat(getFinalArg(args, 0)); + final String finalMessage = tl("helpOp", from.getDisplayName(), message); + ess.getLogger().log(Level.INFO, finalMessage); + + final List recipients = new ArrayList<>(); + for (IUser user : ess.getOnlineUsers()) { + if (user.getBase().hasPermission("essentials.helpop.receive")) { + recipients.add(user); + } + } + + final HelpopMessageSendEvent sendEvent = new HelpopMessageSendEvent(from, recipients, message); + ess.getServer().getPluginManager().callEvent(sendEvent); + + for (IUser recipient : sendEvent.getRecipients()) { + recipient.sendMessage(finalMessage); + } + + return finalMessage; } @Override diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/HelpopMessageSendEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/HelpopMessageSendEvent.java new file mode 100644 index 000000000..7c1c8eff2 --- /dev/null +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/HelpopMessageSendEvent.java @@ -0,0 +1,58 @@ +package net.essentialsx.api.v2.events; + +import com.earth2me.essentials.messaging.IMessageRecipient; +import net.ess3.api.IUser; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import java.util.List; + +/** + * Called just before a message is sent to the helpop channel. + */ +public class HelpopMessageSendEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private final IMessageRecipient sender; + private final List recipients; + private final String message; + + public HelpopMessageSendEvent(final IMessageRecipient sender, final List recipients, final String message) { + this.sender = sender; + this.recipients = recipients; + this.message = message; + } + + /** + * Gets the sender of the helpop message. + * @return the sender. + */ + public IMessageRecipient getSender() { + return sender; + } + + /** + * Gets the recipients of the helpop message. + * @return the recipients. + */ + public List getRecipients() { + return recipients; + } + + /** + * Gets the helpop message to be sent. + * @return the message. + */ + public String getMessage() { + return message; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}