Java Code Examples for org.bukkit.block.Block#isEmpty()

The following examples show how to use org.bukkit.block.Block#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlantsListener.java    From ExoticGarden with GNU General Public License v3.0 6 votes vote down vote up
private void dropFruitFromTree(Block block) {
    for (int x = -1; x < 2; x++) {
        for (int y = -1; y < 2; y++) {
            for (int z = -1; z < 2; z++) {
                // inspect a cube at the reference
                Block fruit = block.getRelative(x, y, z);
                if (fruit.isEmpty()) continue;

                Location loc = fruit.getLocation();
                SlimefunItem check = BlockStorage.check(loc);
                if (check == null) continue;

                for (Tree tree : ExoticGarden.getTrees()) {
                    if (check.getID().equalsIgnoreCase(tree.getFruitID())) {
                        BlockStorage.clearBlockInfo(loc);
                        ItemStack fruits = check.getItem();
                        fruit.getWorld().playEffect(loc, Effect.STEP_SOUND, Material.OAK_LEAVES);
                        fruit.getWorld().dropItemNaturally(loc, fruits);
                        fruit.setType(Material.AIR);
                        break;
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: HeightValidator.java    From RandomTeleport with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean validate(RandomSearcher searcher, Location location) {
    Block block = location.getWorld().getHighestBlockAt(location);
    if (block.getY() > searcher.getMaxY()) {
        block = location.getWorld().getBlockAt(
                block.getX(),
                searcher.getMinY() + searcher.getRandom().nextInt(searcher.getMaxY() - searcher.getMinY()),
                block.getZ()
        );
    }
    while (block.isEmpty()) {
        block = block.getRelative(BlockFace.DOWN);
        if (block == null || block.getY() < searcher.getMinY()) {
            return false;
        }
    }
    location.setY(block.getY());
    return !block.getRelative(BlockFace.UP).getType().isSolid() && !block.getRelative(BlockFace.UP, 2).getType().isSolid();
}
 
Example 3
Source File: TorchBow.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onProjectileHit(ProjectileHitEvent event, DelayedPlayerDetails details) {
	event.getEntity().remove();
	Block b = event.getEntity().getLocation().getBlock();
	if (b.isEmpty()) {
		b.setType(Material.TORCH);
	}
}
 
Example 4
Source File: BlockUtil.java    From GriefDefender with MIT License 5 votes vote down vote up
public Optional<Location> getTargetBlock(Player player, GDPlayerData playerData, int maxDistance, boolean ignoreAir) throws IllegalStateException {
    BlockRay blockRay = BlockRay.from(player).distanceLimit(maxDistance).build();
    boolean waterIsTransparent = false;
    if (player != null && NMSUtil.getInstance().isBlockWater(player.getLocation().getBlock())) {
        waterIsTransparent = true;
    }

    while (blockRay.hasNext()) {
        BlockRayHit blockRayHit = blockRay.next();
        final Block block = blockRayHit.getLocation().getBlock();
        boolean blockTransparent = NMSUtil.getInstance().isBlockTransparent(block);
        final boolean isWaterBlock = NMSUtil.getInstance().isBlockWater(block);
        if (waterIsTransparent && isWaterBlock) {
            continue;
        } else if (isWaterBlock) {
            blockTransparent = false;
        }

        if (ignoreAir) {
            if (!blockTransparent) {
                return Optional.of(blockRayHit.getLocation());
            }
        } else {
            if (!block.isEmpty() && !blockTransparent) {
                return Optional.of(blockRayHit.getLocation());
            }
        }
    }

    return Optional.empty();
}
 
Example 5
Source File: RegionPointProvider.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Indicates whether or not this spawn is safe.
 *
 * @param location Location to check for.
 * @return True or false depending on whether this is a safe spawn point.
 */
private boolean isSafe(Location location) {
  if (!WorldBorders.isInsideBorder(location)) return false;

  Block block = location.getBlock();
  Block above = block.getRelative(BlockFace.UP);
  Block below = block.getRelative(BlockFace.DOWN);

  return block.isEmpty() && above.isEmpty() && BlockVectors.isSupportive(below.getType());
}
 
Example 6
Source File: RegionPointProvider.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Indicates whether or not this spawn is safe.
 *
 * @param location Location to check for.
 * @return True or false depending on whether this is a safe spawn point.
 */
private boolean isSafe(Location location) {
    if(!WorldBorderUtils.isInsideBorder(location)) return false;

    Block block = location.getBlock();
    Block above = block.getRelative(BlockFace.UP);
    Block below = block.getRelative(BlockFace.DOWN);

    return block.isEmpty() && above.isEmpty() && Materials.isColliding(below.getType());
}
 
Example 7
Source File: BlockUtils.java    From AACAdditionPro with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This gets the {@link Block}s around the given block if they are not air/empty.
 *
 * @param block          the block that faces should be checked for other {@link Block}s
 * @param onlyHorizontal whether only the {@link Block}s should be counted that are horizontal around the block or all {@link Block}s (horizontal + vertical)
 *
 * @return a {@link List} of all {@link Block}s which were found.
 */
public static List<Block> getBlocksAround(final Block block, final boolean onlyHorizontal)
{
    final List<Block> blocks = new ArrayList<>(onlyHorizontal ? 4 : 6);
    for (final BlockFace face : onlyHorizontal ? HORIZONTAL_FACES : ALL_FACES) {
        final Block relative = block.getRelative(face);
        if (!relative.isEmpty()) {
            blocks.add(relative);
        }
    }
    return blocks;
}
 
Example 8
Source File: BlockListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onFlow(BlockFromToEvent e) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is BlockFromToEvent event");

    Block bto = e.getToBlock();
    Block bfrom = e.getBlock();
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is BlockFromToEvent event is to " + bto.getType().name() + " from " + bfrom.getType().name());
    Region rto = RedProtect.get().rm.getTopRegion(bto.getLocation());
    Region rfrom = RedProtect.get().rm.getTopRegion(bfrom.getLocation());
    boolean isLiquid = bfrom.isLiquid() || bfrom.getType().name().contains("BUBBLE_COLUMN") || bfrom.getType().name().contains("KELP");
    if (rto != null && isLiquid && !rto.canFlow()) {
        e.setCancelled(true);
        return;
    }
    if (rfrom != null && isLiquid && !rfrom.canFlow()) {
        e.setCancelled(true);
        return;
    }
    if (rto != null && !bto.isEmpty() && !rto.canFlowDamage()) {
        e.setCancelled(true);
        return;
    }

    //deny blocks spread in/out regions
    if (rfrom != null && rto != null && rfrom != rto && !rfrom.sameLeaders(rto)) {
        e.setCancelled(true);
        return;
    }
    if (rfrom == null && rto != null) {
        e.setCancelled(true);
    }
}
 
Example 9
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onFlow(BlockFromToEvent e) {
    RedProtect.get().logger.debug(LogLevel.DEFAULT, "GlobalListener - Is BlockFromToEvent event");

    Block b = e.getToBlock();
    Block bfrom = e.getBlock();
    RedProtect.get().logger.debug(LogLevel.DEFAULT, "GlobalListener - Is BlockFromToEvent event is to " + b.getType().name() + " from " + bfrom.getType().name());
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation());
    if (r != null) {
        return;
    }
    if (bfrom.isLiquid() && !RedProtect.get().config.globalFlagsRoot().worlds.get(b.getWorld().getName()).allow_changes_of.liquid_flow) {
        e.setCancelled(true);
        return;
    }

    if ((bfrom.getType().equals(Material.WATER) || (bfrom.getType().name().contains("WATER") && (bfrom.getType().name().contains("STATIONARY") || bfrom.getType().name().contains("FLOWING"))))
            && !RedProtect.get().config.globalFlagsRoot().worlds.get(b.getWorld().getName()).allow_changes_of.water_flow) {
        e.setCancelled(true);
        return;
    }

    if ((bfrom.getType().equals(Material.LAVA) || (bfrom.getType().name().contains("LAVA") && (bfrom.getType().name().contains("STATIONARY") || bfrom.getType().name().contains("FLOWING"))))
            && !RedProtect.get().config.globalFlagsRoot().worlds.get(b.getWorld().getName()).allow_changes_of.lava_flow) {
        e.setCancelled(true);
        return;
    }

    if (!b.isEmpty() && !RedProtect.get().config.globalFlagsRoot().worlds.get(b.getWorld().getName()).allow_changes_of.flow_damage) {
        e.setCancelled(true);
    }
}
 
Example 10
Source File: CommandHandler.java    From NyaaUtils with MIT License 4 votes vote down vote up
@SubCommand(value = "tpall", permission = "nu.tpall")
public void tpall(CommandSender sender, Arguments args) {
    Player p = asPlayer(sender);
    int r = args.nextInt();
    Block center = p.getLocation().getBlock();
    int minX = center.getX() - r;
    int minZ = center.getZ() - r;
    int maxX = center.getX() + r;
    int maxZ = center.getZ() + r;
    int maxY = center.getY() + 1;
    List<Location> locations = new ArrayList<>();
    int playerCount = Bukkit.getOnlinePlayers().size() - 1;
    for (int x = minX; x <= maxX; x++) {
        for (int z = minZ; z <= maxZ; z++) {
            for (int i = 0; i <= 16; i++) {
                Block b = p.getWorld().getBlockAt(x, maxY - i, z);
                if (b.getType().isSolid()) {
                    Block b2 = b.getRelative(BlockFace.UP, 1);
                    Block b3 = b.getRelative(BlockFace.UP, 2);
                    if ((b2.isEmpty() || b2.isPassable()) && (b3.isEmpty() || b3.isPassable()) && !b2.isLiquid() && !b3.isLiquid()) {
                        Location loc = b.getBoundingBox().getCenter().toLocation(b.getWorld());
                        loc.setY(b.getBoundingBox().getMaxY());
                        locations.add(loc.clone());
                        break;
                    }
                }
            }
        }
    }
    if (locations.size() < playerCount) {
        msg(sender, "user.tpall.error");
        return;
    }
    Collections.shuffle(locations);
    int success = 0;
    for (Player player : Bukkit.getOnlinePlayers()) {
        if (!player.isDead() && player.getUniqueId() != p.getUniqueId()) {
            if (player.getWorld() == p.getWorld() && player.getLocation().distance(p.getLocation()) + 3 <= r) {
                continue;
            }
            Location old = player.getLocation().clone();
            if (player.teleport(locations.get(success))) {
                success++;
                plugin.ess.getUser(player).setLastLocation(old);
            }
        }
    }
    msg(sender, "user.tpall.success", success);
}
 
Example 11
Source File: FlagObjective.java    From CardinalPGM with MIT License 4 votes vote down vote up
private boolean validatePosition(Block block) {
    return ((block.getRelative(BlockFace.DOWN).getType().isSolid() || (dropOnWater && block.getRelative(BlockFace.DOWN).getType().equals(Material.STATIONARY_WATER))) &&  block.isEmpty() && block.getRelative(BlockFace.UP).isEmpty());
}
 
Example 12
Source File: ActiveMiner.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private void setPistonState(Block block, boolean extended) {
    if (!running) {
        return;
    }

    try {
        // Smoke Particles around the Chest for dramatic effect
        Location particleLoc = chest.getLocation().clone().add(0, -1, 0);
        block.getWorld().spawnParticle(Particle.SMOKE_NORMAL, particleLoc, 20, 0.7, 0.7, 0.7, 0);

        if (block.getType() == Material.MOVING_PISTON) {
            // Yeah it isn't really cool when this happens
            block.getRelative(BlockFace.UP).setType(Material.AIR);
        }
        else if (block.getType() == Material.PISTON) {
            Block above = block.getRelative(BlockFace.UP);

            // Check if the above block is valid
            if (above.isEmpty() || above.getType() == Material.PISTON_HEAD) {
                Piston piston = (Piston) block.getBlockData();

                // Check if the piston is actually facing upwards
                if (piston.getFacing() == BlockFace.UP) {
                    setExtended(block, piston, extended);
                }
                else {
                    // The pistons must be facing upwards
                    stop("machines.INDUSTRIAL_MINER.piston-facing");
                }
            }
            else {
                // The pistons must be facing upwards
                stop("machines.INDUSTRIAL_MINER.piston-space");
            }
        }
        else {
            // The piston has been destroyed
            stop("machines.INDUSTRIAL_MINER.destroyed");
        }
    }
    catch (Exception e) {
        Slimefun.getLogger().log(Level.SEVERE, e, () -> "An Error occurred while moving a Piston for an Industrial Miner at " + new BlockPosition(block));
        stop();
    }
}
 
Example 13
Source File: EconomyManager.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
public static long getRegionValue(Region r) {
    long regionCost = 0;
    World w = RedProtect.get().getServer().getWorld(r.getWorld());
    int maxX = r.getMaxMbrX();
    int minX = r.getMinMbrX();
    int maxZ = r.getMaxMbrZ();
    int minZ = r.getMinMbrZ();
    int factor;
    for (int x = minX; x < maxX; x++) {
        for (int y = 0; y < 256; y++) {
            for (int z = minZ; z < maxZ; z++) {

                Block b = w.getBlockAt(x, y, z);
                if (b.isEmpty()) {
                    continue;
                }

                if (b.getState() instanceof InventoryHolder) {
                    Inventory inv = ((InventoryHolder) b.getState()).getInventory();

                    if (inv.getSize() == 54) {
                        factor = 2;
                    } else {
                        factor = 1;
                    }

                    for (ItemStack item : inv.getContents()) {
                        if (item == null || item.getAmount() == 0) {
                            continue;
                        }
                        regionCost = regionCost + ((RedProtect.get().config.ecoRoot().items.values.getOrDefault(item.getType().name(), 0L) * item.getAmount()) / factor);
                        if (item.getEnchantments().size() > 0) {
                            for (Enchantment enchant : item.getEnchantments().keySet()) {
                                regionCost = regionCost + ((RedProtect.get().config.ecoRoot().enchantments.values.getOrDefault(enchant.getName(), 0L) * item.getEnchantments().get(enchant)) / factor);
                            }
                        }
                    }
                } else {
                    regionCost = regionCost + RedProtect.get().config.ecoRoot().items.values.getOrDefault(b.getType().name(), 0L);
                }
            }
        }
    }
    return regionCost;
}
 
Example 14
Source File: RedProtectUtil.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
private boolean isSecure(Location loc) {
    Block b = loc.add(0, -1, 0).getBlock();
    return (!b.isLiquid() && !b.isEmpty()) || b.getType().name().contains("LAVA");
}
 
Example 15
Source File: EntityEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onEntityChangeBlockEvent(EntityChangeBlockEvent event) {
    if (!GDFlags.BLOCK_BREAK) {
        return;
    }

    final Block block = event.getBlock();
    if (block.isEmpty()) {
        return;
    }
    final World world = event.getBlock().getWorld();
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUID())) {
        return;
    }

    final Location location = block.getLocation();
    final GDClaim targetClaim = this.baseStorage.getClaimAt(location);

    final Entity source = event.getEntity();
    GDPermissionUser user = null;
    if (source instanceof Tameable) {
        final UUID uuid = NMSUtil.getInstance().getTameableOwnerUUID(source);
        if (uuid != null) {
            user = PermissionHolderCache.getInstance().getOrCreateUser(uuid);
        }
    }
    if (user == null && !NMSUtil.getInstance().isEntityMonster(event.getEntity())) {
        final GDEntity gdEntity = EntityTracker.getCachedEntity(event.getEntity().getEntityId());
        if (gdEntity != null) {
            user = PermissionHolderCache.getInstance().getOrCreateUser(gdEntity.getOwnerUUID());
        }
        if (user == null && source instanceof FallingBlock) {
            // always allow blocks to fall if no user found
            return;
        }
    }
    final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, targetClaim, Flags.BLOCK_BREAK, event.getEntity(), event.getBlock(), user, TrustTypes.BUILDER, true);
    if (result == Tristate.FALSE) {
        event.setCancelled(true);
        return;
    }
}
 
Example 16
Source File: SitListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClickBlock(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && !event.hasItem()) {
        Block block = event.getClickedBlock();
        BlockFace face = event.getBlockFace();
        if (face == BlockFace.DOWN || block.isLiquid() || !plugin.cfg.sit_blocks.contains(block.getType())) {
            return;
        }
        Block relative = block.getRelative(0, 1, 0);
        Player player = event.getPlayer();
        if (messageCooldown.getIfPresent(player.getUniqueId()) != null) {
            return;
        }
        messageCooldown.put(player.getUniqueId(), true);
        if (!player.hasPermission("nu.sit") || !enabledPlayers.contains(player.getUniqueId()) || player.isInsideVehicle() || !player.getPassengers().isEmpty() || player.getGameMode() == GameMode.SPECTATOR || !player.isOnGround()) {
            return;
        }
        if (relative.isLiquid() || !(relative.isEmpty() || relative.isPassable())) {
            player.sendMessage(I18n.format("user.sit.invalid_location"));
            return;
        }
        Vector vector = block.getBoundingBox().getCenter().clone();
        Location loc = vector.setY(block.getBoundingBox().getMaxY()).toLocation(player.getWorld()).clone();
        for (SitLocation sl : plugin.cfg.sit_locations.values()) {
            if (sl.blocks != null && sl.x != null && sl.y != null && sl.z != null && sl.blocks.contains(block.getType().name())) {
                loc.add(sl.x, sl.y, sl.z);
            }
        }
        if (block.getBlockData() instanceof Directional) {
            face = ((Directional) block.getBlockData()).getFacing();
            if (face == BlockFace.EAST) {
                loc.setYaw(90);
            } else if (face == BlockFace.WEST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(-180);
            }
        } else {
            if (face == BlockFace.WEST) {
                loc.setYaw(90);
            } else if (face == BlockFace.EAST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(-180);
            } else {
                loc.setYaw(player.getEyeLocation().getYaw());
            }
        }
        for (Entity e : loc.getWorld().getNearbyEntities(loc, 0.5, 0.7, 0.5)) {
            if (e instanceof LivingEntity) {
                if (e.hasMetadata(metadata_key) || (e instanceof Player && e.isInsideVehicle() && e.getVehicle().hasMetadata(metadata_key))) {
                    player.sendMessage(I18n.format("user.sit.invalid_location"));
                    return;
                }
            }
        }
        Location safeLoc = player.getLocation().clone();
        ArmorStand armorStand = loc.getWorld().spawn(loc, ArmorStand.class, (e) -> {
            e.setVisible(false);
            e.setPersistent(false);
            e.setCanPickupItems(false);
            e.setBasePlate(false);
            e.setArms(false);
            e.setMarker(true);
            e.setInvulnerable(true);
            e.setGravity(false);
        });
        if (armorStand != null) {
            armorStand.setMetadata(metadata_key, new FixedMetadataValue(plugin, true));
            if (armorStand.addPassenger(player)) {
                safeLocations.put(player.getUniqueId(), safeLoc);
            } else {
                armorStand.remove();
            }
        }
    }
}
 
