Stop shop updater when server is empty and restart on join

This commit is contained in:
Eric 2017-02-08 21:49:23 +01:00
parent 9583b77413
commit 3459a8ccba
3 changed files with 21 additions and 0 deletions

View File

@ -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
*/

View File

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

View File

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