From ce87947be057c050fcd7fc563bad729fa898ac6a Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 4 Jun 2020 21:25:07 +0200 Subject: [PATCH] Improved performance SaveDataTask is not used yet, cause it isn't already tested --- plugin.yml | 2 +- src/fr/topeka/levelSync/Main.java | 7 ++++ .../levelSync/listener/EventsListener.java | 17 +++------ .../topeka/levelSync/tasks/SaveDataTask.java | 35 +++++++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/fr/topeka/levelSync/tasks/SaveDataTask.java diff --git a/plugin.yml b/plugin.yml index 63cf1a4..27fc985 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: levelSync main: fr.topeka.levelSync.Main -version: 1.0-beta +version: 1.1.1 author: Topeka_ description: levelSync - a Minecraft plugin which sync vanilla experience between servers website: https://github.com/sexiestchiba/levelSync diff --git a/src/fr/topeka/levelSync/Main.java b/src/fr/topeka/levelSync/Main.java index 061cc12..f3befb2 100644 --- a/src/fr/topeka/levelSync/Main.java +++ b/src/fr/topeka/levelSync/Main.java @@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin; import fr.topeka.levelSync.listener.EventsListener; 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"); msg_syncError = getConfig().getString("message.syncError"); msg_dbError = getConfig().getString("message.dbError"); +// SaveDataTask task = new SaveDataTask(this); +// task.runTaskTimerAsynchronously(this, 6000, 3600); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); this.getLogger().warning("An error occured during loading, disabling plugin..."); @@ -40,4 +43,8 @@ public class Main extends JavaPlugin{ _sql.closeConnection(); this.getLogger().info("Plugin disabled"); } + + public Sql getSql() { + return _sql; + } } diff --git a/src/fr/topeka/levelSync/listener/EventsListener.java b/src/fr/topeka/levelSync/listener/EventsListener.java index 2d5854a..b1b8787 100644 --- a/src/fr/topeka/levelSync/listener/EventsListener.java +++ b/src/fr/topeka/levelSync/listener/EventsListener.java @@ -24,13 +24,11 @@ public class EventsListener implements Listener { @EventHandler public void onJoin(PlayerJoinEvent event) { - Player p = event.getPlayer(); - if(sql.checkConnection()) { Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { - @Override public void run() { - if(p != null) { + Player p = event.getPlayer(); + if(sql.checkConnection()) { if(p.isOnline()) { try { float[] values = sql.getPlayerLevel(p); @@ -41,19 +39,16 @@ public class EventsListener implements Listener { p.sendMessage("[LevelSync] " + plugin.msg_syncError); } } + }else { + p.sendMessage("[LevelSync] " + plugin.msg_syncError); } - } }, 20L); - }else { - p.sendMessage("[LevelSync] " + plugin.msg_syncError); - } } @EventHandler public void onQuit(PlayerQuitEvent event) { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override public void run() { Player p = event.getPlayer(); @@ -67,9 +62,7 @@ public class EventsListener implements Listener { }else { p.sendMessage("[LevelSync] " + plugin.msg_dbError); } - - } - + } }); } } diff --git a/src/fr/topeka/levelSync/tasks/SaveDataTask.java b/src/fr/topeka/levelSync/tasks/SaveDataTask.java new file mode 100644 index 0000000..9f3e043 --- /dev/null +++ b/src/fr/topeka/levelSync/tasks/SaveDataTask.java @@ -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(); + } + + } + +}