Add HelpopMessageSentEvent (#5490)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
YanisBft 2023-08-29 00:41:40 +02:00 committed by GitHub
parent 72ba87c509
commit a3a71afcef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 7 deletions

View File

@ -3,9 +3,13 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.Console; import com.earth2me.essentials.Console;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.messaging.IMessageRecipient;
import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.FormatUtil;
import net.ess3.api.IUser;
import net.essentialsx.api.v2.events.HelpopMessageSendEvent;
import org.bukkit.Server; import org.bukkit.Server;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -20,7 +24,7 @@ public class Commandhelpop extends EssentialsCommand {
@Override @Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
user.setDisplayNick(); user.setDisplayNick();
final String message = sendMessage(server, user.getDisplayName(), args); final String message = sendMessage(server, user, args);
if (!user.isAuthorized("essentials.helpop.receive")) { if (!user.isAuthorized("essentials.helpop.receive")) {
user.sendMessage(message); user.sendMessage(message);
} }
@ -28,17 +32,33 @@ public class Commandhelpop extends EssentialsCommand {
@Override @Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { 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) { if (args.length < 1) {
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
final String message = tl("helpOp", from, FormatUtil.stripFormat(getFinalArg(args, 0)));
ess.getLogger().log(Level.INFO, message); final String message = FormatUtil.stripFormat(getFinalArg(args, 0));
ess.broadcastMessage("essentials.helpop.receive", message); final String finalMessage = tl("helpOp", from.getDisplayName(), message);
return message; ess.getLogger().log(Level.INFO, finalMessage);
final List<IUser> 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 @Override

View File

@ -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<IUser> recipients;
private final String message;
public HelpopMessageSendEvent(final IMessageRecipient sender, final List<IUser> 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<IUser> 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;
}
}