From b5850e3eda6e872461bc541e6b7706d6bb4cfdb5 Mon Sep 17 00:00:00 2001 From: Laika Date: Mon, 2 May 2022 13:50:20 -0700 Subject: [PATCH] City, Nation, and Player persistence halfway done, loading works now saving needs to be done. --- src/main/java/moe/oko/opennaw/OpenNAW.java | 12 +- src/main/java/moe/oko/opennaw/model/City.java | 10 ++ .../java/moe/oko/opennaw/model/Nation.java | 4 +- .../moe/oko/opennaw/storage/NAWDatabase.java | 137 ++++++++++++++++++ .../moe/oko/opennaw/util/CityHandler.java | 6 + .../moe/oko/opennaw/util/ConfigHelper.java | 7 + .../moe/oko/opennaw/util/NationHandler.java | 16 ++ src/main/resources/config.yml | 8 +- 8 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 src/main/java/moe/oko/opennaw/storage/NAWDatabase.java diff --git a/src/main/java/moe/oko/opennaw/OpenNAW.java b/src/main/java/moe/oko/opennaw/OpenNAW.java index 35f9b53..78944ef 100644 --- a/src/main/java/moe/oko/opennaw/OpenNAW.java +++ b/src/main/java/moe/oko/opennaw/OpenNAW.java @@ -3,6 +3,7 @@ package moe.oko.opennaw; import moe.oko.opennaw.command.CityCommand; import moe.oko.opennaw.command.NationCommand; import moe.oko.opennaw.listener.ActionListener; +import moe.oko.opennaw.storage.NAWDatabase; import moe.oko.opennaw.util.*; import net.luckperms.api.LuckPerms; import org.bukkit.Bukkit; @@ -19,6 +20,8 @@ public final class OpenNAW extends JavaPlugin { public CityHandler cityHandler; + public NAWDatabase database; + public ConfigHelper configHelper; public GroupHandler groupHandler; @@ -49,7 +52,14 @@ public final class OpenNAW extends JavaPlugin { cityHandler = new CityHandler(); 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 this.getCommand("nation").setExecutor(new NationCommand()); diff --git a/src/main/java/moe/oko/opennaw/model/City.java b/src/main/java/moe/oko/opennaw/model/City.java index 7a06642..eea149b 100644 --- a/src/main/java/moe/oko/opennaw/model/City.java +++ b/src/main/java/moe/oko/opennaw/model/City.java @@ -27,6 +27,16 @@ public class City { 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() { return name; } diff --git a/src/main/java/moe/oko/opennaw/model/Nation.java b/src/main/java/moe/oko/opennaw/model/Nation.java index b9365ed..52363b3 100644 --- a/src/main/java/moe/oko/opennaw/model/Nation.java +++ b/src/main/java/moe/oko/opennaw/model/Nation.java @@ -28,10 +28,10 @@ public class Nation { } //Db Constructor - public Nation(String name, Group group, HashMap players, String dynmapIcon, Location spawn) { + public Nation(String name, Group group, String dynmapIcon, Location spawn) { this.name = name; this.group = group; - this.players = players; + this.players = new HashMap<>(); this.dynmapIcon = dynmapIcon; this.spawn = spawn; } diff --git a/src/main/java/moe/oko/opennaw/storage/NAWDatabase.java b/src/main/java/moe/oko/opennaw/storage/NAWDatabase.java new file mode 100644 index 0000000..a0bc125 --- /dev/null +++ b/src/main/java/moe/oko/opennaw/storage/NAWDatabase.java @@ -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 loadCities() { + try { + OpenNAW.getInstance().getLogger().info("Loading Cities..."); + PreparedStatement pullCity = connection.prepareStatement(SELECT_CITIES); + ResultSet rs = pullCity.executeQuery(); + List 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 loadNations() { + try { + OpenNAW.getInstance().getLogger().info("Loading Nations..."); + PreparedStatement pullNation = connection.prepareStatement(SELECT_NATION); + ResultSet rs = pullNation.executeQuery(); + List 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."); + } + } +} diff --git a/src/main/java/moe/oko/opennaw/util/CityHandler.java b/src/main/java/moe/oko/opennaw/util/CityHandler.java index cbd6288..c26e9a3 100644 --- a/src/main/java/moe/oko/opennaw/util/CityHandler.java +++ b/src/main/java/moe/oko/opennaw/util/CityHandler.java @@ -49,6 +49,12 @@ public class CityHandler { return null; } + public void loadCityList(List cities) { + for(City city : cities) { + this.cities.put(city.getName(), city); + } + } + public List getCityList() { List cityList = new ArrayList(); for (City city : cities.values()) { diff --git a/src/main/java/moe/oko/opennaw/util/ConfigHelper.java b/src/main/java/moe/oko/opennaw/util/ConfigHelper.java index 1977eb0..de877b1 100644 --- a/src/main/java/moe/oko/opennaw/util/ConfigHelper.java +++ b/src/main/java/moe/oko/opennaw/util/ConfigHelper.java @@ -1,6 +1,8 @@ package moe.oko.opennaw.util; +import moe.oko.opennaw.storage.NAWDatabase; import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.ConfigurationSection; public class ConfigHelper { private Configuration config; @@ -13,4 +15,9 @@ public class ConfigHelper { public int cityDamageOnStrike() { 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")); + } } diff --git a/src/main/java/moe/oko/opennaw/util/NationHandler.java b/src/main/java/moe/oko/opennaw/util/NationHandler.java index 54d73b3..eb5f3aa 100644 --- a/src/main/java/moe/oko/opennaw/util/NationHandler.java +++ b/src/main/java/moe/oko/opennaw/util/NationHandler.java @@ -4,6 +4,7 @@ import moe.oko.opennaw.OpenNAW; import moe.oko.opennaw.model.City; import moe.oko.opennaw.model.Nation; import net.luckperms.api.model.group.Group; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.ArrayList; @@ -51,6 +52,21 @@ public class NationHandler { 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 nations) { + for(Nation nation : nations) { + this.nations.put(nation.getName(), nation); + } + } + public List getNationList() { List nationList = new ArrayList(); for (Nation nation : nations.values()) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5d53a1f..4217714 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,10 @@ # OpenNAW Configuration city: #The amount of damage a city takes on the point being striked - damageOnStrike: 5 \ No newline at end of file + damageOnStrike: 5 +sql: + host: "localhost" + port: 3306 + database: "NAW" + username: "Maxopoly" + password: "Squidlover42069" \ No newline at end of file