removed useless config elements and added a way to choose if you want to keep an history or not of all the backs and homes set

This commit is contained in:
Louis Vallat 2020-04-06 19:35:40 +02:00
parent 42bbfb5f0d
commit 993279974b
2 changed files with 36 additions and 39 deletions

View File

@ -27,11 +27,13 @@ import java.util.stream.Collectors;
*/
public class BackHome extends JavaPlugin implements Listener, TabCompleter {
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 static final String DB_NAME_KEY = "data.name";
private static final String HOME_TABLE = "Home";
private static final String BACK_TABLE = "Back";
private static final String HOME_HISTORY_TABLE = "HomeHistory";
private static final String BACK_HISTORY_TABLE = "BackHistory";
private static final String HOME_KEEP_HISTORY_KEY = "data.home.keep-history";
private static final String BACK_KEEP_HISTORY_KEY = "data.back.keep-history";
private Connection connexion;
@ -63,9 +65,9 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private void createTable() throws SQLException {
this.connexion = getConnection();
createHomeTable();
createHomeHistoryTable();
createBackTable();
createBackHistoryTable();
if (getConfig().getBoolean(HOME_KEEP_HISTORY_KEY)) createHomeHistoryTable();
if (getConfig().getBoolean(BACK_KEEP_HISTORY_KEY)) createBackHistoryTable();
this.connexion.close();
}
@ -170,7 +172,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 " + getConfig().getString(BACK_TABLE_KEY) + " WHERE uuid = ?;")) {
"SELECT x, y, z, yaw, pitch, world FROM " + BACK_TABLE + " WHERE uuid = ?;")) {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
teleportPlayerFromPreparedStatement(sender, recherche);
removeBack(((Player) sender));
@ -202,7 +204,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 " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;")) {
"SELECT x, y, z, yaw, pitch, world FROM " + HOME_TABLE + " WHERE uuid = ? AND name = ?;")) {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
recherche.setString(2, name);
removeBack(((Player) sender));
@ -242,7 +244,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
float pitch = res.getFloat("pitch");
String world = res.getString("world");
((Player) sender).teleport(new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch));
sender.sendMessage(ChatColor.GREEN + "Teleported you back successfully.");
sender.sendMessage(ChatColor.GREEN + "Teleported you successfully.");
}
}
}
@ -284,9 +286,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private String getArgsAsString(String[] args) {
StringBuilder name = new StringBuilder();
for (String arg : args) {
if (name.length() != 0) {
name.append(" ");
}
if (name.length() != 0) name.append(" ");
name.append(arg);
}
return name.toString();
@ -325,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 " + getConfig().getString(BACK_TABLE_KEY)
+ "DELETE FROM " + BACK_TABLE
+ " WHERE uuid = ?;"
)) {
stmt.setString(1, backedPlayer.getUniqueId().toString());
@ -348,9 +348,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO "
+ getConfig().getString(HOME_TABLE_KEY) + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
+ HOME_TABLE + "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
if (getConfig().getBoolean(HOME_KEEP_HISTORY_KEY))
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + getConfig().getString(HOME_HISTORY_TABLE_KEY)
"INSERT INTO " + HOME_HISTORY_TABLE
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, player.getUniqueId().toString());
@ -379,9 +380,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + getConfig().getString(BACK_TABLE_KEY) + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
"INSERT INTO " + BACK_TABLE + "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
if (getConfig().getBoolean(BACK_KEEP_HISTORY_KEY))
stmts.add(this.connexion.prepareStatement(
"INSERT INTO " + getConfig().getString(BACK_HISTORY_TABLE_KEY)
"INSERT INTO " + BACK_HISTORY_TABLE
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, player.getUniqueId().toString());
@ -408,7 +410,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 " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;"
+ "DELETE FROM " + HOME_TABLE + " WHERE uuid = ? AND name = ?;"
)) {
stmt.setString(1, backedPlayer.toString());
stmt.setString(2, homeName);
@ -428,12 +430,10 @@ 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 " + getConfig().getString(HOME_TABLE_KEY) + " WHERE uuid = ?;")) {
"SELECT name FROM " + HOME_TABLE + " WHERE uuid = ?;")) {
recherche.setString(1, player.toString());
try (ResultSet res = recherche.executeQuery()) {
while (res.next()) {
homeNames.add(res.getString("name"));
}
while (res.next()) homeNames.add(res.getString("name"));
}
}
this.connexion.close();
@ -447,7 +447,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createBackHistoryTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(BACK_HISTORY_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + BACK_HISTORY_TABLE
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
@ -471,7 +471,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createBackTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(BACK_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + BACK_TABLE
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT UNIQUE, "
@ -495,7 +495,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createHomeHistoryTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(HOME_HISTORY_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + HOME_HISTORY_TABLE
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "
@ -520,7 +520,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
*/
private void createHomeTable() throws SQLException {
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + getConfig().getString(HOME_TABLE_KEY)
+ "CREATE TABLE IF NOT EXISTS " + HOME_TABLE
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
+ "uuid TEXT, "

View File

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