1
0
minecraft-tweaks-2a03/src/main/java/party/_2a03/mc/util/Database.java
Ryan Fox e5a9c1b04d
Ignore Mojang's names & skins
Being able to control people's names and skins is loads of fun!
2023-05-31 20:45:46 -07:00

164 lines
4.4 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;
import java.util.UUID;
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," +
"name TEXT," +
"skinval TEXT," +
"skinsig TEXT," +
"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(UUID uuid) {
return getPlayer(uuid.toString());
}
public static PlayerData getPlayer(String uuid) {
try {
PreparedStatement pstmt;
pstmt = conn.prepareStatement(
"SELECT home, name, skinval, skinsig FROM players WHERE uuid = ?;");
pstmt.setString(1, uuid);
ResultSet res = pstmt.executeQuery();
if (res.next()) {
String homeData = res.getString("home");
PlayerPosition home = new PlayerPosition();
if (homeData != "" && homeData != null)
home = new PlayerPosition(new JSONArray(homeData));
String name = res.getString("name");
String skinval = res.getString("skinval");
String skinsig = res.getString("skinsig");
return new PlayerData(uuid, home, name, skinval, skinsig);
}
pstmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
return new PlayerData(uuid);
}
public static void setPlayerTextures(String uuid, String skinval, String skinsig) {
try {
PreparedStatement pstmt;
pstmt = conn.prepareStatement(
"INSERT INTO players (uuid, skinval, skinsig) " +
"VALUES(?, ?, ?) " +
"ON CONFLICT(uuid) " +
"DO UPDATE SET skinval=?, skinsig=?;");
pstmt.setString(1, uuid);
pstmt.setString(2, skinval);
pstmt.setString(3, skinsig);
pstmt.setString(4, skinval);
pstmt.setString(5, skinsig);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
}
public static void setPlayerName(String uuid, String name) {
try {
PreparedStatement pstmt;
pstmt = conn.prepareStatement(
"INSERT INTO players (uuid, name) " +
"VALUES(?, ?) " +
"ON CONFLICT(uuid) " +
"DO UPDATE SET name=?;");
pstmt.setString(1, uuid);
pstmt.setString(2, name);
pstmt.setString(3, name);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
}
}
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);
}
}
}