Improved performance

SaveDataTask is not used yet, cause it isn't already tested
This commit is contained in:
Quentin Legot 2020-06-04 21:25:07 +02:00
parent d7f410a35c
commit ce87947be0
4 changed files with 48 additions and 13 deletions

View File

@ -1,6 +1,6 @@
name: levelSync name: levelSync
main: fr.topeka.levelSync.Main main: fr.topeka.levelSync.Main
version: 1.0-beta version: 1.1.1
author: Topeka_ author: Topeka_
description: levelSync - a Minecraft plugin which sync vanilla experience between servers description: levelSync - a Minecraft plugin which sync vanilla experience between servers
website: https://github.com/sexiestchiba/levelSync website: https://github.com/sexiestchiba/levelSync

View File

@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import fr.topeka.levelSync.listener.EventsListener; import fr.topeka.levelSync.listener.EventsListener;
import fr.topeka.levelSync.sql.Sql; import fr.topeka.levelSync.sql.Sql;
//import fr.topeka.levelSync.tasks.SaveDataTask;
/** /**
* *
@ -28,6 +29,8 @@ public class Main extends JavaPlugin{
this.getLogger().info("[levelSync] Plugin enabled"); this.getLogger().info("[levelSync] Plugin enabled");
msg_syncError = getConfig().getString("message.syncError"); msg_syncError = getConfig().getString("message.syncError");
msg_dbError = getConfig().getString("message.dbError"); msg_dbError = getConfig().getString("message.dbError");
// SaveDataTask task = new SaveDataTask(this);
// task.runTaskTimerAsynchronously(this, 6000, 3600);
} catch (ClassNotFoundException | SQLException e) { } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); e.printStackTrace();
this.getLogger().warning("An error occured during loading, disabling plugin..."); this.getLogger().warning("An error occured during loading, disabling plugin...");
@ -40,4 +43,8 @@ public class Main extends JavaPlugin{
_sql.closeConnection(); _sql.closeConnection();
this.getLogger().info("Plugin disabled"); this.getLogger().info("Plugin disabled");
} }
public Sql getSql() {
return _sql;
}
} }

View File

@ -24,13 +24,11 @@ public class EventsListener implements Listener {
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent event) { public void onJoin(PlayerJoinEvent event) {
Player p = event.getPlayer();
if(sql.checkConnection()) {
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if(p != null) { Player p = event.getPlayer();
if(sql.checkConnection()) {
if(p.isOnline()) { if(p.isOnline()) {
try { try {
float[] values = sql.getPlayerLevel(p); float[] values = sql.getPlayerLevel(p);
@ -41,19 +39,16 @@ public class EventsListener implements Listener {
p.sendMessage("[LevelSync] " + plugin.msg_syncError); p.sendMessage("[LevelSync] " + plugin.msg_syncError);
} }
} }
}else {
p.sendMessage("[LevelSync] " + plugin.msg_syncError);
} }
} }
}, 20L); }, 20L);
}else {
p.sendMessage("[LevelSync] " + plugin.msg_syncError);
}
} }
@EventHandler @EventHandler
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
Player p = event.getPlayer(); Player p = event.getPlayer();
@ -67,9 +62,7 @@ public class EventsListener implements Listener {
}else { }else {
p.sendMessage("[LevelSync] " + plugin.msg_dbError); p.sendMessage("[LevelSync] " + plugin.msg_dbError);
} }
}
}
}); });
} }
} }

View File

@ -0,0 +1,35 @@
package fr.topeka.levelSync.tasks;
import java.sql.SQLException;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import fr.topeka.levelSync.Main;
public class SaveDataTask extends BukkitRunnable {
private Main main;
public SaveDataTask(Main main) {
this.main = main;
}
@Override
public void run() {
int n = 0;
try {
main.getLogger().info("[LevelSync] Starting saving data task");
for(Player p : main.getServer().getOnlinePlayers()) {
main.getSql().setPlayerLevel(p);
n++;
}
main.getLogger().info("[LevelSync] saving data task done for " + n + " players");
}catch(SQLException e) {
main.getLogger().warning("[LevelSync] " + main.msg_syncError);
e.printStackTrace();
}
}
}