package dk.thoerup.bukkit.hoeruputils; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.world.ChunkUnloadEvent; public class StickyChunk implements Listener, CommandExecutor { public class ChunkBean { public ChunkBean(Chunk c) { this.world = c.getWorld().getName(); this.x = c.getX(); this.z = c.getZ(); this.x_min = this.x - 1; this.x_max = this.x + 1; this.z_min = this.z - 1; this.z_max = this.z + 1; } public String world; public int x; public int z; public int x_min; public int x_max; public int z_min; public int z_max; } List chunks = Collections.synchronizedList( new ArrayList() ); @Override public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { if ( ! (sender instanceof Player) ) { sender.sendMessage("stickychunk is not a console command"); return false; } Player p = (Player) sender; if (p.isOp()==false && p.hasPermission("hoeruputils.chunks")==false) { sender.sendMessage("you don't have permission to do this"); return false; } Location l = p.getLocation(); ChunkBean chunk = new ChunkBean( l.getWorld().getChunkAt( l ) ); chunks.add(chunk); sender.sendMessage("chunk registeret ok"); return true; } @EventHandler public void chunkUnload(ChunkUnloadEvent event) { synchronized(chunks) { for (ChunkBean chunk : chunks) { Chunk c = event.getChunk(); if (c.getWorld().getName().equals( chunk.world )) { int x = c.getX(); int z = c.getZ(); if (x >= chunk.x_min && x <= chunk.x_max && z>=chunk.z_min && z<=chunk.z_max) { //System.out.println("Cancelling unload " + x + " " + z); event.setCancelled(true); return; // } } } } } }