1
0
minecraft-tweaks-2a03/src/main/java/party/_2a03/mc/util/Database.java
Ryan Fox 6ff2142538
Add ASN field to players table
Ban evaders will often use the accounts of whitelisted users. This will
allow me to lock specific users into their ASN to prevent ban evaders
from connecting via VPNs or other residential connections (unless
they're from the same ISP).
2020-10-27 17:59:25 +00:00

111 lines
2.9 KiB
Java

package party._2a03.mc.util;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import party._2a03.mc.util.PlayerData;
import party._2a03.mc.util.PlayerPosition;
public class Database {
private static final Logger LOGGER = LogManager.getLogger();
private static Connection conn = null;
private static File file;
public static void init(File configDir) {
file = new File(configDir, "2a03.db");
}
public static void open() throws Exception {
LOGGER.info("Loading 2a03.party database");
String url = "jdbc:sqlite:";
url = url.concat(file.getPath());
conn = DriverManager.getConnection(url);
try {
Statement stmt;
stmt = conn.createStatement();
stmt.executeUpdate(
"CREATE TABLE IF NOT EXISTS players (" +
"uuid TEXT NOT NULL UNIQUE," +
"home TEXT," +
"asn INTEGER," +
"PRIMARY KEY(uuid)" +
");");
stmt.executeUpdate(
"CREATE TABLE IF NOT EXISTS claims (" +
"id TEXT NOT NULL UNIQUE," +
"owner TEXT NOT NULL," +
"members TEXT NOT NULL," +
"rules TEXT NOT NULL," +
"PRIMARY KEY(id)" +
");");
stmt.executeUpdate(
"CREATE TABLE IF NOT EXISTS areas (" +
"chunk_x INTEGER NOT NULL," +
"chunk_z INTEGER NOT NULL," +
"min_x INTEGER NOT NULL," +
"min_z INTEGER NOT NULL," +
"max_x INTEGER NOT NULL," +
"max_z INTEGER NOT NULL," +
"claim TEXT NOT NULL" +
");");
stmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
LOGGER.info("Database loaded");
}
public static void close() {
LOGGER.info("Unloading 2a03.party database");
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
}
public static PlayerData getPlayer(String uuid) {
try {
PreparedStatement pstmt;
pstmt = conn.prepareStatement(
"SELECT home FROM players WHERE uuid = ?;");
pstmt.setString(1, uuid);
ResultSet res = pstmt.executeQuery();
if (res.next()) {
PlayerPosition home = new PlayerPosition(new JSONArray(res.getString("home")));
return new PlayerData(uuid, home);
}
pstmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
return new PlayerData(uuid);
}
public static void setPlayerHome(String uuid, PlayerPosition home) {
try {
PreparedStatement pstmt;
pstmt = conn.prepareStatement(
"INSERT INTO players (uuid, home) " +
"VALUES(?, ?) " +
"ON CONFLICT(uuid) " +
"DO UPDATE SET home=?;");
pstmt.setString(1, uuid);
pstmt.setString(2, home.getJSON().toString());
pstmt.setString(3, home.getJSON().toString());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
}
}