Persistence Done.
This commit is contained in:
parent
b5850e3eda
commit
59e1866c59
|
@ -76,6 +76,9 @@ public final class OpenNAW extends JavaPlugin {
|
|||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
info("Starting persistence save.");
|
||||
cityHandler.saveCities();
|
||||
nationHandler.saveNations();
|
||||
info("OpenNAW is disabled!");
|
||||
}
|
||||
|
||||
|
@ -87,6 +90,8 @@ public final class OpenNAW extends JavaPlugin {
|
|||
return dynmapHandler;
|
||||
}
|
||||
|
||||
public NAWDatabase getDatabase() { return database; }
|
||||
|
||||
public ActionLogger getActionLogger() {
|
||||
return actionLogger;
|
||||
}
|
||||
|
|
|
@ -5,15 +5,13 @@ import moe.oko.opennaw.model.City;
|
|||
import moe.oko.opennaw.model.Nation;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
public class NAWDatabase {
|
||||
|
||||
|
@ -21,28 +19,39 @@ public class NAWDatabase {
|
|||
|
||||
private final String INITIALIZE_NATION_PLAYERS = "CREATE TABLE IF NOT EXISTS `nation_players`" +
|
||||
"(`nation_name` VARCHAR(50) NOT NULL COMMENT 'The name of the nation for the player to join'," +
|
||||
"`player_uuid` VARCHAR(50) NOT NULL COMMENT 'The UUID of the player who is in that nation')";
|
||||
"`player_uuid` VARCHAR(50) NOT NULL COMMENT 'The UUID of the player who is in that nation', PRIMARY KEY (`player_uuid`)";
|
||||
private final String INITIALIZE_NATIONS = "CREATE TABLE IF NOT EXISTS `nations` (" +
|
||||
"`name` VARCHAR(50) NOT NULL COMMENT 'Nation name'," +
|
||||
"`nation_name` VARCHAR(50) NOT NULL COMMENT 'Nation name'," +
|
||||
"`groupName` VARCHAR(50) NOT NULL COMMENT 'Name for the luckperms group.', " +
|
||||
"`dynmapIcon` VARCHAR(50) NOT NULL COMMENT 'National Icon for Dynmap'," +
|
||||
"`spawnworld` VARCHAR(50) NOT NULL," +
|
||||
"`spawnx` INT NOT NULL DEFAULT 0," +
|
||||
"`spawny` INT NOT NULL DEFAULT 0," +
|
||||
"`spawnz` INT NOT NULL DEFAULT 0)";
|
||||
"`spawnz` INT NOT NULL DEFAULT 0), PRIMARY KEY (`nation_name`)";
|
||||
private final String INITIALIZE_CITIES = "CREATE TABLE `cities` (" +
|
||||
"`name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'City Name'," +
|
||||
"`city_name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'City Name'," +
|
||||
"`resource` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'Resource the city provides'," +
|
||||
"`owner` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'The name of the nation that owns it'," +
|
||||
"`pointworld` VARCHAR(50) NOT NULL DEFAULT ''," +
|
||||
"`pointx` INT NOT NULL DEFAULT 0," +
|
||||
"`pointy` INT NOT NULL DEFAULT 0," +
|
||||
"`pointz` INT NOT NULL DEFAULT 0) COMMENT='Cities on the NAW map';";
|
||||
"`pointz` INT NOT NULL DEFAULT 0) COMMENT='Cities on the NAW map', PRIMARY KEY (`city_name`);";
|
||||
private final String SELECT_CITIES = "SELECT * FROM `cities`;";
|
||||
private final String SELECT_NATION = "SELECT * FROM `nations`;";
|
||||
|
||||
private final String SELECT_PLAYERS = "SELECT * FROM `nation_players`;";
|
||||
|
||||
//name 1,8; group 2,9; icon 3,10; world 4,11; x 5,12; y 6,13; z 7,14;
|
||||
//14
|
||||
private final String SAVE_NATION = "INSERT INTO `nations`(nation_name,groupName,dynmapIcon, spawnworld,spawnx,spawny,spawnz) VALUES " +
|
||||
"(?,?,?,?,?,?,?)" +
|
||||
"ON DUPLICATE KEY UPDATE nation_name=?, groupName=?, dynmapIcon=?, spawnworld=?, spawnx=?, spawny=?, spawnz=?;";
|
||||
//MAKE SURE YOU RUN AS BATCH (nation, 1,3) (playeruuid, 2,4);
|
||||
private final String SAVE_PLAYER = "INSERT INTO `nation_players`(nation_name, player_uuid) VALUES (?,?) " +
|
||||
"ON DUPLICATE KEY UPDATE nation_name=?, player_uuid=?;";
|
||||
//name 1,8; resource 2,9; owner 3,10; world 4,11; x 5,12; y 6,13; z 7,14;
|
||||
private final String SAVE_CITY = "INSERT INTO `cities`(city_name, resource, OWNER, pointworld, pointx, pointy, pointz) VALUES (?,?,?,?,?,?,?)" +
|
||||
"ON DUPLICATE KEY UPDATE city_name=?, resource=?, OWNER=?, pointworld=?, pointx=?, pointy=?, pointz=?;";
|
||||
|
||||
public NAWDatabase(String username, String password, String host, int port, String database) {
|
||||
//all relevant
|
||||
try {
|
||||
|
@ -134,4 +143,88 @@ public class NAWDatabase {
|
|||
OpenNAW.getInstance().getLogger().severe("Failed to load players.");
|
||||
}
|
||||
}
|
||||
|
||||
public void saveNations(Collection<Nation> nations) {
|
||||
try {
|
||||
OpenNAW.getInstance().getLogger().info("Starting nation save...");
|
||||
//name 1,8; group 2,9; icon 3,10; world 4,11; x 5,12; y 6,13; z 7,14;
|
||||
//14
|
||||
PreparedStatement nationSave = connection.prepareStatement(SAVE_NATION);
|
||||
int i = 0;
|
||||
for(Nation nation : nations) {
|
||||
nationSave.setString(1, nation.getName());
|
||||
nationSave.setString(8, nation.getName());
|
||||
nationSave.setString(2, nation.getGroup().getName());
|
||||
nationSave.setString(9, nation.getGroup().getName());
|
||||
nationSave.setString(3, nation.getDynmapIcon());
|
||||
nationSave.setString(10, nation.getDynmapIcon());
|
||||
var loc = nation.getSpawn();
|
||||
nationSave.setString(4, loc.getWorld().getName());
|
||||
nationSave.setString(11, loc.getWorld().getName());
|
||||
nationSave.setInt(5, loc.getBlockX());
|
||||
nationSave.setInt(12, loc.getBlockX());
|
||||
nationSave.setInt(6, loc.getBlockY());
|
||||
nationSave.setInt(13, loc.getBlockY());
|
||||
nationSave.setInt(7, loc.getBlockZ());
|
||||
nationSave.setInt(14, loc.getBlockZ());
|
||||
|
||||
//Values are set, this is autistic. Run.
|
||||
nationSave.addBatch();
|
||||
i++;
|
||||
|
||||
PreparedStatement playersSave = connection.prepareStatement(SAVE_PLAYER);
|
||||
//MAKE SURE YOU RUN AS BATCH (nation, 1,3) (playeruuid, 2,4);
|
||||
int pc = 0;
|
||||
for(OfflinePlayer nationPlayer : nation.getPlayerMap().values()) {
|
||||
//Oh nnononono
|
||||
playersSave.setString(1, nation.getName());
|
||||
playersSave.setString(3, nation.getName());
|
||||
playersSave.setString(2, nationPlayer.getUniqueId().toString());
|
||||
playersSave.setString(4, nationPlayer.getUniqueId().toString());
|
||||
playersSave.addBatch();
|
||||
pc++;
|
||||
}
|
||||
//Run all at once to avoid ratelimit.
|
||||
playersSave.executeBatch();
|
||||
OpenNAW.getInstance().getLogger().info("Saved " + pc + " players for " + nation.getName() + ".");
|
||||
}
|
||||
nationSave.executeBatch();
|
||||
OpenNAW.getInstance().getLogger().info("Saved " + i + " nations.");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
OpenNAW.getInstance().getLogger().severe("Failed to save nations.");
|
||||
}
|
||||
}
|
||||
|
||||
public void saveCities(Collection<City> cities) {
|
||||
try {
|
||||
PreparedStatement citySave = connection.prepareStatement(SAVE_CITY);
|
||||
int i = 0;
|
||||
for(City city : cities) {
|
||||
//name 1,8; resource 2,9; owner 3,10; world 4,11; x 5,12; y 6,13; z 7,14;
|
||||
citySave.setString(1, city.getName());
|
||||
citySave.setString(8, city.getName());
|
||||
citySave.setString(2, city.getResource());
|
||||
citySave.setString(9, city.getResource());
|
||||
citySave.setString(3, city.getOwner().getName());
|
||||
citySave.setString(10, city.getOwner().getName());
|
||||
var loc = city.getPoint();
|
||||
citySave.setString(4, loc.getWorld().getName());
|
||||
citySave.setString(11, loc.getWorld().getName());
|
||||
citySave.setInt(5, loc.getBlockX());
|
||||
citySave.setInt(12, loc.getBlockX());
|
||||
citySave.setInt(6, loc.getBlockY());
|
||||
citySave.setInt(13, loc.getBlockY());
|
||||
citySave.setInt(7, loc.getBlockZ());
|
||||
citySave.setInt(14, loc.getBlockZ());
|
||||
citySave.addBatch();
|
||||
i++;
|
||||
}
|
||||
citySave.executeBatch();
|
||||
OpenNAW.getInstance().getLogger().info("Saved " + i + " cities.");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
OpenNAW.getInstance().getLogger().severe("Failed to save cities.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,4 +62,8 @@ public class CityHandler {
|
|||
}
|
||||
return cityList;
|
||||
}
|
||||
|
||||
public void saveCities() {
|
||||
OpenNAW.getInstance().getDatabase().saveCities(cities.values());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,4 +74,8 @@ public class NationHandler {
|
|||
}
|
||||
return nationList;
|
||||
}
|
||||
|
||||
public void saveNations() {
|
||||
OpenNAW.getInstance().getDatabase().saveNations(nations.values());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue