public class IronGolemListener implements MobListener { private static final ItemStack ITEM = new ItemBuilder(Material.IRON_NUGGET) .glowing(true) .name("&f&lForbidden Bottle Cap") .lore("&7Was probably used to dig a hole in a prison at some point..") .build(); private static final String PROTECTED_BLOCKS = "BEDROCK END PORTAL"; private static final Vector UP_VELOCITY = new Vector(0, 2, 0); @Override public ItemStack getItem() { return ITEM; } @Override public boolean isType(EntityType type) { return type == EntityType.IRON_GOLEM; } @Override public void onClick(PlayerInteractEvent event) { Location location = event.getPlayer().getLocation(); Set locations = getNearbyLocations(location, 8); Set entities = getNearbyEntities(location, 4, event.getPlayer()); for(Location loc : locations) { Block block = loc.getBlock(); BlockData blockData = block.getBlockData().clone(); block.setType(Material.AIR); block.getWorld().spawnFallingBlock(loc, blockData).setVelocity(UP_VELOCITY); } for(Entity entity : entities) { entity.setVelocity(UP_VELOCITY); } } private Set getNearbyLocations(Location location, int radius) { Set set = new HashSet<>(); Random random = ThreadLocalRandom.current(); for (int x = -radius; x <= radius; x++) { for (int y = -radius; y <= radius; y++) { for (int z = -radius; z <= radius; z++) { Location loc = location.clone().add(x, y, z); if(loc.distanceSquared(location) > radius * radius) continue; Block block = loc.getBlock(); Material type = block.getType(); Block above = block.getRelative(0, 1, 0); if(block.getType().isAir() || !above.getType().isAir()) continue; if (isMaterialProtected(type)) continue; if (random.nextBoolean()) set.add(loc); } } } return set; } private Set getNearbyEntities(Location location, int radius, Player originalPlayer) { Set set = new HashSet<>(); for (Entity entity : location.getNearbyEntities(radius, radius, radius)) { if (entity.getUniqueId().equals(originalPlayer.getUniqueId())) continue; set.add(entity); } return set; } private boolean isMaterialProtected(Material material) { for (String materialName : PROTECTED_BLOCKS.split(" ")) { if (material.name().contains(materialName)) { return true; } } return false; } }