Use database from core plugin in coordinates plugin
This commit is contained in:
@@ -5,7 +5,7 @@ import org.bukkit.command.PluginCommand;
|
|||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import nl.interestingcorner.coordinates.commands.CoordinatesCommand;
|
import nl.interestingcorner.coordinates.commands.CoordinatesCommand;
|
||||||
import nl.interestingcorner.coordinates.db.DatabaseManager;
|
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||||
import nl.interestingcorner.coordinates.gui.CoordinatesGUIListener;
|
import nl.interestingcorner.coordinates.gui.CoordinatesGUIListener;
|
||||||
|
|
||||||
public class App extends JavaPlugin {
|
public class App extends JavaPlugin {
|
||||||
@@ -13,12 +13,7 @@ public class App extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
getLogger().info("Eyooo we boutta get lit!");
|
getLogger().info("Eyooo we boutta get lit!");
|
||||||
|
CoordinatesDatabaseManager.INSTANCE.initialize(this);
|
||||||
if (!DatabaseManager.INSTANCE.initialize(this)) {
|
|
||||||
getLogger().severe("Could not initialize database. Exiting!");
|
|
||||||
setEnabled(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
getLogger().info("Successfully initialized database");
|
getLogger().info("Successfully initialized database");
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
||||||
@@ -29,10 +24,6 @@ public class App extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
getLogger().info("Eyooo we outta here!");
|
getLogger().info("Eyooo we outta here!");
|
||||||
|
|
||||||
if (!DatabaseManager.INSTANCE.close()) {
|
|
||||||
getLogger().severe("Error while trying to close db connection");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerCommands() {
|
private void registerCommands() {
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
package nl.interestingcorner.coordinates.commands;
|
package nl.interestingcorner.coordinates.commands;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.command.Command;
|
|
||||||
import org.bukkit.command.CommandExecutor;
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.chat.ComponentStyle;
|
|
||||||
import net.md_5.bungee.api.chat.TextComponent;
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
|
||||||
import nl.interestingcorner.coordinates.db.Coordinate;
|
import nl.interestingcorner.coordinates.db.Coordinate;
|
||||||
import nl.interestingcorner.coordinates.db.DatabaseManager;
|
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||||
import nl.interestingcorner.coordinates.db.MinecraftColor;
|
import nl.interestingcorner.coordinates.db.MinecraftColor;
|
||||||
|
|
||||||
public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||||
@@ -72,7 +67,7 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO check if item doesnt already exist, coordinate with same values, create isEquals method for coordinate
|
//TODO check if item doesnt already exist, coordinate with same values, create isEquals method for coordinate
|
||||||
return DatabaseManager.INSTANCE
|
return CoordinatesDatabaseManager.INSTANCE
|
||||||
.addCoordinate(name, // name
|
.addCoordinate(name, // name
|
||||||
description, // description
|
description, // description
|
||||||
new Coordinate.Position(playerLocation.getBlockX(), playerLocation.getBlockY(),
|
new Coordinate.Position(playerLocation.getBlockX(), playerLocation.getBlockY(),
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import java.util.List;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||||
import nl.interestingcorner.coordinates.db.Coordinate;
|
import nl.interestingcorner.coordinates.db.Coordinate;
|
||||||
import nl.interestingcorner.coordinates.db.DatabaseManager;
|
|
||||||
import nl.interestingcorner.coordinates.gui.CoordinatesGUI;
|
import nl.interestingcorner.coordinates.gui.CoordinatesGUI;
|
||||||
|
|
||||||
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||||
@@ -16,10 +16,10 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
|||||||
if (sender instanceof Player player) {
|
if (sender instanceof Player player) {
|
||||||
List<Coordinate> coords;
|
List<Coordinate> coords;
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
coords = DatabaseManager.INSTANCE.getAllCoordinates();
|
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates();
|
||||||
} else if (args[1].equalsIgnoreCase("world")) {
|
} else if (args[1].equalsIgnoreCase("world")) {
|
||||||
String world = player.getWorld().getName();
|
String world = player.getWorld().getName();
|
||||||
coords = DatabaseManager.INSTANCE.getAllCoordinates(world);
|
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates(world);
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage("Invalid argument: " + args[1]);
|
sender.sendMessage("Invalid argument: " + args[1]);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package nl.interestingcorner.coordinates.db;
|
package nl.interestingcorner.coordinates.db;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -12,49 +10,15 @@ import java.util.logging.Level;
|
|||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public enum DatabaseManager {
|
public enum CoordinatesDatabaseManager implements nl.interestingcorner.core.db.DatabaseInitializeListener{
|
||||||
|
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private JavaPlugin app;
|
private JavaPlugin app;
|
||||||
|
|
||||||
/**
|
public void initialize(JavaPlugin app) {
|
||||||
* Initializes the database. Creates the file and sets up the db structure.
|
|
||||||
*
|
|
||||||
* @param app the app to use for logging
|
|
||||||
* @return true if the database was initialized successfully. False if not.
|
|
||||||
*/
|
|
||||||
public boolean initialize(JavaPlugin app) {
|
|
||||||
this.app = app;
|
this.app = app;
|
||||||
|
|
||||||
if (!setupDatabase()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!initializeTables()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes the connectino to the database
|
|
||||||
*
|
|
||||||
* @return true if the connection closed succesfully, false if not.
|
|
||||||
*/
|
|
||||||
public boolean close() {
|
|
||||||
try {
|
|
||||||
if (this.connection != null && !this.connection.isClosed()) {
|
|
||||||
this.connection.close();
|
|
||||||
}
|
|
||||||
} catch (SQLException ex) {
|
|
||||||
this.app.getLogger().log(Level.SEVERE, "Error while closing the databse connection: {0}", ex.getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,35 +118,6 @@ public enum DatabaseManager {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets up the connectino to the database. Creates a new .db file if it
|
|
||||||
* doesn't exist yet.
|
|
||||||
*
|
|
||||||
* @return true if the connection was set up successfully. False if not.
|
|
||||||
*/
|
|
||||||
private boolean setupDatabase() {
|
|
||||||
if (!this.app.getDataFolder().exists()) {
|
|
||||||
if (!this.app.getDataFolder().mkdirs()) {
|
|
||||||
app.getLogger().severe("Could not create data folder");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File dbFile = new File(app.getDataFolder(), "database.db");
|
|
||||||
String url = "jdbc:sqlite:" + dbFile.getAbsolutePath();
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.connection = DriverManager.getConnection(url);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
this.app.getLogger().log(Level.SEVERE, "Could not connect to database file {0}: {1}",
|
|
||||||
new Object[] { dbFile, e.getMessage() });
|
|
||||||
}
|
|
||||||
|
|
||||||
this.app.getLogger().log(Level.INFO, "Connected to SQLite database: {0}", dbFile.getName());
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initializes the tables for the database.
|
* initializes the tables for the database.
|
||||||
*
|
*
|
||||||
@@ -208,4 +143,12 @@ public enum DatabaseManager {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initializeDatabaseTables(Connection connection) {
|
||||||
|
this.connection = connection;
|
||||||
|
if (!initializeTables()) {
|
||||||
|
this.app.getLogger().severe("Could not initialize coordinates database tables");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user