Skip to content

Instantly share code, notes, and snippets.

@crast
Created February 24, 2013 19:15
Show Gist options
  • Save crast/5025100 to your computer and use it in GitHub Desktop.
Save crast/5025100 to your computer and use it in GitHub Desktop.
package us.crast.mondochest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import org.bukkit.util.BlockVector;
import us.crast.utils.CollectionUtil;
public class CheckerTask implements Runnable {
private static final int DEADLINE_MS = 50;
private MondoChest plugin;
private ArrayList<TaskEntry> tasks = new ArrayList<TaskEntry>();
private int currentIndex;
public CheckerTask(MondoChest plugin) {
this.plugin = plugin;
fillTasks();
}
private void fillTasks() {
for (Map<BlockVector, BankSet> worldBanks : plugin.getBankManager().getBanks().values()) {
for (Map.Entry<BlockVector, BankSet> entry: worldBanks.entrySet()) {
BankSet bank = entry.getValue();
for (ChestManager cm : bank.allChestManagers()) {
tasks.add(new TaskEntry(bank, cm));
}
}
}
// Sort the tasks by world and block location to keep things in similar chunks together
Collections.sort(tasks);
currentIndex = tasks.size();
}
@Override
public void run() {
long deadline = System.currentTimeMillis() + DEADLINE_MS;
while (--currentIndex >= 0) {
TaskEntry task = tasks.get(currentIndex);
// Going to re-check stuff here
if (System.currentTimeMillis() > deadline) {
// re-schedule me for later
break;
}
}
}
}
class TaskEntry implements Comparable<TaskEntry> {
public BankSet bank;
public ChestManager chest;
TaskEntry(BankSet bank, ChestManager chest) {
this.bank = bank;
this.chest = chest;
}
public Integer getChunkX() {
return chest.getChest1().getBlockX() / 16;
}
public Integer getChunkZ() {
return chest.getChest1().getBlockZ() / 16;
}
@Override
public int compareTo(TaskEntry other) {
if (other == null) {
return 1;
}
int result = bank.getWorld().compareTo(other.bank.getWorld());
if (result != 0) return result;
result = getChunkX().compareTo(other.getChunkX());
if (result != 0) return result;
result = getChunkZ().compareTo(other.getChunkZ());
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment