package de.epiceric.shopchest.utils;

import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.event.ShopUpdateEvent;
import org.bukkit.Bukkit;

public class ShopUpdater extends Thread {

    private boolean running;
    private long maxDelta;
    private long lastTime;

    public ShopUpdater(ShopChest plugin) {
        setMaxDelta(plugin.getShopChestConfig().update_quality.getTime());
    }

    public synchronized void setMaxDelta(long maxDelta) {
        this.maxDelta = maxDelta * 50;
    }

    @Override
    public synchronized void start() {
        super.start();
        running = true;
        lastTime = System.currentTimeMillis();
    }

    public synchronized void cancel() {
        running = false;
        super.interrupt();
    }

    @Override
    public void run() {
        while(running) {
            long timeNow = System.currentTimeMillis();
            long timeElapsed = timeNow - lastTime;

            if (timeElapsed >= maxDelta) {
                Bukkit.getPluginManager().callEvent(new ShopUpdateEvent());
                lastTime = timeNow;
            }
        }
    }
}

@@ -148,16 +148,60 @@
 .Bukkit;
+%0Aimport org.bukkit.scheduler.BukkitRunnable;
 %0A%0Apublic
@@ -237,16 +237,47 @@
 read %7B%0A%0A
+    private ShopChest plugin;%0A%0A
     priv
@@ -348,24 +348,24 @@
  lastTime;%0A%0A
-
     public S
@@ -391,24 +391,54 @@
 t plugin) %7B%0A
+        this.plugin = plugin;%0A
         setM
@@ -1097,65 +1097,239 @@
     
-Bukkit.getPluginManager().callEvent(new ShopUpdateEvent()
+new BukkitRunnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        Bukkit.getPluginManager().callEvent(new ShopUpdateEvent());%0A                    %7D%0A                %7D.runTask(plugin
 );%0A 

Call ShopUpdateEvent synchronously