City, Nation, and Player persistence halfway done, loading works now saving needs to be done.
This commit is contained in:
parent
6d9bb1c083
commit
b5850e3eda
|
@ -3,6 +3,7 @@ package moe.oko.opennaw;
|
||||||
import moe.oko.opennaw.command.CityCommand;
|
import moe.oko.opennaw.command.CityCommand;
|
||||||
import moe.oko.opennaw.command.NationCommand;
|
import moe.oko.opennaw.command.NationCommand;
|
||||||
import moe.oko.opennaw.listener.ActionListener;
|
import moe.oko.opennaw.listener.ActionListener;
|
||||||
|
import moe.oko.opennaw.storage.NAWDatabase;
|
||||||
import moe.oko.opennaw.util.*;
|
import moe.oko.opennaw.util.*;
|
||||||
import net.luckperms.api.LuckPerms;
|
import net.luckperms.api.LuckPerms;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -19,6 +20,8 @@ public final class OpenNAW extends JavaPlugin {
|
||||||
|
|
||||||
public CityHandler cityHandler;
|
public CityHandler cityHandler;
|
||||||
|
|
||||||
|
public NAWDatabase database;
|
||||||
|
|
||||||
public ConfigHelper configHelper;
|
public ConfigHelper configHelper;
|
||||||
|
|
||||||
public GroupHandler groupHandler;
|
public GroupHandler groupHandler;
|
||||||
|
@ -49,7 +52,14 @@ public final class OpenNAW extends JavaPlugin {
|
||||||
cityHandler = new CityHandler();
|
cityHandler = new CityHandler();
|
||||||
groupHandler = new GroupHandler(this.luckPerms);
|
groupHandler = new GroupHandler(this.luckPerms);
|
||||||
|
|
||||||
info("OpenNAW is enabled!");
|
info("OpenNAW is initialized.");
|
||||||
|
info("Starting database load.");
|
||||||
|
database = configHelper.fetchDatabase();
|
||||||
|
info("Database initialized. Starting to fetch all values");
|
||||||
|
nationHandler.loadNationList(database.loadNations());
|
||||||
|
database.loadPlayers();
|
||||||
|
cityHandler.loadCityList(database.loadCities());
|
||||||
|
//Do not change above load order.
|
||||||
|
|
||||||
// Register Commands & Events
|
// Register Commands & Events
|
||||||
this.getCommand("nation").setExecutor(new NationCommand());
|
this.getCommand("nation").setExecutor(new NationCommand());
|
||||||
|
|
|
@ -27,6 +27,16 @@ public class City {
|
||||||
this.health = 300;
|
this.health = 300;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DB Const
|
||||||
|
public City(String name, String resource, Location location, Nation owner) {
|
||||||
|
this.name = name;
|
||||||
|
this.resource = resource;
|
||||||
|
this.point = location;
|
||||||
|
this.owner = owner;
|
||||||
|
this.health = 300;
|
||||||
|
this.state = CityState.NORMAL;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,10 +28,10 @@ public class Nation {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Db Constructor
|
//Db Constructor
|
||||||
public Nation(String name, Group group, HashMap<UUID, OfflinePlayer> players, String dynmapIcon, Location spawn) {
|
public Nation(String name, Group group, String dynmapIcon, Location spawn) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.group = group;
|
this.group = group;
|
||||||
this.players = players;
|
this.players = new HashMap<>();
|
||||||
this.dynmapIcon = dynmapIcon;
|
this.dynmapIcon = dynmapIcon;
|
||||||
this.spawn = spawn;
|
this.spawn = spawn;
|
||||||
}
|
}
|
||||||
|
|
137
src/main/java/moe/oko/opennaw/storage/NAWDatabase.java
Normal file
137
src/main/java/moe/oko/opennaw/storage/NAWDatabase.java
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
package moe.oko.opennaw.storage;
|
||||||
|
|
||||||
|
import moe.oko.opennaw.OpenNAW;
|
||||||
|
import moe.oko.opennaw.model.City;
|
||||||
|
import moe.oko.opennaw.model.Nation;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class NAWDatabase {
|
||||||
|
|
||||||
|
private Connection connection; //SQL Conn instance
|
||||||
|
|
||||||
|
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')";
|
||||||
|
private final String INITIALIZE_NATIONS = "CREATE TABLE IF NOT EXISTS `nations` (" +
|
||||||
|
"`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)";
|
||||||
|
private final String INITIALIZE_CITIES = "CREATE TABLE `cities` (" +
|
||||||
|
"`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';";
|
||||||
|
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`;";
|
||||||
|
|
||||||
|
public NAWDatabase(String username, String password, String host, int port, String database) {
|
||||||
|
//all relevant
|
||||||
|
try {
|
||||||
|
connection = DriverManager.getConnection("jdbc:mysql//" + host + ":" + port + "/" + database, username, password);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
OpenNAW.getInstance().getLogger().severe("FAILED TO INITIALIZE PERSISTENT MYSQL DATABASE.");
|
||||||
|
}
|
||||||
|
//Unfugg Tables on startup
|
||||||
|
initializeTables();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeTables() {
|
||||||
|
try {
|
||||||
|
OpenNAW.getInstance().getLogger().info("Table initialization start.");
|
||||||
|
PreparedStatement nationPlayersInit = connection.prepareStatement(INITIALIZE_NATION_PLAYERS);
|
||||||
|
nationPlayersInit.execute();
|
||||||
|
PreparedStatement nationsInit = connection.prepareStatement(INITIALIZE_NATIONS);
|
||||||
|
nationsInit.execute();
|
||||||
|
PreparedStatement cityInit = connection.prepareStatement(INITIALIZE_CITIES);
|
||||||
|
cityInit.execute();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
OpenNAW.getInstance().getLogger().severe("Failed to initialize tables for sql.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> loadCities() {
|
||||||
|
try {
|
||||||
|
OpenNAW.getInstance().getLogger().info("Loading Cities...");
|
||||||
|
PreparedStatement pullCity = connection.prepareStatement(SELECT_CITIES);
|
||||||
|
ResultSet rs = pullCity.executeQuery();
|
||||||
|
List<City> cityLoad = new ArrayList<>();
|
||||||
|
var i = 0;
|
||||||
|
while(rs.next()) {
|
||||||
|
var name = rs.getString(1);
|
||||||
|
var resource = rs.getString(2);
|
||||||
|
var nation = OpenNAW.getInstance().getNationHandler().getNationByName(rs.getString(3));
|
||||||
|
var loc = new Location(Bukkit.getWorld(rs.getString(4)), rs.getInt(5), rs.getInt(6), rs.getInt(7));
|
||||||
|
cityLoad.add(new City(name, resource, loc, nation));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
OpenNAW.getInstance().getLogger().info(i + " cities loaded.");
|
||||||
|
return cityLoad;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
OpenNAW.getInstance().getLogger().severe("Failed to pull cities from db.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Nation> loadNations() {
|
||||||
|
try {
|
||||||
|
OpenNAW.getInstance().getLogger().info("Loading Nations...");
|
||||||
|
PreparedStatement pullNation = connection.prepareStatement(SELECT_NATION);
|
||||||
|
ResultSet rs = pullNation.executeQuery();
|
||||||
|
List<Nation> nationLoad = new ArrayList<>();
|
||||||
|
var i = 0;
|
||||||
|
while(rs.next()) {
|
||||||
|
var name = rs.getString(1);
|
||||||
|
var group = OpenNAW.getInstance().getGroupHandler().getGroupFromString(rs.getString(2));
|
||||||
|
var icon = rs.getString(3);
|
||||||
|
var location = new Location(Bukkit.getWorld(rs.getString(4)), rs.getInt(5), rs.getInt(6), rs.getInt(7));
|
||||||
|
nationLoad.add(new Nation(name, group, icon, location));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
OpenNAW.getInstance().getLogger().info(i + " nations loaded.");
|
||||||
|
return nationLoad;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
OpenNAW.getInstance().getLogger().severe("Failed to pull nations from db.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadPlayers() {
|
||||||
|
try {
|
||||||
|
PreparedStatement pullPlayers = connection.prepareStatement(SELECT_PLAYERS);
|
||||||
|
ResultSet rs = pullPlayers.executeQuery();
|
||||||
|
OpenNAW.getInstance().getLogger().info("Loading Players...");
|
||||||
|
var i = 0;
|
||||||
|
while(rs.next()) {
|
||||||
|
OpenNAW.getInstance().getNationHandler().insertPlayerIntoNation(UUID.fromString(rs.getString(2)), rs.getString(1));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
OpenNAW.getInstance().getLogger().info(i + " players loaded.");
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
OpenNAW.getInstance().getLogger().severe("Failed to load players.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -49,6 +49,12 @@ public class CityHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadCityList(List<City> cities) {
|
||||||
|
for(City city : cities) {
|
||||||
|
this.cities.put(city.getName(), city);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> getCityList() {
|
public List<String> getCityList() {
|
||||||
List<String> cityList = new ArrayList<String>();
|
List<String> cityList = new ArrayList<String>();
|
||||||
for (City city : cities.values()) {
|
for (City city : cities.values()) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package moe.oko.opennaw.util;
|
package moe.oko.opennaw.util;
|
||||||
|
|
||||||
|
import moe.oko.opennaw.storage.NAWDatabase;
|
||||||
import org.bukkit.configuration.Configuration;
|
import org.bukkit.configuration.Configuration;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
public class ConfigHelper {
|
public class ConfigHelper {
|
||||||
private Configuration config;
|
private Configuration config;
|
||||||
|
@ -13,4 +15,9 @@ public class ConfigHelper {
|
||||||
public int cityDamageOnStrike() {
|
public int cityDamageOnStrike() {
|
||||||
return config.getConfigurationSection("city").getInt("damageOnStrike");
|
return config.getConfigurationSection("city").getInt("damageOnStrike");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NAWDatabase fetchDatabase() {
|
||||||
|
ConfigurationSection cs = config.getConfigurationSection("sql");
|
||||||
|
return new NAWDatabase(cs.getString("username"), cs.getString("password"), cs.getString("host"), cs.getInt("port"), cs.getString("database"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import moe.oko.opennaw.OpenNAW;
|
||||||
import moe.oko.opennaw.model.City;
|
import moe.oko.opennaw.model.City;
|
||||||
import moe.oko.opennaw.model.Nation;
|
import moe.oko.opennaw.model.Nation;
|
||||||
import net.luckperms.api.model.group.Group;
|
import net.luckperms.api.model.group.Group;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -51,6 +52,21 @@ public class NationHandler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Database loading players.
|
||||||
|
public void insertPlayerIntoNation(UUID uuid, String nation) {
|
||||||
|
for(var result : nations.values()) {
|
||||||
|
if (result.getName().equals(nation)) {
|
||||||
|
result.getPlayerMap().put(uuid, Bukkit.getOfflinePlayer(uuid));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadNationList(List<Nation> nations) {
|
||||||
|
for(Nation nation : nations) {
|
||||||
|
this.nations.put(nation.getName(), nation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> getNationList() {
|
public List<String> getNationList() {
|
||||||
List<String> nationList = new ArrayList<String>();
|
List<String> nationList = new ArrayList<String>();
|
||||||
for (Nation nation : nations.values()) {
|
for (Nation nation : nations.values()) {
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
# OpenNAW Configuration
|
# OpenNAW Configuration
|
||||||
city:
|
city:
|
||||||
#The amount of damage a city takes on the point being striked
|
#The amount of damage a city takes on the point being striked
|
||||||
damageOnStrike: 5
|
damageOnStrike: 5
|
||||||
|
sql:
|
||||||
|
host: "localhost"
|
||||||
|
port: 3306
|
||||||
|
database: "NAW"
|
||||||
|
username: "Maxopoly"
|
||||||
|
password: "Squidlover42069"
|
Loading…
Reference in a new issue