Release Candidate 3

This commit is contained in:
Paldiu 2021-04-30 13:52:02 -05:00
parent d2087a2f49
commit d8829d4931
4 changed files with 111 additions and 40 deletions

View File

@ -35,18 +35,50 @@ public abstract class Ban implements IBan {
private final String banId; private final String banId;
private final String banReason; 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) { public Ban(SimplexModule<?> plugin, Player player, CommandSender sender) {
this(plugin, player, sender, BanType.TEMPORARY); 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) { public Ban(SimplexModule<?> plugin, Player player, CommandSender sender, BanType type) {
this(plugin, player, sender, type, TickedTime.DAY); 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) { 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); 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) { public Ban(SimplexModule<?> plugin, Player player, CommandSender sender, BanType type, String banId, String banReason, Date banDate, long banDuration) {
this.plugin = plugin; this.plugin = plugin;
this.player = player; this.player = player;
@ -58,13 +90,16 @@ public abstract class Ban implements IBan {
this.banDate = banDate; 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) { public void writeToFile(boolean separateFiles) {
File fileLocation = new File(SimplexCorePlugin.getInstance().getParentFolder(), "bans"); File fileLocation = new File(plugin.getParentFolder(), "bans");
if (separateFiles) { if (separateFiles) {
Yaml yaml = new YamlFactory(SimplexCorePlugin.getInstance()).from(null, fileLocation, player.getName() + ".yml"); Yaml yaml = new YamlFactory(plugin).from(null, fileLocation, player.getName() + ".yml");
yaml.getConfig().createSection(getOffender().toString()); ConfigurationSection section = yaml.getConfig().createSection(getOffender().toString());
ConfigurationSection section = yaml.getConfigurationSection(getOffender()::toString);
section.set("name", player.getName()); section.set("name", player.getName());
section.set("ban_id", banId); section.set("ban_id", banId);
section.set("sender", sender.getName()); section.set("sender", sender.getName());
@ -75,12 +110,25 @@ public abstract class Ban implements IBan {
try { try {
yaml.save(); yaml.save();
} catch (IOException e) { } catch (IOException e) {
SimplexCorePlugin.getInstance().getLogger().severe(e.getMessage()); plugin.getLogger().severe(e.getMessage());
} }
yaml.reload(); yaml.reload();
} else { } else {
// TODO: Write to a single file as separate sections per UUID. Yaml yaml = new YamlFactory(plugin).from(null, fileLocation, "bans.yml");
Yaml yaml = new YamlFactory(SimplexCorePlugin.getInstance()).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();
} }
} }
} }

View File

@ -4,6 +4,7 @@ import io.github.simplexdev.api.IBan;
import io.github.simplexdev.api.func.VoidSupplier; import io.github.simplexdev.api.func.VoidSupplier;
import io.github.simplexdev.simplexcore.chat.Messages; import io.github.simplexdev.simplexcore.chat.Messages;
import io.github.simplexdev.simplexcore.config.Yaml; 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.module.SimplexModule;
import io.github.simplexdev.simplexcore.utils.TickedTime; import io.github.simplexdev.simplexcore.utils.TickedTime;
import io.github.simplexdev.simplexcore.utils.Utilities; 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. * @return A new ban instance.
*/ */

View File

@ -17,12 +17,13 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result; import static org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
public class BanManager extends SimplexListener { public final class BanManager extends SimplexListener {
private final Map<Ban, BanType> banMap = new HashMap<>(); private final Map<Ban, BanType> banMap = new HashMap<>();
private final SimplexModule<?> plugin; private final SimplexModule<?> plugin;
public BanManager(SimplexModule<?> plugin) { BanManager(SimplexModule<?> plugin) {
this.plugin = plugin; this.plugin = plugin;
register(this, plugin);
} }
public void addBan(Ban ban) { public void addBan(Ban ban) {

View File

@ -9,8 +9,9 @@ public class Database {
public static void createTable(String table, String columns) { public static void createTable(String table, String columns) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS ? (?);");
+ table + " (" + columns + ");"); ps.setString(1, table);
ps.setString(2, columns);
ps.executeUpdate(); ps.executeUpdate();
} catch(SQLException ex) { } catch(SQLException ex) {
// TODO // TODO
@ -20,8 +21,10 @@ public class Database {
public static void insertData(String columns, String values, String table) { public static void insertData(String columns, String values, String table) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("INSERT INTO " ps = MySQL.getConnection().prepareStatement("INSERT INTO ?(?) VALUES (?);");
+ table + "(" + columns + ") VALUES (" + values + ");"); ps.setString(1, table);
ps.setString(2, columns);
ps.setString(3, values);
ps.executeUpdate(); ps.executeUpdate();
} catch(SQLException ex) { } catch(SQLException ex) {
// TODO // TODO
@ -31,8 +34,10 @@ public class Database {
public static void deleteData(String table, String column, Object value) { public static void deleteData(String table, String column, Object value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("DELETE FROM " + table + " WHERE " + column + "=?"); ps = MySQL.getConnection().prepareStatement("DELETE FROM ? WHERE ?=?");
ps.setObject(1, value); ps.setString(1, table);
ps.setString(2, column);
ps.setObject(3, value);
ps.executeUpdate(); ps.executeUpdate();
} catch(SQLException ex) { } catch(SQLException ex) {
// TODO // TODO
@ -42,9 +47,12 @@ public class Database {
public static void set(String table, String gate, Object gate_value, String column, Object value) { public static void set(String table, String gate, Object gate_value, String column, Object value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("UPDATE " + table + " SET " + column + "=? WHERE " + gate + "=?"); ps = MySQL.getConnection().prepareStatement("UPDATE ? SET ?=? WHERE ?=?");
ps.setObject(1, value); ps.setString(1, table);
ps.setObject(2, gate_value); ps.setString(2, column);
ps.setString(3, gate);
ps.setObject(4, value);
ps.setObject(5, gate_value);
ps.executeUpdate(); ps.executeUpdate();
} catch(SQLException ex) { } catch(SQLException ex) {
// TODO // TODO
@ -54,9 +62,10 @@ public class Database {
public static boolean exists(String table, String column, Object value) { public static boolean exists(String table, String column, Object value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT * FROM " ps = MySQL.getConnection().prepareStatement("SELECT * FROM ? WHERE ?=?");
+ table + " WHERE " + column + "=?"); ps.setString(1, table);
ps.setObject(1, value); ps.setString(2, column);
ps.setObject(3, value);
ResultSet results = ps.executeQuery(); ResultSet results = ps.executeQuery();
return results.next(); return results.next();
@ -69,9 +78,11 @@ public class Database {
public static String getString(String table, String column, String gate, Object gate_value) { public static String getString(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
String toReturn; String toReturn;
@ -89,9 +100,11 @@ public class Database {
public static int getInt(String table, String column, String gate, Object gate_value) { public static int getInt(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
int toReturn; int toReturn;
@ -109,9 +122,11 @@ public class Database {
public static Double getDouble(String table, String column, String gate, Object gate_value) { public static Double getDouble(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
double toReturn; double toReturn;
@ -129,9 +144,11 @@ public class Database {
public static long getLong(String table, String column, String gate, Object gate_value) { public static long getLong(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
long toReturn; long toReturn;
@ -149,9 +166,11 @@ public class Database {
public static byte getByte(String table, String column, String gate, Object gate_value) { public static byte getByte(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
byte toReturn; byte toReturn;
@ -169,9 +188,11 @@ public class Database {
public static Object get(String table, String column, String gate, Object gate_value) { public static Object get(String table, String column, String gate, Object gate_value) {
PreparedStatement ps; PreparedStatement ps;
try { try {
ps = MySQL.getConnection().prepareStatement("SELECT " + column + " FROM " + table ps = MySQL.getConnection().prepareStatement("SELECT ? FROM ? WHERE ?=?");
+ " WHERE " + gate + "=?"); ps.setString(1, column);
ps.setObject(1, gate_value); ps.setString(2, table);
ps.setString(3, gate);
ps.setObject(4, gate_value);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
Object toReturn; Object toReturn;