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

View file

@ -134,20 +134,29 @@ public class ChatProcessing {
return true; 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; ChatPlayer mp;
if (player != null) if (player != null)
mp = TBMCPlayerBase.getPlayer(player.getUniqueId(), ChatPlayer.class); mp = TBMCPlayerBase.getPlayer(player.getUniqueId(), ChatPlayer.class);
else //Due to the online player map, getPlayer() can be more efficient than getAs() else //Due to the online player map, getPlayer() can be more efficient than getAs()
mp = e.getUser().getAs(ChatPlayer.class); //May be null 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(); Color colormode = channel.Color().get();
if (mp != null && mp.OtherColorMode != null) if (mp != null && mp.OtherColorMode != null)
colormode = mp.OtherColorMode; colormode = mp.OtherColorMode;
@ -226,8 +235,8 @@ public class ChatProcessing {
} }
static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player, static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player,
@Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier, @Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier,
String origin) { String origin) {
TellrawPart json = new TellrawPart(""); TellrawPart json = new TellrawPart("");
ChatOnlyComponent.tellrawCreate(mp, json); //TODO: Make nice API ChatOnlyComponent.tellrawCreate(mp, json); //TODO: Make nice API
json.addExtra( json.addExtra(

View file

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