package com.elmakers.mine.bukkit.magic;

import com.elmakers.mine.bukkit.api.block.BlockData;
import com.elmakers.mine.bukkit.block.UndoList;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;

public class PhysicsHandler implements Listener {
	private final MagicController controller;
	private long timeout = 0;
	private long timeoutBuffer = 2000;

	public PhysicsHandler(MagicController controller)
	{
		this.controller = controller;
	}
	
	@EventHandler
	public void onBlockPhysics(BlockPhysicsEvent event)
	{
		if (!allowPhysics(event.getBlock()))
		{
			event.setCancelled(true);
		}
	}
	
	protected boolean allowPhysics(Block block)
	{
		if (timeout == 0) {
			controller.unregisterPhysicsHandler(this);
			return true;
		}
		if (System.currentTimeMillis() > timeout) {
			controller.unregisterPhysicsHandler(this);
			timeout = 0;
			return true;
		}

		BlockData registered = UndoList.getBlockData(block.getLocation());
		if (registered == null) {
			return true;
		}
		com.elmakers.mine.bukkit.api.block.UndoList registeredList = registered.getUndoList();
		if (!registeredList.getApplyPhysics()) {
			timeout += timeoutBuffer;
			return false;
		}

		return true;
	}

	public long getTimeout() {
		return timeout;
	}

    public void setInterval(long interval)
    {
        this.timeout = Math.max(this.timeout, System.currentTimeMillis() + interval);
    }
}

@@ -826,20 +826,27 @@
 ;%0A%09%09%7D%0A%09%09
-if (
+long now = 
 System.c
@@ -863,16 +863,27 @@
 Millis()
+;%0A%09%09if (now
  %3E timeo
@@ -1220,16 +1220,17 @@
 cs()) %7B%0A
+%0A
 %09%09%09timeo
@@ -1236,10 +1236,24 @@
 out 
-+
 =
+ Math.min(now +
  tim
@@ -1262,16 +1262,26 @@
 utBuffer
+, timeout)
 ;%0A%09%09%09ret

Fix Physics handler getting stuck on