From acebf37359d249f9dc42dcda93a9afafa070b0ed Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 13 May 2017 16:08:45 +0200 Subject: [PATCH] Addede chat preprocess event --- .../lib/TBMCChatPreprocessEvent.java | 68 +++++++++++++++++++ .../buttondevteam/lib/chat/TBMCChatAPI.java | 7 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/buttondevteam/lib/TBMCChatPreprocessEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCChatPreprocessEvent.java b/src/main/java/buttondevteam/lib/TBMCChatPreprocessEvent.java new file mode 100644 index 0000000..8f8f9d2 --- /dev/null +++ b/src/main/java/buttondevteam/lib/TBMCChatPreprocessEvent.java @@ -0,0 +1,68 @@ +package buttondevteam.lib; + +import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import buttondevteam.lib.chat.Channel; + +/** + * Can be used to change messages before it's sent. + * + * @author NorbiPeti + * + */ +public class TBMCChatPreprocessEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private Channel channel; + private CommandSender sender; + private String message; + private boolean cancelled; + + public TBMCChatPreprocessEvent(CommandSender sender, Channel channel, String message) { + this.sender = sender; + this.channel = channel; + this.message = message; // TODO: Message object with data? + } + + /* + * public TBMCPlayer getPlayer() { return TBMCPlayer.getPlayer(sender); // TODO: Get Chroma user } + */ + + public Channel getChannel() { + return channel; + } + + public CommandSender getSender() { + return sender; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 5dd1bdf..86e7c37 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -19,6 +19,7 @@ import org.reflections.util.ConfigurationBuilder; import buttondevteam.core.CommandCaller; import buttondevteam.core.MainPlugin; import buttondevteam.lib.TBMCChatEvent; +import buttondevteam.lib.TBMCChatPreprocessEvent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Channel.RecipientTestResult; @@ -214,6 +215,10 @@ public class TBMCChatAPI { public static boolean SendChatMessage(Channel channel, CommandSender sender, String message) { if (!Channel.getChannels().contains(channel)) throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); + TBMCChatPreprocessEvent eventPre = new TBMCChatPreprocessEvent(sender, channel, message); + Bukkit.getPluginManager().callEvent(eventPre); + if (eventPre.isCancelled()) + return true; int score; if (channel.filteranderrormsg == null) score = -1; @@ -225,7 +230,7 @@ public class TBMCChatAPI { } score = result.score; } - TBMCChatEvent event = new TBMCChatEvent(sender, channel, message, score); + TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score); Bukkit.getPluginManager().callEvent(event); return event.isCancelled(); }