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/
.classpath
.project
.settings/

View File

@ -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();
}
}
});

View File

@ -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) {}
}
}

View File

@ -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");