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

The following examples show how to use org.bukkit.block.Block#isPassable() . 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: BlockUtils.java    From AACAdditionPro with GNU General Public License v3.0 4 votes vote down vote up
/**
 * This determines if an {@link org.bukkit.inventory.InventoryView} can be opened by interacting with the {@link Block}.
 */
public static boolean isInventoryOpenable(@NotNull final Block block)
{
    // The block actually is holding an inventory (and therefore is not e.g. dirt)
    if (block.getState() instanceof InventoryHolder) {
        // Additional checks for cats and occluding blocks necessary?
        if (FREE_SPACE_CONTAINERS.contains(block.getType())) {
            final Block aboveBlock = block.getRelative(BlockFace.UP);

            switch (ServerVersion.getActiveServerVersion()) {
                case MC188:
                case MC112:
                    // 1.8.8 and 1.12 doesn't provide isPassable.
                    // Make sure that the block above is not obstructed by blocks
                    return FREE_SPACE_CONTAINERS_ALLOWED_MATERIALS.contains(aboveBlock.getType());
                // Cannot check for cats on 1.8 and 1.12 as the server version doesn't provide the newer methods.
                case MC113:
                    // Make sure that the block above is not obstructed by blocks
                    if (!(FREE_SPACE_CONTAINERS_ALLOWED_MATERIALS.contains(aboveBlock.getType()) ||
                          aboveBlock.isPassable()
                    ))
                    {
                        return false;
                    }

                    // Make sure that the block above is not obstructed by cats
                    return aboveBlock.getWorld().getNearbyEntities(aboveBlock.getLocation(), 0.5, 1, 0.5, entity -> entity.getType() == EntityType.OCELOT).isEmpty();
                case MC114:
                case MC115:
                    // Make sure that the block above is not obstructed by blocks
                    if (!(FREE_SPACE_CONTAINERS_ALLOWED_MATERIALS.contains(aboveBlock.getType()) ||
                          aboveBlock.isPassable()
                    ))
                    {
                        return false;
                    }

                    // Make sure that the block above is not obstructed by cats
                    return aboveBlock.getWorld().getNearbyEntities(aboveBlock.getLocation(), 0.5, 1, 0.5, entity -> entity.getType() == EntityType.CAT).isEmpty();
                default:
                    throw new UnknownMinecraftVersion();
            }
        }
        return true;
    }
    return false;
}
 
Example 2
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 3
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);
}