From 855024075440a58e50079f9561371709d3c69bac Mon Sep 17 00:00:00 2001 From: Taah Date: Sun, 27 Feb 2022 22:28:00 -0800 Subject: [PATCH] - Start the indefinite ban system, currently working with manually adding to the configuration. - /plex reload reloads the indefinite bans - Fixed bug where AdminChat showed up with symbols still and not colorized in adminchat --- src/main/java/dev/plex/Plex.java | 4 + .../java/dev/plex/command/impl/PlexCMD.java | 8 ++ .../dev/plex/listener/impl/BanListener.java | 21 +++++ .../java/dev/plex/punishment/Punishment.java | 5 ++ .../plex/punishment/PunishmentManager.java | 84 +++++++++++++++++-- .../dev/plex/storage/RedisConnection.java | 44 +++------- src/main/java/dev/plex/util/PlexLog.java | 27 +++++- src/main/java/dev/plex/util/PlexUtils.java | 2 +- src/main/resources/indefbans.yml | 13 +++ src/main/resources/messages.yml | 1 + 10 files changed, 166 insertions(+), 43 deletions(-) create mode 100644 src/main/resources/indefbans.yml diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index 27210a4..9e04cc9 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -35,6 +35,7 @@ public class Plex extends JavaPlugin private static Plex plugin; public Config config; public Config messages; + public Config indefBans; private StorageType storageType = StorageType.SQLITE; private SQLConnection sqlConnection; @@ -64,6 +65,7 @@ public class Plex extends JavaPlugin plugin = this; config = new Config(this, "config.yml"); messages = new Config(this, "messages.yml"); + indefBans = new Config(this, "indefbans.yml"); sqlConnection = new SQLConnection(); mongoConnection = new MongoConnection(); @@ -75,6 +77,7 @@ public class Plex extends JavaPlugin { config.load(); messages.load(); + indefBans.load(); system = config.getString("commands.permissions"); try @@ -122,6 +125,7 @@ public class Plex extends JavaPlugin PlexLog.log("Rank Manager initialized"); punishmentManager = new PunishmentManager(); + punishmentManager.mergeIndefiniteBans(); // banManager = new BanManager(); PlexLog.log("Punishment System initialized"); diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index a6ad370..ad51b88 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -31,12 +31,20 @@ public class PlexCMD extends PlexCommand if (args[0].equalsIgnoreCase("reload")) { checkRank(sender, Rank.SENIOR_ADMIN, "plex.reload"); + Plex.get().config.load(); send(sender, "Reloaded config file"); + Plex.get().messages.load(); send(sender, "Reloaded messages file"); + + Plex.get().indefBans.load(); + Plex.get().getPunishmentManager().mergeIndefiniteBans(); + send(sender, "Reloaded indefinite bans"); + Plex.get().getRankManager().importDefaultRanks(); send(sender, "Imported ranks"); + send(sender, "Plex successfully reloaded."); } else if (args[0].equalsIgnoreCase("redis")) diff --git a/src/main/java/dev/plex/listener/impl/BanListener.java b/src/main/java/dev/plex/listener/impl/BanListener.java index f66fdb8..fd70b34 100644 --- a/src/main/java/dev/plex/listener/impl/BanListener.java +++ b/src/main/java/dev/plex/listener/impl/BanListener.java @@ -13,6 +13,27 @@ public class BanListener extends PlexListener @EventHandler public void onPreLogin(AsyncPlayerPreLoginEvent event) { + if (plugin.getPunishmentManager().isIndefUUIDBanned(event.getUniqueId())) + { + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, + Punishment.generateIndefBanMessage("UUID")); + return; + } + + if (plugin.getPunishmentManager().isIndefIPBanned(event.getAddress().getHostAddress())) + { + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, + Punishment.generateIndefBanMessage("IP")); + return; + } + + if (plugin.getPunishmentManager().isIndefUserBanned(event.getName())) + { + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, + Punishment.generateIndefBanMessage("username")); + return; + } + if (plugin.getPunishmentManager().isBanned(event.getUniqueId())) { PunishedPlayer player = PlayerCache.getPunishedPlayer(event.getUniqueId()); diff --git a/src/main/java/dev/plex/punishment/Punishment.java b/src/main/java/dev/plex/punishment/Punishment.java index 3aadd6f..eb31025 100644 --- a/src/main/java/dev/plex/punishment/Punishment.java +++ b/src/main/java/dev/plex/punishment/Punishment.java @@ -49,6 +49,11 @@ public class Punishment DATE_FORMAT.format(punishment.getEndDate()), punishment.getPunisher() == null ? "CONSOLE" : MojangUtils.getInfo(punishment.getPunisher().toString()).getUsername()); } + public static Component generateIndefBanMessage(String type) + { + return PlexUtils.messageComponent("indefBanMessage", type, banUrl); + } + public static Punishment fromJson(String json) { return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()).create().fromJson(json, Punishment.class); diff --git a/src/main/java/dev/plex/punishment/PunishmentManager.java b/src/main/java/dev/plex/punishment/PunishmentManager.java index 79b93ef..5ab1adb 100644 --- a/src/main/java/dev/plex/punishment/PunishmentManager.java +++ b/src/main/java/dev/plex/punishment/PunishmentManager.java @@ -2,9 +2,12 @@ package dev.plex.punishment; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; import dev.plex.Plex; import dev.plex.PlexBase; import dev.plex.cache.PlayerCache; +import dev.plex.player.PlexPlayer; import dev.plex.player.PunishedPlayer; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; @@ -12,6 +15,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; +import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -21,6 +25,7 @@ import java.util.UUID; import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.json.JSONObject; import org.json.JSONTokener; @@ -28,6 +33,77 @@ import redis.clients.jedis.Jedis; public class PunishmentManager extends PlexBase { + private final List bannedIPs = Lists.newArrayList(); + private final List bannedUsernames = Lists.newArrayList(); + private final List bannedUUIDs = Lists.newArrayList(); + + public void mergeIndefiniteBans() + { + this.bannedUsernames.clear(); + this.bannedIPs.clear(); + this.bannedUUIDs.clear(); + + this.bannedUUIDs.addAll(Plex.get().indefBans.getStringList("uuids").stream().filter(string -> + { + try + { + UUID uuid = UUID.fromString(string); + return true; + } catch (IllegalArgumentException e) + { + return false; + } + }).map(UUID::fromString).toList()); + + this.bannedIPs.addAll(Plex.get().indefBans.getStringList("ips")); + this.bannedUsernames.addAll(Plex.get().indefBans.getStringList("usernames")); + + PlexLog.log("Loaded {0} UUID(s), {1} IP(s), and {2} username(s) into the indefinite banned list", this.bannedUUIDs.size(), this.bannedIPs.size(), this.bannedUsernames.size()); + + if (Plex.get().getRedisConnection().isEnabled()) + { + PlexLog.log("Resetting redis indefinite bans lists and asynchronously uploading from configuration"); + Plex.get().getRedisConnection().runAsync(jedis -> { + jedis.set("indefbanned-uuids", new Gson().toJson(this.bannedUUIDs)); + jedis.set("indefbanned-ips", new Gson().toJson(this.bannedIPs)); + jedis.set("indefbanned-users", new Gson().toJson(this.bannedUsernames)); + this.bannedIPs.clear(); + this.bannedUsernames.clear(); + this.bannedUUIDs.clear(); + }); + } + } + + public boolean isIndefUUIDBanned(UUID uuid) + { + if (Plex.get().getRedisConnection().isEnabled()) + { + List uuids = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-uuids"), new TypeToken>(){}.getType()); + return uuids.contains(uuid); + } + return this.bannedUUIDs.contains(uuid); + } + + public boolean isIndefIPBanned(String ip) + { + if (Plex.get().getRedisConnection().isEnabled()) + { + List ips = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-ips"), new TypeToken>(){}.getType()); + return ips.contains(ip); + } + return this.bannedIPs.contains(ip); + } + + public boolean isIndefUserBanned(String username) + { + if (Plex.get().getRedisConnection().isEnabled()) + { + List users = new Gson().fromJson(Plex.get().getRedisConnection().getJedis().get("indefbanned-users"), new TypeToken>(){}.getType()); + return users.contains(username); + } + return this.bannedUsernames.contains(username); + } + public void insertPunishment(PunishedPlayer player, Punishment punishment) { File file = player.getPunishmentsFile(); @@ -123,7 +199,7 @@ public class PunishmentManager extends PlexBase } }); } - catch (IllegalArgumentException e) + catch (IllegalArgumentException ignored) { } @@ -195,12 +271,6 @@ public class PunishmentManager extends PlexBase private void issuePunishment(PunishedPlayer player, Punishment punishment) { - /*if (punishment.getType() == PunishmentType.BAN) - { -// Ban ban = new Ban(punishment.getPunished(), (punishment.getPunisher() == null ? null : punishment.getPunisher()), "", punishment.getReason(), punishment.getEndDate()); -// Plex.get().getBanManager().executeBan(ban); - } - else*/ if (punishment.getType() == PunishmentType.FREEZE) { player.setFrozen(true); diff --git a/src/main/java/dev/plex/storage/RedisConnection.java b/src/main/java/dev/plex/storage/RedisConnection.java index 2afafff..64d8fc7 100644 --- a/src/main/java/dev/plex/storage/RedisConnection.java +++ b/src/main/java/dev/plex/storage/RedisConnection.java @@ -4,42 +4,12 @@ import dev.plex.PlexBase; import dev.plex.util.PlexLog; import redis.clients.jedis.Jedis; +import java.util.function.Consumer; + public class RedisConnection extends PlexBase { private Jedis jedis; - /*public JedisPool openPool() - { - JedisPoolConfig jedisConfig = new JedisPoolConfig(); - //jedisConfig.setMaxIdle(10); - //jedisConfig.setMaxTotal(100); - ClassLoader previous = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(RedisConnection.class.getClassLoader()); - this.pool = new JedisPool(jedisConfig, plugin.config.getString("data.side.hostname"), - plugin.config.getInt("data.side.port")); - Thread.currentThread().setContextClassLoader(previous); - PlexLog.log("Connected to Redis!"); - return pool; - } - - public Jedis getJedis() - { - try - { - this.jedis = pool.getResource(); - } - catch (Exception ex) - { - PlexLog.error("An error occurred with Redis."); - ex.printStackTrace(); - } - if (plugin.config.getBoolean("data.side.auth")) - { - jedis.auth(plugin.config.getString("data.side.password")); - } - return jedis; - }*/ - public Jedis getJedis() { try @@ -60,6 +30,16 @@ public class RedisConnection extends PlexBase return jedis; } + public void runAsync(Consumer jedisConsumer) + { + new Thread(() -> { + try (Jedis jedis = getJedis()) + { + jedisConsumer.accept(jedis); + } + }).start(); + } + public final boolean isEnabled() { return plugin.config.getBoolean("data.side.enabled"); diff --git a/src/main/java/dev/plex/util/PlexLog.java b/src/main/java/dev/plex/util/PlexLog.java index 878b44b..2ba7a48 100644 --- a/src/main/java/dev/plex/util/PlexLog.java +++ b/src/main/java/dev/plex/util/PlexLog.java @@ -8,18 +8,39 @@ public class PlexLog extends PlexBase { private static final boolean debugEnabled = plugin.config.getBoolean("debug"); - public static void log(String message) + public static void log(String message, Object... strings) { + for (int i = 0; i < strings.length; i++) + { + if (message.contains("{" + i + "}")) + { + message = message.replace("{" + i + "}", strings[i].toString()); + } + } Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.YELLOW + "[Plex] " + ChatColor.GRAY + "%s", message)); } - public static void error(String message) + public static void error(String message, Object... strings) { + for (int i = 0; i < strings.length; i++) + { + if (message.contains("{" + i + "}")) + { + message = message.replace("{" + i + "}", strings[i].toString()); + } + } Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.RED + "[Plex Error] " + ChatColor.GOLD + "%s", message)); } - public static void debug(String message) + public static void debug(String message, Object... strings) { + for (int i = 0; i < strings.length; i++) + { + if (message.contains("{" + i + "}")) + { + message = message.replace("{" + i + "}", strings[i].toString()); + } + } if (debugEnabled) { Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.DARK_PURPLE + "[Plex Debug] " + ChatColor.GOLD + "%s", message)); diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index bb87349..7d8bfee 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -158,7 +158,7 @@ public class PlexUtils extends PlexBase public static Component messageComponent(String entry, Object... objects) { - return MiniMessage.miniMessage().parse(messageString(entry, objects)); + return MiniMessage.miniMessage().parse(LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacyAmpersand().deserialize(messageString(entry, objects)))); } public static String messageString(String entry, Object... objects) diff --git a/src/main/resources/indefbans.yml b/src/main/resources/indefbans.yml new file mode 100644 index 0000000..534c8c3 --- /dev/null +++ b/src/main/resources/indefbans.yml @@ -0,0 +1,13 @@ +# Plex Indefinite Bans File +# Players with their UUID / IP / Usernames in here will be indefinitely banned until removed + +# List of permanently banned UUIDs +# If you want to get someone's UUID, use https://api.ashcon.app/mojang/v2/user/ +uuids: [] + +# List of permanently banned IP addresses +ips: [] + +# List of permanently banned UUIDs +# If you want to get someone's username, use https://api.ashcon.app/mojang/v2/user/, or just remember it +usernames: [] \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index a802a04..d731d05 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -15,6 +15,7 @@ # 3. Expiry # 4. Punisher banMessage: "You have been banned! You may appeal at .\nReason: \nEnd date: \nBanned by: " +indefBanMessage: "Your is currently banned! You may appeal at ." test: "this is a test message!" # 1. The command sender's username variableTest: "variable test with !"