Move player data from config to database
The plugin will not automatically migrate player data to the database. I'll add the script I used for that, then bump the minor version number like the coward I am.
This commit is contained in:
parent
03264c281f
commit
a0c50e0fb0
@ -8,7 +8,7 @@ import net.minecraft.server.command.ServerCommandSource;
|
|||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.LiteralText;
|
import net.minecraft.text.LiteralText;
|
||||||
import net.minecraft.util.registry.RegistryKey;
|
import net.minecraft.util.registry.RegistryKey;
|
||||||
import party._2a03.mc.util.Config;
|
import party._2a03.mc.util.Database;
|
||||||
import party._2a03.mc.util.PlayerData;
|
import party._2a03.mc.util.PlayerData;
|
||||||
import party._2a03.mc.util.PlayerPosition;
|
import party._2a03.mc.util.PlayerPosition;
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ public class HomeCommand {
|
|||||||
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = CommandManager.literal("home").executes(ctx -> {
|
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = CommandManager.literal("home").executes(ctx -> {
|
||||||
ServerCommandSource source = ctx.getSource();
|
ServerCommandSource source = ctx.getSource();
|
||||||
ServerPlayerEntity sender = source.getPlayer();
|
ServerPlayerEntity sender = source.getPlayer();
|
||||||
PlayerPosition position = Config.getPlayer(sender.getUuid().toString()).getHome();
|
PlayerPosition position = Database.getPlayer(sender.getUuid().toString()).getHome();
|
||||||
if (position.registrykey == null) {
|
if (position.registrykey == null) {
|
||||||
source.sendFeedback(new LiteralText("Home not found, do /home set"), false);
|
source.sendFeedback(new LiteralText("Home not found, do /home set"), false);
|
||||||
return -1;
|
return -1;
|
||||||
@ -30,7 +30,7 @@ public class HomeCommand {
|
|||||||
literalargumentbuilder.then(CommandManager.literal("set").executes(ctx -> {
|
literalargumentbuilder.then(CommandManager.literal("set").executes(ctx -> {
|
||||||
ServerCommandSource source = ctx.getSource();
|
ServerCommandSource source = ctx.getSource();
|
||||||
ServerPlayerEntity sender = source.getPlayer();
|
ServerPlayerEntity sender = source.getPlayer();
|
||||||
PlayerData playerdata = Config.getPlayer(sender.getUuid().toString());
|
PlayerData playerdata = Database.getPlayer(sender.getUuid().toString());
|
||||||
double x = sender.getX();
|
double x = sender.getX();
|
||||||
double y = sender.getY();
|
double y = sender.getY();
|
||||||
double z = sender.getZ();
|
double z = sender.getZ();
|
||||||
@ -39,7 +39,6 @@ public class HomeCommand {
|
|||||||
RegistryKey registrykey = sender.getServerWorld().getRegistryKey();
|
RegistryKey registrykey = sender.getServerWorld().getRegistryKey();
|
||||||
PlayerPosition location = new PlayerPosition(x, y, z, yaw, pitch, registrykey);
|
PlayerPosition location = new PlayerPosition(x, y, z, yaw, pitch, registrykey);
|
||||||
playerdata.setHome(location);
|
playerdata.setHome(location);
|
||||||
Config.setPlayer(playerdata);
|
|
||||||
source.sendFeedback(new LiteralText("Your home has been updated"), true);
|
source.sendFeedback(new LiteralText("Your home has been updated"), true);
|
||||||
return 1;
|
return 1;
|
||||||
}));
|
}));
|
||||||
@ -49,7 +48,7 @@ public class HomeCommand {
|
|||||||
}).then(CommandManager.argument("UUID", StringArgumentType.word()).executes(ctx -> {
|
}).then(CommandManager.argument("UUID", StringArgumentType.word()).executes(ctx -> {
|
||||||
ServerCommandSource source = ctx.getSource();
|
ServerCommandSource source = ctx.getSource();
|
||||||
ServerPlayerEntity sender = source.getPlayer();
|
ServerPlayerEntity sender = source.getPlayer();
|
||||||
PlayerData playerdata = Config.getPlayer(StringArgumentType.getString(ctx, "UUID"));
|
PlayerData playerdata = Database.getPlayer(StringArgumentType.getString(ctx, "UUID"));
|
||||||
double x = sender.getX();
|
double x = sender.getX();
|
||||||
double y = sender.getY();
|
double y = sender.getY();
|
||||||
double z = sender.getZ();
|
double z = sender.getZ();
|
||||||
@ -58,7 +57,6 @@ public class HomeCommand {
|
|||||||
RegistryKey registrykey = sender.getServerWorld().getRegistryKey();
|
RegistryKey registrykey = sender.getServerWorld().getRegistryKey();
|
||||||
PlayerPosition location = new PlayerPosition(x, y, z, yaw, pitch, registrykey);
|
PlayerPosition location = new PlayerPosition(x, y, z, yaw, pitch, registrykey);
|
||||||
playerdata.setHome(location);
|
playerdata.setHome(location);
|
||||||
Config.setPlayer(playerdata);
|
|
||||||
source.sendFeedback(new LiteralText("User's home has been updated (" + StringArgumentType.getString(ctx, "UUID") + ")"), true);
|
source.sendFeedback(new LiteralText("User's home has been updated (" + StringArgumentType.getString(ctx, "UUID") + ")"), true);
|
||||||
return 1;
|
return 1;
|
||||||
})));
|
})));
|
||||||
|
@ -44,40 +44,6 @@ public class Config {
|
|||||||
LOGGER.info("Configuration loaded");
|
LOGGER.info("Configuration loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PlayerData getPlayer(String uuid) {
|
|
||||||
JSONArray members = json.getJSONArray("members");
|
|
||||||
PlayerPosition home = null;
|
|
||||||
for (int i = 0; i < members.length(); ++i) {
|
|
||||||
JSONObject item = members.getJSONObject(i);
|
|
||||||
String item_uuid = item.getString("uuid");
|
|
||||||
if (item_uuid.equals(uuid)) {
|
|
||||||
home = new PlayerPosition(item.getJSONArray("home"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (home == null) {
|
|
||||||
home = new PlayerPosition();
|
|
||||||
}
|
|
||||||
return new PlayerData(uuid, home);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setPlayer(PlayerData player) {
|
|
||||||
JSONArray members = json.getJSONArray("members");
|
|
||||||
int playerIndex = -1;
|
|
||||||
for (int i = 0; i < members.length(); ++i) {
|
|
||||||
JSONObject item = members.getJSONObject(i);
|
|
||||||
String item_uuid = item.getString("uuid");
|
|
||||||
if (item_uuid.equals(player.getUUID())) {
|
|
||||||
playerIndex = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (playerIndex >= 0) {
|
|
||||||
members.remove(playerIndex);
|
|
||||||
}
|
|
||||||
members.put(player.getJSON());
|
|
||||||
json.put("members", members);
|
|
||||||
saveConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JSONArray getData(String key) {
|
public static JSONArray getData(String key) {
|
||||||
return json.getJSONArray(key);
|
return json.getJSONArray(key);
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,15 @@ package party._2a03.mc.util;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
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 {
|
public class Database {
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
@ -21,6 +27,19 @@ public class Database {
|
|||||||
String url = "jdbc:sqlite:";
|
String url = "jdbc:sqlite:";
|
||||||
url = url.concat(file.getPath());
|
url = url.concat(file.getPath());
|
||||||
conn = DriverManager.getConnection(url);
|
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," +
|
||||||
|
"PRIMARY KEY(uuid)" +
|
||||||
|
");");
|
||||||
|
stmt.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
LOGGER.info("Database loaded");
|
LOGGER.info("Database loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,4 +52,40 @@ public class Database {
|
|||||||
LOGGER.error(e.getMessage(), 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,18 @@ package party._2a03.mc.util;
|
|||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import party._2a03.mc.util.Database;
|
||||||
import party._2a03.mc.util.PlayerPosition;
|
import party._2a03.mc.util.PlayerPosition;
|
||||||
|
|
||||||
public class PlayerData {
|
public class PlayerData {
|
||||||
private String uuid;
|
private String uuid;
|
||||||
private PlayerPosition home;
|
private PlayerPosition home;
|
||||||
|
|
||||||
|
public PlayerData(String p_uuid) {
|
||||||
|
this.uuid = p_uuid;
|
||||||
|
this.home = new PlayerPosition();
|
||||||
|
}
|
||||||
|
|
||||||
public PlayerData(String p_uuid, PlayerPosition p_home) {
|
public PlayerData(String p_uuid, PlayerPosition p_home) {
|
||||||
this.uuid = p_uuid;
|
this.uuid = p_uuid;
|
||||||
this.home = p_home;
|
this.home = p_home;
|
||||||
@ -23,6 +29,7 @@ public class PlayerData {
|
|||||||
|
|
||||||
public void setHome(PlayerPosition location) {
|
public void setHome(PlayerPosition location) {
|
||||||
this.home = location;
|
this.home = location;
|
||||||
|
Database.setPlayerHome(this.uuid, this.home);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject getJSON() {
|
public JSONObject getJSON() {
|
||||||
|
@ -4,7 +4,6 @@ import org.json.JSONArray;
|
|||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
import net.minecraft.util.registry.RegistryKey;
|
import net.minecraft.util.registry.RegistryKey;
|
||||||
import party._2a03.mc.util.Config;
|
|
||||||
|
|
||||||
public class PlayerPosition {
|
public class PlayerPosition {
|
||||||
public double x;
|
public double x;
|
||||||
|
Loading…
Reference in New Issue
Block a user