Example 17
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
private GDClaim findNearbyClaim(Player player, GDPlayerData playerData, int maxDistance, boolean hidingVisuals) {
    if (maxDistance <= 20) {
        maxDistance = 100;
    }
    BlockRay blockRay = BlockRay.from(player).distanceLimit(maxDistance).build();
    GDClaim playerClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
    GDClaim firstClaim = null;
    GDClaim claim = null;
    playerData.lastNonAirInspectLocation = null;
    playerData.lastValidInspectLocation = null;
    while (blockRay.hasNext()) {
        BlockRayHit blockRayHit = blockRay.next();
        Location location = blockRayHit.getLocation();
        claim = this.dataStore.getClaimAt(location);
        if (firstClaim == null && !claim.isWilderness()) {
            if (hidingVisuals) {
                if (claim.hasActiveVisual(player)) {
                    firstClaim = claim;
                }
            } else {
                firstClaim = claim;
            }
        }

        if (playerData.lastNonAirInspectLocation == null && !location.getBlock().isEmpty()) {
            playerData.lastNonAirInspectLocation = location;
        }
        if (claim != null && !claim.isWilderness() && !playerClaim.getUniqueId().equals(claim.getUniqueId())) {
            playerData.lastValidInspectLocation = location;
        }

        final Block block = location.getBlock();
        if (!block.isEmpty() && !NMSUtil.getInstance().isBlockTransparent(block)) {
            break;
        }
    }

    if (claim == null || claim.isWilderness()) {
        if (firstClaim == null) {
            return GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(player.getWorld().getUID()).getWildernessClaim();
        }
        return firstClaim;
    }

    return claim;
}