Using the ChatMessage for the event too

Forgot to commit this lol
This commit is contained in:
Norbi Peti 2018-12-27 01:16:09 +01:00
parent df5befe1d9
commit ff246b8c8b
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 29 additions and 19 deletions

View file

@ -1,8 +1,9 @@
package buttondevteam.lib; package buttondevteam.lib;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.chat.ChatMessage;
import lombok.Getter; import lombok.Getter;
import lombok.experimental.Delegate;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -16,22 +17,19 @@ import javax.annotation.Nullable;
*/ */
@Getter @Getter
public class TBMCChatEvent extends TBMCChatEventBase { public class TBMCChatEvent extends TBMCChatEventBase {
public TBMCChatEvent(CommandSender sender, ChromaGamerBase user, Channel channel, String message, int score, boolean fromcmd, String groupid, boolean ignoreSenderPermissions, String origin) { public TBMCChatEvent(Channel channel, ChatMessage cm, Channel.RecipientTestResult rtr) {
super(channel, message, score, groupid); super(channel, cm.getMessage(), rtr.score, rtr.groupID);
this.sender = sender; this.cm = cm;
this.user = user;
this.fromcmd = fromcmd;
this.ignoreSenderPermissions = ignoreSenderPermissions;
this.origin = origin;
} }
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private final CommandSender sender; @Delegate //<-- Backwards compatibility
private final boolean fromcmd; private ChatMessage cm;
private final boolean ignoreSenderPermissions;
private final ChromaGamerBase user; private boolean isIgnoreSenderPermissions() {
private final String origin; return cm.getPermCheck() != cm.getSender();
}
// TODO: Message object with data? // TODO: Message object with data?
/** /**
@ -39,7 +37,7 @@ public class TBMCChatEvent extends TBMCChatEventBase {
*/ */
@Override @Override
public boolean shouldSendTo(CommandSender sender) { public boolean shouldSendTo(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender)) if (isIgnoreSenderPermissions() && sender.equals(this.cm.getSender()))
return true; //Allow sending the message no matter what return true; //Allow sending the message no matter what
return super.shouldSendTo(sender); return super.shouldSendTo(sender);
} }
@ -49,7 +47,7 @@ public class TBMCChatEvent extends TBMCChatEventBase {
*/ */
@Override @Override
public int getMCScore(CommandSender sender) { public int getMCScore(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender)) if (isIgnoreSenderPermissions() && sender.equals(this.cm.getSender()))
return getScore(); //Send in the correct group no matter what return getScore(); //Send in the correct group no matter what
return super.getMCScore(sender); return super.getMCScore(sender);
} }
@ -60,7 +58,7 @@ public class TBMCChatEvent extends TBMCChatEventBase {
@Nullable @Nullable
@Override @Override
public String getGroupID(CommandSender sender) { public String getGroupID(CommandSender sender) {
if (isIgnoreSenderPermissions() && sender.equals(this.sender)) if (isIgnoreSenderPermissions() && sender.equals(this.cm.getSender()))
return getGroupID(); //Send in the correct group no matter what return getGroupID(); //Send in the correct group no matter what
return super.getGroupID(sender); return super.getGroupID(sender);
} }

View file

@ -4,6 +4,7 @@ import buttondevteam.lib.player.ChromaGamerBase;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@Builder @Builder
@ -20,7 +21,8 @@ public class ChatMessage {
/** /**
* The message to send as the user. * The message to send as the user.
*/ */
private final String message; @Setter
private String message;
/** /**
* Indicates whether the message comes from running a command (like /tableflip). Implemented to be used from Discord. * Indicates whether the message comes from running a command (like /tableflip). Implemented to be used from Discord.
*/ */
@ -35,6 +37,15 @@ public class ChatMessage {
*/ */
private final String origin; private final String origin;
/**
* The sender which we should check for permissions. Same as {@link #sender} by default.
*
* @return The perm check or the sender
*/
public CommandSender getPermCheck() {
return permCheck == null ? sender : permCheck;
}
private static ChatMessageBuilder builder() { private static ChatMessageBuilder builder() {
return new ChatMessageBuilder(); return new ChatMessageBuilder();
} }

View file

@ -228,7 +228,7 @@ public class TBMCChatAPI {
public static boolean SendChatMessage(ChatMessage cm, Channel channel) { public static boolean SendChatMessage(ChatMessage cm, Channel channel) {
if (!Channel.getChannels().contains(channel)) if (!Channel.getChannels().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); throw new RuntimeException("Channel " + channel.DisplayName + " not registered!");
val permcheck = cm.getPermCheck() == null ? cm.getSender() : cm.getPermCheck(); val permcheck = cm.getPermCheck();
RecipientTestResult rtr = getScoreOrSendError(channel, permcheck); RecipientTestResult rtr = getScoreOrSendError(channel, permcheck);
int score = rtr.score; int score = rtr.score;
if (score == Channel.SCORE_SEND_NOPE || rtr.groupID == null) if (score == Channel.SCORE_SEND_NOPE || rtr.groupID == null)
@ -237,8 +237,9 @@ public class TBMCChatAPI {
Bukkit.getPluginManager().callEvent(eventPre); Bukkit.getPluginManager().callEvent(eventPre);
if (eventPre.isCancelled()) if (eventPre.isCancelled())
return true; return true;
cm.setMessage(eventPre.getMessage());
TBMCChatEvent event; TBMCChatEvent event;
event = new TBMCChatEvent(cm.getSender(), cm.getUser(), channel, eventPre.getMessage(), score, cm.isFromCommand(), rtr.groupID, permcheck != cm.getSender(), cm.getOrigin()); event = new TBMCChatEvent(channel, cm, rtr);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
return event.isCancelled(); return event.isCancelled();
} }