diff --git a/pom.xml b/pom.xml
index 099b710..6e0c865 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
com.palmergames.bukkit
TownyChat
jar
- 0.62
+ 0.63
diff --git a/resources/Channels.yml b/resources/Channels.yml
index ccb83ea..b384291 100644
--- a/resources/Channels.yml
+++ b/resources/Channels.yml
@@ -40,6 +40,7 @@ Channels:
permission: 'towny.chat.general'
default: true
range: '-1'
+ spam_time: '0.5'
town:
commands: [tc]
@@ -48,6 +49,7 @@ Channels:
messagecolour: '&b'
permission: 'towny.chat.town'
range: '-1'
+ spam_time: '0.5'
nation:
commands: [nc]
@@ -56,6 +58,7 @@ Channels:
messagecolour: '&e'
permission: 'towny.chat.nation'
range: '-1'
+ spam_time: '0.5'
alliance:
commands: [ac]
@@ -64,6 +67,7 @@ Channels:
messagecolour: '&a'
permission: 'towny.chat.alliance'
range: '-1'
+ spam_time: '0.5'
admin:
commands: [a,admin]
diff --git a/resources/changelog.txt b/resources/changelog.txt
index f6a1592..1e9178e 100644
--- a/resources/changelog.txt
+++ b/resources/changelog.txt
@@ -271,6 +271,9 @@ v0.62:
messagecolour: '&a'
permission: 'towny.chat.alliance'
range: '-1'
-
-
-
\ No newline at end of file
+v0.63:
+ - Change: spam_time has been moved from the chatconfig.yml, to the channels.yml
+ - You do not have to set a spam_time for each channel, only the ones you want limited.
+ - setting spam_time: '0.5' in your general channel will make the global channel behave as normal.
+ - default channels.yml has spam_time: '0.5' set for general, town, nation and alliance channels; remaining channels have no spam_time set.
+ - in your chatconfig.yml you can remove the "spam_time: '0.5'" line before or after updating.
\ No newline at end of file
diff --git a/src/com/palmergames/bukkit/TownyChat/channels/Channel.java b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java
index efb0507..101c2d2 100644
--- a/src/com/palmergames/bukkit/TownyChat/channels/Channel.java
+++ b/src/com/palmergames/bukkit/TownyChat/channels/Channel.java
@@ -2,11 +2,16 @@ package com.palmergames.bukkit.TownyChat.channels;
import java.util.List;
import java.util.Set;
+import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
+import com.palmergames.bukkit.towny.TownyMessaging;
+import com.palmergames.bukkit.towny.TownySettings;
+
public abstract class Channel {
private String name;
@@ -16,6 +21,8 @@ public abstract class Channel {
private double range;
private boolean hooked=false;
private boolean autojoin=true;
+ private double spamtime;
+ private WeakHashMap Spammers = new WeakHashMap<>();
protected ConcurrentMap absentPlayers = null;
protected ConcurrentMap mutedPlayers = null;
@@ -230,4 +237,42 @@ public abstract class Channel {
public boolean isAutoJoin() {
return autojoin;
}
+
+ public double getSpam_time() {
+ return spamtime;
+ }
+
+ public void setSpam_time(double spamtime) {
+ this.spamtime = spamtime;
+ }
+
+ /**
+ * Test if this player is spamming chat.
+ * One message every 2 seconds limit
+ *
+ * @param player
+ * @return
+ */
+ public boolean isSpam(Player player) {
+
+ long timeNow = System.currentTimeMillis();
+ long spam = timeNow;
+
+ if (Spammers.containsKey(player)) {
+ spam = Spammers.get(player);
+ Spammers.remove(player);
+ } else {
+ // No record found so ensure we don't trigger for spam
+ spam -= ((getSpam_time() + 1)*1000);
+ }
+
+ Spammers.put(player, timeNow);
+
+ if (timeNow - spam < (getSpam_time()*1000)) {
+ TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("tc_err_unable_to_talk_you_are_spamming"));
+ return true;
+ }
+ return false;
+ }
+
}
\ No newline at end of file
diff --git a/src/com/palmergames/bukkit/TownyChat/config/ChatConfigNodes.java b/src/com/palmergames/bukkit/TownyChat/config/ChatConfigNodes.java
index 14d66e0..ef4813f 100644
--- a/src/com/palmergames/bukkit/TownyChat/config/ChatConfigNodes.java
+++ b/src/com/palmergames/bukkit/TownyChat/config/ChatConfigNodes.java
@@ -71,11 +71,6 @@ public enum ChatConfigNodes {
"# -------------",
"# Bold = &l, Italics = &o, Underlined = &n,",
"# Magic = &k, Strike = &m, Reset = &r"),
- SPAM_TIME(
- "spam_time",
- "0.5",
- "",
- "# Spam_time set the minimum time between messages to be flagged as spam"),
CHANNEL_FORMATS(
"channel_formats",
"",
diff --git a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java
index 8e8fc15..d29a7b4 100644
--- a/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java
+++ b/src/com/palmergames/bukkit/TownyChat/config/ChatSettings.java
@@ -18,7 +18,6 @@ import java.util.Map;
public class ChatSettings extends tag_formats {
- private static Double spam_time;
private static boolean modify_chat;
private static boolean per_world;
private static boolean alone_message;
@@ -196,21 +195,6 @@ public class ChatSettings extends tag_formats {
}
- /**
- * @return the spam_time
- */
- public static Double getSpam_time() {
- return spam_time;
- }
-
- /**
- * @param d
- * the spam_time to set
- */
- public static void setSpam_time(Double d) {
- ChatSettings.spam_time = d;
- }
-
/**
* @return the modify_chat
*/
diff --git a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java
index 11d8262..171827d 100644
--- a/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java
+++ b/src/com/palmergames/bukkit/TownyChat/config/ConfigurationHandler.java
@@ -63,9 +63,6 @@ public class ConfigurationHandler {
// Load channels if the file is NOT empty
if (allChannelNodes != null) {
for (String channelKey : allChannelNodes.keySet()) {
- if (channelKey.equalsIgnoreCase("spam_time"))
- ChatSettings.setSpam_time((Double)allChannelNodes.get(channelKey));
-
Map thisChannelNode = (Map) allChannelNodes.get(channelKey);
Channel channel = new StandardChannel(plugin, channelKey.toLowerCase());
@@ -117,6 +114,10 @@ public class ConfigurationHandler {
plugin.getLogger().info("Default Channel set to " + channel.getName());
}
}
+
+ if (key.equalsIgnoreCase("spam_time")) {
+ channel.setSpam_time(Double.valueOf(element.toString()));
+ }
if (key.equalsIgnoreCase("channeltag"))
if (element instanceof String)
@@ -181,9 +182,6 @@ public class ConfigurationHandler {
for (String Key : file.keySet()) {
- if (Key.equalsIgnoreCase("spam_time"))
- ChatSettings.setSpam_time( Double.parseDouble((file.get(Key)).toString()) );
-
if (Key.equalsIgnoreCase("modify_chat")) {
Map subNodes = (Map) file.get(Key);
@@ -300,104 +298,8 @@ public class ConfigurationHandler {
}
}
-
-// 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)? "0.5" : Double.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()));
-// newConfig = newConfig.replace("[modify_alone_message]", (defaults)? "false" : Boolean.toString(ChatSettings.isUsingAloneMessage()));
-// newConfig = newConfig.replace("[modify_alone_message_string]", (defaults)? "No one in range can hear you or you are alone in this channel." : (ChatSettings.getUsingAloneMessageString()));
-//
-// 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/listener/TownyChatPlayerListener.java b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java
index 06e83b0..e67eeb5 100644
--- a/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java
+++ b/src/com/palmergames/bukkit/TownyChat/listener/TownyChatPlayerListener.java
@@ -26,7 +26,6 @@ import java.util.WeakHashMap;
public class TownyChatPlayerListener implements Listener {
private Chat plugin;
- private WeakHashMap SpamTime = new WeakHashMap<>();
public WeakHashMap directedChat = new WeakHashMap<>();
public TownyChatPlayerListener(Chat instance) {
@@ -109,11 +108,6 @@ public class TownyChatPlayerListener implements Listener {
// Check if essentials has this player muted.
if (!isMuted(player)) {
- if (isSpam(player)) {
- event.setCancelled(true);
- return;
- }
-
/*
* If this was directed chat send it via the relevant channel
*/
@@ -128,9 +122,10 @@ public class TownyChatPlayerListener implements Listener {
event.setCancelled(true);
return;
}
-
- // TODO: Give this a toggle in the debug mode, it's highly annoying and spams logs, it's been disabled for now.
-// plugin.getLogger().info("onPlayerChat: Processing directed message for " + channel.getName());
+ if (channel.isSpam(player)) {
+ event.setCancelled(true);
+ return;
+ }
channel.chatProcess(event);
return;
}
@@ -147,6 +142,10 @@ public class TownyChatPlayerListener implements Listener {
event.setCancelled(true);
return;
}
+ if (channel.isSpam(player)) {
+ event.setCancelled(true);
+ return;
+ }
/*
* Channel Chat mode set
* Process the chat
@@ -166,7 +165,10 @@ public class TownyChatPlayerListener implements Listener {
event.setCancelled(true);
return;
}
-
+ if (channel.isSpam(player)) {
+ event.setCancelled(true);
+ return;
+ }
channel.chatProcess(event);
return;
}
@@ -210,34 +212,4 @@ public class TownyChatPlayerListener implements Listener {
}
return false;
}
-
-
- /**
- * Test if this player is spamming chat.
- * One message every 2 seconds limit
- *
- * @param player
- * @return
- */
- private boolean isSpam(Player player) {
-
- long timeNow = System.currentTimeMillis();
- long spam = timeNow;
-
- if (SpamTime.containsKey(player)) {
- spam = SpamTime.get(player);
- SpamTime.remove(player);
- } else {
- // No record found so ensure we don't trigger for spam
- spam -= ((ChatSettings.getSpam_time() + 1)*1000);
- }
-
- SpamTime.put(player, timeNow);
-
- if (timeNow - spam < (ChatSettings.getSpam_time()*1000)) {
- TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("tc_err_unable_to_talk_you_are_spamming"));
- return true;
- }
- return false;
- }
}
\ No newline at end of file