From a78195037b0d81507aa9a8243fb2107fbc1670ca Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 15 Jun 2017 12:25:42 +0200 Subject: [PATCH] Fixed high CPU usage Closes #70 --- .../de/epiceric/shopchest/config/Config.java | 6 +- .../shopchest/event/ShopUpdateEvent.java | 45 ------------ .../listeners/ShopUpdateListener.java | 9 --- .../epiceric/shopchest/utils/ShopUpdater.java | 73 +++++++++++-------- .../epiceric/shopchest/utils/ShopUtils.java | 14 +--- 5 files changed, 50 insertions(+), 97 deletions(-) delete mode 100644 src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index e1bc2a2..add3172 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -1,10 +1,10 @@ package de.epiceric.shopchest.config; import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.event.ShopUpdateEvent; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.sql.Database; import de.epiceric.shopchest.utils.ItemUtils; +import de.epiceric.shopchest.utils.ShopUpdater; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.inventory.ItemStack; @@ -25,7 +25,7 @@ public class Config { private LanguageConfiguration langConfig; /** The quality of hologram and item updating (performance saving, or better quality) **/ - public ShopUpdateEvent.UpdateQuality update_quality; + public ShopUpdater.UpdateQuality update_quality; /** The item with which a player can click a shop to retrieve information **/ public ItemStack shop_info_item; @@ -338,7 +338,7 @@ public class Config { public void reload(boolean firstLoad, boolean langReload, boolean showMessages) { plugin.reloadConfig(); - update_quality = ShopUpdateEvent.UpdateQuality.valueOf(plugin.getConfig().getString("update-quality")); + update_quality = ShopUpdater.UpdateQuality.valueOf(plugin.getConfig().getString("update-quality")); shop_info_item = ItemUtils.getItemStack(plugin.getConfig().getString("shop-info-item")); wg_allow_create_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.create-shop"); wg_allow_use_admin_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.use-admin-shop"); diff --git a/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java deleted file mode 100644 index 939ce81..0000000 --- a/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.epiceric.shopchest.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** - * Called when the shop updater runs
- * It's not recommended to listen to this event! - */ -public class ShopUpdateEvent extends Event { - - public enum UpdateQuality { - SLOWEST(31L), - SLOWER(24L), - SLOW(17L), - NORMAL(10L), - FAST(7L), - FASTER(4L), - FASTEST(1L); - - private long time; - - UpdateQuality(long time) { - this.time = time; - } - - public long getTime() { - return time; - } - } - - private static final HandlerList handlers = new HandlerList(); - - public ShopUpdateEvent() { - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index 4e0aac5..de32403 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -1,11 +1,9 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.event.ShopUpdateEvent; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.Callback; import de.epiceric.shopchest.utils.ShopUpdater; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -24,13 +22,6 @@ public class ShopUpdateListener implements Listener { this.plugin = plugin; } - @EventHandler - public void onShopUpdate(ShopUpdateEvent e) { - for (Player p : Bukkit.getOnlinePlayers()) { - plugin.getShopUtils().updateShops(p); - } - } - @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent e) { restartShopUpdater(e.getPlayer()); diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java index 33423fe..52ea673 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java @@ -1,37 +1,59 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.event.ShopUpdateEvent; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; -public class ShopUpdater extends Thread { +import java.util.Collection; + +public class ShopUpdater extends BukkitRunnable { + + public enum UpdateQuality { + SLOWEST(31L), + SLOWER(24L), + SLOW(17L), + NORMAL(10L), + FAST(7L), + FASTER(4L), + FASTEST(1L); + + private long interval; + + UpdateQuality(long interval) { + this.interval = interval; + } + + public long getInterval() { + return interval; + } + } private ShopChest plugin; private boolean running; - private long maxDelta; - private long lastTime; + private long interval; public ShopUpdater(ShopChest plugin) { this.plugin = plugin; - setMaxDelta(plugin.getShopChestConfig().update_quality.getTime()); + setInterval(plugin.getShopChestConfig().update_quality.getInterval()); } - public synchronized void setMaxDelta(long maxDelta) { - this.maxDelta = maxDelta * 50; + public synchronized void setInterval(long interval) { + this.interval = interval; + } + + public synchronized void start() { + super.runTaskTimerAsynchronously(plugin, interval, interval); + running = true; } @Override - public synchronized void start() { - super.start(); - running = true; - lastTime = System.currentTimeMillis(); - } - public synchronized void cancel() { - running = false; - super.interrupt(); + if (running) { + running = false; + super.cancel(); + } } public boolean isRunning() { @@ -40,23 +62,14 @@ public class ShopUpdater extends Thread { @Override public void run() { - while(running) { - if (Bukkit.getOnlinePlayers().isEmpty()) { - cancel(); - } + Collection players = Bukkit.getOnlinePlayers(); - long timeNow = System.currentTimeMillis(); - long timeElapsed = timeNow - lastTime; + if (players.isEmpty()) { + cancel(); + } - if (timeElapsed >= maxDelta) { - new BukkitRunnable() { - @Override - public void run() { - Bukkit.getPluginManager().callEvent(new ShopUpdateEvent()); - } - }.runTask(plugin); - lastTime = timeNow; - } + for (Player p : players) { + plugin.getShopUtils().updateShops(p); } } } diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 5f09f80..b3e5e85 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -5,7 +5,6 @@ import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.shop.Shop; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; @@ -216,7 +215,10 @@ public class ShopUtils { if (reloadConfig) { plugin.getShopChestConfig().reload(false, true, showConsoleMessages); plugin.getHologramFormat().reload(); - plugin.getUpdater().setMaxDelta(plugin.getShopChestConfig().update_quality.getTime()); + plugin.getUpdater().cancel(); + + plugin.setUpdater(new ShopUpdater(plugin)); + plugin.getUpdater().start(); } plugin.getShopDatabase().connect(new Callback(plugin) { @@ -362,8 +364,6 @@ public class ShopUtils { * @param player Player to show the update */ public void updateShop(Shop shop, Player player) { - if (!shop.getLocation().getChunk().isLoaded()) return; - double holoDistSqr = Math.pow(plugin.getShopChestConfig().maximal_distance, 2); double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2); @@ -372,12 +372,6 @@ public class ShopUtils { if (distSqr <= holoDistSqr) { if (shop.getHologram() != null) { - Material type = shop.getLocation().getBlock().getType(); - - if (type != Material.CHEST && type != Material.TRAPPED_CHEST) { - plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error); - return; - } if (!shop.getHologram().isVisible(player)) { shop.getHologram().showPlayer(player); }