diff --git a/src/Channels.yml b/src/Channels.yml new file mode 100644 index 0000000..d51f68a --- /dev/null +++ b/src/Channels.yml @@ -0,0 +1,85 @@ +# This file contains all configuration data for channels +# The formating for them remains in the Towny Config and world files +# so they can still be used on a per world basis. + +# Channels are toggleable using any of the commands listed +# eg. /tc (would put you in town chat) +# doing it again would take you out +# So long as you had the permission node for that channel. + +# The channel type is either GLOBAL, TOWN, NATION or DEFAULT. +# These specify what chat formating section they will use +# and where the chat will go +# (town goes to all town residents, depending on the range setting) + +# channeltag is applied if the chat format for that channel has the {channelTag} + +# messagecolour sets the colour of the message when sent. + +# range is a setting which allows greater control over each channel. +# this will set the maximum distance between players who can hear the message. +# range is in blocks (if set to a limit). +# +# -1 = no limits +# 0 = same world only +# any positive value = limited range in the same world. + +# Text colouring +# -------------- +# Black = &0, Navy = &1, Green = &2, Blue = &3, Red = &4 +# Purple = &5, Gold = &6, LightGray = &7, Gray = &8 +# DarkPurple = &9, LightGreen = &a, LightBlue = &b +# Rose = &c, LightPurple = &d, Yellow = &e, White = &f + +# Spam_time set the minimum time between messages to be flagged as spam +spam_time: 2 + + +Channels: + general: + commands: [g] + type: GLOBAL + channeltag: '&f[g]' + messagecolour: '&f' + permission: 'towny.chat.global' + range: '-1' + + town: + commands: [tc] + type: TOWN + channeltag: '&f[&3TC&f]' + messagecolour: '&b' + permission: 'towny.chat.town' + range: '-1' + + nation: + commands: [nc] + type: NATION + channeltag: '&f[&6NC&f]' + messagecolour: '&e' + permission: 'towny.chat.nation' + range: '-1' + + admin: + commands: [a,admin] + type: DEFAULT + channeltag: '&f[&4ADMIN&f]' + messagecolour: '&c' + permission: 'towny.chat.admin' + range: '-1' + + mod: + commands: [m,mod] + type: DEFAULT + channeltag: '&f[&9MOD&f]' + messagecolour: '&5' + permission: 'towny.chat.mod' + range: '-1' + + local: + commands: [l,lc] + type: GLOBAL + channeltag: '&f[local]' + messagecolour: '&f' + permission: 'towny.chat.local' + range: '100' \ No newline at end of file diff --git a/src/changelog.txt b/src/changelog.txt index 8aa3c58..b4f10a6 100644 --- a/src/changelog.txt +++ b/src/changelog.txt @@ -66,4 +66,10 @@ v0.10: v0.11: - Fixed channel tag for the global channel. - Fixed range test trying to cross worlds. +v0.12: + - Major rewrite to move all channel handling into TownyChat. + - All chat channels are now defined in their own file 'Channels.yml' found in the Towny/Settings folder. + - Range limits can now be applied to town and nation chat. + - You can create two identical channels, one with a range limit and one without. The player will use whichever they have permission for. + - towny_default_modes now uses the full chat channel name. To allow use of the default channel it would be towny_default_modes: 'general' \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/Chat.java b/src/com/palmergames/bukkit/TownyChat/Chat.java index faff22a..388767c 100644 --- a/src/com/palmergames/bukkit/TownyChat/Chat.java +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -11,8 +11,10 @@ import org.dynmap.DynmapAPI; import com.ensifera.animosity.craftirc.CraftIRC; import com.palmergames.bukkit.TownyChat.CraftIRCHandler; +import com.palmergames.bukkit.TownyChat.channels.ChannelsHolder; import com.palmergames.bukkit.TownyChat.event.TownyPlayerHighestListener; import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; +import com.palmergames.bukkit.TownyChat.util.FileMgmt; import com.palmergames.bukkit.towny.Towny; /** @@ -27,6 +29,7 @@ public class Chat extends JavaPlugin { private Logger logger = Logger.getLogger("com.palmergames.bukkit.TownyChat"); private TownyPlayerHighestListener TownyPlayerListener; + private ChannelsHolder channels = new ChannelsHolder(this); protected PluginManager pm; private Towny towny = null; @@ -34,6 +37,8 @@ public class Chat extends JavaPlugin { private DynmapAPI dynMap = null; private CraftIRCHandler irc = null; + + private long spam_time = 2; @Override public void onEnable() { @@ -46,7 +51,8 @@ public class Chat extends JavaPlugin { * This executes the task with a 1 tick delay avoiding the bukkit * depends bug. */ - if ((towny == null) || (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1)) { + if ((towny == null) || (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1) + || (!loadChannels())) { /* * We either failed to find Towny or the Scheduler failed to * register the task. @@ -56,7 +62,12 @@ public class Chat extends JavaPlugin { pm.disablePlugin(this); return; } - + + } + + private boolean loadChannels() { + FileMgmt.checkFolders(new String[] { getRootPath(), getChannelsPath() }); + return channels.loadChannels(getChannelsPath(), "Channels.yml"); } @Override @@ -95,11 +106,40 @@ public class Chat extends JavaPlugin { } public void registerEvents() { - TownyPlayerListener = new TownyPlayerHighestListener(towny, irc, dynMap); + TownyPlayerListener = new TownyPlayerHighestListener(this, irc, dynMap); pm.registerEvent(Event.Type.PLAYER_CHAT, TownyPlayerListener, Priority.Highest, this); //Run this lower so we go before herochat? ( it needs to see us cancel). pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, TownyPlayerListener, Priority.Highest, this); } + + public String getRootPath() { + return getTowny().getDataFolder().getPath(); + } + + public String getChannelsPath() { + return getRootPath() + FileMgmt.fileSeparator() + "settings"; + } + + /** + * @return the channels + */ + public ChannelsHolder getChannelsHandler() { + return channels; + } + + /** + * @return the spam_time + */ + public long getSpam_time() { + return spam_time; + } + + /** + * @param spam_time the spam_time to set + */ + public void setSpam_time(long spam_time) { + this.spam_time = spam_time; + } public Logger getLogger() { return logger; diff --git a/src/com/palmergames/bukkit/TownyChat/channels/Channel.java b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java new file mode 100644 index 0000000..3586339 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java @@ -0,0 +1,102 @@ +package com.palmergames.bukkit.TownyChat.channels; + +import java.util.List; + +public class Channel { + + private String name; + private List commands; + private channelTypes type; + private String channelTag, messageColour, permission; + private double range; + + /** + * Constructor + * + * @param name + */ + public Channel(String name) { + super(); + this.name = name; + } + /** + * @return the name + */ + public String getName() { + return name; + } + /** + * @return the commands + */ + public List getCommands() { + return commands; + } + /** + * @param commands the commands to set + */ + public void setCommands(List commands) { + this.commands = commands; + } + /** + * @return the type + */ + public channelTypes getType() { + return type; + } + /** + * @param type the type to set + */ + public void setType(channelTypes type) { + this.type = type; + } + /** + * @return the channelTag + */ + public String getChannelTag() { + return channelTag; + } + /** + * @param channelTag the channelTag to set + */ + public void setChannelTag(String channelTag) { + this.channelTag = channelTag; + } + /** + * @return the messageColour + */ + public String getMessageColour() { + return messageColour; + } + /** + * @param messageColour the messageColour to set + */ + public void setMessageColour(String messageColour) { + this.messageColour = messageColour; + } + /** + * @return the permission + */ + public String getPermission() { + return permission; + } + /** + * @param permission the permission to set + */ + public void setPermission(String permission) { + this.permission = permission; + } + /** + * @return the range + */ + public double getRange() { + return range; + } + /** + * @param range the range to set + */ + public void setRange(double range) { + this.range = range; + } + + +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java b/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java new file mode 100644 index 0000000..2963c16 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java @@ -0,0 +1,166 @@ +package com.palmergames.bukkit.TownyChat.channels; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bukkit.entity.Player; + +import com.palmergames.bukkit.TownyChat.Chat; +import com.palmergames.bukkit.TownyChat.util.FileMgmt; +import com.palmergames.bukkit.towny.object.TownyUniverse; + +/** + * @author ElgarL + * + */ +public class ChannelsHolder { + + private Chat plugin; + + /** Constructor + * + * @param towny + */ + public ChannelsHolder(Chat plugin) { + super(); + this.plugin = plugin; + } + + + // Container for all channels + private Map channels = new HashMap(); + + /** + * @return the channels + */ + public Map getAllChannels() { + return channels; + } + + /** + * @param channels the channels to set + */ + public void setChannels(HashMap channels) { + this.channels = channels; + } + + public boolean isChannel(String channelName) { + return channels.containsKey(channelName.toLowerCase()); + } + + public Channel getChannel(String channelName) { + return channels.get(channelName.toLowerCase()); + } + + /** + * Get the relevant channel for this command + * if the player is permitted to use it. + * + * @param player + * @param command + * @return channel or null if not permitted + */ + public Channel getChannel(Player player, String command) { + + for (Channel channel: channels.values()) { + if (channel.getCommands().contains(command.toLowerCase())) { + if (!plugin.getTowny().isPermissions() + || (plugin.getTowny().isPermissions() && ((TownyUniverse.getPermissionSource().hasPermission(player, channel.getPermission())) + || (channel.getPermission().isEmpty())))) + return channel; + + } + } + return null; + } + + public Channel getChannel(Player player, channelTypes type) { + + for (Channel channel: channels.values()) { + if (channel.getType().equals(type)) { + if (!plugin.getTowny().isPermissions() + || (plugin.getTowny().isPermissions() && ((TownyUniverse.getPermissionSource().hasPermission(player, channel.getPermission())) + || (channel.getPermission().isEmpty())))) + return channel; + + } + } + return null; + } + + /** + * Load all Channels form the Channels.yml + * If it doesn't exist, create it from + * the resource in this jar. + * + * @param filepath + * @param defaultRes + * @return + */ + @SuppressWarnings("unchecked") + public boolean loadChannels(String filepath, String defaultRes) { + + String filename = filepath + FileMgmt.fileSeparator() + defaultRes; + + Map file = FileMgmt.getFile(filename, defaultRes); + if (file != null) { + + // Parse the channels + Map allChannelNodes = (Map) file.get("Channels"); + + // Load channels if the file is NOT empty + if (allChannelNodes != null) { + for (String channelKey : allChannelNodes.keySet()) { + if (channelKey.equalsIgnoreCase("spam_time")) + plugin.setSpam_time((Long)allChannelNodes.get(channelKey)); + + + Map thisChannelNode = (Map) allChannelNodes.get(channelKey); + Channel channel = new Channel(channelKey.toLowerCase()); + + for (String key : thisChannelNode.keySet()) { + Object element = thisChannelNode.get(key); + + if (key.equalsIgnoreCase("commands")) + if (element instanceof ArrayList) + channel.setCommands((List) element); + else if (element instanceof String) + channel.setCommands(Arrays.asList(element.toString())); + + if (key.equalsIgnoreCase("type")) + if (element instanceof String) + channel.setType(channelTypes.valueOf(element.toString())); + + if (key.equalsIgnoreCase("channeltag")) + if (element instanceof String) + channel.setChannelTag(element.toString()); + + if (key.equalsIgnoreCase("messagecolour")) + if (element instanceof String) + channel.setMessageColour(element.toString()); + + if (key.equalsIgnoreCase("permission")) + if (element instanceof String) + channel.setPermission(element.toString()); + + if (key.equalsIgnoreCase("range")) + channel.setRange(Double.valueOf(element.toString())); + } + + channels.put(channel.getName(), channel); + + //System.out.print("Channel: " + channel.getName() + " : Type : " + channel.getType().name()); + } + return true; + + } + } + return false; + } + + + +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/channels/channelTypes.java b/src/com/palmergames/bukkit/TownyChat/channels/channelTypes.java new file mode 100644 index 0000000..c4eb2b4 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/channels/channelTypes.java @@ -0,0 +1,9 @@ +package com.palmergames.bukkit.TownyChat.channels; + + +public enum channelTypes { + TOWN, + NATION, + DEFAULT, + GLOBAL +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java b/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java index 530a933..c653500 100644 --- a/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java +++ b/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java @@ -10,12 +10,13 @@ import org.bukkit.event.player.PlayerListener; import org.dynmap.DynmapAPI; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.NotRegisteredException; -import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.TownyException; -import com.palmergames.bukkit.towny.TownyLogger; import com.palmergames.bukkit.towny.TownyMessaging; import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.TownyChat.channels.Channel; +import com.palmergames.bukkit.TownyChat.channels.channelTypes; import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; +import com.palmergames.bukkit.TownyChat.Chat; import com.palmergames.bukkit.TownyChat.CraftIRCHandler; import com.palmergames.bukkit.TownyChat.TownyChatFormatter; import com.palmergames.bukkit.towny.object.Nation; @@ -26,14 +27,14 @@ import com.palmergames.bukkit.util.ChatTools; import com.palmergames.util.StringMgmt; public class TownyPlayerHighestListener extends PlayerListener { - private final Towny towny; + private final Chat plugin; private final CraftIRCHandler ircHander; private final DynmapAPI dynMap; private HashMap SpamTime = new HashMap(); - public TownyPlayerHighestListener(Towny instance, CraftIRCHandler irc, DynmapAPI dynMap) { - this.towny = instance; + public TownyPlayerHighestListener(Chat instance, CraftIRCHandler irc, DynmapAPI dynMap) { + this.plugin = instance; this.ircHander = irc; this.dynMap = dynMap; } @@ -50,43 +51,41 @@ public class TownyPlayerHighestListener extends PlayerListener { return; } String split[] = event.getMessage().split("\\ "); - String command = split[0].trim().toLowerCase(); + String command = split[0].trim().toLowerCase().replace("/", ""); String message = ""; if (split.length > 1) message = StringMgmt.join(StringMgmt.remFirstArg(split), " "); - if (TownySettings.chatChannelExists(command)) { - event.setMessage(message); + Channel channel = plugin.getChannelsHandler().getChannel(player, command); + if (channel != null) { + System.out.print("Channel Found"); + event.setMessage(message); - //if not muted and has permission + // if not muted and has permission if (!isMuted(player)) { - + if (isSpam(player)) { event.setCancelled(true); return; } - - if (!towny.isPermissions() || (towny.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(player, TownySettings.getChatChannelPermission(command)))) { + event.setMessage(message); + + // If no message toggle the chat mode. + if (message.isEmpty()) { + if (plugin.getTowny().hasPlayerMode(player, channel.getName())) + plugin.getTowny().removePlayerMode(player); + else + plugin.getTowny().setPlayerMode(player, new String[]{channel.getName()}, true); - event.setMessage(message); - - // If no message toggle the chat mode. - if (message.isEmpty()) { - towny.setPlayerChatMode(player, command.replace("/", "")); - - } else { - // Process the chat - chatProcess(event, command, player); - - } - event.setCancelled(true); - } else { - TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_command_disable")); - event.setCancelled(true); + // Process the chat + chatProcess(event, channel, player); + } + event.setCancelled(true); + } } } @@ -95,86 +94,93 @@ public class TownyPlayerHighestListener extends PlayerListener { public void onPlayerChat(PlayerChatEvent event) { Player player = event.getPlayer(); - + // Check if essentials has this player muted. if (!isMuted(player)) { - + if (isSpam(player)) { event.setCancelled(true); return; } - - for (String channel : TownySettings.getChatChannels()) { - if (towny.hasPlayerMode(player, channel.replace("/", ""))) { - if (!towny.isPermissions() || (towny.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(player, TownySettings.getChatChannelPermission(channel)))) { - // Channel Chat - // Process the chat - chatProcess(event, channel, player); - event.setCancelled(true); - return; - } + + for (Channel channel : plugin.getChannelsHandler().getAllChannels().values()) { + if (plugin.getTowny().hasPlayerMode(player, channel.getName())) { + // Channel Chat mode set + // Process the chat + chatProcess(event, channel, player); + event.setCancelled(true); + return; } } - if (TownySettings.isUsingModifyChat()) { - try { - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatGlobalChannelFormat()); - Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); - - TownyChatEvent chatEvent = new TownyChatEvent(event, resident); - event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); - } catch (NotRegisteredException e) { - // World or resident not registered with Towny - e.printStackTrace(); - } + // Find a global channel this player has permissions for. + Channel channel = plugin.getChannelsHandler().getChannel(player, channelTypes.GLOBAL); + + if (channel != null) { + chatProcess(event, channel, player); + event.setCancelled(true); + return; } - // No modes set so this must be open/global chat. - if (!towny.isPermissions() || (towny.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(player, TownySettings.getChatChannelPermission("/g")))) - parseGlobalChannelChatCommand(event, "/g", player); - else - return; - - } - event.setCancelled(true); + + if (TownySettings.isUsingModifyChat()) { + try { + event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatGlobalChannelFormat()); + Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); + + TownyChatEvent chatEvent = new TownyChatEvent(event, resident); + event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); + } catch (NotRegisteredException e) { + // World or resident not registered with Towny + e.printStackTrace(); + } + } } + + - private void chatProcess(PlayerChatEvent event, String command, Player player) { + private void chatProcess(PlayerChatEvent event, Channel chan, Player player) { - if (command.equalsIgnoreCase("/tc")) { - // Town Chat - parseTownChatCommand(event, "/tc", player); - - } else if (command.equalsIgnoreCase("/nc")) { - // Nation chat - parseNationChatCommand(event, "/nc", player); - - } else if (command.equalsIgnoreCase("/g")) { - // Global chat - parseGlobalChannelChatCommand(event, "/g", player); - - } else { + + channelTypes exec = channelTypes.valueOf(chan.getType().name()); + + switch (exec) { + + case TOWN: + parseTownChatCommand(event, chan, player); + break; + + case NATION: + parseNationChatCommand(event, chan, player); + break; + + case DEFAULT: + parseDefaultChannelChatCommand(event, chan, player); + + break; + + case GLOBAL: + parseGlobalChannelChatCommand(event, chan, player); - // Deal with custom channel cases (mod, admin etc) - parseDefaultChannelChatCommand(event, command, player); } } - private void parseTownChatCommand(PlayerChatEvent event, String command, Player player) { + private void parseTownChatCommand(PlayerChatEvent event, Channel chan, Player player) { try { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Town town = resident.getTown(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatTownChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) + .getChatTownChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); String msg = chatEvent.getFormat().replace("%1$s", event.getPlayer().getDisplayName()).replace("%2$s", event.getMessage()); - //TownyLogger.log.info(ChatTools.stripColour(msg)); + plugin.getLogger().info(ChatTools.stripColour("[Town Msg] " + town.getName() + ": " + msg)); // Relay to IRC if (ircHander != null) @@ -182,24 +188,36 @@ public class TownyPlayerHighestListener extends PlayerListener { sendSpy(player, "[Town Msg] " + town.getName() + ": " + msg); - TownyMessaging.sendTownMessage(town, msg); + // Send the town message + int count = 0; + for (Player test : TownyUniverse.getOnlinePlayers(town)) + if ((testDistance(player, test, chan.getRange())) && (!plugin.getTowny().hasPlayerMode(test, "spy"))) { + count++; + test.sendMessage(msg); + } + + if (count <= 1) + player.sendMessage(TownySettings.parseSingleLineString("&cYou feel so longely.")); + + } catch (NotRegisteredException x) { TownyMessaging.sendErrorMsg(player, x.getError()); } } - private void parseNationChatCommand(PlayerChatEvent event, String command, Player player) { + private void parseNationChatCommand(PlayerChatEvent event, Channel chan, Player player) { try { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Nation nation = resident.getTown().getNation(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatNationChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) + .getChatNationChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); String msg = chatEvent.getFormat().replace("%1$s", event.getPlayer().getDisplayName()).replace("%2$s", event.getMessage()); - //TownyLogger.log.info(ChatTools.stripColour(msg)); + plugin.getLogger().info(ChatTools.stripColour("[Nation Msg] " + nation.getName() + ": " + msg)); // Relay to IRC if (ircHander != null) @@ -207,24 +225,36 @@ public class TownyPlayerHighestListener extends PlayerListener { sendSpy(player, "[Nation Msg] " + nation.getName() + ": " + msg); - TownyMessaging.sendNationMessage(nation, msg); + // Send the town message + int count = 0; + for (Player test : TownyUniverse.getOnlinePlayers(nation)) + if ((testDistance(player, test, chan.getRange())) && (!plugin.getTowny().hasPlayerMode(test, "spy"))) { + count++; + test.sendMessage(msg); + } + + if (count <= 1) + player.sendMessage(TownySettings.parseSingleLineString("&cYou feel so longely.")); + } catch (NotRegisteredException x) { TownyMessaging.sendErrorMsg(player, x.getError()); } } - private void parseDefaultChannelChatCommand(PlayerChatEvent event, String command, Player player) { + private void parseDefaultChannelChatCommand(PlayerChatEvent event, Channel chan, Player player) { try { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); - Boolean bEssentials = towny.isEssentials(); + Boolean bEssentials = plugin.getTowny().isEssentials(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatDefaultChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + if (TownySettings.isUsingModifyChat()) + event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) + .getChatDefaultChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); String msg = chatEvent.getFormat().replace("%1$s", event.getPlayer().getDisplayName()).replace("%2$s", event.getMessage()); - TownyLogger.log.info(ChatTools.stripColour(msg)); + plugin.getLogger().info(ChatTools.stripColour(msg)); // Relay to IRC if (ircHander != null) @@ -232,12 +262,13 @@ public class TownyPlayerHighestListener extends PlayerListener { sendSpy(player, msg); + int count = 0; for (Player test : TownyUniverse.getOnlinePlayers()) { - if (!towny.isPermissions() || (towny.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(test, TownySettings.getChatChannelPermission(command)))) { + if (!plugin.getTowny().isPermissions() || (plugin.getTowny().isPermissions() && TownyUniverse.getPermissionSource().hasPermission(test, chan.getPermission()))) { if (bEssentials) { try { - User targetUser = towny.getEssentials().getUser(test); + User targetUser = plugin.getTowny().getEssentials().getUser(test); // Don't send this message if the user is ignored if (targetUser.isIgnoredPlayer(player.getName())) continue; @@ -245,9 +276,13 @@ public class TownyPlayerHighestListener extends PlayerListener { // Failed to fetch user so ignore. } } - if ((testDistance(player, test, TownySettings.getChatChannelRange(command))) && (!towny.hasPlayerMode(test, "spy"))) + if ((testDistance(player, test, chan.getRange())) && (!plugin.getTowny().hasPlayerMode(test, "spy"))) { + count++; TownyMessaging.sendMessage(test, msg); + } } + if (count <= 1) + player.sendMessage(TownySettings.parseSingleLineString("&cYou feel so longely.")); } } catch (NotRegisteredException x) { @@ -255,20 +290,20 @@ public class TownyPlayerHighestListener extends PlayerListener { } } - private void parseGlobalChannelChatCommand(PlayerChatEvent event, String command, Player player) { + private void parseGlobalChannelChatCommand(PlayerChatEvent event, Channel chan, Player player) { try { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); - Boolean bEssentials = towny.isEssentials(); + Boolean bEssentials = plugin.getTowny().isEssentials(); - if (TownySettings.isUsingModifyChat()) { - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatGlobalChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); - } + if (TownySettings.isUsingModifyChat()) + event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) + .getChatGlobalChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); String msg = chatEvent.getFormat().replace("%1$s", event.getPlayer().getDisplayName()).replace("%2$s", event.getMessage()); - TownyLogger.log.info(ChatTools.stripColour(msg)); + plugin.getLogger().info(ChatTools.stripColour(msg)); // Relay to IRC if (ircHander != null) @@ -279,12 +314,13 @@ public class TownyPlayerHighestListener extends PlayerListener { if (dynMap != null) dynMap.postPlayerMessageToWeb(player, event.getMessage()); + int count = 0; for (Player test : TownyUniverse.getOnlinePlayers()) { - if (!towny.isPermissions() || (towny.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(test, TownySettings.getChatChannelPermission(command)))) { + if (!plugin.getTowny().isPermissions() || (plugin.getTowny().isPermissions() && TownyUniverse.getPermissionSource().hasPermission(test, chan.getPermission()))) { if (bEssentials) { try { - User targetUser = towny.getEssentials().getUser(test); + User targetUser = plugin.getTowny().getEssentials().getUser(test); // Don't send this message if the user is ignored if (targetUser.isIgnoredPlayer(player.getName())) continue; @@ -292,9 +328,13 @@ public class TownyPlayerHighestListener extends PlayerListener { // Failed to fetch user so ignore. } } - if (testDistance(player, test, TownySettings.getChatChannelRange(command)) && (!towny.hasPlayerMode(test, "spy"))) + if (testDistance(player, test, chan.getRange()) && (!plugin.getTowny().hasPlayerMode(test, "spy"))) { + count++; TownyMessaging.sendMessage(test, msg); + } } + if (count <= 1) + player.sendMessage(TownySettings.parseSingleLineString("&cYou feel so longely.")); } // TownyMessaging.sendNationMessage(nation, chatEvent.getFormat()); @@ -306,7 +346,7 @@ public class TownyPlayerHighestListener extends PlayerListener { private void sendSpy(Player player, String msg) { for (Player test : TownyUniverse.getOnlinePlayers()) { - if (towny.hasPlayerMode(test, "spy")) { + if (plugin.getTowny().hasPlayerMode(test, "spy")) { TownyMessaging.sendMessage(test, msg); } } @@ -314,9 +354,9 @@ public class TownyPlayerHighestListener extends PlayerListener { } private boolean isMuted(Player player) { // Check if essentials has this player muted. - if (towny.isEssentials()) { + if (plugin.getTowny().isEssentials()) { try { - if (towny.getEssentials().getUser(player).isMuted()) { + if (plugin.getTowny().getEssentials().getUser(player).isMuted()) { TownyMessaging.sendErrorMsg(player, "Unable to talk...You are currently muted!"); return true; } @@ -345,12 +385,12 @@ public class TownyPlayerHighestListener extends PlayerListener { SpamTime.remove(player); } else { // No record found so ensure we don't trigger for spam - spam -= 3; + spam -= (plugin.getSpam_time() + 1); } SpamTime.put(player, timeNow); - if (timeNow - spam < 2) { + if (timeNow - spam < plugin.getSpam_time()) { TownyMessaging.sendErrorMsg(player, "Unable to talk...You are spamming!"); return true; } diff --git a/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java b/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java new file mode 100644 index 0000000..a44e0a3 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java @@ -0,0 +1,164 @@ +package com.palmergames.bukkit.TownyChat.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Map; + +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; +import org.yaml.snakeyaml.reader.UnicodeReader; + + + +public class FileMgmt { + + public static void checkFolders(String[] folders) { + for (String folder : folders) { + File f = new File(folder); + if (!(f.exists() && f.isDirectory())) + f.mkdir(); + } + } + + @SuppressWarnings("unchecked") + public static Map getFile(String filepath, String resource) { + + try { + File f = new File(filepath); + if (!(f.exists() && f.isFile())) { + // Populate a new file + try { + String resString = convertStreamToString("/" + resource); + FileMgmt.stringToFile(resString, filepath); + + } catch (IOException e) { + // No resource file found + e.printStackTrace(); + return null; + } + } + + f = new File(filepath); + + Yaml yamlChannels = new Yaml(new SafeConstructor()); + Object channelsRootDataNode; + + FileInputStream fileInputStream = new FileInputStream(f); + try { + channelsRootDataNode = yamlChannels.load(new UnicodeReader(fileInputStream)); + if (channelsRootDataNode == null) { + throw new NullPointerException(); + } + } catch (Exception ex) { + throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + f.getPath(), ex); + } finally { + fileInputStream.close(); + } + + if (channelsRootDataNode instanceof Map) + return (Map) channelsRootDataNode; + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + // pass a resource name and it will return it's contents as a string + public static String convertStreamToString(String name) throws IOException { + if (name != null) { + Writer writer = new StringWriter(); + InputStream is = FileMgmt.class.getResourceAsStream(name); + + char[] buffer = new char[1024]; + try { + Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + int n; + while ((n = reader.read(buffer)) != -1) { + writer.write(buffer, 0, n); + } + } catch (IOException e) { + System.out.println("Exception "); + } finally { + try { + is.close(); + } catch (NullPointerException e) { + // Failed to open a stream + throw new IOException(); + } + } + return writer.toString(); + } else { + return ""; + } + } + + /** + * Wrapper for stringToFile writes a string to a file making all newline + * codes platform specific + * + * @param source + * @param FileName + * @return + */ + public static boolean stringToFile(String source, String FileName) { + + if (source != null) { + // Save the string to file (*.yml) + try { + return stringToFile(source, new File(FileName)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return false; + + } + + /** + * Writes the contents of a string to a file. + * + * @param source + * String to write. + * @param file + * File to write to. + * @return True on success. + * @throws IOException + */ + public static boolean stringToFile(String source, File file) throws IOException { + + try { + + OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); + + // BufferedWriter out = new BufferedWriter(new + // FileWriter(FileName)); + + source.replaceAll("\n", System.getProperty("line.separator")); + + out.write(source); + out.close(); + return true; + + } catch (IOException e) { + System.out.println("Exception "); + return false; + } + } + + public static String fileSeparator() { + return System.getProperty("file.separator"); + } +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index a5cf365..7c151ef 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: TownyChat main: com.palmergames.bukkit.TownyChat.Chat -version: 0.11 +version: 0.12 author: ElgarL, Shade website: http://code.google.com/a/eclipselabs.org/p/towny/ description: >