Addede chat preprocess event

This commit is contained in:
Norbi Peti 2017-05-13 16:08:45 +02:00
parent f5ce88faf6
commit acebf37359
2 changed files with 74 additions and 1 deletions

View file

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

View file

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