mirror of
https://github.com/darksaid98/TownyChat.git
synced 2026-12-02 06:28:58 +00:00
Support for hooking into TownyChat before a message is sent out
This commit is contained in:
1 parent
4c448655eb
commit
07c5da2ccb
4 files changed
+137
-2
No files matched your search
@@ -16,6 +16,7 @@ public abstract class Channel {
|
||||
private channelTypes type;
|
||||
private String channelTag, messageColour, permission, leavePermission, craftIRCTag;
|
||||
private double range;
|
||||
private boolean hooked=false;
|
||||
protected ConcurrentMap<String, Integer> absentPlayers = null;
|
||||
protected ConcurrentMap<String, Integer> mutedPlayers = null;
|
||||
|
||||
@@ -223,4 +224,12 @@ public abstract class Channel {
|
||||
public Set<String> getMuteList() {
|
||||
return mutedPlayers.keySet();
|
||||
}
|
||||
|
||||
public void setHooked(boolean hooked) {
|
||||
this.hooked = hooked;
|
||||
}
|
||||
|
||||
public boolean isHooked() {
|
||||
return hooked;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.dynmap.DynmapAPI;
|
||||
@@ -15,6 +17,7 @@ import com.palmergames.bukkit.TownyChat.Chat;
|
||||
import com.palmergames.bukkit.TownyChat.CraftIRCHandler;
|
||||
import com.palmergames.bukkit.TownyChat.TownyChatFormatter;
|
||||
import com.palmergames.bukkit.TownyChat.config.ChatSettings;
|
||||
import com.palmergames.bukkit.TownyChat.events.AsyncChatHookEvent;
|
||||
import com.palmergames.bukkit.TownyChat.listener.LocalTownyChatEvent;
|
||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
||||
import com.palmergames.bukkit.towny.exceptions.TownyException;
|
||||
@@ -27,12 +30,12 @@ import com.palmergames.bukkit.util.BukkitTools;
|
||||
public class StandardChannel extends Channel {
|
||||
|
||||
private Chat plugin;
|
||||
|
||||
public StandardChannel(Chat instance, String name) {
|
||||
super(name);
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void chatProcess(AsyncPlayerChatEvent event) {
|
||||
|
||||
@@ -106,6 +109,21 @@ public class StandardChannel extends Channel {
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().addAll(recipients);
|
||||
|
||||
if (isHooked()) {
|
||||
AsyncChatHookEvent hookEvent = new AsyncChatHookEvent(event, this);
|
||||
Bukkit.getServer().getPluginManager().callEvent(hookEvent);
|
||||
if (hookEvent.isCancelled()) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (hookEvent.isChanged()) {
|
||||
event.setMessage(hookEvent.getMessage());
|
||||
event.setFormat(hookEvent.getFormat());
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().addAll(hookEvent.getRecipients());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform any last channel specific functions
|
||||
* like logging this chat and relaying to IRC/Dynmap.
|
||||
|
||||
@@ -77,7 +77,17 @@ public class ConfigurationHandler {
|
||||
if (key.equalsIgnoreCase("type"))
|
||||
if (element instanceof String)
|
||||
channel.setType(channelTypes.valueOf(element.toString()));
|
||||
|
||||
|
||||
if (key.equalsIgnoreCase("hooked")) {
|
||||
if (element instanceof Boolean) {
|
||||
channel.setHooked((Boolean)element);
|
||||
} else if (element instanceof String) {
|
||||
channel.setHooked(Boolean.parseBoolean(element.toString()));
|
||||
} else if (element instanceof Integer) {
|
||||
channel.setHooked(Integer.parseInt(element.toString()) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (key.equalsIgnoreCase("channeltag"))
|
||||
if (element instanceof String)
|
||||
channel.setChannelTag(element.toString());
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.palmergames.bukkit.TownyChat.events;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
|
||||
import com.palmergames.bukkit.TownyChat.channels.Channel;
|
||||
|
||||
/*
|
||||
* Allows other plugins to hook into a chat message being accepted into any of the channels
|
||||
*/
|
||||
public class AsyncChatHookEvent extends Event {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
protected AsyncPlayerChatEvent event = null;
|
||||
protected boolean changed = false;
|
||||
protected Channel channel=null;
|
||||
|
||||
public AsyncChatHookEvent(AsyncPlayerChatEvent event, Channel channel) {
|
||||
this.event = event;
|
||||
this.changed = false;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Channel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if the hooked event was changed
|
||||
*/
|
||||
public boolean isChanged() {
|
||||
return changed;
|
||||
}
|
||||
|
||||
/*
|
||||
* Informs TownyChat if the event was changed or not
|
||||
*/
|
||||
public void setChanged(boolean changed) {
|
||||
this.changed = changed;
|
||||
}
|
||||
|
||||
public AsyncPlayerChatEvent getAsyncPlayerChatEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return event.getFormat();
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return event.getMessage();
|
||||
}
|
||||
|
||||
public Set<Player> getRecipients() {
|
||||
return event.getRecipients();
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return event.isCancelled();
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return event.getPlayer();
|
||||
}
|
||||
|
||||
public void SetRecipients(Set<Player> recipients) {
|
||||
changed = true;
|
||||
event.getRecipients().clear();
|
||||
event.getRecipients().addAll(recipients);
|
||||
}
|
||||
|
||||
public void SetFormat(String format) {
|
||||
changed = true;
|
||||
event.setFormat(format);
|
||||
}
|
||||
|
||||
public void SetMessage(String message) {
|
||||
changed = true;
|
||||
event.setFormat(message);
|
||||
}
|
||||
|
||||
public void SetCancelled(boolean cancel) {
|
||||
changed = (cancel == true);
|
||||
event.setCancelled(cancel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user