removed target files and fixed sonarint warnings

This commit is contained in:
Louis Vallat 2020-04-06 18:11:26 +02:00
parent f72c5f8af9
commit 17e953e9f3
6 changed files with 122 additions and 161 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea .idea
BackHome.iml *.iml
target

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -56,6 +57,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
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.");
getPluginLoader().disablePlugin(this);
} }
} }
@ -68,7 +70,10 @@ 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(); if (new File(PATH).mkdirs()) {
getLogger().log(Level.SEVERE, "Cannot create directory '" + PATH + "'.");
getPluginLoader().disablePlugin(this);
}
} }
/** /**
@ -100,13 +105,11 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity().getPlayer(); Player player = event.getEntity().getPlayer();
try { try {
removeBack(player.getUniqueId()); removeBack(player);
addBack(player.getUniqueId(), addBack(player);
player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(),
player.getLocation().getYaw(), player.getLocation().getPitch(),
player.getLocation().getWorld().getName());
} catch (SQLException e) { } catch (SQLException e) {
event.getEntity().getPlayer().sendMessage(ChatColor.RED + "Your back hasn't been set due to an error, " + assert player != null;
player.sendMessage(ChatColor.RED + "Your back hasn't been set due to an error, " +
"but your death coordinates were X:" + player.getLocation().getX() "but your death coordinates were X:" + player.getLocation().getX()
+ " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ()); + " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ());
getLogger().warning(e.getMessage()); getLogger().warning(e.getMessage());
@ -134,11 +137,11 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
public boolean onCommand(CommandSender sender, Command cmd, String label, public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) { String[] args) {
if (sender instanceof Player) { if (sender instanceof Player) {
if (setHomeCommand(sender, cmd, args)) return true; if (backCommand(sender, cmd)) return true;
if (setBackCommand(sender, cmd, args)) return true;
if (homeCommand(sender, cmd, args)) return true; if (homeCommand(sender, cmd, args)) return true;
if (backCommand(sender, cmd, args)) return true; if (setBackCommand(sender, cmd)) return true;
if (delHomeCommand(sender, cmd, args)) return true; if (setHomeCommand(sender, cmd, args)) return true;
return delHomeCommand(sender, cmd, args);
} else { } else {
sender.sendMessage(ChatColor.RED + "This command can't be issued by a non-player."); sender.sendMessage(ChatColor.RED + "This command can't be issued by a non-player.");
} }
@ -181,25 +184,19 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* *
* @param sender the player that sent the command. * @param sender the player that sent the command.
* @param cmd the command. * @param cmd the command.
* @param args the args following the command.
* @return true if the command didn't fail and true if it has worked correctly. * @return true if the command didn't fail and true if it has worked correctly.
*/ */
private boolean backCommand(CommandSender sender, Command cmd, String[] args) { private boolean backCommand(CommandSender sender, Command cmd) {
if (cmd.getName().equalsIgnoreCase("back")) { if (cmd.getName().equalsIgnoreCase("back")) {
try { try {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement( 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 " + 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 y = ((Player) sender).getLocation().getY();
double z = ((Player) sender).getLocation().getZ();
float yaw = ((Player) sender).getLocation().getYaw();
float pitch = ((Player) sender).getLocation().getPitch();
String worldName = ((Player) sender).getLocation().getWorld().getName();
teleportPlayerFromPreparedStatement(sender, recherche); teleportPlayerFromPreparedStatement(sender, recherche);
removeBack(((Player) sender).getUniqueId()); removeBack(((Player) sender));
addBack(((Player) sender).getUniqueId(), x, y, z, yaw, pitch, worldName); addBack(((Player) sender));
}
this.connexion.close(); this.connexion.close();
} catch (SQLException e) { } catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while trying to execute this command. " + sender.sendMessage(ChatColor.RED + "An error occured while trying to execute this command. " +
@ -225,19 +222,14 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
try { try {
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( 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 " + 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));
addBack(((Player) sender).getUniqueId(), addBack(((Player) sender));
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
((Player) sender).getLocation().getWorld().getName());
teleportPlayerFromPreparedStatement(sender, recherche); teleportPlayerFromPreparedStatement(sender, recherche);
}
this.connexion.close(); this.connexion.close();
} else { } else {
sender.sendMessage(ChatColor.RED + "The home named '" + name + "' doesn't exist."); sender.sendMessage(ChatColor.RED + "The home named '" + name + "' doesn't exist.");
@ -293,14 +285,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
" '" + name + "'."); " '" + name + "'.");
return true; return true;
} }
addHome(((Player) sender).getUniqueId(), addHome(((Player) sender), name);
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getWorld().getName(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
name);
sender.sendMessage(ChatColor.GREEN + "Your home '" + name + "' has been set successfully."); sender.sendMessage(ChatColor.GREEN + "Your home '" + name + "' has been set successfully.");
} catch (SQLException e) { } catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later."); sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later.");
@ -333,20 +318,13 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* *
* @param sender the player. * @param sender the player.
* @param cmd the command the player sent. * @param cmd the command the player sent.
* @param args the args after the command
* @return true if the command was good and false otherwise * @return true if the command was good and false otherwise
*/ */
private boolean setBackCommand(CommandSender sender, Command cmd, String[] args) { private boolean setBackCommand(CommandSender sender, Command cmd) {
if (cmd.getName().equalsIgnoreCase("setback")) { if (cmd.getName().equalsIgnoreCase("setback")) {
try { try {
removeBack(((Player) sender).getUniqueId()); removeBack(((Player) sender));
addBack(((Player) sender).getUniqueId(), addBack(((Player) sender));
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch(),
((Player) sender).getLocation().getWorld().getName());
sender.sendMessage(ChatColor.GREEN + "Your back has been set successfully."); sender.sendMessage(ChatColor.GREEN + "Your back has been set successfully.");
} catch (SQLException e) { } catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later."); sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later.");
@ -364,31 +342,28 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @param backedPlayer the player that is being backed. * @param backedPlayer the player that is being backed.
* @throws SQLException if there were a SQL problem * @throws SQLException if there were a SQL problem
*/ */
private void removeBack(UUID backedPlayer) throws SQLException { private void removeBack(Player backedPlayer) throws SQLException {
if (backedPlayer != null) {
this.connexion = getConnection(); this.connexion = getConnection();
PreparedStatement stmt = this.connexion.prepareStatement("" try (PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + config.getProperty(BACK_TABLE_KEY) + "DELETE FROM " + config.getProperty(BACK_TABLE_KEY)
+ " WHERE uuid = ?;" + " WHERE uuid = ?;"
); )) {
stmt.setString(1, backedPlayer.toString()); stmt.setString(1, backedPlayer.getUniqueId().toString());
stmt.execute(); stmt.execute();
}
connexion.close(); connexion.close();
} }
}
/** /**
* Add a home to a player. * Add a home to a player.
* *
* @param homedPlayer the player that needs a new home.
* @param x x coordinate for the player
* @param y y coordinate for the player
* @param z z coordinate for the player
* @param world the world name
* @param yaw yaw for the player
* @param pitch pitch for a player
* @param name name for the new home * @param name name for the new home
* @throws SQLException if there were a SQL exception. * @throws SQLException if there were a SQL exception.
*/ */
private void addHome(UUID homedPlayer, double x, double y, double z, String world, float yaw, float pitch, String name) throws SQLException { private void addHome(Player player, String name) throws SQLException {
if (player != null && player.getLocation().getWorld() != null) {
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(
@ -398,33 +373,28 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
"INSERT INTO " + config.getProperty(HOME_HISTORY_TABLE_KEY) "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, player.getUniqueId().toString());
stmt.setString(2, name); stmt.setString(2, name);
stmt.setString(3, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())); stmt.setString(3, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
stmt.setDouble(4, x); stmt.setDouble(4, player.getLocation().getX());
stmt.setDouble(5, y); stmt.setDouble(5, player.getLocation().getY());
stmt.setDouble(6, z); stmt.setDouble(6, player.getLocation().getZ());
stmt.setFloat(7, yaw); stmt.setFloat(7, player.getLocation().getYaw());
stmt.setFloat(8, pitch); stmt.setFloat(8, player.getLocation().getPitch());
stmt.setString(9, world); stmt.setString(9, player.getLocation().getWorld().getName());
stmt.execute(); stmt.execute();
} }
connexion.close(); connexion.close();
} }
}
/** /**
* Add a back for a player. * Add a back for a player.
* *
* @param backedPlayer the player to add a back to.
* @param x its x coordinate
* @param y its y coordinate
* @param z its z coordinate
* @param world the world where the player were
* @param yaw the player's yaw
* @param pitch the player's pitch
* @throws SQLException if an SQL error occured. * @throws SQLException if an SQL error occured.
*/ */
private void addBack(UUID backedPlayer, double x, double y, double z, float yaw, float pitch, String world) throws SQLException { private void addBack(Player player) throws SQLException {
if (player != null && player.getLocation().getWorld() != null) {
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(
@ -433,18 +403,19 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
"INSERT INTO " + config.getProperty(BACK_HISTORY_TABLE_KEY) "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, player.getUniqueId().toString());
stmt.setString(2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); stmt.setString(2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
stmt.setDouble(3, x); stmt.setDouble(3, player.getLocation().getX());
stmt.setDouble(4, y); stmt.setDouble(4, player.getLocation().getY());
stmt.setDouble(5, z); stmt.setDouble(5, player.getLocation().getY());
stmt.setFloat(6, yaw); stmt.setFloat(6, player.getLocation().getYaw());
stmt.setFloat(7, pitch); stmt.setFloat(7, player.getLocation().getPitch());
stmt.setString(8, world); stmt.setString(8, player.getLocation().getWorld().getName());
stmt.execute(); stmt.execute();
} }
connexion.close(); connexion.close();
} }
}
/** /**
* Remove a home from a player. * Remove a home from a player.
@ -454,12 +425,13 @@ 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("" try (PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + config.getProperty(HOME_TABLE_KEY) + " 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);
stmt.execute(); stmt.execute();
}
connexion.close(); connexion.close();
} }
@ -473,14 +445,15 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private List<String> getHomeList(UUID player) throws SQLException { private List<String> getHomeList(UUID player) throws SQLException {
this.connexion = getConnection(); this.connexion = getConnection();
List<String> homeNames = new ArrayList<>(); List<String> homeNames = new ArrayList<>();
PreparedStatement recherche = this.connexion.prepareStatement( try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + config.getProperty(HOME_TABLE_KEY) + " 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()) {
homeNames.add(res.getString("name")); homeNames.add(res.getString("name"));
} }
} }
}
this.connexion.close(); this.connexion.close();
return homeNames; return homeNames;
} }
@ -491,8 +464,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there is a SQL problem. * @throws SQLException if there is a SQL problem.
*/ */
private void createBackHistoryTable() throws SQLException { private void createBackHistoryTable() throws SQLException {
PreparedStatement stmt; try (PreparedStatement stmt = connexion.prepareStatement(""
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_HISTORY_TABLE_KEY) + "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_HISTORY_TABLE_KEY)
+ "(" + "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -505,9 +477,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, " + "pitch FLOAT, "
+ "world TEXT" + "world TEXT"
+ ");" + ");"
); )) {
stmt.execute(); stmt.execute();
} }
}
/** /**
* Create the /back table. * Create the /back table.
@ -515,8 +488,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there were a SQL problem. * @throws SQLException if there were a SQL problem.
*/ */
private void createBackTable() throws SQLException { private void createBackTable() throws SQLException {
PreparedStatement stmt; try (PreparedStatement stmt = connexion.prepareStatement(""
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY) + "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY)
+ "(" + "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -529,9 +501,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, " + "pitch FLOAT, "
+ "world TEXT" + "world TEXT"
+ ");" + ");"
); )) {
stmt.execute(); stmt.execute();
} }
}
/** /**
* Create the /home history table. * Create the /home history table.
@ -539,8 +512,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there were a SQL problem. * @throws SQLException if there were a SQL problem.
*/ */
private void createHomeHistoryTable() throws SQLException { private void createHomeHistoryTable() throws SQLException {
PreparedStatement stmt; try (PreparedStatement stmt = connexion.prepareStatement(""
stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_HISTORY_TABLE_KEY) + "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_HISTORY_TABLE_KEY)
+ "(" + "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, " + "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
@ -554,9 +526,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, " + "pitch FLOAT, "
+ "world TEXT" + "world TEXT"
+ ");" + ");"
); )) {
stmt.execute(); stmt.execute();
} }
}
/** /**
* Create the /home table. * Create the /home table.
@ -564,7 +537,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there were a SQL problem. * @throws SQLException if there were a SQL problem.
*/ */
private void createHomeTable() throws SQLException { private void createHomeTable() throws SQLException {
PreparedStatement stmt = connexion.prepareStatement("" try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_TABLE_KEY) + "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_TABLE_KEY)
+ "(" + "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, " + "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
@ -578,7 +551,8 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, " + "pitch FLOAT, "
+ "world TEXT" + "world TEXT"
+ ");" + ");"
); )) {
stmt.execute(); stmt.execute();
} }
} }
}

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

View File

@ -1,9 +0,0 @@
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