Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active February 24, 2019 15:09
Show Gist options
  • Save Cryptite/e94b268a4610d6d3e9a030af362e86af to your computer and use it in GitHub Desktop.
Save Cryptite/e94b268a4610d6d3e9a030af362e86af to your computer and use it in GitHub Desktop.
Basic Queue which spreads out block changes over a series of ticks.
public class BlockQueue implements Runnable {
private final TheArtifact plugin;
private PriorityBlockingQueue<StringBlock> queue;
public BlockQueue(TheArtifact plugin) {
this.plugin = plugin;
queue = new PriorityBlockingQueue<>(20000, Comparator.comparing(o1 -> o1.priority));
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 0, 2);
}
public void queueBlock(StringBlock block) {
queue.add(block);
}
public synchronized void queueBlocks(Collection<StringBlock> blocks) {
queue.addAll(blocks);
}
@Override
public void run() {
int changed = 0;
long now = System.nanoTime();
boolean playersOnline = !Bukkit.getOnlinePlayers().isEmpty();
while (!queue.isEmpty()) {
StringBlock current = queue.poll();
current.set();
changed++;
//If this operation took more than 5ms, break so we don't lock the main thread for very long at a time.
if (getElapsedFromNanos(now) >= (playersOnline ? 5 : 100)) break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment