· 5 years ago · Mar 23, 2020, 06:28 PM
1public class MysticNavigator extends JavaPlugin {
2 private String dataFolder;
3 private List<GameMode> gameModes;
4 private List<Arena> arenas;
5 private MysticNavigator plugin;
6 private Util util;
7 private Config configFile;
8 private WorldEditPlugin worldEdit;
9
10 /**
11 * onEnable() method
12 */
13 @Override
14 public void onEnable() {
15 List<Location> locationList = new ArrayList<>();
16 this.dataFolder = getDataFolder().toString();
17 this.plugin = this;
18 this.util = new Util(plugin);
19 this.gameModes = new ArrayList<>();
20 this.arenas = new ArrayList<>();
21 this.configFile = new Config(plugin);
22 this.worldEdit = (WorldEditPlugin) plugin.getServer().getPluginManager().getPlugin("WorldEdit");
23
24 setupDataFolder();
25
26 initializeGameModes();
27
28 initializeCommands();
29 }
30
31 /**
32 * onDisable() method
33 */
34 @Override
35 public void onDisable() {
36 }
37
38
39 /**
40 * @param name Name of GameMode
41 * @return Weather if GameMode exists
42 */
43 public boolean gameModeExists(String name) {
44 return (plugin.getGameMode(name) != null);
45 }
46
47
48 /**
49 * @return Weather if a Game Mode exists or not.
50 */
51 public boolean existGameModes() {
52 return (getGameModes().isEmpty());
53 }
54
55 /**
56 * Get the Util class
57 *
58 * @return Util class
59 */
60 public Util getUtil() {
61 return util;
62 }
63
64 /**
65 * Initialize all Game Modes
66 */
67 private void initializeGameModes() {
68
69 if(getConfig().getBoolean(getConfigFile().ENABLE_ARENAS)) {
70 // Check if there are any GameModes
71 if ((getUtil().getGameModes() == null))
72 return;
73
74 // Initialize GameModes
75 for (String gameMode : getUtil().getGameModes()) {
76 GameMode gm = new GameMode(gameMode, plugin, dataFolder);
77
78 if (!gameModes.contains(gm) && !gameMode.equals("default"))
79 gameModes.add(gm);
80 }
81 }
82
83 if(getConfig().getBoolean(getConfigFile().ENABLE_ARENAS)) {
84 // Initialize Arenas
85 if (!getUtil().getArenas().isEmpty()) {
86 for (String arenaName : getUtil().getArenas()) {
87 try {
88 arenas.add(new Arena(plugin, arenaName, getArenaLocationFirst(arenaName), getArenaLocationSecond(arenaName)));
89 getLogger().info("Arena " + arenaName + " has been loaded.");
90 } catch (IOException e) {
91 getLogger().info(e.getMessage());
92 }
93 }
94 } else {
95 getLogger().info("No arenas Found!");
96
97
98 String sql = "CREATE TABLE IF NOT EXISTS Arenas (\n"
99 + " id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
100 + " xFirst real,\n"
101 + " yFirst real,\n"
102 + " zFirst real,\n"
103 + " xSecond real,\n"
104 + " ySecond real,\n"
105 + " zSecond real,\n"
106 + "name text, \n"
107 + "resetTime int, \n"
108 + " world text"
109 + ");";
110
111 try (Connection conn = plugin.getUtil().getDatabase(dataFolder);
112 Statement state = conn.createStatement()) {
113 state.execute(sql);
114 } catch (SQLException e) {
115 e.printStackTrace();
116 }
117 }
118 }
119
120 }
121
122 /**
123 * Initialize the CommandKits
124 */
125 private void initializeCommands() {
126 if(getConfig().getBoolean(getConfigFile().ENABLE_GAME_MODES)) {
127 me.s1mple.CommandKits.MysticNavigator mn = new me.s1mple.CommandKits.MysticNavigator(plugin);
128 }
129
130 if(getConfig().getBoolean(getConfigFile().ENABLE_ARENAS)) {
131 MysticNavigatorArena mna = new MysticNavigatorArena(plugin);
132 }
133 }
134
135 /**
136 * List all GameModes
137 *
138 * @return A list with all Game Modes.
139 */
140 public List<GameMode> getGameModes() {
141 return gameModes;
142 }
143
144 /**
145 * Get a GameMode by name.
146 *
147 * @param name Name of GameMode
148 * @return GameMode
149 */
150 public GameMode getGameMode(String name) {
151 if (getGameModes().isEmpty())
152 return null;
153
154 for (GameMode g : getGameModes()) {
155 if (g.getName().equalsIgnoreCase(name))
156 return g;
157 }
158 return null;
159 }
160
161 /**
162 * Remove a GameMode
163 *
164 * @param gameMode GameMode
165 */
166 public void removeGameMode(GameMode gameMode) {
167 try {
168 gameMode.delete();
169 gameModes.remove(gameMode);
170 } catch (Exception ignored) {
171 }
172
173 }
174
175 /**
176 * Add a new GameMode
177 *
178 * @param name Name of the GameMode
179 * @return If the GameMode was successfully added.
180 */
181 public GameMode addGameMode(String name) {
182 return (gameModes.add(new GameMode(name, plugin, dataFolder))) ? (new GameMode(name, plugin, dataFolder)) : null;
183 }
184
185 /**
186 * @return Configuration class to make editing config easier.
187 */
188 public Config getConfigFile() {
189 return this.configFile;
190 }
191
192 // Set the data Folder up
193 private void setupDataFolder() {
194 // Check if Config File Exists, if not create one
195 if (!new File(getConfig().getCurrentPath()).exists()) {
196 new File(getConfig().getCurrentPath());
197 getConfigFile().setupConfig();
198 }
199
200 // Check if database exists, if not create one
201 if (!this.util.dbExists(dataFolder)) {
202 try (Connection conn = this.util.getDatabase(dataFolder)) {
203 if (conn != null) {
204 getLogger().info("A new database has been created.");
205 }
206
207 } catch (SQLException e) {
208 getLogger().info(e.getMessage());
209 }
210 }
211
212 }
213
214 /**
215 * Get First location of an Arena
216 *
217 * @param name Name of Arena.
218 * @return First Location of the Arena.
219 */
220 public Location getArenaLocationFirst(String name) {
221 Location loc = null;
222 String world;
223 double x;
224 double y;
225 double z;
226
227 String sql = "SELECT world, xFirst, yFirst, zFirst FROM Arenas WHERE name='" + name + "'";
228 try (Connection conn = plugin.getUtil().getDatabase(dataFolder)) {
229 ResultSet res = conn.createStatement().executeQuery(sql);
230
231 world = res.getString("world");
232 x = res.getDouble("xFirst");
233 y = res.getDouble("yFirst");
234 z = res.getDouble("zFirst");
235 res.close();
236
237 loc = new Location(plugin.getServer().getWorld(world), x, y, z);
238
239 } catch (SQLException e) {
240 System.out.println(e.getMessage());
241 }
242
243 return loc;
244 }
245
246 /**
247 * @param name Name of the Arena
248 * @return Seond Location of the arena.
249 */
250 public Location getArenaLocationSecond(String name) {
251 Location loc = null;
252 String world;
253 double x;
254 double y;
255 double z;
256
257 String sql = "SELECT world, xSecond, ySecond, zSecond FROM Arenas WHERE name='" + name + "'";
258 try (Connection conn = plugin.getUtil().getDatabase(dataFolder)) {
259 ResultSet res = conn.createStatement().executeQuery(sql);
260
261 world = res.getString("world");
262 x = res.getDouble("xSecond");
263 y = res.getDouble("ySecond");
264 z = res.getDouble("zSecond");
265 res.close();
266
267 loc = new Location(plugin.getServer().getWorld(world), x, y, z);
268
269 } catch (SQLException e) {
270 System.out.println(e.getMessage());
271 }
272
273 return loc;
274 }
275
276 /**
277 * @param name Name of the Arena you want to get.
278 * @return An Arena
279 */
280 public Arena getArena(String name) {
281 Arena arenaFound = null;
282
283 for (Arena arena : arenas) {
284 if (arena.getName().equalsIgnoreCase(name)) {
285 arenaFound = arena;
286 }
287 }
288
289 return arenaFound;
290 }
291
292 /**
293 * @return List of the Names of all arenas that exist
294 */
295 public List<String> getArenasByName() {
296 return plugin.getUtil().getArenas();
297 }
298
299 /**
300 * @return List of all Arenas that exist
301 */
302 public List<Arena> getArenas() {
303 return arenas;
304 }
305
306 /**
307 * @return Instance of the WorldEdit plugin.
308 */
309 public WorldEditPlugin getWorldEdit() {
310 return worldEdit;
311 }
312
313 /**
314 * Create a new Arena
315 *
316 * @param name Name of the Arena
317 * @param locationFirst Lowest Location of the arena
318 * @param locationSecond Highest Location of the arena
319 */
320 public void createArena(String name, Location locationFirst, Location locationSecond) throws IOException {
321 new Arena(plugin, name, locationFirst, locationSecond);
322 getUtil().getArenas();
323 }
324
325 /**
326 * Remove an Arena
327 *
328 * @param name Name of arena
329 * @return If arena was successfully removed
330 */
331 public boolean removeArena(String name) {
332 Arena arena = getArena(name);
333 arena.remove();
334 return arenas.remove(arena);
335 }
336
337}