From 86a2a415116fc67e51ef69606d307d3c230aa61b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 13 Jun 2018 23:45:58 +0200 Subject: [PATCH] SendChatMessage refactor, ignoreSenderPermissions Used for channelcon --- .../java/buttondevteam/lib/TBMCChatEvent.java | 46 ++++++++++++++++++- .../buttondevteam/lib/TBMCChatEventBase.java | 6 ++- .../buttondevteam/lib/chat/TBMCChatAPI.java | 33 +++++++++++-- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEvent.java b/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEvent.java index 0dab094..1f161d8 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEvent.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEvent.java @@ -5,6 +5,8 @@ import lombok.Getter; import org.bukkit.command.CommandSender; import org.bukkit.event.HandlerList; +import javax.annotation.Nullable; + /** * Make sure to only send the message to users where {@link #shouldSendTo(CommandSender)} returns true. * @@ -17,15 +19,55 @@ public class TBMCChatEvent extends TBMCChatEventBase { super(channel, message, score, groupid); this.sender = sender; this.fromcmd = fromcmd; - } + this.ignoreSenderPermissions = false; + } + + public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score, boolean fromcmd, String groupid, boolean ignoreSenderPermissions) { + super(channel, message, score, groupid); + this.sender = sender; + this.fromcmd = fromcmd; + this.ignoreSenderPermissions = ignoreSenderPermissions; + } private static final HandlerList handlers = new HandlerList(); private CommandSender sender; private boolean fromcmd; + private final boolean ignoreSenderPermissions; // TODO: Message object with data? - @Override + /** + * This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true. + */ + @Override + public boolean shouldSendTo(CommandSender sender) { + if (isIgnoreSenderPermissions() && sender.equals(this.sender)) + return true; //Allow sending the message no matter what + return super.shouldSendTo(sender); + } + + /** + * This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true. + */ + @Override + public int getMCScore(CommandSender sender) { + if (isIgnoreSenderPermissions() && sender.equals(this.sender)) + return getScore(); //Send in the correct group no matter what + return super.getMCScore(sender); + } + + /** + * This will allow the sender of the message if {@link #isIgnoreSenderPermissions()} is true. + */ + @Nullable + @Override + public String getGroupID(CommandSender sender) { + if (isIgnoreSenderPermissions() && sender.equals(this.sender)) + return getGroupID(); //Send in the correct group no matter what + return super.getGroupID(sender); + } + + @Override public HandlerList getHandlers() { return handlers; } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEventBase.java b/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEventBase.java index 9265b0c..77dd9fd 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEventBase.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/TBMCChatEventBase.java @@ -48,12 +48,14 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable { } /** - * Note: Errors are sent to the sender automatically + * Note: Errors are sent to the sender automatically
+ * + * Null means don't send */ @Nullable public String getGroupID(CommandSender sender) { if (channel.filteranderrormsg == null) - return null; + return "everyone"; RecipientTestResult result = channel.filteranderrormsg.apply(sender); return result.errormessage == null ? result.groupID : null; } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 2a5762c..894c9d2 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -231,21 +231,44 @@ public class TBMCChatAPI { * @return The event cancelled state */ public static boolean SendChatMessage(Channel channel, CommandSender sender, String message, boolean fromcommand) { + return sendChatMessageInternal(channel, sender, message, fromcommand, sender); + } + + private static boolean sendChatMessageInternal(Channel channel, CommandSender sender, String message, boolean fromcommand, CommandSender permcheck) { if (!Channel.getChannels().contains(channel)) throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); + RecipientTestResult rtr = getScoreOrSendError(channel, permcheck); + int score = rtr.score; + if (score == -1 || rtr.groupID == null) + return true; TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(sender, channel, message); Bukkit.getPluginManager().callEvent(eventPre); if (eventPre.isCancelled()) return true; - RecipientTestResult rtr = getScoreOrSendError(channel, sender); - int score = rtr.score; - if (score == -1 || rtr.groupID == null) - return true; - TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID); + TBMCChatEvent event; + if (permcheck == sender) + event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID); + else + event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score, fromcommand, rtr.groupID, true); Bukkit.getPluginManager().callEvent(event); return event.isCancelled(); } + /** + * Sends a chat message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}.
+ * This will not check if the sender has permission. + * + * @param channel The channel to send to + * @param sender The sender to send from + * @param message The message to send + * @param fromcommand Whether this message comes from running a command, used to determine whether to delete Discord messages for example + * @param permcheck The sender to check permissions + * @return The event cancelled state + */ + public static boolean SendChatMessageDontCheckSender(Channel channel, CommandSender sender, String message, boolean fromcommand, CommandSender permcheck) { + return sendChatMessageInternal(channel, sender, message, fromcommand, permcheck); + } + /** * Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}. *