- 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.
This commit is contained in:
Llm Dl committed 2019-11-14 07:05:46 -06:00
1 parent 4e9999019b
commit 78c7bd146b
8 files changed
+72 -167

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@
<groupId>com.palmergames.bukkit</groupId>
<artifactId>TownyChat</artifactId>
<packaging>jar</packaging>
<version>0.62</version>
<version>0.63</version>
<licenses>
<license>
+4
View File
@@ -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]
+6 -3
View File
@@ -271,6 +271,9 @@ v0.62:
messagecolour: '&a'
permission: 'towny.chat.alliance'
range: '-1'
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.
@@ -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<Player, Long> Spammers = new WeakHashMap<>();
protected ConcurrentMap<String, Integer> absentPlayers = null;
protected ConcurrentMap<String, Integer> 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;
}
}
@@ -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",
"",
@@ -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
*/
@@ -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<String, Object> thisChannelNode = (Map<String, Object>) 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<String, Object> subNodes = (Map<String, Object>) 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;
// }
}
@@ -26,7 +26,6 @@ import java.util.WeakHashMap;
public class TownyChatPlayerListener implements Listener {
private Chat plugin;
private WeakHashMap<Player, Long> SpamTime = new WeakHashMap<>();
public WeakHashMap<Player, String> 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;
}
}