From 6be514cdf90512d6221887d734f3291aedb1647d Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Tue, 29 Nov 2011 09:09:32 +0100 Subject: [PATCH] Added LocalWorld.queueBlockBreakEffect. --- .../java/com/sk89q/worldedit/LocalWorld.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/LocalWorld.java b/src/main/java/com/sk89q/worldedit/LocalWorld.java index 27e587d0c..5d4921eae 100644 --- a/src/main/java/com/sk89q/worldedit/LocalWorld.java +++ b/src/main/java/com/sk89q/worldedit/LocalWorld.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit; +import java.util.PriorityQueue; import java.util.Random; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseItemStack; @@ -354,4 +355,49 @@ public abstract class LocalWorld { public boolean playEffect(Vector position, int type, int data) { return false; } + + private class QueuedEffect implements Comparable{ + private final Vector position; + private final int blockId; + private final double priority; + public QueuedEffect(Vector position, int blockId, double priority) { + this.position = position; + this.blockId = blockId; + this.priority = priority; + } + + public void play() { + playEffect(position, 2001, blockId); + } + + @Override + public int compareTo(QueuedEffect other) { + return Double.compare(priority, other.priority); + } + } + + private final PriorityQueue effectQueue = new PriorityQueue(); + private int taskId = -1; + public boolean queueBlockBreakEffect(ServerInterface server, Vector position, int blockId, double priority) { + if (taskId == -1) { + taskId = server.schedule(0, 1, new Runnable() { + public void run() { + int max = Math.max(1, Math.min(30, effectQueue.size() / 3)); + for (int i = 0; i < max; ++i) { + if (effectQueue.isEmpty()) return; + + effectQueue.poll().play(); + } + } + }); + } + + if (taskId == -1) { + return false; + } + + effectQueue.offer(new QueuedEffect(position, blockId, priority)); + + return true; + } }