diff --git a/src/main/java/dev/plex/cache/player/PlayerCache.java b/src/main/java/dev/plex/cache/player/PlayerCache.java index 35eba1d..05c6f07 100644 --- a/src/main/java/dev/plex/cache/player/PlayerCache.java +++ b/src/main/java/dev/plex/cache/player/PlayerCache.java @@ -26,7 +26,6 @@ public class PlayerCache // { // return punishedPlayerMap; // } - public static Map getPlexPlayerMap() { return plexPlayerMap; diff --git a/src/main/java/dev/plex/cache/sql/SQLPunishment.java b/src/main/java/dev/plex/cache/sql/SQLPunishment.java index b519166..84fbce6 100644 --- a/src/main/java/dev/plex/cache/sql/SQLPunishment.java +++ b/src/main/java/dev/plex/cache/sql/SQLPunishment.java @@ -47,7 +47,8 @@ public class SQLPunishment punishment.setIp(set.getString("ip")); punishments.add(punishment); } - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -75,7 +76,8 @@ public class SQLPunishment punishment.setIp(set.getString("ip")); punishments.add(punishment); } - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -102,7 +104,8 @@ public class SQLPunishment statement.setLong(9, punishment.getEndDate().toInstant(ZoneOffset.UTC).toEpochMilli()); PlexLog.debug("Executing punishment"); statement.execute(); - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -121,7 +124,8 @@ public class SQLPunishment statement.setString(3, uuid.toString()); statement.setString(4, PunishmentType.BAN.name()); statement.executeUpdate(); - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/dev/plex/command/impl/BanCMD.java b/src/main/java/dev/plex/command/impl/BanCMD.java index 1730930..17780bf 100644 --- a/src/main/java/dev/plex/command/impl/BanCMD.java +++ b/src/main/java/dev/plex/command/impl/BanCMD.java @@ -14,6 +14,9 @@ import dev.plex.punishment.PunishmentType; import dev.plex.rank.enums.Rank; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; import net.kyori.adventure.text.Component; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; @@ -22,10 +25,6 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.time.LocalDateTime; -import java.util.List; -import java.util.UUID; - @CommandParameters(name = "ban", usage = "/ [reason]", aliases = "offlineban,gtfo", description = "Bans a player, offline or online") @CommandPermissions(level = Rank.ADMIN, permission = "plex.ban", source = RequiredCommandSource.ANY) @@ -75,7 +74,8 @@ public class BanCMD extends PlexCommand { reason = StringUtils.join(args, " ", 1, args.length); punishment.setReason(reason); - } else + } + else { punishment.setReason("No reason provided."); } diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index fca8344..51c4c1d 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -94,11 +94,13 @@ public class PlexPlayer if (player.isOp()) { return Rank.OP; - } else + } + else { return Rank.NONOP; } - } else + } + else { return Rank.valueOf(rank.toUpperCase()); } diff --git a/src/main/java/dev/plex/punishment/PunishmentManager.java b/src/main/java/dev/plex/punishment/PunishmentManager.java index fb1a1da..1553524 100644 --- a/src/main/java/dev/plex/punishment/PunishmentManager.java +++ b/src/main/java/dev/plex/punishment/PunishmentManager.java @@ -100,41 +100,15 @@ public class PunishmentManager extends PlexBase plexPlayer.getPunishments().add(punishment); if (Plex.get().getStorageType() == StorageType.MONGODB) { - CompletableFuture.runAsync(() -> { + CompletableFuture.runAsync(() -> + { DataUtils.update(plexPlayer); }); - } else { + } + else + { Plex.get().getSqlPunishment().insertPunishment(punishment); } - - /*File file = player.getPunishmentsFile(); - - try - { - if (isNotEmpty(file)) - { - JSONTokener tokener = new JSONTokener(new FileInputStream(file)); - JSONObject object = new JSONObject(tokener); - object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON()); - addToRedis(player, file, object); - } - else - { - JSONObject object = new JSONObject(); - Map> punishments = Maps.newHashMap(); - - List punishmentList = Lists.newArrayList(); - punishmentList.add(punishment.toJSON()); - - punishments.put("punishments", punishmentList); - object.put(punishment.getPunished().toString(), punishments); - addToRedis(player, file, object); - } - } - catch (IOException e) - { - e.printStackTrace(); - }*/ } private boolean isNotEmpty(File file) @@ -152,7 +126,8 @@ public class PunishmentManager extends PlexBase public CompletableFuture isAsyncBanned(UUID uuid) { - return CompletableFuture.supplyAsync(() -> { + return CompletableFuture.supplyAsync(() -> + { PlexPlayer player = DataUtils.getPlayer(uuid); player.loadPunishments(); return player.getPunishments().stream().anyMatch(punishment -> punishment.getType() == PunishmentType.BAN && punishment.isActive()); @@ -173,68 +148,21 @@ public class PunishmentManager extends PlexBase { if (Plex.get().getStorageType() == StorageType.MONGODB) { - return CompletableFuture.supplyAsync(() -> { + return CompletableFuture.supplyAsync(() -> + { List players = Plex.get().getMongoPlayerData().getPlayers(); return players.stream().map(PlexPlayer::getPunishments).flatMap(Collection::stream).filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList(); }); - } else { + } + else + { CompletableFuture> future = new CompletableFuture<>(); - Plex.get().getSqlPunishment().getPunishments().whenComplete((punishments, throwable) -> { + Plex.get().getSqlPunishment().getPunishments().whenComplete((punishments, throwable) -> + { future.complete(punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList()); }); return future; } - /*List punishments = Lists.newArrayList(); - - if (Plex.get().getRedisConnection().isEnabled()) - { - Jedis jedis = Plex.get().getRedisConnection().getJedis(); - for (String key : jedis.keys("*")) - { - try - { - UUID uuid = UUID.fromString(key); - String jsonPunishmentString = jedis.get(uuid.toString()); - JSONObject object = new JSONObject(jsonPunishmentString); - for (Object json : object.getJSONObject(uuid.toString()).getJSONArray("punishments")) - { - Punishment punishment = Punishment.fromJson(json.toString()); - if (punishment.isActive() && punishment.getType() == PunishmentType.BAN) - { - punishments.add(punishment); - } - } - } - catch (IllegalArgumentException ignored) - { - } - } - } - else - { - File fileDir = new File(plugin.getDataFolder() + File.separator + "punishments"); - for (File file : Objects.requireNonNull(fileDir.listFiles())) - { - if (isNotEmpty(file)) - { - try (FileInputStream fis = new FileInputStream(file)) - { - JSONTokener tokener = new JSONTokener(fis); - JSONObject object = new JSONObject(tokener); - object.keySet().stream().findFirst().ifPresent(key -> - { - JSONObject obj = object.getJSONObject(key); - punishments.addAll(obj.getJSONArray("punishments").toList().stream().map(Object::toString).map(Punishment::fromJson).filter(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN).toList()); - }); - } - catch (IOException e) - { - e.printStackTrace(); - } - } - } - }*/ -// return punishments; } public void unban(Punishment punishment) @@ -246,61 +174,18 @@ public class PunishmentManager extends PlexBase { if (Plex.get().getStorageType() == StorageType.MONGODB) { - return CompletableFuture.runAsync(() -> { - PlexPlayer plexPlayer = DataUtils.getPlayer(uuid); - plexPlayer.setPunishments(plexPlayer.getPunishments().stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN) - .peek(punishment -> punishment.setActive(false)).collect(Collectors.toList())); - DataUtils.update(plexPlayer); + return CompletableFuture.runAsync(() -> + { + PlexPlayer plexPlayer = DataUtils.getPlayer(uuid); + plexPlayer.setPunishments(plexPlayer.getPunishments().stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN) + .peek(punishment -> punishment.setActive(false)).collect(Collectors.toList())); + DataUtils.update(plexPlayer); }); - } else { + } + else + { return Plex.get().getSqlPunishment().removeBan(uuid); } - /*if (Plex.get().getRedisConnection().isEnabled()) - { - Jedis jedis = Plex.get().getRedisConnection().getJedis(); - - String jsonPunishmentString = jedis.get(uuid.toString()); - JSONObject object = new JSONObject(jsonPunishmentString); - setActive(uuid, object, false); - jedis.set(uuid.toString(), object.toString()); - } - - PunishedPlayer player = PlayerCache.getPunishedPlayer(uuid); - - File file = player.getPunishmentsFile(); - if (isNotEmpty(file)) - { - try (FileInputStream fis = new FileInputStream(file)) - { - JSONTokener tokener = new JSONTokener(fis); - JSONObject object = new JSONObject(tokener); - setActive(uuid, object, false); - FileWriter writer = new FileWriter(file); - writer.append(object.toString()); - writer.flush(); - writer.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } - }*/ - } - - private void setActive(UUID uuid, JSONObject object, boolean active) - { - List punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); - while (punishments.stream().anyMatch(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN)) - { - punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).findFirst().ifPresent(punishment -> - { - int index = punishments.indexOf(punishment); - punishment.setActive(active); - punishments.set(index, punishment); - }); - } - object.getJSONObject(uuid.toString()).getJSONArray("punishments").clear(); - object.getJSONObject(uuid.toString()).getJSONArray("punishments").putAll(punishments.stream().map(Punishment::toJSON).collect(Collectors.toList())); } private void doPunishment(PlexPlayer player, Punishment punishment) diff --git a/src/main/java/dev/plex/storage/SQLConnection.java b/src/main/java/dev/plex/storage/SQLConnection.java index 688cf4b..b499377 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -39,7 +39,8 @@ public class SQLConnection extends PlexBase { dataSource.setJdbcUrl("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath()); plugin.setStorageType(StorageType.SQLITE); - } else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb")) + } + else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb")) { Class.forName("org.mariadb.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mariadb://" + host + ":" + port + "/" + database); @@ -47,7 +48,8 @@ public class SQLConnection extends PlexBase dataSource.setPassword(password); Plex.get().setStorageType(StorageType.MARIADB); } - } catch (ClassNotFoundException throwables) + } + catch (ClassNotFoundException throwables) { throwables.printStackTrace(); } @@ -76,7 +78,8 @@ public class SQLConnection extends PlexBase "`active` BOOLEAN, " + "`endDate` BIGINT" + ");").execute(); - } catch (SQLException throwables) + } + catch (SQLException throwables) { throwables.printStackTrace(); } @@ -87,7 +90,8 @@ public class SQLConnection extends PlexBase try { return dataSource.getConnection(); - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 140651a..c9e566a 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -76,11 +76,13 @@ public class PlexUtils extends PlexBase if (Plex.get().getStorageType() == StorageType.MARIADB) { PlexLog.log("Successfully enabled MySQL!"); - } else if (Plex.get().getStorageType() == StorageType.SQLITE) + } + else if (Plex.get().getStorageType() == StorageType.SQLITE) { PlexLog.log("Successfully enabled SQLite!"); } - } catch (SQLException e) + } + catch (SQLException e) { if (Plex.get().getMongoConnection().getDatastore() != null) { @@ -191,10 +193,12 @@ public class PlexUtils extends PlexBase if (config.getString(path) == null) { color = def; - } else if (ChatColor.getByChar(config.getString(path)) == null) + } + else if (ChatColor.getByChar(config.getString(path)) == null) { color = def; - } else + } + else { color = ChatColor.getByChar(config.getString(path)); } @@ -240,13 +244,14 @@ public class PlexUtils extends PlexBase private static void readGameRules(World world, String s) { String gameRule = s.split(";")[0]; - T value = (T) s.split(";")[1]; - GameRule rule = (GameRule) GameRule.getByName(gameRule); + T value = (T)s.split(";")[1]; + GameRule rule = (GameRule)GameRule.getByName(gameRule); if (rule != null && check(value).getClass().equals(rule.getType())) { world.setGameRule(rule, value); PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value); - } else + } + else { PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value)); } @@ -286,7 +291,7 @@ public class PlexUtils extends PlexBase try { URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + HttpURLConnection connection = (HttpURLConnection)u.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; @@ -298,7 +303,8 @@ public class PlexUtils extends PlexBase in.close(); connection.disconnect(); return new JSONParser().parse(content.toString()); - } catch (IOException | ParseException ex) + } + catch (IOException | ParseException ex) { return null; } @@ -307,13 +313,13 @@ public class PlexUtils extends PlexBase public static UUID getFromName(String name) { JSONObject profile; - profile = (JSONObject) simpleGET("https://api.ashcon.app/mojang/v2/user/" + name); + profile = (JSONObject)simpleGET("https://api.ashcon.app/mojang/v2/user/" + name); if (profile == null) { PlexLog.error("Profile from Ashcon API returned null!"); return null; } - String uuidString = (String) profile.get("uuid"); + String uuidString = (String)profile.get("uuid"); return UUID.fromString(uuidString); } @@ -331,12 +337,14 @@ public class PlexUtils extends PlexBase { Class clazz = Class.forName(info.getName()); classes.add(clazz); - } catch (ClassNotFoundException ex) + } + catch (ClassNotFoundException ex) { PlexLog.error("Unable to find class " + info.getName() + " in " + packageName); } }); - } catch (IOException ex) + } + catch (IOException ex) { PlexLog.error("Something went wrong while fetching classes from " + packageName); throw new RuntimeException(ex); @@ -353,7 +361,7 @@ public class PlexUtils extends PlexBase { if (clazz.getSuperclass() == subType || Arrays.asList(clazz.getInterfaces()).contains(subType)) { - classes.add((Class) clazz); + classes.add((Class)clazz); } }); return Collections.unmodifiableSet(classes);