added a configuration file reader
This commit is contained in:
parent
18da94758f
commit
f72c5f8af9
@ -13,6 +13,7 @@ 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;
|
||||
@ -31,46 +32,27 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
/**
|
||||
* The path for the plugin's files.
|
||||
*/
|
||||
private final String path = "./plugins/BackHome";
|
||||
public static final String PATH = "./plugins/BackHome/";
|
||||
|
||||
/**
|
||||
* Database name.
|
||||
*/
|
||||
private final String dbName = "BackHome.data";
|
||||
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";
|
||||
|
||||
/**
|
||||
* 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 ConfigFileReader config;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
try {
|
||||
initDirectory();
|
||||
createTable();
|
||||
config = new ConfigFileReader();
|
||||
config.load();
|
||||
Bukkit.getServer().getPluginManager().registerEvents(this, this);
|
||||
getLogger().info("Done enabling.");
|
||||
} catch (SQLException ex) {
|
||||
} catch (SQLException | IOException ex) {
|
||||
getLogger().warning(ex.getMessage());
|
||||
getLogger().warning("You cannot use this plugin while this error "
|
||||
+ "hasn't been fixed.");
|
||||
@ -86,7 +68,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
* Initialize the plugin's directory.
|
||||
*/
|
||||
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 {
|
||||
return DriverManager.getConnection("jdbc:sqlite:"
|
||||
+ this.path + File.separator
|
||||
+ this.dbName);
|
||||
+ PATH + config.getProperty(DB_NAME_KEY));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@ -208,7 +189,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
try {
|
||||
this.connexion = getConnection();
|
||||
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());
|
||||
double x = ((Player) sender).getLocation().getX();
|
||||
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)) {
|
||||
this.connexion = getConnection();
|
||||
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(2, name);
|
||||
removeBack(((Player) sender).getUniqueId());
|
||||
@ -386,7 +367,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
private void removeBack(UUID backedPlayer) throws SQLException {
|
||||
this.connexion = getConnection();
|
||||
PreparedStatement stmt = this.connexion.prepareStatement(""
|
||||
+ "DELETE FROM " + this.tableNameBack
|
||||
+ "DELETE FROM " + config.getProperty(BACK_TABLE_KEY)
|
||||
+ " WHERE uuid = ?;"
|
||||
);
|
||||
stmt.setString(1, backedPlayer.toString());
|
||||
@ -412,9 +393,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
List<PreparedStatement> stmts = new ArrayList<>();
|
||||
stmts.add(this.connexion.prepareStatement(
|
||||
"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(
|
||||
"INSERT INTO " + this.tableNameHomeHistory
|
||||
"INSERT INTO " + config.getProperty(HOME_HISTORY_TABLE_KEY)
|
||||
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
|
||||
for (PreparedStatement stmt : stmts) {
|
||||
stmt.setString(1, homedPlayer.toString());
|
||||
@ -447,9 +428,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
this.connexion = getConnection();
|
||||
List<PreparedStatement> stmts = new ArrayList<>();
|
||||
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(
|
||||
"INSERT INTO " + this.tableNameBackHistory
|
||||
"INSERT INTO " + config.getProperty(BACK_HISTORY_TABLE_KEY)
|
||||
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
|
||||
for (PreparedStatement stmt : stmts) {
|
||||
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 {
|
||||
this.connexion = getConnection();
|
||||
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(2, homeName);
|
||||
@ -493,7 +474,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
this.connexion = getConnection();
|
||||
List<String> homeNames = new ArrayList<>();
|
||||
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());
|
||||
try (ResultSet res = recherche.executeQuery()) {
|
||||
while (res.next()) {
|
||||
@ -512,7 +493,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
private void createBackHistoryTable() throws SQLException {
|
||||
PreparedStatement stmt;
|
||||
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, "
|
||||
+ "uuid TEXT, "
|
||||
@ -536,7 +517,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
private void createBackTable() throws SQLException {
|
||||
PreparedStatement stmt;
|
||||
stmt = connexion.prepareStatement(""
|
||||
+ "CREATE TABLE IF NOT EXISTS " + tableNameBack
|
||||
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY)
|
||||
+ "("
|
||||
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
||||
+ "uuid TEXT UNIQUE, "
|
||||
@ -560,7 +541,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
private void createHomeHistoryTable() throws SQLException {
|
||||
PreparedStatement stmt;
|
||||
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, "
|
||||
+ "uuid TEXT, "
|
||||
@ -584,7 +565,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
|
||||
*/
|
||||
private void createHomeTable() throws SQLException {
|
||||
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, "
|
||||
+ "uuid TEXT, "
|
||||
|
56
src/main/java/louisvallat/xyz/backhome/ConfigFileReader.java
Normal file
56
src/main/java/louisvallat/xyz/backhome/ConfigFileReader.java
Normal 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);
|
||||
}
|
||||
}
|
5
src/main/resources/default.properties
Normal file
5
src/main/resources/default.properties
Normal 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
|
5
target/classes/default.properties
Normal file
5
target/classes/default.properties
Normal 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
|
BIN
target/classes/louisvallat/xyz/backhome/BackHome.class
Normal file
BIN
target/classes/louisvallat/xyz/backhome/BackHome.class
Normal file
Binary file not shown.
BIN
target/classes/louisvallat/xyz/backhome/ConfigFileReader.class
Normal file
BIN
target/classes/louisvallat/xyz/backhome/ConfigFileReader.class
Normal file
Binary file not shown.
9
target/classes/plugin.yml
Normal file
9
target/classes/plugin.yml
Normal 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
|
Loading…
Reference in New Issue
Block a user