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
BackHome.iml
*.iml
target

View File

@ -20,6 +20,7 @@ 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;
/**
@ -56,6 +57,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
getLogger().warning(ex.getMessage());
getLogger().warning("You cannot use this plugin while this error "
+ "hasn't been fixed.");
getPluginLoader().disablePlugin(this);
}
}
@ -68,7 +70,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* Initialize the plugin's directory.
*/
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) {
Player player = event.getEntity().getPlayer();
try {
removeBack(player.getUniqueId());
addBack(player.getUniqueId(),
player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(),
player.getLocation().getYaw(), player.getLocation().getPitch(),
player.getLocation().getWorld().getName());
removeBack(player);
addBack(player);
} 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()
+ " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ());
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,
String[] args) {
if (sender instanceof Player) {
if (setHomeCommand(sender, cmd, args)) return true;
if (setBackCommand(sender, cmd, args)) return true;
if (backCommand(sender, cmd)) return true;
if (homeCommand(sender, cmd, args)) return true;
if (backCommand(sender, cmd, args)) return true;
if (delHomeCommand(sender, cmd, args)) return true;
if (setBackCommand(sender, cmd)) return true;
if (setHomeCommand(sender, cmd, args)) return true;
return delHomeCommand(sender, cmd, args);
} else {
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 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.
*/
private boolean backCommand(CommandSender sender, Command cmd, String[] args) {
private boolean backCommand(CommandSender sender, Command cmd) {
if (cmd.getName().equalsIgnoreCase("back")) {
try {
this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(BACK_TABLE_KEY) + " WHERE uuid = ?;");
try (PreparedStatement recherche = this.connexion.prepareStatement(
"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();
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);
removeBack(((Player) sender).getUniqueId());
addBack(((Player) sender).getUniqueId(), x, y, z, yaw, pitch, worldName);
removeBack(((Player) sender));
addBack(((Player) sender));
}
this.connexion.close();
} catch (SQLException e) {
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 {
if (getHomeList(((Player) sender).getUniqueId()).contains(name)) {
this.connexion = getConnection();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ? AND name = ?;");
try (PreparedStatement recherche = this.connexion.prepareStatement(
"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());
addBack(((Player) sender).getUniqueId(),
((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());
removeBack(((Player) sender));
addBack(((Player) sender));
teleportPlayerFromPreparedStatement(sender, recherche);
}
this.connexion.close();
} else {
sender.sendMessage(ChatColor.RED + "The home named '" + name + "' doesn't exist.");
@ -293,14 +285,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
" '" + name + "'.");
return true;
}
addHome(((Player) sender).getUniqueId(),
((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);
addHome(((Player) sender), name);
sender.sendMessage(ChatColor.GREEN + "Your home '" + name + "' has been set successfully.");
} catch (SQLException e) {
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 cmd the command the player sent.
* @param args the args after the command
* @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")) {
try {
removeBack(((Player) sender).getUniqueId());
addBack(((Player) sender).getUniqueId(),
((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());
removeBack(((Player) sender));
addBack(((Player) sender));
sender.sendMessage(ChatColor.GREEN + "Your back has been set successfully.");
} catch (SQLException e) {
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.
* @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();
PreparedStatement stmt = this.connexion.prepareStatement(""
try (PreparedStatement stmt = this.connexion.prepareStatement(""
+ "DELETE FROM " + config.getProperty(BACK_TABLE_KEY)
+ " WHERE uuid = ?;"
);
stmt.setString(1, backedPlayer.toString());
)) {
stmt.setString(1, backedPlayer.getUniqueId().toString());
stmt.execute();
}
connexion.close();
}
}
/**
* 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
* @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();
List<PreparedStatement> stmts = new ArrayList<>();
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)
+ "(uuid, name, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"));
for (PreparedStatement stmt : stmts) {
stmt.setString(1, homedPlayer.toString());
stmt.setString(1, player.getUniqueId().toString());
stmt.setString(2, name);
stmt.setString(3, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
stmt.setDouble(4, x);
stmt.setDouble(5, y);
stmt.setDouble(6, z);
stmt.setFloat(7, yaw);
stmt.setFloat(8, pitch);
stmt.setString(9, world);
stmt.setDouble(4, player.getLocation().getX());
stmt.setDouble(5, player.getLocation().getY());
stmt.setDouble(6, player.getLocation().getZ());
stmt.setFloat(7, player.getLocation().getYaw());
stmt.setFloat(8, player.getLocation().getPitch());
stmt.setString(9, player.getLocation().getWorld().getName());
stmt.execute();
}
connexion.close();
}
}
/**
* 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.
*/
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();
List<PreparedStatement> stmts = new ArrayList<>();
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)
+ "(uuid, date, x, y, z, yaw, pitch, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"));
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.setDouble(3, x);
stmt.setDouble(4, y);
stmt.setDouble(5, z);
stmt.setFloat(6, yaw);
stmt.setFloat(7, pitch);
stmt.setString(8, world);
stmt.setDouble(3, player.getLocation().getX());
stmt.setDouble(4, player.getLocation().getY());
stmt.setDouble(5, player.getLocation().getY());
stmt.setFloat(6, player.getLocation().getYaw());
stmt.setFloat(7, player.getLocation().getPitch());
stmt.setString(8, player.getLocation().getWorld().getName());
stmt.execute();
}
connexion.close();
}
}
/**
* 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 {
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 = ?;"
);
)) {
stmt.setString(1, backedPlayer.toString());
stmt.setString(2, homeName);
stmt.execute();
}
connexion.close();
}
@ -473,14 +445,15 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
private List<String> getHomeList(UUID player) throws SQLException {
this.connexion = getConnection();
List<String> homeNames = new ArrayList<>();
PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ?;");
try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT name FROM " + config.getProperty(HOME_TABLE_KEY) + " WHERE uuid = ?;")) {
recherche.setString(1, player.toString());
try (ResultSet res = recherche.executeQuery()) {
while (res.next()) {
homeNames.add(res.getString("name"));
}
}
}
this.connexion.close();
return homeNames;
}
@ -491,8 +464,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there is a SQL problem.
*/
private void createBackHistoryTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_HISTORY_TABLE_KEY)
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -505,9 +477,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
)) {
stmt.execute();
}
}
/**
* Create the /back table.
@ -515,8 +488,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there were a SQL problem.
*/
private void createBackTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(BACK_TABLE_KEY)
+ "("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -529,9 +501,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
)) {
stmt.execute();
}
}
/**
* 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.
*/
private void createHomeHistoryTable() throws SQLException {
PreparedStatement stmt;
stmt = connexion.prepareStatement(""
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_HISTORY_TABLE_KEY)
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
@ -554,9 +526,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
)) {
stmt.execute();
}
}
/**
* Create the /home table.
@ -564,7 +537,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
* @throws SQLException if there were a SQL problem.
*/
private void createHomeTable() throws SQLException {
PreparedStatement stmt = connexion.prepareStatement(""
try (PreparedStatement stmt = connexion.prepareStatement(""
+ "CREATE TABLE IF NOT EXISTS " + config.getProperty(HOME_TABLE_KEY)
+ "("
+ "id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
@ -578,7 +551,8 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
+ "pitch FLOAT, "
+ "world TEXT"
+ ");"
);
)) {
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