/** * Find a position for the player to stand that is not inside a block. Blocks above the player * will be iteratively tested until there is a series of two free blocks. The player will be * teleported to that free position. * * @param player */ public static void findFreePosition(Player player) { Location loc = player.getLocation(); int x = loc.getBlockX(); int y = Math.max(0, loc.getBlockY()); int origY = y; int z = loc.getBlockZ(); World world = player.getWorld(); byte free = 0; while (y <= world.getMaxHeight() + 1) { if (BlockType.canPassThrough(world.getBlockTypeIdAt(x, y, z))) { free++; } else { free = 0; } if (free == 2) { if (y - 1 != origY || y == 1) { loc.setX(x + 0.5); loc.setY(y); loc.setZ(z + 0.5); if (y <= 2 && world.getBlockAt(x, 0, z).getTypeId() == BlockID.AIR) { world.getBlockAt(x, 0, z).setTypeId(20); loc.setY(2); } player.setFallDistance(0F); player.teleport(loc); } return; } y++; } }
/** * Find a position for the player to stand that is not inside a block. Blocks above the player * will be iteratively tested until there is a series of two free blocks. The player will be * teleported to that free position. * * @param player */ public void findFreePosition(Player player) { Location loc = player.getLocation(); int x = loc.getBlockX(); int y = Math.max(0, loc.getBlockY()); int origY = y; int z = loc.getBlockZ(); World world = player.getWorld(); byte free = 0; while (y <= 129) { if (BlockType.canPassThrough(world.getBlockTypeIdAt(x, y, z))) { free++; } else { free = 0; } if (free == 2) { if (y - 1 != origY) { loc.setX(x + 0.5); loc.setY(y); loc.setZ(z + 0.5); player.teleportTo(loc); } return; } y++; } }
@EventHandler(ignoreCancelled = true) public void onRightClick(PlayerInteractEvent event) { if (!plugin.getConfiguration().chairEnabled) return; if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; if (event.getClickedBlock() == null || !plugin.getConfiguration().chairBlocks.contains(event.getClickedBlock().getTypeId())) return; BukkitPlayer player = new BukkitPlayer(plugin, event.getPlayer()); // Now everything looks good, continue; if (player.getPlayer().getItemInHand() == null || !player.getPlayer().getItemInHand().getType().isBlock() || player.getPlayer().getItemInHand().getTypeId() == 0) { if (plugin.getConfiguration().chairSneak != player.getPlayer().isSneaking()) return; if (!player.hasPermission("craftbook.mech.chair.use")) { player.printError("mech.use-permission"); return; } if (hasChair(player.getPlayer())) { // Stand removeChair(player.getPlayer()); event.getPlayer().teleport(event.getClickedBlock().getLocation().add(0.5, 1.5, 0.5)); } else { // Sit if (hasChair(event.getClickedBlock())) { player.print("This seat is already occupied."); return; } if (BlockType.canPassThrough(event.getClickedBlock().getRelative(0, -1, 0).getTypeId())) { player.printError("This chair has nothing below it!"); return; } player .getPlayer() .teleport( event.getClickedBlock().getLocation().add(0.5, 0, 0.5)); // Teleport to the seat addChair(player.getPlayer(), event.getClickedBlock()); } } }
private static boolean occupiable(Block block) { return BlockType.canPassThrough(block.getTypeId()); }