Now buildable.

This commit is contained in:
Paul Reilly
2023-03-30 20:36:00 -05:00
parent 8a58782d99
commit ee39e6f534
15 changed files with 258 additions and 248 deletions

View File

@ -1,14 +1,5 @@
package me.totalfreedom.totalfreedommod.sql;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.banning.Ban;
@ -16,6 +7,10 @@ import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import java.sql.*;
import java.text.MessageFormat;
import java.util.UUID;
public class SQLite extends FreedomService
{
private final String FILE_NAME = "database.db";
@ -41,8 +36,7 @@ public class SQLite extends FreedomService
{
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder() + "/" + FILE_NAME);
FLog.info("Successfully connected to the database.");
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to connect to the database: " + e.getMessage());
}
@ -56,8 +50,7 @@ public class SQLite extends FreedomService
{
connection.close();
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to disconnect from the database: " + e.getMessage());
}
@ -68,54 +61,65 @@ public class SQLite extends FreedomService
try
{
DatabaseMetaData meta = connection.getMetaData();
if (tableExists(meta, "bans"))
if (tableNotExists(meta, "bans"))
{
try
{
connection.createStatement().execute("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the bans table: " + e.getMessage());
}
createBanTable();
}
if (tableExists(meta, "admins"))
if (tableNotExists(meta, "admins"))
{
try
{
connection.createStatement().execute("CREATE TABLE `admins` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the admins table: " + e.getMessage());
}
createAdminsTable();
}
if (tableExists(meta, "players"))
if (tableNotExists(meta, "players"))
{
try
{
connection.createStatement().execute("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `master_builder` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the players table: " + e.getMessage());
}
createPlayersTable();
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to check tables on database: " + e.getMessage());
}
}
private void createPlayersTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `master_builder` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the players table: " + e.getMessage());
}
}
private void createAdminsTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `admins` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the admins table: " + e.getMessage());
}
}
private void createBanTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the bans table: " + e.getMessage());
}
}
public void truncate(String table)
{
try
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM ?"))
{
connection.createStatement().execute("DELETE FROM " + table);
}
catch (SQLException e)
statement.setString(1, table);
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to truncate " + table + ": " + e.getMessage());
}
@ -123,12 +127,18 @@ public class SQLite extends FreedomService
public ResultSet getBanList() throws SQLException
{
return connection.createStatement().executeQuery("SELECT * FROM bans");
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM bans"))
{
return statement.executeQuery();
}
}
public ResultSet getAdminList() throws SQLException
{
return connection.createStatement().executeQuery("SELECT * FROM admins");
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM admins"))
{
return statement.executeQuery();
}
}
public void setAdminValue(Admin admin, String key, Object value)
@ -140,8 +150,7 @@ public class SQLite extends FreedomService
statement = setUnknownType(statement, 1, value);
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to update admin value:");
FLog.severe(e);
@ -157,8 +166,7 @@ public class SQLite extends FreedomService
statement = setUnknownType(statement, 1, value);
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to update player value: " + e.getMessage());
}
@ -169,25 +177,21 @@ public class SQLite extends FreedomService
if (value == null)
{
statement.setString(index, null);
}
else if (value.getClass().equals(String.class))
} else if (value.getClass().equals(String.class))
{
String v = (String)value;
String v = (String) value;
statement.setString(index, v);
}
else if (value.getClass().equals(Integer.class))
} else if (value.getClass().equals(Integer.class))
{
int v = (int)value;
int v = (int) value;
statement.setInt(index, v);
}
else if (value.getClass().equals(Boolean.class))
} else if (value.getClass().equals(Boolean.class))
{
boolean v = (boolean)value;
boolean v = (boolean) value;
statement.setBoolean(index, v);
}
else if (value.getClass().equals(Long.class))
} else if (value.getClass().equals(Long.class))
{
long v = (long)value;
long v = (long) value;
statement.setLong(index, v);
}
return statement;
@ -199,16 +203,13 @@ public class SQLite extends FreedomService
if (value instanceof String)
{
result = resultSet.getString(key);
}
else if (value instanceof Integer)
} else if (value instanceof Integer)
{
result = resultSet.getInt(key);
}
else if (value instanceof Boolean)
} else if (value instanceof Boolean)
{
result = resultSet.getObject(key);
}
else if (value instanceof Long)
} else if (value instanceof Long)
{
result = resultSet.getLong(key);
}
@ -217,9 +218,9 @@ public class SQLite extends FreedomService
public void addAdmin(Admin admin)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO admins VALUES (?, ?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO admins VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, admin.getUuid().toString());
statement.setString(2, FUtil.listToString(admin.getIps()));
statement.setString(3, admin.getRank().toString());
@ -229,8 +230,7 @@ public class SQLite extends FreedomService
statement.setBoolean(7, admin.getPotionSpy());
statement.setString(8, admin.getAcFormat());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add admin:");
FLog.severe(e);
@ -239,13 +239,12 @@ public class SQLite extends FreedomService
public void addPlayer(PlayerData player)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, player.getUuid().toString());
statement.setString(2, FUtil.listToString(player.getIps()));
statement.setString(3, FUtil.listToString(player.getNotes()));
statement.setString(4, player.getTag());
statement.setString(4, FUtil.miniMessage(player.getTag()));
statement.setString(5, player.getDiscordID());
statement.setBoolean(6, player.isMasterBuilder());
statement.setString(7, player.getRideMode().name());
@ -256,8 +255,7 @@ public class SQLite extends FreedomService
statement.setString(12, player.getLoginMessage());
statement.setBoolean(13, player.hasInspection());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add player:");
FLog.severe(e);
@ -266,15 +264,13 @@ public class SQLite extends FreedomService
public ResultSet getAdminByUuid(UUID uuid)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM admins WHERE uuid=''{0}''", uuid.toString())))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM admins WHERE uuid=''{0}''", uuid.toString()));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get admin by name:");
FLog.severe(e);
@ -285,15 +281,13 @@ public class SQLite extends FreedomService
public ResultSet getPlayerByUuid(UUID uuid)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid=''{0}''", uuid.toString())))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid=''{0}''", uuid.toString()));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get player by UUID:");
FLog.severe(e);
@ -304,11 +298,10 @@ public class SQLite extends FreedomService
public ResultSet getMasterBuilders()
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM players WHERE master_builder=true"))
{
return connection.createStatement().executeQuery("SELECT * FROM players WHERE master_builder=true");
}
catch (SQLException e)
return resultSet;
} catch (SQLException e)
{
FLog.severe("Failed to get Master Builders:");
FLog.severe(e);
@ -319,15 +312,13 @@ public class SQLite extends FreedomService
public ResultSet getPlayerByIp(String ip)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE ips LIKE ''%{0}%''", ip)))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE ips LIKE ''%{0}%''", ip));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get player by ip:");
FLog.severe(e);
@ -338,11 +329,11 @@ public class SQLite extends FreedomService
public void removeAdmin(Admin admin)
{
try
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM admins where name=?"))
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM admins where name=''{0}''", admin.getName()));
}
catch (SQLException e)
statement.setString(1, admin.getName());
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to remove admin:");
FLog.severe(e);
@ -351,9 +342,8 @@ public class SQLite extends FreedomService
public void addBan(Ban ban)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, ban.getUsername());
String uuid = null;
if (ban.hasUUID())
@ -367,8 +357,7 @@ public class SQLite extends FreedomService
statement.setLong(6, ban.getExpiryUnix());
statement.setString(7, ban.getReason());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add ban: " + e.getMessage());
}
@ -378,19 +367,28 @@ public class SQLite extends FreedomService
{
try
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM bans WHERE name=''{0}''", ban.getUsername()));
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM bans WHERE name=?"))
{
statement.setString(1, ban.getUsername());
statement.executeUpdate();
}
for (String ip : ban.getIps())
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM bans WHERE ips LIKE ''%{0}%''", ip));
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM bans WHERE ips LIKE %?%"))
{
statement.setString(1, ip);
statement.executeUpdate();
}
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to remove ban: " + e.getMessage());
}
}
public boolean tableExists(DatabaseMetaData meta, String name) throws SQLException
// We've changed this to read tableNotExists because it's more accurate in context.
public boolean tableNotExists(DatabaseMetaData meta, String name) throws SQLException
{
return !meta.getTables(null, null, name, null).next();
}