From d8829d49316ba994ab22da6e9d20f7efb7393d95 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 30 Apr 2021 13:52:02 -0500 Subject: [PATCH] Release Candidate 3 --- .../simplexdev/simplexcore/ban/Ban.java | 62 ++++++++++++-- .../simplexcore/ban/BanFactory.java | 3 +- .../simplexcore/ban/BanManager.java | 5 +- .../simplexdev/simplexcore/sql/Database.java | 81 ++++++++++++------- 4 files changed, 111 insertions(+), 40 deletions(-) diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/Ban.java b/src/main/java/io/github/simplexdev/simplexcore/ban/Ban.java index 803182b..9a51f04 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/Ban.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/Ban.java @@ -35,18 +35,50 @@ public abstract class Ban implements IBan { private final String banId; private final String banReason; + /** + * Creates a new Ban Entry. + * @param plugin Your plugin instance + * @param player The player to be banned + * @param sender The command sender. + */ public Ban(SimplexModule plugin, Player player, CommandSender sender) { this(plugin, player, sender, BanType.TEMPORARY); } + /** + * Creates a new Ban Entry. + * @param plugin Your plugin instance. + * @param player The player to be banned. + * @param sender The command sender. + * @param type The type of ban. See {@link BanType}. + */ public Ban(SimplexModule plugin, Player player, CommandSender sender, BanType type) { this(plugin, player, sender, type, TickedTime.DAY); } + /** + * Creates a new Ban Entry. + * @param plugin Your plugin instance. + * @param player The player to be banned. + * @param sender The command sender. + * @param type The type of ban. See {@link BanType}. + * @param banDuration How long the ban should last. + */ public Ban(SimplexModule plugin, Player player, CommandSender sender, BanType type, long banDuration) { this(plugin, player, sender, type, Utilities.generateBanId(type), Messages.BAN.getMessage(), new Date(), banDuration); } + /** + * Creates a new Ban Entry. + * @param plugin Your plugin instance. + * @param player The player to be banned. + * @param sender The command sender. + * @param type The type of ban. See {@link BanType}. + * @param banId A custom Ban ID. + * @param banReason The reason why the user was banned. + * @param banDate The date when the ban was created. + * @param banDuration How long the ban should last. + */ public Ban(SimplexModule plugin, Player player, CommandSender sender, BanType type, String banId, String banReason, Date banDate, long banDuration) { this.plugin = plugin; this.player = player; @@ -58,13 +90,16 @@ public abstract class Ban implements IBan { this.banDate = banDate; } + /** + * Writes the Ban to a file. + * @param separateFiles Whether or not to create individual files for players or store them all in one bans.yml file. + */ public void writeToFile(boolean separateFiles) { - File fileLocation = new File(SimplexCorePlugin.getInstance().getParentFolder(), "bans"); + File fileLocation = new File(plugin.getParentFolder(), "bans"); if (separateFiles) { - Yaml yaml = new YamlFactory(SimplexCorePlugin.getInstance()).from(null, fileLocation, player.getName() + ".yml"); - yaml.getConfig().createSection(getOffender().toString()); - ConfigurationSection section = yaml.getConfigurationSection(getOffender()::toString); + Yaml yaml = new YamlFactory(plugin).from(null, fileLocation, player.getName() + ".yml"); + ConfigurationSection section = yaml.getConfig().createSection(getOffender().toString()); section.set("name", player.getName()); section.set("ban_id", banId); section.set("sender", sender.getName()); @@ -75,12 +110,25 @@ public abstract class Ban implements IBan { try { yaml.save(); } catch (IOException e) { - SimplexCorePlugin.getInstance().getLogger().severe(e.getMessage()); + plugin.getLogger().severe(e.getMessage()); } yaml.reload(); } else { - // TODO: Write to a single file as separate sections per UUID. - Yaml yaml = new YamlFactory(SimplexCorePlugin.getInstance()).from(null, fileLocation, "bans.yml"); + Yaml yaml = new YamlFactory(plugin).from(null, fileLocation, "bans.yml"); + ConfigurationSection section = yaml.getConfig().createSection(getOffender().toString()); + section.set("name", player.getName()); + section.set("ban_id", banId); + section.set("sender", sender.getName()); + section.set("reason", banReason); + section.set("duration", banDuration); + section.set("date", banDate.getTime()); + section.set("type", type.toString()); + try { + yaml.save(); + } catch (IOException ex) { + plugin.getLogger().severe(ex.getMessage()); + } + yaml.reload(); } } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java index ac64608..3ee27fa 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java @@ -4,6 +4,7 @@ import io.github.simplexdev.api.IBan; import io.github.simplexdev.api.func.VoidSupplier; import io.github.simplexdev.simplexcore.chat.Messages; import io.github.simplexdev.simplexcore.config.Yaml; +import io.github.simplexdev.simplexcore.config.YamlFactory; import io.github.simplexdev.simplexcore.module.SimplexModule; import io.github.simplexdev.simplexcore.utils.TickedTime; import io.github.simplexdev.simplexcore.utils.Utilities; @@ -67,7 +68,7 @@ public final class BanFactory { } /** - * Creates a new instance of the abstract class Ban. + * Creates a new Ban Entry. * * @return A new ban instance. */ diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/BanManager.java b/src/main/java/io/github/simplexdev/simplexcore/ban/BanManager.java index e936d6b..1b46305 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/BanManager.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/BanManager.java @@ -17,12 +17,13 @@ import java.util.concurrent.atomic.AtomicReference; import static org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result; -public class BanManager extends SimplexListener { +public final class BanManager extends SimplexListener { private final Map banMap = new HashMap<>(); private final SimplexModule plugin; - public BanManager(SimplexModule plugin) { + BanManager(SimplexModule plugin) { this.plugin = plugin; + register(this, plugin); } public void addBan(Ban ban) { diff --git a/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java b/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java index 9bc989d..481fac2 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java +++ b/src/main/java/io/github/simplexdev/simplexcore/sql/Database.java @@ -9,8 +9,9 @@ public class Database { public static void createTable(String table, String columns) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " - + table + " (" + columns + ");"); + ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS ? (?);"); + ps.setString(1, table); + ps.setString(2, columns); ps.executeUpdate(); } catch(SQLException ex) { // TODO @@ -20,8 +21,10 @@ public class Database { public static void insertData(String columns, String values, String table) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("INSERT INTO " - + table + "(" + columns + ") VALUES (" + values + ");"); + ps = MySQL.getConnection().prepareStatement("INSERT INTO ?(?) VALUES (?);"); + ps.setString(1, table); + ps.setString(2, columns); + ps.setString(3, values); ps.executeUpdate(); } catch(SQLException ex) { // TODO @@ -31,8 +34,10 @@ public class Database { public static void deleteData(String table, String column, Object value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("DELETE FROM " + table + " WHERE " + column + "=?"); - ps.setObject(1, value); + ps = MySQL.getConnection().prepareStatement("DELETE FROM ? WHERE ?=?"); + ps.setString(1, table); + ps.setString(2, column); + ps.setObject(3, value); ps.executeUpdate(); } catch(SQLException ex) { // TODO @@ -42,9 +47,12 @@ public class Database { public static void set(String table, String gate, Object gate_value, String column, Object value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("UPDATE " + table + " SET " + column + "=? WHERE " + gate + "=?"); - ps.setObject(1, value); - ps.setObject(2, gate_value); + ps = MySQL.getConnection().prepareStatement("UPDATE ? SET ?=? WHERE ?=?"); + ps.setString(1, table); + ps.setString(2, column); + ps.setString(3, gate); + ps.setObject(4, value); + ps.setObject(5, gate_value); ps.executeUpdate(); } catch(SQLException ex) { // TODO @@ -54,9 +62,10 @@ public class Database { public static boolean exists(String table, String column, Object value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT * FROM " - + table + " WHERE " + column + "=?"); - ps.setObject(1, value); + ps = MySQL.getConnection().prepareStatement("SELECT * FROM ? WHERE ?=?"); + ps.setString(1, table); + ps.setString(2, column); + ps.setObject(3, value); ResultSet results = ps.executeQuery(); return results.next(); @@ -69,9 +78,11 @@ public class Database { public static String getString(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); String toReturn; @@ -89,9 +100,11 @@ public class Database { public static int getInt(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); int toReturn; @@ -109,9 +122,11 @@ public class Database { public static Double getDouble(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); double toReturn; @@ -129,9 +144,11 @@ public class Database { public static long getLong(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); long toReturn; @@ -149,9 +166,11 @@ public class Database { public static byte getByte(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); byte toReturn; @@ -169,9 +188,11 @@ public class Database { public static Object get(String table, String column, String gate, Object gate_value) { PreparedStatement ps; try { - ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table - + " WHERE " + gate + "=?"); - ps.setObject(1, gate_value); + ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?"); + ps.setString(1, column); + ps.setString(2, table); + ps.setString(3, gate); + ps.setObject(4, gate_value); ResultSet rs = ps.executeQuery(); Object toReturn;