From 8a4a49421f000d9cf6a7eebb6847588f8ea79348 Mon Sep 17 00:00:00 2001 From: ElgarL Date: Sun, 15 Jan 2012 22:21:05 +0000 Subject: [PATCH] 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' - Moved ALL chat settings to 'ChatConfig.yml' found in the Towny/Settings folder. --- src/ChatConfig.yml | 81 +++++ src/changelog.txt | 1 + .../palmergames/bukkit/TownyChat/Chat.java | 25 +- .../bukkit/TownyChat/TownyChatFormatter.java | 24 +- .../TownyChat/channels/ChannelsHolder.java | 85 +---- .../TownyChat/channels/channelFormats.java | 108 ++++++ .../bukkit/TownyChat/config/ChatSettings.java | 255 ++++++++++++++ .../config/ConfigurationHandler.java | 324 ++++++++++++++++++ .../event/TownyPlayerHighestListener.java | 31 +- .../bukkit/TownyChat/tasks/onLoadedTask.java | 8 + .../bukkit/TownyChat/util/FileMgmt.java | 8 +- 11 files changed, 828 insertions(+), 122 deletions(-) create mode 100644 src/ChatConfig.yml create mode 100644 src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java create mode 100644 src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java create mode 100644 src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java diff --git a/src/ChatConfig.yml b/src/ChatConfig.yml new file mode 100644 index 0000000..ef68543 --- /dev/null +++ b/src/ChatConfig.yml @@ -0,0 +1,81 @@ + # The formats below will specify the changes made to the player chat when talking. + # These keys can be used in other plugins that modify the chat format like iChat. + + # {worldname} - Displays the world the player is currently in. + # {town} - Displays town name if a member of a town. + # {townformatted} - Displays town name (if a member of a town) using tag_format.town. + # {towntag} - Displays the formated town tag (if a member of a town) using modify_chat.tag_format.town. + # {towntagoverride} - Displays the formated town tag (if a member of a town and present) or falls back to the full name (using modify_chat.tag_format.town). + + # {nation} - Displays nation name if a member of a nation. + # {nationformatted} - Displays nation name (if a member of a nation) using tag_format.town. + # {nationtag} - Displays the formated nation tag (if a member of a nation) using modify_chat.tag_format.nation. + # {nationtagoverride} - Displays the formated nation tag (if a member of a nation and present) or falls back to the full name (using modify_chat.tag_format.nation). + + # {townytag} - Displays the formated town/nation tag as specified in modify_chat.tag_format.both. + # {townyformatted} - Displays the formated full town/nation names as specified in modify_chat.tag_format.both. + # {townytagoverride} - Displays the formated town/nation tag (if present) or falls back to the full names (using modify_chat.tag_format.both). + + # {title} - Towny resident Title + # {surname} - Towny resident surname + # {townynameprefix} - Towny name prefix taken from the townLevel/nationLevels + # {townynamepostfix} - Towny name postfix taken from the townLevel/nationLevels. + # {townyprefix} - Towny resident title, or townynameprefix if no title exists + # {townypostfix} - Towny resident surname, or townynamepostfix if no surname exists + + # {townycolor} - Towny name colour for king/mayor/resident + # {group} - Players group name pulled from your permissions plugin + # {permprefix} - Permission group prefix + # {permsuffix} - Permission group suffix. + + # {playername} - Default player name. + # {modplayername} - Modified player name (use if Towny is over writing some other plugins changes). + # {msg} - The message sent. + + # {channelTag} - Defined in the channels entry in Channels.yml + # {msgcolour} - Defined in the channels entry in Channels.yml + + # 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: [spam_time] + + channel_formats: + # This is the format which will be used for GLOBAL chat/channels. + # This is also the format used when you have modify_chat.enable: true, but use other plugins to handle chat. + global: '[globalformat]' + # TOWN channel types. + town: '[townformat]' + # NATION channel types. + nation: '[nationformat]' + # DEFAULT channel types. + default: '[defaultformat]' + + tag_formats: + world: '[tag_world]' + town: '[tag_town]' + nation: '[tag_nation]' + # First %s is the nation tag, second is the town tag. + both: '[tag_both]' + + # Chat colours + colour: + king: '[colour_king]' + mayor: '[colour_mayor]' + resident: '[colour_resident]' + + modify_chat: + # This is used for when you don't have another plugin to format chat. + # or you want some other plugin to handle open chat, but want Towny formatting. + # It will cause Towny to overwrite any other plugins alterations to the format. + enable: '[modify_enable]' + # If true the chat formats will be read from below to allow per world formatting. + # These can then be altered individually. + per_world: '[modify_per_world]' + + worlds: diff --git a/src/changelog.txt b/src/changelog.txt index b4f10a6..9afab70 100644 --- a/src/changelog.txt +++ b/src/changelog.txt @@ -72,4 +72,5 @@ v0.12: - 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' + - Moved ALL chat settings to 'ChatConfig.yml' found in the Towny/Settings folder. \ 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 388767c..0e599a0 100644 --- a/src/com/palmergames/bukkit/TownyChat/Chat.java +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -12,6 +12,7 @@ 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.config.ConfigurationHandler; import com.palmergames.bukkit.TownyChat.event.TownyPlayerHighestListener; import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; import com.palmergames.bukkit.TownyChat.util.FileMgmt; @@ -30,6 +31,7 @@ public class Chat extends JavaPlugin { private Logger logger = Logger.getLogger("com.palmergames.bukkit.TownyChat"); private TownyPlayerHighestListener TownyPlayerListener; private ChannelsHolder channels = new ChannelsHolder(this); + private ConfigurationHandler configuration = new ConfigurationHandler(this); protected PluginManager pm; private Towny towny = null; @@ -37,8 +39,6 @@ public class Chat extends JavaPlugin { private DynmapAPI dynMap = null; private CraftIRCHandler irc = null; - - private long spam_time = 2; @Override public void onEnable() { @@ -52,7 +52,7 @@ public class Chat extends JavaPlugin { * depends bug. */ if ((towny == null) || (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1) - || (!loadChannels())) { + || (!load())) { /* * We either failed to find Towny or the Scheduler failed to * register the task. @@ -65,9 +65,9 @@ public class Chat extends JavaPlugin { } - private boolean loadChannels() { + private boolean load() { FileMgmt.checkFolders(new String[] { getRootPath(), getChannelsPath() }); - return channels.loadChannels(getChannelsPath(), "Channels.yml"); + return configuration.loadChannels(getChannelsPath(), "/Channels.yml"); } @Override @@ -112,6 +112,8 @@ public class Chat extends JavaPlugin { pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, TownyPlayerListener, Priority.Highest, this); } + + public String getRootPath() { return getTowny().getDataFolder().getPath(); } @@ -128,17 +130,10 @@ public class Chat extends JavaPlugin { } /** - * @return the spam_time + * @return the data */ - 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 ConfigurationHandler getConfigurationHandler() { + return configuration; } public Logger getLogger() { diff --git a/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java index 5f881fe..366ec26 100644 --- a/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java +++ b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java @@ -4,11 +4,11 @@ import java.util.regex.Pattern; import com.palmergames.bukkit.towny.NotRegisteredException; import com.palmergames.bukkit.towny.TownyFormatter; -import com.palmergames.bukkit.towny.TownySettings; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.TownyUniverse; +import com.palmergames.bukkit.TownyChat.config.ChatSettings; import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; import com.palmergames.bukkit.TownyChat.util.StringReplaceManager; @@ -19,7 +19,7 @@ public class TownyChatFormatter { replacer.registerFormatReplacement(Pattern.quote("{worldname}"), new TownyChatReplacerCallable() { @Override public String call(String match, TownyChatEvent event) throws Exception { - return String.format(TownySettings.getChatWorldFormat(), event.getEvent().getPlayer().getWorld().getName()); + return String.format(ChatSettings.getWorldTag(), event.getEvent().getPlayer().getWorld().getName()); } }); replacer.registerFormatReplacement(Pattern.quote("{town}"), new TownyChatReplacerCallable() { @@ -131,7 +131,7 @@ public class TownyChatFormatter { replacer.registerFormatReplacement(Pattern.quote("{townycolor}"), new TownyChatReplacerCallable() { @Override public String call(String match, TownyChatEvent event) throws Exception { - return event.getResident().isMayor() ? (event.getResident().isKing() ? TownySettings.getKingColour() : TownySettings.getMayorColour()) : ""; + return event.getResident().isMayor() ? (event.getResident().isKing() ? ChatSettings.getKingColour() : ChatSettings.getMayorColour()) : ""; } }); replacer.registerFormatReplacement(Pattern.quote("{group}"), new TownyChatReplacerCallable() { @@ -228,13 +228,13 @@ public class TownyChatFormatter { // Output depending on what tags are present if ((!tTag.isEmpty()) && (!nTag.isEmpty())) - return String.format(TownySettings.getChatTownNationTagFormat(), nTag, tTag); + return String.format(ChatSettings.getBothTags(), nTag, tTag); if (!nTag.isEmpty()) - return String.format(TownySettings.getChatNationTagFormat(), nTag); + return String.format(ChatSettings.getNationTag(), nTag); if (!tTag.isEmpty()) - return String.format(TownySettings.getChatTownTagFormat(), tTag); + return String.format(ChatSettings.getTownTag(), tTag); } } catch (NotRegisteredException e) { @@ -247,11 +247,11 @@ public class TownyChatFormatter { try { if (resident.hasTown()) if (full) - return String.format(TownySettings.getChatTownTagFormat(), resident.getTown().getName()); + return String.format(ChatSettings.getTownTag(), resident.getTown().getName()); else if (resident.getTown().hasTag()) - return String.format(TownySettings.getChatTownTagFormat(), resident.getTown().getTag()); + return String.format(ChatSettings.getTownTag(), resident.getTown().getTag()); else if (override) - return String.format(TownySettings.getChatTownTagFormat(), resident.getTown().getName()); + return String.format(ChatSettings.getTownTag(), resident.getTown().getName()); } catch (NotRegisteredException e) { } @@ -262,11 +262,11 @@ public class TownyChatFormatter { try { if (resident.hasNation()) if (full) - return String.format(TownySettings.getChatNationTagFormat(), resident.getTown().getNation().getName()); + return String.format(ChatSettings.getNationTag(), resident.getTown().getNation().getName()); else if (resident.getTown().getNation().hasTag()) - return String.format(TownySettings.getChatNationTagFormat(), resident.getTown().getNation().getTag()); + return String.format(ChatSettings.getNationTag(), resident.getTown().getNation().getTag()); else if (override) - return String.format(TownySettings.getChatNationTagFormat(), resident.getTown().getNation().getName()); + return String.format(ChatSettings.getNationTag(), resident.getTown().getNation().getName()); } catch (NotRegisteredException e) { } diff --git a/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java b/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java index 2963c16..568d102 100644 --- a/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java +++ b/src/com/palmergames/bukkit/TownyChat/channels/ChannelsHolder.java @@ -1,15 +1,11 @@ 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; /** @@ -22,7 +18,7 @@ public class ChannelsHolder { /** Constructor * - * @param towny + * @param plugin */ public ChannelsHolder(Chat plugin) { super(); @@ -47,6 +43,13 @@ public class ChannelsHolder { this.channels = channels; } + public void addChannel(Channel chan) { + if (isChannel(chan.getName())) + channels.remove(chan); + + channels.put(chan.getName(), chan); + } + public boolean isChannel(String channelName) { return channels.containsKey(channelName.toLowerCase()); } @@ -90,77 +93,5 @@ public class ChannelsHolder { } 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/channelFormats.java b/src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java new file mode 100644 index 0000000..b6d9df1 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/channels/channelFormats.java @@ -0,0 +1,108 @@ +package com.palmergames.bukkit.TownyChat.channels; + + + +public class channelFormats { + + /** + * Constructor + * + * @param name + */ + public channelFormats(String name) { + super(); + this.name = name.toLowerCase(); + } + + private String name, GLOBAL, TOWN, NATION, DEFAULT; + + /** + * @return a clone of this channelFormats + */ + public channelFormats clone(String name) { + + channelFormats clone = new channelFormats(name.toLowerCase()); + + clone.setGLOBAL(this.getGLOBAL()); + clone.setTOWN(this.getTOWN()); + clone.setNATION(this.getNATION()); + clone.setDEFAULT(this.getDEFAULT()); + + return clone; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name + * the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the gLOBAL + */ + public String getGLOBAL() { + return GLOBAL; + } + + /** + * @param GLOBAL + * the gLOBAL to set + */ + public void setGLOBAL(String GLOBAL) { + this.GLOBAL = GLOBAL; + } + + /** + * @return the TOWN + */ + public String getTOWN() { + return this.TOWN; + } + + /** + * @param TOWN + * the TOWN to set + */ + public void setTOWN(String TOWN) { + this.TOWN = TOWN; + } + + /** + * @return the NATION + */ + public String getNATION() { + return this.NATION; + } + + /** + * @param NATION + * the nATION to set + */ + public void setNATION(String NATION) { + this.NATION = NATION; + } + + /** + * @return the DEFAULT + */ + public String getDEFAULT() { + return this.DEFAULT; + } + + /** + * @param DEFAULT + * the dEFAULT to set + */ + public void setDEFAULT(String DEFAULT) { + this.DEFAULT = DEFAULT; + } +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java new file mode 100644 index 0000000..638965e --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java @@ -0,0 +1,255 @@ +package com.palmergames.bukkit.TownyChat.config; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.entity.Player; + +import com.palmergames.bukkit.TownyChat.channels.channelFormats; +import com.palmergames.bukkit.towny.object.TownyUniverse; +import com.palmergames.bukkit.towny.object.TownyWorld; + + +public class ChatSettings extends tag_formats { + + private static long spam_time; + private static boolean modify_chat; + private static boolean per_world; + + private static Map formatGroups = new HashMap(); + + /** + * @return the formatGroups + */ + public static Map getFormatGroups() { + return formatGroups; + } + + /** + * @param formatGroups + * the formatGroups to set + */ + public static void setFormatGroups(Map formatGroups) { + ChatSettings.formatGroups = formatGroups; + } + + /** + * @return the formatGroup + */ + public static channelFormats getFormatGroup(String name) { + return formatGroups.get(name.toLowerCase()); + } + + public static boolean hasFormatGroup(String groupName) { + return formatGroups.containsKey(groupName.toLowerCase()); + } + + public static void addFormatGroup(channelFormats group) { + if (hasFormatGroup(group.getName())) + formatGroups.remove(group.getName()); + + formatGroups.put(group.getName(), group); + } + + public static void removeFormatGroup(String name) { + if (hasFormatGroup(name.toLowerCase())) + formatGroups.remove(name.toLowerCase()); + } + + public static channelFormats getRelevantFormatGroup (Player player) { + + if (isPer_world()) { + String name = player.getWorld().getName(); + if (hasFormatGroup(name)) + return getFormatGroup(name); + } + + return getFormatGroup("channel_formats"); + + } + + /** + * @return the spam_time + */ + public static long getSpam_time() { + return spam_time; + } + + /** + * @param spam_time + * the spam_time to set + */ + public static void setSpam_time(long spam_time) { + ChatSettings.spam_time = spam_time; + } + + /** + * @return the modify_chat + */ + public static boolean isModify_chat() { + return modify_chat; + } + + /** + * @param modify_chat + * the modify_chat to set + */ + public static void setModify_chat(boolean modify_chat) { + ChatSettings.modify_chat = modify_chat; + } + + /** + * @return the per_world + */ + public static boolean isPer_world() { + return per_world; + } + + /** + * @param per_world + * the per_world to set + */ + public static void setPer_world(boolean per_world) { + ChatSettings.per_world = per_world; + } + + /** + * Adds customizable channel formats for each world. + * + * @return true if new worlds we're found + */ + public static boolean populateWorldFormats() { + + boolean updated = false; + + for (TownyWorld world : TownyUniverse.getDataSource().getWorlds()) { + if (!hasFormatGroup(world.getName())) { + addFormatGroup(getFormatGroup("channel_formats").clone(world.getName())); + updated = true; + } + } + + return updated; + + } + +} +////////////////////////////////////////////////// + +class tag_formats extends chat_colours{ + + private static String WorldTag, TownTag, NationTag, BothTags; + + /** + * @return the WORLD + */ + public static String getWorldTag() { + return WorldTag; + } + + /** + * @param WORLD + * the wORLD to set + */ + public static void setWorldTag(String tag) { + tag_formats.WorldTag = tag; + } + + /** + * @return the TOWN + */ + public static String getTownTag() { + return TownTag; + } + + /** + * @param TOWN + * the TOWN to set + */ + public static void setTownTag(String tag) { + tag_formats.TownTag = tag; + } + + /** + * @return the NATION + */ + public static String getNationTag() { + return NationTag; + } + + /** + * @param NATION + * the NATION to set + */ + public static void setNationTag(String tag) { + tag_formats.NationTag = tag; + } + + /** + * @return the BOTH + */ + public static String getBothTags() { + return BothTags; + } + + /** + * @param BOTH + * the BOTH to set + */ + public static void setBothTags(String tag) { + tag_formats.BothTags = tag; + } + +} + +class chat_colours { + + private static String KING, MAYOR, RESIDENT; + + /** + * @return the kING + */ + public static String getKingColour() { + return KING; + } + + /** + * @param KING + * the KING to set + */ + public static void setKingColour(String colour) { + chat_colours.KING = colour; + } + + /** + * @return the MAYOR + */ + public static String getMayorColour() { + return MAYOR; + } + + /** + * @param MAYOR + * the MAYOR to set + */ + public static void setMayorColour(String colour) { + chat_colours.MAYOR = colour; + } + + /** + * @return the RESIDENT + */ + public static String getResidentColour() { + return RESIDENT; + } + + /** + * @param RESIDENT + * the RESIDENT to set + */ + public static void setResidentColour(String tag) { + chat_colours.RESIDENT = tag; + } + +} + diff --git a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java new file mode 100644 index 0000000..95abab5 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java @@ -0,0 +1,324 @@ +package com.palmergames.bukkit.TownyChat.config; + +import java.io.File; +import java.io.IOException; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + + +import com.palmergames.bukkit.TownyChat.Chat; +import com.palmergames.bukkit.TownyChat.channels.Channel; +import com.palmergames.bukkit.TownyChat.channels.channelFormats; +import com.palmergames.bukkit.TownyChat.channels.channelTypes; +import com.palmergames.bukkit.TownyChat.util.FileMgmt; + + + +public class ConfigurationHandler { + + private Chat plugin; + + /** Constructor + * + * @param plugin + */ + public ConfigurationHandler(Chat plugin) { + super(); + this.plugin = plugin; + } + + + /** + * 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 true if the channels were loaded + */ + @SuppressWarnings("unchecked") + public boolean loadChannels(String filepath, String defaultRes) { + + String filename = filepath + FileMgmt.fileSeparator() + defaultRes; + + Map file = FileMgmt.getFile(filename, defaultRes, null); + 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")) + ChatSettings.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())); + } + + plugin.getChannelsHandler().addChannel(channel); + + //System.out.print("Channel: " + channel.getName() + " : Type : " + channel.getType().name()); + } + return true; + + } + } + return false; + } + + /** + * Load the ChatConfig.yml + * + * If it doesn't exist, create it from the resource in this jar. + * + * @param filepath + * @param defaultRes + * @return true if the config was loaded + */ + @SuppressWarnings({ "unchecked" }) + public boolean loadConfig(String filepath, String defaultRes) { + + String filename = filepath + FileMgmt.fileSeparator() + defaultRes; + + // Pass the plugin reference so it can load the defaults if needed. + Map file = FileMgmt.getFile(filename, defaultRes, plugin); + + if (file != null) { + + for (String Key : file.keySet()) { + + if (Key.equalsIgnoreCase("spam_time")) + ChatSettings.setSpam_time( Long.parseLong((file.get(Key)).toString()) ); + + if (Key.equalsIgnoreCase("modify_chat")) { + Map subNodes = (Map) file.get(Key); + + for (String element : subNodes.keySet()) { + if (element.equalsIgnoreCase("enable")) + ChatSettings.setModify_chat(Boolean.valueOf(subNodes.get(element).toString())); + + if (element.equalsIgnoreCase("per_world")) + ChatSettings.setPer_world(Boolean.valueOf(subNodes.get(element).toString())); + } + + } + + if (Key.equalsIgnoreCase("colour")) { + Map subNodes = (Map) file.get(Key); + + for (String element : subNodes.keySet()) { + if (element.equalsIgnoreCase("king")) + ChatSettings.setKingColour(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("mayor")) + ChatSettings.setMayorColour(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("resident")) + ChatSettings.setResidentColour(subNodes.get(element).toString()); + } + + } + + if (Key.equalsIgnoreCase("tag_formats")) { + Map subNodes = (Map) file.get(Key); + + for (String element : subNodes.keySet()) { + if (element.equalsIgnoreCase("world")) + ChatSettings.setWorldTag(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("town")) + ChatSettings.setTownTag(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("nation")) + ChatSettings.setNationTag(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("both")) + ChatSettings.setBothTags(subNodes.get(element).toString()); + } + + } + + if (Key.equalsIgnoreCase("channel_formats")) { + Map subNodes = (Map) file.get(Key); + + channelFormats group = new channelFormats(Key); + + for (String element : subNodes.keySet()) { + if (element.equalsIgnoreCase("global")) + group.setGLOBAL(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("town")) + group.setTOWN(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("nation")) + group.setNATION(subNodes.get(element).toString()); + + if (element.equalsIgnoreCase("default")) + group.setDEFAULT(subNodes.get(element).toString()); + } + ChatSettings.addFormatGroup(group); + + } + + if (Key.equalsIgnoreCase("worlds")) { + Map allWorlds = (Map) file.get(Key); + + for (String worlds : allWorlds.keySet()) { + Map world = (Map) allWorlds.get(worlds); + + channelFormats group = new channelFormats(worlds); + + for (String element : world.keySet()) { + if (element.equalsIgnoreCase("global")) + group.setGLOBAL(world.get(element).toString()); + + if (element.equalsIgnoreCase("town")) + group.setTOWN(world.get(element).toString()); + + if (element.equalsIgnoreCase("nation")) + group.setNATION(world.get(element).toString()); + + if (element.equalsIgnoreCase("default")) + group.setDEFAULT(world.get(element).toString()); + } + System.out.print("Adding: " + group.getName()); + ChatSettings.addFormatGroup(group); + } + + } + + } + + if (ChatSettings.populateWorldFormats()) + saveConfig(filename); + + return true; + + } + + return false; + } + + private void saveConfig(String filepath) { + + File file = new File(filepath); + + if (file.exists() && file.isFile()) { + + String newConfig; + try { + newConfig = FileMgmt.convertStreamToString("/ChatConfig.yml"); + newConfig = setConfigs(newConfig, false); + + FileMgmt.stringToFile(newConfig, filepath); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + } else { + // Big error. No file found. + } + + } + + /** + * Build the new Config file to save. + * + * @param newConfig + * @param defaults apply defaults or data from settings. + * @return the config in a string + */ + public String setConfigs(String newConfig, boolean defaults) { + + String global = "{channelTag} {worldname}{townytagoverride}{townycolor}{permprefix}{group} {townyprefix}{modplayername}{townypostfix}{permsuffix}&f: {msgcolour}{msg}"; + String town = "{channelTag} {townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f: {msgcolour}{msg}"; + String nation = "{channelTag}{towntagoverride}{townycolor}{permprefix}{townyprefix}{playername}{townypostfix}{permsuffix}&f: {msgcolour}{msg}"; + String default_ = "{channelTag} {permprefix}{playername}{permsuffix}&f: {msgcolour}{msg}"; + + String tag_world = "&f[&f%s&f] "; + String tag_town = "&f[&3%s&f] "; + String tag_nation = "&f[&e%s&f] "; + String tag_both = "&f[&6%s&f|&3%s&f] "; + + String king = "&6"; + String mayor = "&b"; + String resident = "&f"; + + + newConfig = newConfig.replace("[spam_time]", (defaults)? "2" : Long.toString(ChatSettings.getSpam_time())); + + newConfig = newConfig.replace("[globalformat]", (defaults)? global : ChatSettings.getFormatGroup("channel_formats").getGLOBAL()); + newConfig = newConfig.replace("[townformat]", (defaults)? town : ChatSettings.getFormatGroup("channel_formats").getTOWN()); + newConfig = newConfig.replace("[nationformat]", (defaults)? nation : ChatSettings.getFormatGroup("channel_formats").getNATION()); + newConfig = newConfig.replace("[defaultformat]", (defaults)? default_ : ChatSettings.getFormatGroup("channel_formats").getDEFAULT()); + + newConfig = newConfig.replace("[tag_world]", (defaults)? tag_world : ChatSettings.getWorldTag()); + newConfig = newConfig.replace("[tag_town]", (defaults)? tag_town : ChatSettings.getTownTag()); + newConfig = newConfig.replace("[tag_nation]", (defaults)? tag_nation : ChatSettings.getNationTag()); + newConfig = newConfig.replace("[tag_both]", (defaults)? tag_both : ChatSettings.getBothTags()); + + newConfig = newConfig.replace("[colour_king]", (defaults)? king : ChatSettings.getKingColour()); + newConfig = newConfig.replace("[colour_mayor]", (defaults)? mayor : ChatSettings.getMayorColour()); + newConfig = newConfig.replace("[colour_resident]", (defaults)? resident : ChatSettings.getResidentColour()); + + newConfig = newConfig.replace("[modify_enable]", (defaults)? "true" : Boolean.toString(ChatSettings.isModify_chat())); + newConfig = newConfig.replace("[modify_per_world]", (defaults)? "false" : Boolean.toString(ChatSettings.isPer_world())); + + for (String key : ChatSettings.getFormatGroups().keySet()) { + if (!key.equalsIgnoreCase("channel_formats")) { + channelFormats world = ChatSettings.getFormatGroup(key); + + newConfig += " " + key + ":" + System.getProperty("line.separator"); + + newConfig += " global: '" + ((defaults)? global : world.getGLOBAL()) + "'" + System.getProperty("line.separator"); + newConfig += " town: '" + ((defaults)? town : world.getTOWN()) + "'" + System.getProperty("line.separator"); + newConfig += " nation: '" + ((defaults)? nation : world.getNATION()) + "'" + System.getProperty("line.separator"); + newConfig += " default: '" + ((defaults)? default_ : world.getDEFAULT()) + "'" + System.getProperty("line.separator"); + } + + } + + + return newConfig; + } + +} \ 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 1313989..4c8f997 100644 --- a/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java +++ b/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java @@ -15,6 +15,7 @@ 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.config.ChatSettings; import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; import com.palmergames.bukkit.TownyChat.Chat; import com.palmergames.bukkit.TownyChat.CraftIRCHandler; @@ -124,9 +125,9 @@ public class TownyPlayerHighestListener extends PlayerListener { } - if (TownySettings.isUsingModifyChat()) { + if (ChatSettings.isModify_chat()) { try { - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).getChatGlobalChannelFormat()); + event.setFormat(ChatSettings.getRelevantFormatGroup(player).getGLOBAL()); Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); @@ -173,8 +174,7 @@ public class TownyPlayerHighestListener extends PlayerListener { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Town town = resident.getTown(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) - .getChatTownChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); + event.setFormat(ChatSettings.getRelevantFormatGroup(player).getTOWN().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); @@ -201,7 +201,7 @@ public class TownyPlayerHighestListener extends PlayerListener { } catch (NotRegisteredException x) { - TownyMessaging.sendErrorMsg(player, x.getError()); + TownyMessaging.sendErrorMsg(player, x.getMessage()); } } @@ -210,8 +210,7 @@ public class TownyPlayerHighestListener extends PlayerListener { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Nation nation = resident.getTown().getNation(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) - .getChatNationChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); + event.setFormat(ChatSettings.getRelevantFormatGroup(player).getNATION().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); @@ -237,7 +236,7 @@ public class TownyPlayerHighestListener extends PlayerListener { player.sendMessage(TownySettings.parseSingleLineString("&cYou feel so lonely.")); } catch (NotRegisteredException x) { - TownyMessaging.sendErrorMsg(player, x.getError()); + TownyMessaging.sendErrorMsg(player, x.getMessage()); } } @@ -246,8 +245,7 @@ public class TownyPlayerHighestListener extends PlayerListener { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Boolean bEssentials = plugin.getTowny().isEssentials(); - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) - .getChatDefaultChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); + event.setFormat(ChatSettings.getRelevantFormatGroup(player).getDEFAULT().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); @@ -285,7 +283,7 @@ public class TownyPlayerHighestListener extends PlayerListener { } } catch (NotRegisteredException x) { - TownyMessaging.sendErrorMsg(player, x.getError()); + TownyMessaging.sendErrorMsg(player, x.getMessage()); } } @@ -294,9 +292,8 @@ public class TownyPlayerHighestListener extends PlayerListener { Resident resident = TownyUniverse.getDataSource().getResident(player.getName()); Boolean bEssentials = plugin.getTowny().isEssentials(); - if (TownySettings.isUsingModifyChat()) - event.setFormat(TownyUniverse.getDataSource().getWorld(player.getWorld().getName()) - .getChatGlobalChannelFormat().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); + if (ChatSettings.isModify_chat()) + event.setFormat(ChatSettings.getRelevantFormatGroup(player).getGLOBAL().replace("{channelTag}", chan.getChannelTag()).replace("{msgcolour}", chan.getMessageColour())); TownyChatEvent chatEvent = new TownyChatEvent(event, resident); event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); @@ -338,7 +335,7 @@ public class TownyPlayerHighestListener extends PlayerListener { // TownyMessaging.sendNationMessage(nation, chatEvent.getFormat()); } catch (NotRegisteredException x) { - TownyMessaging.sendErrorMsg(player, x.getError()); + TownyMessaging.sendErrorMsg(player, x.getMessage()); } } @@ -384,12 +381,12 @@ public class TownyPlayerHighestListener extends PlayerListener { SpamTime.remove(player); } else { // No record found so ensure we don't trigger for spam - spam -= (plugin.getSpam_time() + 1); + spam -= (ChatSettings.getSpam_time() + 1); } SpamTime.put(player, timeNow); - if (timeNow - spam < plugin.getSpam_time()) { + if (timeNow - spam < ChatSettings.getSpam_time()) { TownyMessaging.sendErrorMsg(player, "Unable to talk...You are spamming!"); return true; } diff --git a/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java b/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java index 2ea0f53..9fd3ad3 100644 --- a/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java +++ b/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java @@ -24,6 +24,14 @@ public class onLoadedTask implements Runnable { public void run() { if (this.towny.isEnabled()) { + // Setup our config here as we now have Towny loaded. + if (this.towny.isError() || (!plugin.getConfigurationHandler().loadConfig(plugin.getChannelsPath(), "ChatConfig.yml"))) { + + plugin.getLogger().severe("disabling TownyChat"); + plugin.getServer().getPluginManager().disablePlugin(plugin); + return; + } + plugin.getLogger().info("-******* TownyChat enabled *******-"); plugin.registerEvents(); } diff --git a/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java b/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java index a44e0a3..1122b12 100644 --- a/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java +++ b/src/com/palmergames/bukkit/TownyChat/util/FileMgmt.java @@ -17,6 +17,8 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.reader.UnicodeReader; +import com.palmergames.bukkit.TownyChat.Chat; + public class FileMgmt { @@ -30,7 +32,7 @@ public class FileMgmt { } @SuppressWarnings("unchecked") - public static Map getFile(String filepath, String resource) { + public static Map getFile(String filepath, String resource, Chat plugin) { try { File f = new File(filepath); @@ -38,6 +40,10 @@ public class FileMgmt { // Populate a new file try { String resString = convertStreamToString("/" + resource); + // If we have a plugin reference pass to load default. + if (plugin != null) + resString = plugin.getConfigurationHandler().setConfigs(resString, true); + FileMgmt.stringToFile(resString, filepath); } catch (IOException e) {