Silence relog messages.

Prevents a player from logging out and then immediately back in from
adding low value messages to chat.
This commit is contained in:
ryanhamshire 2016-07-01 10:46:39 -07:00
parent a97d5c191a
commit 95bfa6e472
3 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,39 @@
/*
GriefPrevention Server Plugin for Minecraft
Copyright (C) 2016 Ryan Hamshire
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.ryanhamshire.GriefPrevention;
import org.bukkit.Bukkit;
//sends a message to all online players
//used to send delayed messages, for example a quit message after the player has been gone a while
class BroadcastMessageTask implements Runnable
{
private String message;
public BroadcastMessageTask(String message)
{
this.message = message;
}
@Override
public void run()
{
Bukkit.getServer().broadcastMessage(this.message);
}
}

View File

@ -139,6 +139,7 @@ public class GriefPrevention extends JavaPlugin
public String config_spam_warningMessage; //message to show a player who is close to spam level
public String config_spam_allowedIpAddresses; //IP addresses which will not be censored
public int config_spam_deathMessageCooldownSeconds; //cooldown period for death messages (per player) in seconds
public int config_spam_logoutMessageDelaySeconds; //delay before a logout message will be shown (only if the player stays offline that long)
HashMap<World, Boolean> config_pvp_specifiedWorlds; //list of worlds where pvp anti-grief rules apply, according to the config file
public boolean config_pvp_protectFreshSpawns; //whether to make newly spawned players immune until they pick up an item
@ -567,7 +568,8 @@ public class GriefPrevention extends JavaPlugin
this.config_spam_banMessage = config.getString("GriefPrevention.Spam.BanMessage", "Banned for spam.");
String slashCommandsToMonitor = config.getString("GriefPrevention.Spam.MonitorSlashCommands", "/me;/global;/local");
slashCommandsToMonitor = config.getString("GriefPrevention.Spam.ChatSlashCommands", slashCommandsToMonitor);
this.config_spam_deathMessageCooldownSeconds = config.getInt("GriefPrevention.Spam.DeathMessageCooldownSeconds", 120);
this.config_spam_deathMessageCooldownSeconds = config.getInt("GriefPrevention.Spam.DeathMessageCooldownSeconds", 120);
this.config_spam_logoutMessageDelaySeconds = config.getInt("GriefPrevention.Spam.Logout Message Delay In Seconds", 10);
this.config_pvp_protectFreshSpawns = config.getBoolean("GriefPrevention.PvP.ProtectFreshSpawns", true);
this.config_pvp_punishLogout = config.getBoolean("GriefPrevention.PvP.PunishLogout", true);
@ -809,6 +811,7 @@ public class GriefPrevention extends JavaPlugin
outConfig.set("GriefPrevention.Spam.BanMessage", this.config_spam_banMessage);
outConfig.set("GriefPrevention.Spam.AllowedIpAddresses", this.config_spam_allowedIpAddresses);
outConfig.set("GriefPrevention.Spam.DeathMessageCooldownSeconds", this.config_spam_deathMessageCooldownSeconds);
outConfig.set("GriefPrevention.Spam.Logout Message Delay In Seconds", this.config_spam_logoutMessageDelaySeconds);
for(World world : worlds)
{

View File

@ -910,6 +910,22 @@ class PlayerEventHandler implements Listener
player.teleport(returnLocation);
}
}
//if we're holding a logout message for this player, don't send that or this event's join message
if(GriefPrevention.instance.config_spam_logoutMessageDelaySeconds > 0)
{
String joinMessage = event.getJoinMessage();
if(joinMessage != null && !joinMessage.isEmpty())
{
Integer taskID = this.heldLogoutMessages.get(player.getUniqueId());
if(taskID != null && Bukkit.getScheduler().isQueued(taskID))
{
Bukkit.getScheduler().cancelTask(taskID);
player.sendMessage(event.getJoinMessage());
event.setJoinMessage("");
}
}
}
}
//when a player spawns, conditionally apply temporary pvp protection
@ -964,6 +980,7 @@ class PlayerEventHandler implements Listener
}
//when a player quits...
private HashMap<UUID, Integer> heldLogoutMessages = new HashMap<UUID, Integer>();
@EventHandler(priority = EventPriority.HIGHEST)
void onPlayerQuit(PlayerQuitEvent event)
{
@ -1034,6 +1051,19 @@ class PlayerEventHandler implements Listener
//drop data about this player
this.dataStore.clearCachedPlayerData(playerID);
//send quit message later, but only if the player stays offline
if(GriefPrevention.instance.config_spam_logoutMessageDelaySeconds > 0)
{
String quitMessage = event.getQuitMessage();
if(quitMessage != null && !quitMessage.isEmpty())
{
BroadcastMessageTask task = new BroadcastMessageTask(quitMessage);
int taskID = Bukkit.getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 20L * GriefPrevention.instance.config_spam_logoutMessageDelaySeconds);
this.heldLogoutMessages.put(playerID, taskID);
event.setQuitMessage("");
}
}
}
//determines whether or not a login or logout notification should be silenced, depending on how many there have been in the last minute