SendChatMessage refactor, ignoreSenderPermissions

Used for channelcon
This commit is contained in:
Norbi Peti 2018-06-13 23:45:58 +02:00
parent a691278ea9
commit 86a2a41511
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 76 additions and 9 deletions

View file

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

View file

@ -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<br>
*
* 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;
}

View file

@ -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)}.<br>
* 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)}.
*