Skip to content

Instantly share code, notes, and snippets.

@aikar
Created November 28, 2015 06:02
Show Gist options
  • Save aikar/19aca25d8dc63391c4bc to your computer and use it in GitHub Desktop.
Save aikar/19aca25d8dc63391c4bc to your computer and use it in GitHub Desktop.
public static class RandomChestSpawnListener extends BukkitUtil.Listener {
private final Consumer<Location> chestLoc;
private final String prefix;
private final long delay;
private final int randChance;
public RandomChestSpawnListener(Consumer<Location> chestLoc, String prefix, long delay, int randChance) {
this.chestLoc = chestLoc;
this.prefix = prefix;
this.delay = delay;
this.randChance = randChance;
}
long lastSpawn = System.currentTimeMillis();
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
long now = System.currentTimeMillis();
Chunk chunk = event.getChunk();
if (EmpireServer.getServer() == EmpireServer.STAGE || Util.isTownWorld(chunk.getWorld()) || chunk.getWorld().getEnvironment() != World.Environment.NORMAL) {
return;
}
if (now - lastSpawn > delay && NumUtil.rand(1, randChance) == 1) {
final int chunkX = chunk.getX();
final int chunkZ = chunk.getZ();
int x = NumUtil.rand(chunkX << 4, ((chunkX + 1) << 4) - 1);
int z = NumUtil.rand(chunkZ << 4, ((chunkZ + 1) << 4) - 1);
final Block block = chunk.getWorld().getHighestBlockAt(x, z).getRelative(BlockFace.UP);
if (block.getType() == Material.AIR && Residence.getResidence(block) == null) {
block.setType(Material.CHEST);
final Location loc = block.getLocation();
chestLoc.accept(loc);
lastSpawn = now;
for (Player player : chunk.getWorld().getPlayers()) {
if (Util.isWithinDistance(loc, player.getLocation(), 16*13)) {
Util.sendMsg(player, prefix
+ NumUtil.rand((chunkX << 4) - 8, (chunkX << 4) + 24)
+ ","
+ NumUtil.rand((chunkZ << 4) - 8, (chunkZ << 4) + 24)
);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment