· 6 years ago · Jul 28, 2019, 03:00 AM
1package me.razman121.HazardMCKingdoms.MySQL;
2
3import java.sql.PreparedStatement;
4import java.sql.ResultSet;
5import java.sql.SQLException;
6import java.util.ArrayList;
7import java.util.UUID;
8
9import org.bukkit.entity.Player;
10
11import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
12
13import me.razman121.HazardMCKingdoms.Main.Main;
14import me.razman121.HazardMCKingdoms.Objects.Kingdom;
15import me.razman121.HazardMCKingdoms.Utils.Utils;
16
17public class MySQL_KingdomData {
18
19 /* Creates The KingdomData Table */
20 public static void createTable() {
21 try {
22 PreparedStatement statement = Main.getInstance().getConnection()
23 .prepareStatement("CREATE TABLE ? (`ID` INT(255) NOT NULL AUTO_INCREMENT, "
24 + "`NAME` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `MAX_ALLOWED_PLAYERS` "
25 + "INT(255) NOT NULL, `CURRENT_PLAYER_COUNT` INT(255) NOT NULL, `LEVEL` INT(255) NOT NULL, "
26 + "`FORT_COUNT` INT(255) NOT NULL, `OUTPOST_COUNT` INT(255) NOT NULL, `VILLAGE_COUNT` INT(255) "
27 + "NOT NULL, `CITY_COUNT` INT(255) NOT NULL, `QUARRY_COUNT` INT(255) NOT NULL, `LUMBER_YARD_COUNT` "
28 + "INT(255) NOT NULL, `BALANCE` INT(255) NOT NULL, `CREATION_DATE` DATE NOT NULL, `PLAYERS` "
29 + "VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `UUID` VARCHAR(255) CHARACTER SET"
30 + " utf8 COLLATE utf8_bin NOT NULL, PRIMARY KEY (`ID`))");
31 statement.setString(1, Main.getInstance().KINGDOMS_TABLE);
32 statement.execute();
33 Utils.log("Successfully created the " + Main.getInstance().KINGDOMS_TABLE + " table!");
34 statement.close();
35 } catch (SQLException e) {
36 Utils.warn("Error: Could not create the " + Main.getInstance().KINGDOMS_TABLE + " table!");
37 e.printStackTrace();
38 }
39 }
40
41 /* Deletes The KingdomData Table */
42 public static void deleteTable() {
43 try {
44 PreparedStatement statement = Main.getInstance().getConnection().prepareStatement("DROP TABLE ?");
45 statement.setString(1, Main.getInstance().KINGDOMS_TABLE);
46 statement.execute();
47 Utils.log("Successfully deleted the " + Main.getInstance().KINGDOMS_TABLE + " table!");
48 statement.close();
49 } catch (SQLException e) {
50 Utils.warn("Error: Could not delete the " + Main.getInstance().KINGDOMS_TABLE + " table!");
51 e.printStackTrace();
52 }
53 }
54
55 /* Checks if the Table exists */
56 public static boolean tableExists() {
57
58 try {
59 PreparedStatement statement = Main.getInstance().getConnection().prepareStatement("SELECT * FROM ?");
60 try {
61 ResultSet results = statement.executeQuery();
62
63 if (results != null) {
64 statement.close();
65 return true;
66 } else {
67 statement.close();
68 return false;
69 }
70 } catch (MySQLSyntaxErrorException ex) {
71 return false;
72 }
73 } catch (SQLException e) {
74 e.printStackTrace();
75 }
76 return false;
77 }
78
79 @SuppressWarnings("deprecation")
80 public static void createKingdom(Kingdom kingdom) {
81 String name = kingdom.getName();
82 Integer max_allowed_players = kingdom.getMaxAllowedPlayers();
83 Integer current_player_count = kingdom.getCurrentPlayerCount();
84 Integer kingdom_level = kingdom.getKingdomLevel();
85 Integer fort_count = kingdom.getFortCount();
86 Integer outpost_count = kingdom.getOutPostCount();
87 Integer village_count = kingdom.getVillagesCount();
88 Integer city_count = kingdom.getCityCount();
89 Integer quarry_count = kingdom.getQuarryCount();
90 Integer lumber_yard_count = kingdom.getLumberYardCount();
91 Integer kingdom_balance = kingdom.getKingdomBalance();
92 java.util.Date creation_date = kingdom.getCreationDate();
93 java.sql.Date date = new java.sql.Date(creation_date.getSeconds() * 1000);
94 ArrayList<Player> players = kingdom.getPlayersInKingdom();
95 UUID kingdom_uuid = kingdom.getKingdomUUID();
96
97 try {
98 PreparedStatement statement = Main.getInstance().getConnection().prepareStatement(
99 "INSERT INTO ? (`NAME` , `MAX_ALLOWED_PLAYERS`, `CURRENT_PLAYER_COUNT`,"
100 + " `LEVEL`, `FORT_COUNT`, `OUTPOST_COUNT`, `VILLAGE_COUNT`, `CITY_COUNT`, "
101 + "`QUARRY_COUNT`, `LUMBER_YARD_COUNT`, `BALANCE`, `CREATION_DATE`, `PLAYERS`,"
102 + " `UUID`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
103 statement.setString(1, name);
104 statement.setInt(2, max_allowed_players);
105 statement.setInt(3, current_player_count);
106 statement.setInt(4, kingdom_level);
107 statement.setInt(5, fort_count);
108 statement.setInt(6, outpost_count);
109 statement.setInt(7, village_count);
110 statement.setInt(8, city_count);
111 statement.setInt(9, quarry_count);
112 statement.setInt(10, lumber_yard_count);
113 statement.setInt(11, kingdom_balance);
114 statement.setDate(12, date);
115 statement.setString(13, players.toString());
116 statement.setString(14, kingdom_uuid.toString());
117 statement.executeUpdate();
118 Utils.log("Successfully added the kingdom, \"" + name + "\", to the database!");
119 statement.close();
120
121 } catch (SQLException e) {
122 Utils.warn("Error: Could not add the kingdom, \"" + name + "\", to the database!");
123 e.printStackTrace();
124 }
125 }
126
127}