Remove some bloat

This commit is contained in:
destro174 2022-02-14 09:43:00 +01:00
parent 0e3a2e9791
commit fdfea84c45

View File

@ -140,407 +140,6 @@ class PlayerEventHandler implements Listener
bannedWordFinder = new WordFinder(instance.dataStore.loadBannedWords());
}
//when a player chats, monitor for spam
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
synchronized void onPlayerChat(AsyncPlayerChatEvent event)
{
Player player = event.getPlayer();
if (!player.isOnline())
{
event.setCancelled(true);
return;
}
String message = event.getMessage();
boolean muted = this.handlePlayerChat(player, message, event);
Set<Player> recipients = event.getRecipients();
//muted messages go out to only the sender
if (muted)
{
recipients.clear();
recipients.add(player);
}
//soft muted messages go out to all soft muted players
else if (this.dataStore.isSoftMuted(player.getUniqueId()))
{
String notificationMessage = "(Muted " + player.getName() + "): " + message;
Set<Player> recipientsToKeep = new HashSet<>();
for (Player recipient : recipients)
{
if (this.dataStore.isSoftMuted(recipient.getUniqueId()))
{
recipientsToKeep.add(recipient);
}
else if (recipient.hasPermission("griefprevention.eavesdrop"))
{
recipient.sendMessage(ChatColor.GRAY + notificationMessage);
}
}
recipients.clear();
recipients.addAll(recipientsToKeep);
GriefPrevention.AddLogEntry(notificationMessage, CustomLogEntryTypes.MutedChat, false);
}
//troll and excessive profanity filter
else if (!player.hasPermission("griefprevention.spam") && this.bannedWordFinder.hasMatch(message))
{
//allow admins to see the soft-muted text
String notificationMessage = "(Muted " + player.getName() + "): " + message;
for (Player recipient : recipients)
{
if (recipient.hasPermission("griefprevention.eavesdrop"))
{
recipient.sendMessage(ChatColor.GRAY + notificationMessage);
}
}
//limit recipients to sender
recipients.clear();
recipients.add(player);
//if player not new warn for the first infraction per play session.
if (!GriefPrevention.isNewToServer(player))
{
PlayerData playerData = instance.dataStore.getPlayerData(player.getUniqueId());
if (!playerData.profanityWarned)
{
playerData.profanityWarned = true;
GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoProfanity);
event.setCancelled(true);
return;
}
}
//otherwise assume chat troll and mute all chat from this sender until an admin says otherwise
else if (instance.config_trollFilterEnabled)
{
GriefPrevention.AddLogEntry("Auto-muted new player " + player.getName() + " for profanity shortly after join. Use /SoftMute to undo.", CustomLogEntryTypes.AdminActivity);
GriefPrevention.AddLogEntry(notificationMessage, CustomLogEntryTypes.MutedChat, false);
instance.dataStore.toggleSoftMute(player.getUniqueId());
}
}
//remaining messages
else
{
//enter in abridged chat logs
makeSocialLogEntry(player.getName(), message);
//based on ignore lists, remove some of the audience
if (!player.hasPermission("griefprevention.notignorable"))
{
Set<Player> recipientsToRemove = new HashSet<>();
PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId());
for (Player recipient : recipients)
{
if (!recipient.hasPermission("griefprevention.notignorable"))
{
if (playerData.ignoredPlayers.containsKey(recipient.getUniqueId()))
{
recipientsToRemove.add(recipient);
}
else
{
PlayerData targetPlayerData = this.dataStore.getPlayerData(recipient.getUniqueId());
if (targetPlayerData.ignoredPlayers.containsKey(player.getUniqueId()))
{
recipientsToRemove.add(recipient);
}
}
}
}
recipients.removeAll(recipientsToRemove);
}
}
}
//returns true if the message should be muted, true if it should be sent
private boolean handlePlayerChat(Player player, String message, PlayerEvent event)
{
//FEATURE: automatically educate players about claiming land
//watching for message format how*claim*, and will send a link to the basics video
if (this.howToClaimPattern == null)
{
this.howToClaimPattern = Pattern.compile(this.dataStore.getMessage(Messages.HowToClaimRegex), Pattern.CASE_INSENSITIVE);
}
if (this.howToClaimPattern.matcher(message).matches())
{
if (instance.creativeRulesApply(player.getLocation()))
{
GriefPrevention.sendMessage(player, TextMode.Info, Messages.CreativeBasicsVideo2, 10L, DataStore.CREATIVE_VIDEO_URL);
}
else
{
GriefPrevention.sendMessage(player, TextMode.Info, Messages.SurvivalBasicsVideo2, 10L, DataStore.SURVIVAL_VIDEO_URL);
}
}
//FEATURE: automatically educate players about the /trapped command
//check for "trapped" or "stuck" to educate players about the /trapped command
String trappedwords = this.dataStore.getMessage(
Messages.TrappedChatKeyword
);
if (!trappedwords.isEmpty())
{
String[] checkWords = trappedwords.split(";");
for (String checkWord : checkWords)
{
if (!message.contains("/trapped")
&& message.contains(checkWord))
{
GriefPrevention.sendMessage(
player,
TextMode.Info,
Messages.TrappedInstructions,
10L
);
break;
}
}
}
//FEATURE: monitor for chat and command spam
if (!instance.config_spam_enabled) return false;
//if the player has permission to spam, don't bother even examining the message
if (player.hasPermission("griefprevention.spam")) return false;
//examine recent messages to detect spam
SpamAnalysisResult result = this.spamDetector.AnalyzeMessage(player.getUniqueId(), message, System.currentTimeMillis());
//apply any needed changes to message (like lowercasing all-caps)
if (event instanceof AsyncPlayerChatEvent)
{
((AsyncPlayerChatEvent) event).setMessage(result.finalMessage);
}
//don't allow new players to chat after logging in until they move
PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId());
if (playerData.noChatLocation != null)
{
Location currentLocation = player.getLocation();
if (currentLocation.getBlockX() == playerData.noChatLocation.getBlockX() &&
currentLocation.getBlockZ() == playerData.noChatLocation.getBlockZ())
{
GriefPrevention.sendMessage(player, TextMode.Err, Messages.NoChatUntilMove, 10L);
result.muteReason = "pre-movement chat";
}
else
{
playerData.noChatLocation = null;
}
}
//filter IP addresses
if (result.muteReason == null)
{
if (instance.containsBlockedIP(message))
{
//block message
result.muteReason = "IP address";
}
}
//take action based on spam detector results
if (result.shouldBanChatter)
{
if (instance.config_spam_banOffenders)
{
//log entry
GriefPrevention.AddLogEntry("Banning " + player.getName() + " for spam.", CustomLogEntryTypes.AdminActivity);
//kick and ban
PlayerKickBanTask task = new PlayerKickBanTask(player, instance.config_spam_banMessage, "GriefPrevention Anti-Spam", true);
instance.getServer().getScheduler().scheduleSyncDelayedTask(instance, task, 1L);
}
else
{
//log entry
GriefPrevention.AddLogEntry("Kicking " + player.getName() + " for spam.", CustomLogEntryTypes.AdminActivity);
//just kick
PlayerKickBanTask task = new PlayerKickBanTask(player, "", "GriefPrevention Anti-Spam", false);
instance.getServer().getScheduler().scheduleSyncDelayedTask(instance, task, 1L);
}
}
else if (result.shouldWarnChatter)
{
//warn and log
GriefPrevention.sendMessage(player, TextMode.Warn, instance.config_spam_warningMessage, 10L);
GriefPrevention.AddLogEntry("Warned " + player.getName() + " about spam penalties.", CustomLogEntryTypes.Debug, true);
}
if (result.muteReason != null)
{
//mute and log
GriefPrevention.AddLogEntry("Muted " + result.muteReason + ".");
GriefPrevention.AddLogEntry("Muted " + player.getName() + " " + result.muteReason + ":" + message, CustomLogEntryTypes.Debug, true);
return true;
}
return false;
}
//when a player uses a slash command...
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
synchronized void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
String message = event.getMessage();
String[] args = message.split(" ");
String command = args[0].toLowerCase();
CommandCategory category = this.getCommandCategory(command);
Player player = event.getPlayer();
PlayerData playerData = null;
//if a whisper
if (category == CommandCategory.Whisper && args.length > 1)
{
//determine target player, might be NULL
Player targetPlayer = instance.getServer().getPlayer(args[1]);
//softmute feature
if (this.dataStore.isSoftMuted(player.getUniqueId()) && targetPlayer != null && !this.dataStore.isSoftMuted(targetPlayer.getUniqueId()))
{
event.setCancelled(true);
return;
}
//if eavesdrop enabled and sender doesn't have the eavesdrop immunity permission, eavesdrop
if (instance.config_whisperNotifications && !player.hasPermission("griefprevention.eavesdropimmune"))
{
//except for when the recipient has eavesdrop immunity
if (targetPlayer == null || !targetPlayer.hasPermission("griefprevention.eavesdropimmune"))
{
StringBuilder logMessageBuilder = new StringBuilder();
logMessageBuilder.append("[[").append(event.getPlayer().getName()).append("]] ");
for (int i = 1; i < args.length; i++)
{
logMessageBuilder.append(args[i]).append(" ");
}
String logMessage = logMessageBuilder.toString();
@SuppressWarnings("unchecked")
Collection<Player> players = (Collection<Player>) instance.getServer().getOnlinePlayers();
for (Player onlinePlayer : players)
{
if (onlinePlayer.hasPermission("griefprevention.eavesdrop") && !onlinePlayer.equals(targetPlayer) && !onlinePlayer.equals(player))
{
onlinePlayer.sendMessage(ChatColor.GRAY + logMessage);
}
}
}
}
//ignore feature
if (targetPlayer != null && targetPlayer.isOnline())
{
//if either is ignoring the other, cancel this command
playerData = this.dataStore.getPlayerData(player.getUniqueId());
if (playerData.ignoredPlayers.containsKey(targetPlayer.getUniqueId()) && !targetPlayer.hasPermission("griefprevention.notignorable"))
{
event.setCancelled(true);
GriefPrevention.sendMessage(player, TextMode.Err, Messages.IsIgnoringYou);
return;
}
PlayerData targetPlayerData = this.dataStore.getPlayerData(targetPlayer.getUniqueId());
if (targetPlayerData.ignoredPlayers.containsKey(player.getUniqueId()) && !player.hasPermission("griefprevention.notignorable"))
{
event.setCancelled(true);
GriefPrevention.sendMessage(player, TextMode.Err, Messages.IsIgnoringYou);
return;
}
}
}
//if in pvp, block any pvp-banned slash commands
if (playerData == null) playerData = this.dataStore.getPlayerData(event.getPlayer().getUniqueId());
if ((playerData.inPvpCombat() || playerData.siegeData != null) && instance.config_pvp_blockedCommands.contains(command))
{
event.setCancelled(true);
GriefPrevention.sendMessage(event.getPlayer(), TextMode.Err, Messages.CommandBannedInPvP);
return;
}
//soft mute for chat slash commands
if (category == CommandCategory.Chat && this.dataStore.isSoftMuted(player.getUniqueId()))
{
event.setCancelled(true);
return;
}
//if the slash command used is in the list of monitored commands, treat it like a chat message (see above)
boolean isMonitoredCommand = (category == CommandCategory.Chat || category == CommandCategory.Whisper);
if (isMonitoredCommand)
{
//if anti spam enabled, check for spam
if (instance.config_spam_enabled)
{
event.setCancelled(this.handlePlayerChat(event.getPlayer(), event.getMessage(), event));
}
if (!player.hasPermission("griefprevention.spam") && this.bannedWordFinder.hasMatch(message))
{
event.setCancelled(true);
}
//unless cancelled, log in abridged logs
if (!event.isCancelled())
{
StringBuilder builder = new StringBuilder();
for (String arg : args)
{
builder.append(arg).append(' ');
}
makeSocialLogEntry(event.getPlayer().getName(), builder.toString());
}
}
//if requires access trust, check for permission
isMonitoredCommand = false;
String lowerCaseMessage = message.toLowerCase();
for (String monitoredCommand : instance.config_claims_commandsRequiringAccessTrust)
{
if (lowerCaseMessage.startsWith(monitoredCommand))
{
isMonitoredCommand = true;
break;
}
}
if (isMonitoredCommand)
{
Claim claim = this.dataStore.getClaimAt(player.getLocation(), false, playerData.lastClaim);
if (claim != null)
{
playerData.lastClaim = claim;
Supplier<String> reason = claim.checkPermission(player, ClaimPermission.Access, event);
if (reason != null)
{
GriefPrevention.sendMessage(player, TextMode.Err, reason.get());
event.setCancelled(true);
}
}
}
}
private final ConcurrentHashMap<String, CommandCategory> commandCategoryMap = new ConcurrentHashMap<>();
private CommandCategory getCommandCategory(String commandName)
@ -624,53 +223,6 @@ class PlayerEventHandler implements Listener
private final ConcurrentHashMap<UUID, Date> lastLoginThisServerSessionMap = new ConcurrentHashMap<>();
//when a player attempts to join the server...
@EventHandler(priority = EventPriority.HIGHEST)
void onPlayerLogin(PlayerLoginEvent event)
{
Player player = event.getPlayer();
//all this is anti-spam code
if (instance.config_spam_enabled)
{
//FEATURE: login cooldown to prevent login/logout spam with custom clients
long now = Calendar.getInstance().getTimeInMillis();
//if allowed to join and login cooldown enabled
if (instance.config_spam_loginCooldownSeconds > 0 && event.getResult() == Result.ALLOWED && !player.hasPermission("griefprevention.spam"))
{
//determine how long since last login and cooldown remaining
Date lastLoginThisSession = lastLoginThisServerSessionMap.get(player.getUniqueId());
if (lastLoginThisSession != null)
{
long millisecondsSinceLastLogin = now - lastLoginThisSession.getTime();
long secondsSinceLastLogin = millisecondsSinceLastLogin / 1000;
long cooldownRemaining = instance.config_spam_loginCooldownSeconds - secondsSinceLastLogin;
//if cooldown remaining
if (cooldownRemaining > 0)
{
//DAS BOOT!
event.setResult(Result.KICK_OTHER);
event.setKickMessage("You must wait " + cooldownRemaining + " seconds before logging-in again.");
event.disallow(event.getResult(), event.getKickMessage());
return;
}
}
}
//if logging-in account is banned, remember IP address for later
if (instance.config_smartBan && event.getResult() == Result.KICK_BANNED)
{
this.tempBannedIps.add(new IpBanInfo(event.getAddress(), now + this.MILLISECONDS_IN_DAY, player.getName()));
}
}
//remember the player's ip address
PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId());
playerData.ipAddress = event.getAddress();
}
//when a player successfully joins the server...
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)