From aebca7ee6a6b22fe7fb97e9630cb93c43997b77e Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 25 Sep 2020 20:46:27 +0200 Subject: [PATCH] code slightly reworked --- .gitignore | 1 + .../levelSync/listener/EventsListener.java | 37 ++++++++----------- src/fr/topeka/levelSync/sql/Sql.java | 30 +++------------ .../topeka/levelSync/tasks/SaveDataTask.java | 2 +- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index cc9a220..323a670 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin/ .classpath .project +.settings/ diff --git a/src/fr/topeka/levelSync/listener/EventsListener.java b/src/fr/topeka/levelSync/listener/EventsListener.java index b1b8787..b4e02cd 100644 --- a/src/fr/topeka/levelSync/listener/EventsListener.java +++ b/src/fr/topeka/levelSync/listener/EventsListener.java @@ -28,19 +28,15 @@ public class EventsListener implements Listener { @Override public void run() { Player p = event.getPlayer(); - if(sql.checkConnection()) { - if(p.isOnline()) { - try { - float[] values = sql.getPlayerLevel(p); - p.setExp(values[1]); - p.setLevel((int) values[0]); - } catch (SQLException e) { - e.printStackTrace(); - p.sendMessage("[LevelSync] " + plugin.msg_syncError); - } - } - }else { + try { + sql.checkConnection(); + float[] values = sql.getPlayerLevel(p); + p.setExp(values[1]); + p.setLevel((int) values[0]); + } catch (SQLException e) { p.sendMessage("[LevelSync] " + plugin.msg_syncError); + plugin.getLogger().warning("[LevelSync] " + plugin.msg_syncError); + e.printStackTrace(); } } }, 20L); @@ -51,16 +47,13 @@ public class EventsListener implements Listener { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - Player p = event.getPlayer(); - if(sql.checkConnection()) { - try { - sql.setPlayerLevel(p); - }catch(SQLException e) { - p.sendMessage("[LevelSync] " + plugin.msg_dbError); - e.printStackTrace(); - } - }else { - p.sendMessage("[LevelSync] " + plugin.msg_dbError); + try { + Player p = event.getPlayer(); + sql.checkConnection(); + sql.sendPlayerLevel(p); + }catch(SQLException e) { + plugin.getLogger().warning("[LevelSync] " + plugin.msg_dbError); + e.printStackTrace(); } } }); diff --git a/src/fr/topeka/levelSync/sql/Sql.java b/src/fr/topeka/levelSync/sql/Sql.java index dd2bf72..8c485cc 100644 --- a/src/fr/topeka/levelSync/sql/Sql.java +++ b/src/fr/topeka/levelSync/sql/Sql.java @@ -57,7 +57,7 @@ public class Sql { return values; } - public void setPlayerLevel(Player player) throws SQLException{ + public void sendPlayerLevel(Player player) throws SQLException{ UUID uuid = player.getUniqueId(); int level = player.getLevel(); float xp = player.getExp(); @@ -73,27 +73,9 @@ public class Sql { } } - public boolean checkConnection() { - try { - if(connection == null || connection.isClosed() || !connection.isValid(3)) { - return reConnect(); - } - return true; - } catch (SQLException e) { - e.printStackTrace(); - plugin.getLogger().warning("Could not reconnect to database ! error: " + e.getMessage()); - return false; - } - } - - public boolean reConnect() { - try { - this.connect(); - return true; - }catch(SQLException e) { - e.printStackTrace(); - return false; - } + public void checkConnection() throws SQLException { + if(connection == null || connection.isClosed() || !connection.isValid(1)) + connect(); } public void closeConnection() { @@ -102,8 +84,6 @@ public class Sql { connection.close(); connection = null; } - }catch(SQLException e) { - e.printStackTrace(); - } + }catch(SQLException ignored) {} } } diff --git a/src/fr/topeka/levelSync/tasks/SaveDataTask.java b/src/fr/topeka/levelSync/tasks/SaveDataTask.java index 9f3e043..946db93 100644 --- a/src/fr/topeka/levelSync/tasks/SaveDataTask.java +++ b/src/fr/topeka/levelSync/tasks/SaveDataTask.java @@ -21,7 +21,7 @@ public class SaveDataTask extends BukkitRunnable { try { main.getLogger().info("[LevelSync] Starting saving data task"); for(Player p : main.getServer().getOnlinePlayers()) { - main.getSql().setPlayerLevel(p); + main.getSql().sendPlayerLevel(p); n++; } main.getLogger().info("[LevelSync] saving data task done for " + n + " players");