From 3459a8ccba85dedf79ce5e5ea90760af82fef2d6 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 8 Feb 2017 21:49:23 +0100 Subject: [PATCH] Stop shop updater when server is empty and restart on join --- src/main/java/de/epiceric/shopchest/ShopChest.java | 7 +++++++ .../epiceric/shopchest/listeners/ShopUpdateListener.java | 6 ++++++ .../java/de/epiceric/shopchest/utils/ShopUpdater.java | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index dcc49f6..f2d143f 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -402,6 +402,13 @@ public class ShopChest extends JavaPlugin { return updater; } + /** + * Set the {@link ShopUpdater} that schedules hologram and item updates + */ + public void setUpdater(ShopUpdater updater) { + this.updater = updater; + } + /** * @return Whether the plugin 'Towny' is enabled */ diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index 84f5d88..d751952 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -3,6 +3,7 @@ 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.ShopUpdater; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -27,6 +28,11 @@ public class ShopUpdateListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent e) { + if (!plugin.getUpdater().isRunning()) { + plugin.setUpdater(new ShopUpdater(plugin)); + plugin.getUpdater().start(); + } + for (Shop shop : plugin.getShopUtils().getShops()) { if (shop.getHologram() != null) shop.getHologram().hidePlayer(e.getPlayer()); if (shop.getItem() != null) shop.getItem().setVisible(e.getPlayer(), false); diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java index a934fe8..33423fe 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java @@ -34,9 +34,17 @@ public class ShopUpdater extends Thread { super.interrupt(); } + public boolean isRunning() { + return running; + } + @Override public void run() { while(running) { + if (Bukkit.getOnlinePlayers().isEmpty()) { + cancel(); + } + long timeNow = System.currentTimeMillis(); long timeElapsed = timeNow - lastTime;