From 241fe7a3195116fb157e80fe17a2ae1093489f2e Mon Sep 17 00:00:00 2001 From: Aust1n46 Date: Fri, 19 Feb 2021 17:13:34 -0600 Subject: [PATCH] Overhauled mute system. Improved timed muting. Added new arg parser for mute duration. Fixed cooldown overflow issue. Fixed timed mutes not expiring properly issue. Updated data file format to properly use yml formatting. Properly sync timed mutes over BungeeCord. --- src/Messages.yml | 14 +- .../Aust1n46/chat/MineverseChat.java | 40 ++--- .../chat/api/MineverseChatPlayer.java | 31 ++-- .../api/SynchronizedMineverseChatPlayer.java | 10 +- .../chat/bungee/MineverseChatBungee.java | 8 +- .../Aust1n46/chat/command/mute/Mute.java | 17 +-- .../chat/database/BungeePlayerData.java | 8 +- .../Aust1n46/chat/database/PlayerData.java | 50 ++++--- .../Aust1n46/chat/listeners/ChatListener.java | 67 ++++----- .../chat/listeners/LoginListener.java | 2 +- .../chat/localization/LocalizedMessage.java | 4 + .../Aust1n46/chat/utilities/Format.java | 137 +++++++++++++++++- 12 files changed, 258 insertions(+), 130 deletions(-) diff --git a/src/Messages.yml b/src/Messages.yml index 8b0e3b2..61f7f01 100644 --- a/src/Messages.yml +++ b/src/Messages.yml @@ -8,7 +8,7 @@ BungeeToggleOn: '&6You are now receiving BungeeCord chat.' ClearChatSender: '&aCleared the server chat.' ClearChatServer: '&aYour chat has been cleared.' ChannelCannotMute: '&cYou cannot mute players in this channel: {channel_color}{channel_name}' -ChannelCooldown: '&c{cooldown} {units} of cooldown remaining.' +ChannelCooldown: '&c{cooldown} of cooldown remaining.' ChannelList: '{channel_color}{channel_name} : {channel_alias}' ChannelListHeader: '&6Channel List : Alias' ChannelListWithPermissions: '{channel_color}{channel_name} : {channel_alias} - Permission Required' @@ -17,7 +17,7 @@ ChannelNoPermissionView: '&cYou do not have permission to look at this channel.' ChannelNoSpeakPermissions: '&cYou do not have permission to speak in this channel.' ChannelPlayerListHeader: '&6Players in Channel: {channel_color}{channel_name}' ChannelMuted: '&cYou are muted in this channel: {channel_color}{channel_name}' -ChannelMutedTimed: '&cYou are muted in this channel: {channel_color}{channel_name}&c for {time} more {units}' +ChannelMutedTimed: '&cYou are muted in this channel: {channel_color}{channel_name}&c for {time}' CommandInvalidArguments: '&cInvalid command: {command} {args}' CommandInvalidArgumentsIgnore: '&cInvalid command: /ignore [player] or /ignore list' CommandMustBeRunByPlayer: '&cThis command must be run by a player.' @@ -54,11 +54,11 @@ MustListenOneChannel: '&cYou need to be listening on at least one channel, setti MutePlayerAllPlayer: '&cYou have just been muted in all channels.' MutePlayerAllSender: '&cMuted player &6{player} &cin all channels.' MutePlayerPlayer: '&cYou have just been muted in: {channel_color}{channel_name}' -MutePlayerPlayerTime: '&cYou have just been muted in: {channel_color}{channel_name} &cfor {time} {units}' +MutePlayerPlayerTime: '&cYou have just been muted in: {channel_color}{channel_name} &cfor {time}' MutePlayerSpam: '&cYou have been muted for spamming in: {channel_color}{channel_name}' -MutePlayerSpamTime: '&cYou have been muted for spamming in: {channel_color}{channel_name} &cfor {time} {units}' +MutePlayerSpamTime: '&cYou have been muted for spamming in: {channel_color}{channel_name} &cfor {time}' MutePlayerSender: '&cMuted player &6{player} &cin: {channel_color}{channel_name}' -MutePlayerSenderTime: '&cMuted player &6{player} &cin: {channel_color}{channel_name} &cfor {time} {units}' +MutePlayerSenderTime: '&cMuted player &6{player} &cin: {channel_color}{channel_name} &cfor {time}' NoPlayerToReplyTo: '&cYou do not have anyone to reply to.' NotificationsOff: '&aYou are no longer receiving notifications.' NotificationsOn: '&aYou are now receiving notifications.' @@ -78,6 +78,10 @@ SpyOff: '&6You are no longer spying.' SpyOn: '&6You are now spying.' UnblockCommandPlayer: '&cYou have been unblocked from entering command {command}.' UnblockCommandSender: '&cUnblocked player &6{player} &cfrom entering command {command}.' +UnitsDayPlural: 'days' +UnitsDaySingular: 'day' +UnitsHourPlural: 'hours' +UnitsHourSingular: 'hour' UnitsMinutePlural: 'minutes' UnitsMinuteSingular: 'minute' UnitsSecondPlural: 'seconds' diff --git a/src/mineverse/Aust1n46/chat/MineverseChat.java b/src/mineverse/Aust1n46/chat/MineverseChat.java index 186610b..d292910 100644 --- a/src/mineverse/Aust1n46/chat/MineverseChat.java +++ b/src/mineverse/Aust1n46/chat/MineverseChat.java @@ -377,25 +377,27 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { @Override public void run() { for (MineverseChatPlayer p : MineverseChatAPI.getOnlineMineverseChatPlayers()) { - int time = Format.currentTimeMillis(); + long currentTimeMillis = System.currentTimeMillis(); Iterator iterator = p.getMutes().keySet().iterator(); while (iterator.hasNext()) { - ChatChannel channel = ChatChannel.getChannel(iterator.next()); - int timemark = p.getMutes().get(channel.getName()); - if (timemark == 0) { - continue; - } - if (getConfig().getString("loglevel", "info").equals("debug")) { - System.out.println(time + " " + timemark); - } - if (time >= timemark) { - iterator.remove(); - if (p.isOnline()) { + String channelName = iterator.next(); + if(ChatChannel.isChannel(channelName)) { + ChatChannel channel = ChatChannel.getChannel(channelName); + long timemark = p.getMutes().get(channelName); + if (timemark == 0) { + continue; + } + if (getConfig().getString("loglevel", "info").equals("debug")) { + System.out.println(currentTimeMillis + " " + timemark); + } + if (currentTimeMillis >= timemark) { + iterator.remove(); p.getPlayer().sendMessage(LocalizedMessage.UNMUTE_PLAYER_PLAYER.toString() .replace("{player}", p.getName()).replace("{channel_color}", channel.getColor()) - .replace("{channel_name}", channel.getName())); - } else { - p.setModified(true); + .replace("{channel_name}", channelName)); + if(channel.getBungee()) { + MineverseChat.getInstance().synchronize(p, true); + } } } } @@ -405,7 +407,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { .sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&e - Updating Player Mutes")); } } - }, 0L, 1200L); // one minute interval + }, 0L, 60L); // three second interval } @Override @@ -567,6 +569,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { ChatChannel channel = ChatChannel.getChannel(c); if(channel.getBungee()) { out.writeUTF(channel.getName()); + out.writeLong(mcp.getMutes().get(c)); } } int ignoreCount = 0; @@ -807,9 +810,10 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { // System.out.println(sizeB + " mute size"); for(int b = 0; b < sizeB; b++) { String ch = msgin.readUTF(); + long muteTime = msgin.readLong(); // System.out.println(ch); if(ChatChannel.isChannel(ch)) { - p.addMute(ch, 0); + p.addMute(ch, muteTime); } } // System.out.println(msgin.available() + " available before"); @@ -942,7 +946,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener { Set ignores = new HashSet(); Set listening = new HashSet(); listening.add(current.getName()); - HashMap mutes = new HashMap(); + HashMap mutes = new HashMap(); Set blockedCommands = new HashSet(); String jsonFormat = "Default"; s = new MineverseChatPlayer(uuid, name, current, ignores, listening, mutes, blockedCommands, false, null, true, true, name, jsonFormat, false, false, false, true, true); diff --git a/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java b/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java index 70ca53a..1851b66 100644 --- a/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java +++ b/src/mineverse/Aust1n46/chat/api/MineverseChatPlayer.java @@ -22,7 +22,7 @@ public class MineverseChatPlayer { private ChatChannel currentChannel; private Set ignores; private Set listening; - private HashMap mutes; + private HashMap mutes; private Set blockedCommands; private boolean host; private UUID party; @@ -38,9 +38,9 @@ public class MineverseChatPlayer { private boolean quickChat; private ChatChannel quickChannel; private UUID replyPlayer; - private HashMap cooldowns; + private HashMap cooldowns; private boolean partyChat; - private HashMap> spam; + private HashMap> spam; private boolean modified; private List messages; private String jsonFormat; @@ -49,15 +49,8 @@ public class MineverseChatPlayer { private boolean rangedSpy; private boolean messageToggle; private boolean bungeeToggle; - - //buttons variable no longer used - //mail variable no longer used - @Deprecated - public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set ignores, Set listening, HashMap mutes, Set blockedCommands, List mail, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean buttons, boolean messageToggle, boolean bungeeToggle) { - this(uuid, name, currentChannel, ignores, listening, mutes, blockedCommands, host, party, filter, notifications, nickname, jsonFormat, spy, commandSpy, rangedSpy, messageToggle, bungeeToggle); - } - public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set ignores, Set listening, HashMap mutes, Set blockedCommands, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean messageToggle, boolean bungeeToggle) { + public MineverseChatPlayer(UUID uuid, String name, ChatChannel currentChannel, Set ignores, Set listening, HashMap mutes, Set blockedCommands, boolean host, UUID party, boolean filter, boolean notifications, String nickname, String jsonFormat, boolean spy, boolean commandSpy, boolean rangedSpy, boolean messageToggle, boolean bungeeToggle) { this.uuid = uuid; this.name = name; this.currentChannel = currentChannel; @@ -84,8 +77,8 @@ public class MineverseChatPlayer { this.modified = false; this.messages = new ArrayList(); this.jsonFormat = jsonFormat; - this.cooldowns = new HashMap(); - this.spam = new HashMap>(); + this.cooldowns = new HashMap(); + this.spam = new HashMap>(); this.messageToggle = messageToggle; this.bungeeToggle = bungeeToggle; } @@ -214,11 +207,11 @@ public class MineverseChatPlayer { this.listening.clear(); } - public HashMap getMutes() { + public HashMap getMutes() { return this.mutes; } - public boolean addMute(String channel, int time) { + public boolean addMute(String channel, long time) { if(channel != null && time >= 0) { mutes.put(channel, time); return true; @@ -412,11 +405,11 @@ public class MineverseChatPlayer { this.partyChat = partyChat; } - public HashMap getCooldowns() { + public HashMap getCooldowns() { return this.cooldowns; } - public boolean addCooldown(ChatChannel channel, int time) { + public boolean addCooldown(ChatChannel channel, long time) { if(channel != null && time > 0) { cooldowns.put(channel, time); return true; @@ -436,7 +429,7 @@ public class MineverseChatPlayer { return channel != null && this.cooldowns != null ? this.cooldowns.containsKey(channel) : false; } - public HashMap> getSpam() { + public HashMap> getSpam() { return this.spam; } @@ -446,7 +439,7 @@ public class MineverseChatPlayer { public boolean addSpam(ChatChannel channel) { if(channel != null) { - spam.put(channel, new ArrayList()); + spam.put(channel, new ArrayList()); return true; } return false; diff --git a/src/mineverse/Aust1n46/chat/api/SynchronizedMineverseChatPlayer.java b/src/mineverse/Aust1n46/chat/api/SynchronizedMineverseChatPlayer.java index 591aaa0..f05b06a 100644 --- a/src/mineverse/Aust1n46/chat/api/SynchronizedMineverseChatPlayer.java +++ b/src/mineverse/Aust1n46/chat/api/SynchronizedMineverseChatPlayer.java @@ -9,14 +9,14 @@ import java.util.UUID; public class SynchronizedMineverseChatPlayer { private UUID uuid; private Set listening; - private HashMap mutes; + private HashMap mutes; private Set ignores; private int messagePackets; private List messageData = new ArrayList(); private boolean spy; private boolean messageToggle; - public SynchronizedMineverseChatPlayer(UUID uuid, Set listening, HashMap mutes, Set ignores, boolean spy, boolean messageToggle) { + public SynchronizedMineverseChatPlayer(UUID uuid, Set listening, HashMap mutes, Set ignores, boolean spy, boolean messageToggle) { this.uuid = uuid; this.listening = listening; this.mutes = mutes; @@ -61,15 +61,15 @@ public class SynchronizedMineverseChatPlayer { return this.ignores; } - public void addMute(String channel) { - this.mutes.put(channel, 0); + public void addMute(String channel, long muteTime) { + this.mutes.put(channel, muteTime); } public void removeMute(String channel) { this.mutes.remove(channel); } - public HashMap getMutes() { + public HashMap getMutes() { return this.mutes; } diff --git a/src/mineverse/Aust1n46/chat/bungee/MineverseChatBungee.java b/src/mineverse/Aust1n46/chat/bungee/MineverseChatBungee.java index 27cd8a1..f7c76d7 100644 --- a/src/mineverse/Aust1n46/chat/bungee/MineverseChatBungee.java +++ b/src/mineverse/Aust1n46/chat/bungee/MineverseChatBungee.java @@ -423,7 +423,7 @@ public class MineverseChatBungee extends Plugin implements Listener { UUID uuid = UUID.fromString(in.readUTF()); SynchronizedMineverseChatPlayer smcp = MineverseChatAPI.getSynchronizedMineverseChatPlayer(uuid); if(smcp == null) { - smcp = new SynchronizedMineverseChatPlayer(uuid, new HashSet(), new HashMap(), new HashSet(), false, true); + smcp = new SynchronizedMineverseChatPlayer(uuid, new HashSet(), new HashMap(), new HashSet(), false, true); MineverseChatAPI.addSynchronizedMineverseChatPlayerToMap(smcp); } out.writeUTF("Sync"); @@ -440,6 +440,7 @@ public class MineverseChatBungee extends Plugin implements Listener { for(String channel : smcp.getMutes().keySet()) { //System.out.println(channel); out.writeUTF(channel); + out.writeLong(smcp.getMutes().get(channel)); } //System.out.println(smcp.isSpy() + " spy value"); //System.out.println(out.size() + " size before"); @@ -459,7 +460,7 @@ public class MineverseChatBungee extends Plugin implements Listener { UUID uuid = UUID.fromString(in.readUTF()); SynchronizedMineverseChatPlayer smcp = MineverseChatAPI.getSynchronizedMineverseChatPlayer(uuid); if(smcp == null) { - smcp = new SynchronizedMineverseChatPlayer(uuid, new HashSet(), new HashMap(), new HashSet(), false, true); + smcp = new SynchronizedMineverseChatPlayer(uuid, new HashSet(), new HashMap(), new HashSet(), false, true); MineverseChatAPI.addSynchronizedMineverseChatPlayerToMap(smcp); } smcp.getListening().clear(); @@ -474,8 +475,9 @@ public class MineverseChatBungee extends Plugin implements Listener { //System.out.println(size + " mutes"); for(int b = 0; b < sizeM; b++) { String mute = in.readUTF(); + long muteTime = in.readLong(); //System.out.println(mute); - smcp.addMute(mute); + smcp.addMute(mute, muteTime); } int sizeI = in.read(); for(int c = 0; c < sizeI; c++) { diff --git a/src/mineverse/Aust1n46/chat/command/mute/Mute.java b/src/mineverse/Aust1n46/chat/command/mute/Mute.java index 2006a02..8d990ae 100644 --- a/src/mineverse/Aust1n46/chat/command/mute/Mute.java +++ b/src/mineverse/Aust1n46/chat/command/mute/Mute.java @@ -11,7 +11,6 @@ import mineverse.Aust1n46.chat.localization.LocalizedMessage; import mineverse.Aust1n46.chat.utilities.Format; public class Mute extends MineverseCommand { - private static final int MILLISECONDS_PER_MINUTE = 60000; public Mute(String name) { super(name); @@ -74,24 +73,22 @@ public class Mute extends MineverseCommand { } if (channel.isMutable()) { try { - int datetime = Format.currentTimeMillis(); - int time = Integer.parseInt(args[2]); + long datetime = System.currentTimeMillis(); + long time = Format.parseTimeStringToMillis(args[2]); if (time > 0) { - player.addMute(channel.getName(), datetime + (time * MILLISECONDS_PER_MINUTE)); - String units = LocalizedMessage.UNITS_MINUTE_PLURAL.toString(); - if (time == 1) - units = LocalizedMessage.UNITS_MINUTE_SINGULAR.toString(); + player.addMute(channel.getName(), datetime + time); + String timeString = Format.parseTimeStringFromMillis(time); sender.sendMessage(LocalizedMessage.MUTE_PLAYER_SENDER_TIME.toString() .replace("{player}", player.getName()) .replace("{channel_color}", channel.getColor()) - .replace("{channel_name}", channel.getName()).replace("{time}", time + "") - .replace("{units}", units)); + .replace("{channel_name}", channel.getName()) + .replace("{time}", timeString)); if (player.isOnline()) player.getPlayer() .sendMessage(LocalizedMessage.MUTE_PLAYER_PLAYER_TIME.toString() .replace("{channel_color}", channel.getColor()) .replace("{channel_name}", channel.getName()) - .replace("{time}", time + "").replace("{units}", units)); + .replace("{time}", timeString)); else player.setModified(true); if (channel.getBungee()) { diff --git a/src/mineverse/Aust1n46/chat/database/BungeePlayerData.java b/src/mineverse/Aust1n46/chat/database/BungeePlayerData.java index 591301e..823ae13 100644 --- a/src/mineverse/Aust1n46/chat/database/BungeePlayerData.java +++ b/src/mineverse/Aust1n46/chat/database/BungeePlayerData.java @@ -52,11 +52,11 @@ public class BungeePlayerData { String channel = l.nextToken(); listening.add(channel); } - HashMap mutes = new HashMap(); + HashMap mutes = new HashMap(); StringTokenizer m = new StringTokenizer(playerData.getString(uuidString + ".mutes"), ","); while(m.hasMoreTokens()) { String[] parts = m.nextToken().split(":"); - mutes.put(parts[0], Integer.parseInt(parts[1])); + mutes.put(parts[0], Long.parseLong(parts[1])); } HashSet ignores = new HashSet(); StringTokenizer n = new StringTokenizer(playerData.getString(uuidString + ".ignores"), ","); @@ -115,11 +115,11 @@ public class BungeePlayerData { String channel = l.nextToken(); listening.add(channel); } - HashMap mutes = new HashMap(); + HashMap mutes = new HashMap(); StringTokenizer m = new StringTokenizer(bungeePlayerDataFileConfiguration.getString("mutes"), ","); while(m.hasMoreTokens()) { String[] parts = m.nextToken().split(":"); - mutes.put(parts[0], Integer.parseInt(parts[1])); + mutes.put(parts[0], Long.parseLong(parts[1])); } HashSet ignores = new HashSet(); StringTokenizer n = new StringTokenizer(bungeePlayerDataFileConfiguration.getString("ignores"), ","); diff --git a/src/mineverse/Aust1n46/chat/database/PlayerData.java b/src/mineverse/Aust1n46/chat/database/PlayerData.java index 6e3c513..ad9fe0b 100644 --- a/src/mineverse/Aust1n46/chat/database/PlayerData.java +++ b/src/mineverse/Aust1n46/chat/database/PlayerData.java @@ -14,6 +14,7 @@ import java.util.StringTokenizer; import java.util.UUID; import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -64,7 +65,7 @@ public class PlayerData { listening.add(channel); } } - HashMap mutes = new HashMap(); + HashMap mutes = new HashMap(); StringTokenizer m = new StringTokenizer(playerData.getConfigurationSection("players." + uuidString).getString("mutes"), ","); while(m.hasMoreTokens()) { String[] parts = m.nextToken().split(":"); @@ -73,7 +74,7 @@ public class PlayerData { Bukkit.getConsoleSender().sendMessage("[VentureChat] Null Mute Time: " + parts[0] + " " + name); continue; } - mutes.put(ChatChannel.getChannel(parts[0]).getName(), Integer.parseInt(parts[1])); + mutes.put(ChatChannel.getChannel(parts[0]).getName(), Long.parseLong(parts[1])); } } Set blockedCommands = new HashSet(); @@ -160,18 +161,24 @@ public class PlayerData { listening.add(channel); } } - HashMap mutes = new HashMap(); - StringTokenizer m = new StringTokenizer(playerDataFileYamlConfiguration.getString("mutes"), ","); - while(m.hasMoreTokens()) { - String[] parts = m.nextToken().split(":"); - if(ChatChannel.isChannel(parts[0])) { - if(parts[1].equals("null")) { - Bukkit.getConsoleSender().sendMessage("[VentureChat] Null Mute Time: " + parts[0] + " " + name); - continue; - } - mutes.put(ChatChannel.getChannel(parts[0]).getName(), Integer.parseInt(parts[1])); - } + HashMap mutes = new HashMap(); +// StringTokenizer m = new StringTokenizer(playerDataFileYamlConfiguration.getString("mutes"), ","); +// while(m.hasMoreTokens()) { +// String[] parts = m.nextToken().split(":"); +// if(ChatChannel.isChannel(parts[0])) { +// if(parts[1].equals("null")) { +// Bukkit.getConsoleSender().sendMessage("[VentureChat] Null Mute Time: " + parts[0] + " " + name); +// continue; +// } +// mutes.put(ChatChannel.getChannel(parts[0]).getName(), Long.parseLong(parts[1])); +// } +// } + ConfigurationSection muteSection = playerDataFileYamlConfiguration.getConfigurationSection("mutes"); + for(String channelName : muteSection.getKeys(false)) { + ConfigurationSection channelSection = muteSection.getConfigurationSection(channelName); + mutes.put(channelName, channelSection.getLong("time")); } + Set blockedCommands = new HashSet(); StringTokenizer b = new StringTokenizer(playerDataFileYamlConfiguration.getString("blockedcommands"), ","); while(b.hasMoreTokens()) { @@ -225,11 +232,6 @@ public class PlayerData { ChatChannel c = ChatChannel.getChannel(channel); listening += c.getName() + ","; } - String mutes = ""; - for(String channel : mcp.getMutes().keySet()) { - ChatChannel c = ChatChannel.getChannel(channel); - mutes += c.getName() + ":" + mcp.getMutes().get(c.getName()) + ","; - } String blockedCommands = ""; for(String s : mcp.getBlockedCommands()) { blockedCommands += s + ","; @@ -238,13 +240,13 @@ public class PlayerData { listening = listening.substring(0, listening.length() - 1); } playerDataFileYamlConfiguration.set("listen", listening); - if(mutes.length() > 0) { - mutes = mutes.substring(0, mutes.length() - 1); - } - playerDataFileYamlConfiguration.set("mutes", mutes); - if(blockedCommands.length() > 0) { - blockedCommands = blockedCommands.substring(0, blockedCommands.length() - 1); + + ConfigurationSection muteSection = playerDataFileYamlConfiguration.createSection("mutes"); + for(String channelName : mcp.getMutes().keySet()) { + ConfigurationSection channelSection = muteSection.createSection(channelName); + channelSection.set("time", mcp.getMutes().get(channelName)); } + playerDataFileYamlConfiguration.set("blockedcommands", blockedCommands); playerDataFileYamlConfiguration.set("host", mcp.isHost()); playerDataFileYamlConfiguration.set("party", mcp.hasParty() ? mcp.getParty().toString() : ""); diff --git a/src/mineverse/Aust1n46/chat/listeners/ChatListener.java b/src/mineverse/Aust1n46/chat/listeners/ChatListener.java index 401f95c..d143655 100644 --- a/src/mineverse/Aust1n46/chat/listeners/ChatListener.java +++ b/src/mineverse/Aust1n46/chat/listeners/ChatListener.java @@ -35,9 +35,6 @@ import mineverse.Aust1n46.chat.versions.VersionHandler; //This class listens to chat through the chat event and handles the bulk of the chat channels and formatting. public class ChatListener implements Listener { - private static final int MILLISECONDS_PER_MINUTE = 60000; - private static final int MILLISECONDS_PER_SECOND = 1000; - private MineverseChat plugin = MineverseChat.getInstance(); @EventHandler(priority = EventPriority.NORMAL) @@ -213,22 +210,19 @@ public class ChatListener implements Listener { Boolean filterthis = true; mcp.addListening(eventChannel.getName()); if (mcp.isMuted(eventChannel.getName())) { - if (mcp.getMutes().get(eventChannel.getName()).intValue() > 0) { - int dateTimeMillis = Format.currentTimeMillis(); - String units = LocalizedMessage.UNITS_MINUTE_PLURAL.toString(); - int muteTimeMillis = mcp.getMutes().get(eventChannel.getName()).intValue(); - int remainingMuteTime = (muteTimeMillis - dateTimeMillis) / MILLISECONDS_PER_MINUTE; - if (remainingMuteTime <= 0) { - remainingMuteTime = 1; - } - if (remainingMuteTime == 1) { - units = LocalizedMessage.UNITS_MINUTE_SINGULAR.toString(); + if (mcp.getMutes().get(eventChannel.getName()).longValue() > 0) { + long dateTimeMillis = System.currentTimeMillis(); + long muteTimeMillis = mcp.getMutes().get(eventChannel.getName()).longValue(); + long remainingMuteTime = muteTimeMillis - dateTimeMillis; + if (remainingMuteTime < 1000) { + remainingMuteTime = 1000; } + String timeString = Format.parseTimeStringFromMillis(remainingMuteTime); mcp.getPlayer() .sendMessage(LocalizedMessage.CHANNEL_MUTED_TIMED.toString() .replace("{channel_color}", eventChannel.getColor()) .replace("{channel_name}", eventChannel.getName()) - .replace("{time}", String.valueOf(remainingMuteTime)).replace("{units}", units)); + .replace("{time}", timeString)); } else { mcp.getPlayer() @@ -240,7 +234,6 @@ public class ChatListener implements Listener { return; } Double chDistance = (double) 0; - int chCooldown = 0; String curColor = ""; if(eventChannel.hasPermission() && !mcp.getPlayer().hasPermission(eventChannel.getPermission())) { mcp.getPlayer().sendMessage(LocalizedMessage.CHANNEL_NO_PERMISSION.toString()); @@ -257,22 +250,20 @@ public class ChatListener implements Listener { curColor = eventChannel.getChatColor(); bungee = eventChannel.getBungee(); - int dateTimeSeconds = Format.currentTimeMillis() / MILLISECONDS_PER_SECOND; + long dateTimeSeconds = System.currentTimeMillis() / Format.MILLISECONDS_PER_SECOND; + int chCooldown = 0; if(eventChannel.hasCooldown()) { chCooldown = eventChannel.getCooldown(); } try { if (mcp.hasCooldown(eventChannel)) { - int cooldownTime = mcp.getCooldowns().get(eventChannel).intValue(); + long cooldownTime = mcp.getCooldowns().get(eventChannel).longValue(); if (dateTimeSeconds < cooldownTime) { - int remainingCooldownTime = cooldownTime - dateTimeSeconds; - String units = LocalizedMessage.UNITS_SECOND_PLURAL.toString(); - if (remainingCooldownTime == 1) { - units = LocalizedMessage.UNITS_SECOND_SINGULAR.toString(); - } + long remainingCooldownTime = cooldownTime - dateTimeSeconds; + String cooldownString = Format.parseTimeStringFromMillis(remainingCooldownTime * Format.MILLISECONDS_PER_SECOND); mcp.getPlayer().sendMessage(LocalizedMessage.CHANNEL_COOLDOWN.toString() - .replace("{cooldown}", String.valueOf(remainingCooldownTime)).replace("{units}", units)); + .replace("{cooldown}", cooldownString)); mcp.setQuickChat(false); bungee = false; return; @@ -289,25 +280,23 @@ public class ChatListener implements Listener { if (mcp.hasSpam(eventChannel) && plugin.getConfig().getConfigurationSection("antispam").getBoolean("enabled") && !mcp.getPlayer().hasPermission("venturechat.spam.bypass")) { - int spamcount = mcp.getSpam().get(eventChannel).get(0); - int spamtime = mcp.getSpam().get(eventChannel).get(1); - int spamtimeconfig = plugin.getConfig().getConfigurationSection("antispam").getInt("spamnumber"); - int mutedForTime = plugin.getConfig().getConfigurationSection("antispam").getInt("mutetime", 0); - int dateTime = Format.currentTimeMillis(); + long spamcount = mcp.getSpam().get(eventChannel).get(0); + long spamtime = mcp.getSpam().get(eventChannel).get(1); + long spamtimeconfig = plugin.getConfig().getConfigurationSection("antispam").getLong("spamnumber"); + String mutedForTime = plugin.getConfig().getConfigurationSection("antispam").getString("mutetime", "0"); + long dateTime = System.currentTimeMillis(); if (dateTimeSeconds < spamtime - + plugin.getConfig().getConfigurationSection("antispam").getInt("spamtime")) { + + plugin.getConfig().getConfigurationSection("antispam").getLong("spamtime")) { if (spamcount + 1 >= spamtimeconfig) { - if (mutedForTime > 0) { - mcp.addMute(eventChannel.getName(), dateTime + (mutedForTime * MILLISECONDS_PER_MINUTE)); - String units = LocalizedMessage.UNITS_MINUTE_PLURAL.toString(); - if (mutedForTime == 1) { - units = LocalizedMessage.UNITS_MINUTE_SINGULAR.toString(); - } + long time = Format.parseTimeStringToMillis(mutedForTime); + if (time > 0) { + mcp.addMute(eventChannel.getName(), dateTime + time); + String timeString = Format.parseTimeStringFromMillis(time); mcp.getPlayer() .sendMessage(LocalizedMessage.MUTE_PLAYER_SPAM_TIME.toString() .replace("{channel_color}", eventChannel.getColor()) .replace("{channel_name}", eventChannel.getName()) - .replace("{time}", String.valueOf(mutedForTime)).replace("{units}", units)); + .replace("{time}", timeString)); } else { mcp.addMute(eventChannel.getName(), 0); @@ -316,7 +305,7 @@ public class ChatListener implements Listener { .replace("{channel_color}", eventChannel.getColor()) .replace("{channel_name}", eventChannel.getName())); } - mcp.getSpam().get(eventChannel).set(0, 0); + mcp.getSpam().get(eventChannel).set(0, 0L); mcp.setQuickChat(false); return; } else { @@ -329,12 +318,12 @@ public class ChatListener implements Listener { mcp.getSpam().get(eventChannel).set(0, spamcount + 1); } } else { - mcp.getSpam().get(eventChannel).set(0, 1); + mcp.getSpam().get(eventChannel).set(0, 1L); mcp.getSpam().get(eventChannel).set(1, dateTimeSeconds); } } else { mcp.addSpam(eventChannel); - mcp.getSpam().get(eventChannel).add(0, 1); + mcp.getSpam().get(eventChannel).add(0, 1L); mcp.getSpam().get(eventChannel).add(1, dateTimeSeconds); } diff --git a/src/mineverse/Aust1n46/chat/listeners/LoginListener.java b/src/mineverse/Aust1n46/chat/listeners/LoginListener.java index d54c962..8ba79e0 100644 --- a/src/mineverse/Aust1n46/chat/listeners/LoginListener.java +++ b/src/mineverse/Aust1n46/chat/listeners/LoginListener.java @@ -57,7 +57,7 @@ public class LoginListener implements Listener { Set ignores = new HashSet(); Set listening = new HashSet(); listening.add(current.getName()); - HashMap mutes = new HashMap(); + HashMap mutes = new HashMap(); Set blockedCommands = new HashSet(); String jsonFormat = "Default"; mcp = new MineverseChatPlayer(uuid, name, current, ignores, listening, mutes, blockedCommands, false, null, true, true, name, jsonFormat, false, false, false, true, true); diff --git a/src/mineverse/Aust1n46/chat/localization/LocalizedMessage.java b/src/mineverse/Aust1n46/chat/localization/LocalizedMessage.java index 0a0fa5d..aceffe4 100644 --- a/src/mineverse/Aust1n46/chat/localization/LocalizedMessage.java +++ b/src/mineverse/Aust1n46/chat/localization/LocalizedMessage.java @@ -85,6 +85,10 @@ public enum LocalizedMessage { SPY_ON("SpyOn"), UNBLOCK_COMMAND_PLAYER("UnblockCommandPlayer"), UNBLOCK_COMMAND_SENDER("UnblockCommandSender"), + UNITS_DAY_PLURAL("UnitsDayPlural"), + UNITS_DAY_SINGULAR("UnitsDaySingular"), + UNITS_HOUR_PLURAL("UnitsHourPlural"), + UNITS_HOUR_SINGULAR("UnitsHourSingular"), UNITS_MINUTE_PLURAL("UnitsMinutePlural"), UNITS_MINUTE_SINGULAR("UnitsMinuteSingular"), UNITS_SECOND_PLURAL("UnitsSecondPlural"), diff --git a/src/mineverse/Aust1n46/chat/utilities/Format.java b/src/mineverse/Aust1n46/chat/utilities/Format.java index e29eb26..554bff0 100644 --- a/src/mineverse/Aust1n46/chat/utilities/Format.java +++ b/src/mineverse/Aust1n46/chat/utilities/Format.java @@ -21,6 +21,7 @@ import me.clip.placeholderapi.PlaceholderAPI; import mineverse.Aust1n46.chat.MineverseChat; import mineverse.Aust1n46.chat.api.MineverseChatPlayer; import mineverse.Aust1n46.chat.json.JsonFormat; +import mineverse.Aust1n46.chat.localization.LocalizedMessage; import mineverse.Aust1n46.chat.versions.VersionHandler; /** @@ -38,6 +39,11 @@ public class Format { private static final Pattern LEGACY_CHAT_COLOR_DIGITS_PATTERN = Pattern.compile("&([0-9])"); private static final Pattern LEGACY_CHAT_COLOR_PATTERN = Pattern.compile( "(?= Format.MILLISECONDS_PER_DAY) { + long numberOfDays = millis / Format.MILLISECONDS_PER_DAY; + millis -= Format.MILLISECONDS_PER_DAY * numberOfDays; + + String units = LocalizedMessage.UNITS_DAY_PLURAL.toString(); + if (numberOfDays == 1) { + units = LocalizedMessage.UNITS_DAY_SINGULAR.toString(); + } + timeString += numberOfDays + " " + units + " "; + } + + if(millis >= Format.MILLISECONDS_PER_HOUR) { + long numberOfHours = millis / Format.MILLISECONDS_PER_HOUR; + millis -= Format.MILLISECONDS_PER_HOUR * numberOfHours; - public static int currentTimeMillis() { - return (int) (System.currentTimeMillis() % Integer.MAX_VALUE); + String units = LocalizedMessage.UNITS_HOUR_PLURAL.toString(); + if (numberOfHours == 1) { + units = LocalizedMessage.UNITS_HOUR_SINGULAR.toString(); + } + timeString += numberOfHours + " " + units + " "; + } + + if(millis >= Format.MILLISECONDS_PER_MINUTE) { + long numberOfMinutes = millis / Format.MILLISECONDS_PER_MINUTE; + millis -= Format.MILLISECONDS_PER_MINUTE * numberOfMinutes; + + String units = LocalizedMessage.UNITS_MINUTE_PLURAL.toString(); + if (numberOfMinutes == 1) { + units = LocalizedMessage.UNITS_MINUTE_SINGULAR.toString(); + } + timeString += numberOfMinutes + " " + units + " "; + } + + if(millis >= Format.MILLISECONDS_PER_SECOND) { + long numberOfSeconds = millis / Format.MILLISECONDS_PER_SECOND; + millis -= Format.MILLISECONDS_PER_SECOND * numberOfSeconds; + + String units = LocalizedMessage.UNITS_SECOND_PLURAL.toString(); + if (numberOfSeconds == 1) { + units = LocalizedMessage.UNITS_SECOND_SINGULAR.toString(); + } + timeString += numberOfSeconds + " " + units; + } + return timeString.trim(); + } + + public static long parseTimeStringToMillis(String timeInput) { + long millis = 0L; + timeInput = timeInput.toLowerCase(); + char validChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'd', 'h', 'm', 's' }; + if(containsInvalidChars(validChars, timeInput)) { + return -1; + } + + long countDayTokens = timeInput.chars().filter(ch -> ch == 'd').count(); + long countHourTokens = timeInput.chars().filter(ch -> ch == 'h').count(); + long countMinuteTokens = timeInput.chars().filter(ch -> ch == 'm').count(); + long countSecondTokens = timeInput.chars().filter(ch -> ch == 's').count(); + if(countDayTokens > 1 || countHourTokens > 1 || countMinuteTokens > 1 || countSecondTokens > 1) { + return -1; + } + + int indexOfSecondToken = timeInput.indexOf("s"); + int indexOfMinuteToken = timeInput.indexOf("m"); + int indexOfHourToken = timeInput.indexOf("h"); + int indexOfDayToken = timeInput.indexOf("d"); + if(indexOfDayToken != -1) { + if((indexOfHourToken != -1 && indexOfHourToken < indexOfDayToken) || (indexOfMinuteToken != -1 && indexOfMinuteToken < indexOfDayToken) || (indexOfSecondToken != -1 && indexOfSecondToken < indexOfDayToken)) { + return -1; + } + } + if(indexOfHourToken != -1) { + if((indexOfMinuteToken != -1 && indexOfMinuteToken < indexOfHourToken) || (indexOfSecondToken != -1 && indexOfSecondToken < indexOfHourToken)) { + return -1; + } + } + if(indexOfMinuteToken != -1) { + if((indexOfSecondToken != -1 && indexOfSecondToken < indexOfMinuteToken)) { + return -1; + } + } + + if(indexOfDayToken != -1) { + int numberOfDays = Integer.parseInt(timeInput.substring(0, indexOfDayToken)); + timeInput = timeInput.substring(indexOfDayToken + 1); + millis += MILLISECONDS_PER_DAY * numberOfDays; + } + if(timeInput.length() > 0) { + indexOfHourToken = timeInput.indexOf("h"); + if(indexOfHourToken != -1) { + int numberOfHours = Integer.parseInt(timeInput.substring(0, indexOfHourToken)); + timeInput = timeInput.substring(indexOfHourToken + 1); + millis += MILLISECONDS_PER_HOUR * numberOfHours; + } + } + if(timeInput.length() > 0) { + indexOfMinuteToken = timeInput.indexOf("m"); + if(indexOfMinuteToken != -1) { + int numberOfMinutes = Integer.parseInt(timeInput.substring(0, indexOfMinuteToken)); + timeInput = timeInput.substring(indexOfMinuteToken + 1); + millis += MILLISECONDS_PER_MINUTE * numberOfMinutes; + } + } + if(timeInput.length() > 0) { + indexOfSecondToken = timeInput.indexOf("s"); + if(indexOfSecondToken != -1) { + int numberOfSeconds = Integer.parseInt(timeInput.substring(0, indexOfSecondToken)); + timeInput = timeInput.substring(indexOfSecondToken + 1); + millis += MILLISECONDS_PER_SECOND * numberOfSeconds; + } + } + return millis; + } + + private static boolean containsInvalidChars(char[] validChars, String validate) { + for(char c : validate.toCharArray()) { + boolean isValidChar = false; + for(char v : validChars) { + if(c == v) { + isValidChar = true; + } + } + if(!isValidChar) { + return true; + } + } + return false; } }