added a configuration file reader

This commit is contained in:
Louis Vallat 2020-04-06 15:07:32 +02:00
parent 18da94758f
commit f72c5f8af9
7 changed files with 101 additions and 45 deletions

View File

@ -13,6 +13,7 @@ import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,46 +32,27 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
/** /**
* The path for the plugin's files. * The path for the plugin's files.
*/ */
private final String path = "./plugins/BackHome"; public static final String PATH = "./plugins/BackHome/";
/** private static final String DB_NAME_KEY = "db.name";
* Database name. private static final String HOME_TABLE_KEY = "db.table.home";
*/ private static final String BACK_TABLE_KEY = "db.table.back";
private final String dbName = "BackHome.data"; private static final String HOME_HISTORY_TABLE_KEY = "db.table.home.history";
private static final String BACK_HISTORY_TABLE_KEY = "db.table.back.history";
/**
* Home table name.
*/
private final String tableNameHome = "Home";
/**
* Home table name.
*/
private final String tableNameBack = "Back";
/**
* Home table history name.
*/
private final String tableNameHomeHistory = "HomeHistory";
/**
* Back table history name.
*/
private final String tableNameBackHistory = "BackHistory";
/**
* The connection to the database.
*/
private Connection connexion; private Connection connexion;
private ConfigFileReader config;
@Override @Override
public void onEnable() { public void onEnable() {
try { try {
initDirectory(); initDirectory();
createTable(); createTable();
config = new ConfigFileReader();
config.load();
Bukkit.getServer().getPluginManager().registerEvents(this, this); Bukkit.getServer().getPluginManager().registerEvents(this, this);
getLogger().info("Done enabling."); getLogger().info("Done enabling.");
} catch (SQLException ex) { } catch (SQLException | IOException ex) {
getLogger().warning(ex.getMessage()); getLogger().warning(ex.getMessage());
getLogger().warning("You cannot use this plugin while this error " getLogger().warning("You cannot use this plugin while this error "
+ "hasn't been fixed."); + "hasn't been fixed.");
@ -86,7 +68,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* Initialize the plugin's directory. * Initialize the plugin's directory.
*/ */
private void initDirectory() { private void initDirectory() {
new File(path).mkdirs(); new File(PATH).mkdirs();
} }
/** /**
@ -111,8 +93,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/ */
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:sqlite:" return DriverManager.getConnection("jdbc:sqlite:"
+ this.path + File.separator + PATH + config.getProperty(DB_NAME_KEY));
+ this.dbName);
} }
@EventHandler @EventHandler
@ -208,7 +189,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
try { try {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement( PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + this.tableNameBack + " WHERE uuid = ?;"); "SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(BACK_TABLE_KEY) + " WHERE uuid = ?;");
recherche.setString(1, ((Player) sender).getUniqueId().toString()); recherche.setString(1, ((Player) sender).getUniqueId().toString());
double x = ((Player) sender).getLocation().getX(); double x = ((Player) sender).getLocation().getX();
double y = ((Player) sender).getLocation().getY(); double y = ((Player) sender).getLocation().getY();
@ -245,7 +226,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
if (getHomeList(((Player) sender).getUniqueId()).contains(name)) { if (getHomeList(((Player) sender).getUniqueId()).contains(name)) {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement( PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + this.tableNameHome + " WHERE uuid = ? AND name = ?;"); "SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;");
recherche.setString(1, ((Player) sender).getUniqueId().toString()); recherche.setString(1, ((Player) sender).getUniqueId().toString());
recherche.setString(2, name); recherche.setString(2, name);
removeBack(((Player) sender).getUniqueId()); removeBack(((Player) sender).getUniqueId());
@ -386,7 +367,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void removeBack(UUID backedPlayer) throws SQLException { private void removeBack(UUID backedPlayer) throws SQLException {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement stmt = this.connexion.prepareStatement("" PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + this.tableNameBack + "DELETE FROM " + config.getProperty(BACK_TABLE_KEY)
+ " WHERE uuid = ?;" + " WHERE uuid = ?;"
); );
stmt.setString(1, backedPlayer.toString()); stmt.setString(1, backedPlayer.toString());
@ -412,9 +393,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
List<PreparedStatement> stmts = new ArrayList<>(); List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement( stmts.add(this.connexion.prepareStatement(
"INSERT INTO " "INSERT INTO "
+ this.tableNameHome + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);")); + config.getProperty(HOME_TABLE_KEY) + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement( stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameHomeHistory "INSERT INTO " + config.getProperty(HOME_HISTORY_TABLE_KEY)
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);")); + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) { for (PreparedStatement stmt : stmts) {
stmt.setString(1, homedPlayer.toString()); stmt.setString(1, homedPlayer.toString());
@ -447,9 +428,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
this.connexion = getConnection(); this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>(); List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement( stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameBack + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);")); "INSERT INTO " + config.getProperty(BACK_TABLE_KEY) + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
stmts.add(this.connexion.prepareStatement( stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + this.tableNameBackHistory "INSERT INTO " + config.getProperty(BACK_HISTORY_TABLE_KEY)
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);")); + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) { for (PreparedStatement stmt : stmts) {
stmt.setString(1, backedPlayer.toString()); stmt.setString(1, backedPlayer.toString());
@ -474,7 +455,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void removeHome(UUID backedPlayer, String homeName) throws SQLException { private void removeHome(UUID backedPlayer, String homeName) throws SQLException {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement stmt = this.connexion.prepareStatement("" PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + this.tableNameHome + " WHERE uuid = ? AND name = ?;" + "DELETE FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;"
); );
stmt.setString(1, backedPlayer.toString()); stmt.setString(1, backedPlayer.toString());
stmt.setString(2, homeName); stmt.setString(2, homeName);
@ -493,7 +474,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
this.connexion = getConnection(); this.connexion = getConnection();
List<String> homeNames = new ArrayList<>(); List<String> homeNames = new ArrayList<>();
PreparedStatement recherche = this.connexion.prepareStatement( PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + this.tableNameHome + " WHERE uuid = ?;"); "SELECT name FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ?;");
recherche.setString(1, player.toString()); recherche.setString(1, player.toString());
try (ResultSet res = recherche.executeQuery()) { try (ResultSet res = recherche.executeQuery()) {
while (res.next()) { while (res.next()) {
@ -512,7 +493,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void createBackHistoryTable() throws SQLException { private void createBackHistoryTable() throws SQLException {
PreparedStatement stmt; PreparedStatement stmt;
stmt = connexion.prepareStatement("" stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameBackHistory + "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_HISTORY_TABLE_KEY)
+ "(" + "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, " + "uuid TEXT, "
@ -536,7 +517,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void createBackTable() throws SQLException { private void createBackTable() throws SQLException {
PreparedStatement stmt; PreparedStatement stmt;
stmt = connexion.prepareStatement("" stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameBack + "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY)
+ "(" + "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT UNIQUE, " + "uuid TEXT UNIQUE, "
@ -560,7 +541,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void createHomeHistoryTable() throws SQLException { private void createHomeHistoryTable() throws SQLException {
PreparedStatement stmt; PreparedStatement stmt;
stmt = connexion.prepareStatement("" stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameHomeHistory + "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_HISTORY_TABLE_KEY)
+ "(" + "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, " + "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, " + "uuid TEXT, "
@ -584,7 +565,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/ */
private void createHomeTable() throws SQLException { private void createHomeTable() throws SQLException {
PreparedStatement stmt = connexion.prepareStatement("" PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + tableNameHome + "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_TABLE_KEY)
+ "(" + "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, " + "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, " + "uuid TEXT, "

View File

@ -0,0 +1,56 @@
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,5 @@
db.name=BackHome.data
db.table.home=Home
db.table.back=Back
db.table.home.history=HomeHistory
db.table.back.history=BackHistory

View File

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

Binary file not shown.

View File

@ -0,0 +1,9 @@
name: BackHome
version: 1.0-SNAPSHOT
main: louisvallat.xyz.backhome.BackHome
api-version: 1.13
prefix: BackHome
load: STARTUP
authors: [LouisVallat]
description: A plugin to set and go back to homes.
website: louis-vallat.xyz