Don't allow sending messages too fast

The player needs to wait the configured amount of milliseconds between messages
#115
This commit is contained in:
Norbi Peti 2020-03-03 16:13:39 +01:00
parent 661534b92d
commit 03b91d2fdb
3 changed files with 31 additions and 15 deletions

View file

@ -51,6 +51,7 @@ public class ChatPlayer extends TBMCPlayerBase {
public boolean RainbowPresserColorMode = false;
public Color OtherColorMode = null;
public boolean ChatOnly = false;
public long LastMessageTime = 0L;
public static final int FlairTimeNonPresser = -1;
public static final int FlairTimeCantPress = -2;
@ -59,8 +60,7 @@ public class ChatPlayer extends TBMCPlayerBase {
/**
* Gets the player's flair, optionally formatting for Minecraft.
*
* @param noformats
* The MC formatting codes will be only applied if false
* @param noformats The MC formatting codes will be only applied if false
* @return The flair
*/
public String GetFormattedFlair(boolean noformats) {
@ -100,7 +100,7 @@ public class ChatPlayer extends TBMCPlayerBase {
// PluginMain.Instance.getServer().getScoreboardManager().getMainScoreboard().getTeams().add()
Player p = Bukkit.getPlayer(uuid);
if (p != null)
p.setPlayerListName(String.format("%s%s", p.getDisplayName(), GetFormattedFlair()));
p.setPlayerListName(String.format("%s%s", p.getDisplayName(), GetFormattedFlair()));
}
public short GetFlairColor() {

View file

@ -134,20 +134,29 @@ public class ChatProcessing {
return true;
}
doFunStuff(sender, e, message);
final String channelidentifier = getChannelID(channel, e.getOrigin());
PluginMain.Instance.getServer().getConsoleSender()
.sendMessage(String.format("%s <%s§r> %s", channelidentifier, getSenderName(sender, player), message));
if (Bukkit.getOnlinePlayers().size() == 0) return false; //Don't try to send to nobody (errors on 1.14)
ChatPlayer mp;
if (player != null)
mp = TBMCPlayerBase.getPlayer(player.getUniqueId(), ChatPlayer.class);
else //Due to the online player map, getPlayer() can be more efficient than getAs()
mp = e.getUser().getAs(ChatPlayer.class); //May be null
if (mp != null) {
if (System.nanoTime() - mp.LastMessageTime < 1000 * component.minTimeBetweenMessages().get()) { //0.1s by default
sender.sendMessage("§cYou are sending messages too fast!");
return true;
}
mp.LastMessageTime = System.nanoTime();
}
doFunStuff(sender, e, message);
final String channelidentifier = getChannelID(channel, e.getOrigin());
PluginMain.Instance.getServer().getConsoleSender()
.sendMessage(String.format("%s <%s§r> %s", channelidentifier, getSenderName(sender, player), message));
if (Bukkit.getOnlinePlayers().size() == 0) return false; //Don't try to send to nobody (errors on 1.14)
Color colormode = channel.Color().get();
if (mp != null && mp.OtherColorMode != null)
colormode = mp.OtherColorMode;
@ -226,8 +235,8 @@ public class ChatProcessing {
}
static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player,
@Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier,
String origin) {
@Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier,
String origin) {
TellrawPart json = new TellrawPart("");
ChatOnlyComponent.tellrawCreate(mp, json); //TODO: Make nice API
json.addExtra(

View file

@ -34,6 +34,13 @@ public class FormatterComponent extends Component<PluginMain> {
return getConfig().getData("notificationPitch", 1.0f);
}
/**
* The minimum time between messages in milliseconds.
*/
public ConfigData<Integer> minTimeBetweenMessages() {
return getConfig().getData("minTimeBetweenMessages", 100);
}
@Override
protected void enable() {