implemented Spigot's native methods for what I coded

This commit is contained in:
Louis Vallat 2020-04-06 19:12:32 +02:00
parent 477a041400
commit eec88b2bec
4 changed files with 32 additions and 105 deletions

View File

@ -12,15 +12,12 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
/**
@ -30,32 +27,24 @@ import java.util.stream.Collectors;
*/
public class BackHome extends JavaPlugin implements Listener, TabCompleter {
/**
* The path for the plugin's files.
*/
public static final String PATH = "./plugins/BackHome/";
private static final String DB_NAME_KEY = "db.name";
private static final String HOME_TABLE_KEY = "db.table.home";
private static final String BACK_TABLE_KEY = "db.table.back";
private static final String HOME_HISTORY_TABLE_KEY = "db.table.home.history";
private static final String BACK_HISTORY_TABLE_KEY = "db.table.back.history";
private static final String DB_NAME_KEY = "database.name";
private static final String HOME_TABLE_KEY = "database.tables.home.base";
private static final String BACK_TABLE_KEY = "database.tables.back.base";
private static final String HOME_HISTORY_TABLE_KEY = "database.tables.home.history";
private static final String BACK_HISTORY_TABLE_KEY = "database.tables.back.history";
private Connection connexion;
private ConfigFileReader config;
@Override
public void onEnable() {
try {
initDirectory();
saveDefaultConfig();
createTable();
config = new ConfigFileReader();
config.load();
Bukkit.getServer().getPluginManager().registerEvents(this, this);
getLogger().info("Done enabling.");
} catch (SQLException | IOException ex) {
getLogger().warning(ex.getMessage());
getLogger().warning("You cannot use this plugin while this error "
} catch (SQLException ex) {
getLogger().severe(ex.getMessage());
getLogger().severe("You cannot use this plugin while this error "
+ "hasn't been fixed.");
getPluginLoader().disablePlugin(this);
}
@ -66,16 +55,6 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
getLogger().info("Done disabling.");
}
/**
* Initialize the plugin's directory.
*/
private void initDirectory() {
if (new File(PATH).mkdirs()) {
getLogger().log(Level.SEVERE, "Cannot create directory '" + PATH + "'.");
getPluginLoader().disablePlugin(this);
}
}
/**
* Create the needed tables for storing the data we are using.
*
@ -98,7 +77,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:sqlite:"
+ PATH + config.getProperty(DB_NAME_KEY));
+ getConfig().getString(DB_NAME_KEY));
}
@EventHandler
@ -191,7 +170,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
try {
this.connexion = getConnection();
try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(BACK_TABLE_KEY) + " WHERE uuid = ?;")) {
"SELECT x, y, z, yaw, pitch, world FROM " + getConfig().getString(BACK_TABLE_KEY) + " WHERE uuid = ?;")) {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
teleportPlayerFromPreparedStatement(sender, recherche);
removeBack(((Player) sender));
@ -223,7 +202,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
if (getHomeList(((Player) sender).getUniqueId()).contains(name)) {
this.connexion = getConnection();
try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;")) {
"SELECT x, y, z, yaw, pitch, world FROM " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;")) {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
recherche.setString(2, name);
removeBack(((Player) sender));
@ -346,7 +325,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
if (backedPlayer != null) {
this.connexion = getConnection();
try (PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + config.getProperty(BACK_TABLE_KEY)
+ "DELETE FROM " + getConfig().getString(BACK_TABLE_KEY)
+ " WHERE uuid = ?;"
)) {
stmt.setString(1, backedPlayer.getUniqueId().toString());
@ -368,9 +347,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO "
+ config.getProperty(HOME_TABLE_KEY) + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
+ getConfig().getString(HOME_TABLE_KEY) + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + config.getProperty(HOME_HISTORY_TABLE_KEY)
"INSERT INTO " + getConfig().getString(HOME_HISTORY_TABLE_KEY)
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, player.getUniqueId().toString());
@ -398,9 +377,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + config.getProperty(BACK_TABLE_KEY) + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
"INSERT INTO " + getConfig().getString(BACK_TABLE_KEY) + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + config.getProperty(BACK_HISTORY_TABLE_KEY)
"INSERT INTO " + getConfig().getString(BACK_HISTORY_TABLE_KEY)
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, player.getUniqueId().toString());
@ -426,7 +405,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void removeHome(UUID backedPlayer, String homeName) throws SQLException {
this.connexion = getConnection();
try (PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;"
+ "DELETE FROM " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;"
)) {
stmt.setString(1, backedPlayer.toString());
stmt.setString(2, homeName);
@ -446,7 +425,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
this.connexion = getConnection();
List<String> homeNames = new ArrayList<>();
try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ?;")) {
"SELECT name FROM " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ?;")) {
recherche.setString(1, player.toString());
try (ResultSet res = recherche.executeQuery()) {
while (res.next()) {
@ -465,7 +444,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createBackHistoryTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_HISTORY_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(BACK_HISTORY_TABLE_KEY)
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
@ -489,7 +468,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createBackTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(BACK_TABLE_KEY)
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT UNIQUE, "
@ -513,7 +492,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createHomeHistoryTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_HISTORY_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(HOME_HISTORY_TABLE_KEY)
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
@ -538,7 +517,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createHomeTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(HOME_TABLE_KEY)
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "

View File

@ -1,56 +0,0 @@
package louisvallat.xyz.backhome;
import org.bukkit.Bukkit;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
/**
* A configuration file reader.
*/
public class ConfigFileReader {
private static final String CONFIG_FILE_NAME = "BackHome.properties";
private static final String DEFAULT_CONFIG_FILE_NAME = "default.properties";
private final Properties properties;
public ConfigFileReader() {
this.properties = new Properties();
}
public void load() throws IOException {
Bukkit.getLogger().log(Level.INFO, "Loading configuration file.");
try {
FileInputStream fis = new FileInputStream(BackHome.PATH + CONFIG_FILE_NAME);
properties.load(fis);
fis.close();
} catch (IOException e) {
Bukkit.getLogger().log(Level.WARNING, "Cannot load configuration file '" + CONFIG_FILE_NAME + "'.");
Bukkit.getLogger().log(Level.INFO, "Loading default configuration file instead.");
InputStream is = this.getClass().getResourceAsStream(DEFAULT_CONFIG_FILE_NAME);
if (is != null) {
properties.load(is);
is.close();
}
}
Bukkit.getLogger().log(Level.INFO, "Loading default configuration file instead.");
properties.forEach((key, value) -> Bukkit.getLogger().log(Level.INFO,
String.format("Property '%s' has value '%s'", key, value)));
}
/**
* Get the property related to a specific key.
*
* @param key the key.
* @return the value for this key, null if none.
*/
public String getProperty(String key) {
return this.properties.getProperty(key);
}
}

View File

@ -0,0 +1,9 @@
database:
name: "BackHome.data"
tables:
home:
base: "Home"
history: "HomeHistory"
back:
base: "Back"
history: "BackHistory"

View File

@ -1,5 +0,0 @@
db.name=BackHome.data
db.table.home=Home
db.table.back=Back
db.table.home.history=HomeHistory
db.table.back.history=BackHistory