code slightly reworked

This commit is contained in:
Quentin Legot 2020-09-25 20:46:27 +02:00
parent ce87947be0
commit aebca7ee6a
4 changed files with 22 additions and 48 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
bin/ bin/
.classpath .classpath
.project .project
.settings/

View File

@ -28,19 +28,15 @@ public class EventsListener implements Listener {
@Override @Override
public void run() { public void run() {
Player p = event.getPlayer(); Player p = event.getPlayer();
if(sql.checkConnection()) { try {
if(p.isOnline()) { sql.checkConnection();
try { float[] values = sql.getPlayerLevel(p);
float[] values = sql.getPlayerLevel(p); p.setExp(values[1]);
p.setExp(values[1]); p.setLevel((int) values[0]);
p.setLevel((int) values[0]); } catch (SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
p.sendMessage("[LevelSync] " + plugin.msg_syncError);
}
}
}else {
p.sendMessage("[LevelSync] " + plugin.msg_syncError); p.sendMessage("[LevelSync] " + plugin.msg_syncError);
plugin.getLogger().warning("[LevelSync] " + plugin.msg_syncError);
e.printStackTrace();
} }
} }
}, 20L); }, 20L);
@ -51,16 +47,13 @@ public class EventsListener implements Listener {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
Player p = event.getPlayer(); try {
if(sql.checkConnection()) { Player p = event.getPlayer();
try { sql.checkConnection();
sql.setPlayerLevel(p); sql.sendPlayerLevel(p);
}catch(SQLException e) { }catch(SQLException e) {
p.sendMessage("[LevelSync] " + plugin.msg_dbError); plugin.getLogger().warning("[LevelSync] " + plugin.msg_dbError);
e.printStackTrace(); e.printStackTrace();
}
}else {
p.sendMessage("[LevelSync] " + plugin.msg_dbError);
} }
} }
}); });

View File

@ -57,7 +57,7 @@ public class Sql {
return values; return values;
} }
public void setPlayerLevel(Player player) throws SQLException{ public void sendPlayerLevel(Player player) throws SQLException{
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
int level = player.getLevel(); int level = player.getLevel();
float xp = player.getExp(); float xp = player.getExp();
@ -73,27 +73,9 @@ public class Sql {
} }
} }
public boolean checkConnection() { public void checkConnection() throws SQLException {
try { if(connection == null || connection.isClosed() || !connection.isValid(1))
if(connection == null || connection.isClosed() || !connection.isValid(3)) { connect();
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 closeConnection() { public void closeConnection() {
@ -102,8 +84,6 @@ public class Sql {
connection.close(); connection.close();
connection = null; connection = null;
} }
}catch(SQLException e) { }catch(SQLException ignored) {}
e.printStackTrace();
}
} }
} }

View File

@ -21,7 +21,7 @@ public class SaveDataTask extends BukkitRunnable {
try { try {
main.getLogger().info("[LevelSync] Starting saving data task"); main.getLogger().info("[LevelSync] Starting saving data task");
for(Player p : main.getServer().getOnlinePlayers()) { for(Player p : main.getServer().getOnlinePlayers()) {
main.getSql().setPlayerLevel(p); main.getSql().sendPlayerLevel(p);
n++; n++;
} }
main.getLogger().info("[LevelSync] saving data task done for " + n + " players"); main.getLogger().info("[LevelSync] saving data task done for " + n + " players");