/** * Gets the block at the represented location * * @return Block at the represented location */ public Block getBlock() { return world.getBlockAt(this); }
@Override public Location getFixedSpawnLocation(World world, Random random) { while (true) { int x = random.nextInt(128) - 64; int y = 128 * 3 / 4; int z = random.nextInt(128) - 64; if (world.getBlockAt(x, y, z).isEmpty()) { while (world.getBlockAt(x, y - 1, z).isEmpty() && y > 0) { y--; } return new Location(world, x, y, z); } } }
/** * Sets a block type and add it to the BlockState list. * * @param world the world which contains the block * @param x the x-coordinate of this block * @param y the y-coordinate of this block * @param z the z-coordinate of this block * @param type the new type of this block */ public void setType(World world, int x, int y, int z, Material type) { GlowBlockState state = (GlowBlockState) world.getBlockAt(x, y, z).getState(); state.setType(type); blockStateMap.put(world.getBlockAt(x, y, z).getLocation(), state); }
@Override public void populate(World world, Random random, Chunk chunk) { int sourceX = chunk.getX() << 4; int sourceZ = chunk.getZ() << 4; for (int i = 0; i < random.nextInt(6) + 3; i++) { int x = sourceX + random.nextInt(16); int z = sourceZ + random.nextInt(16); int y = random.nextInt(28) + 4; if (world.getBlockAt(x, y, z).getType() == Material.STONE) { world.getBlockAt(x, y, z).setType(Material.EMERALD_ORE); } } } }
/** * Sets a block type, data and add it to the BlockState list. * * @param world the world which contains the block * @param x the x-coordinate of this block * @param y the y-coordinate of this block * @param z the z-coordinate of this block * @param type the new type of this block * @param data the new data value of this block */ public void setTypeAndRawData(World world, int x, int y, int z, Material type, int data) { GlowBlockState state = (GlowBlockState) world.getBlockAt(x, y, z).getState(); state.setType(type); state.setRawData((byte) data); blockStateMap.put(world.getBlockAt(x, y, z).getLocation(), state); }
/** * Sets a block type and MaterialData, and add it to the BlockState list. * * @param world the world which contains the block * @param x the x-coordinate of this block * @param y the y-coordinate of this block * @param z the z-coordinate of this block * @param type the new type of this block * @param data the new MaterialData of this block */ public void setTypeAndData(World world, int x, int y, int z, Material type, MaterialData data) { GlowBlockState state = (GlowBlockState) world.getBlockAt(x, y, z).getState(); state.setType(type); state.setData(data); blockStateMap.put(world.getBlockAt(x, y, z).getLocation(), state); }
/** * Returns the {@link BlockState} of a block at the given coordinates. * * @param world the world which contains the block * @param x the x-coordinate * @param y the y-coordinate * @param z the z-coordinate * @return The {@link BlockState} state. */ public BlockState getBlockState(World world, int x, int y, int z) { Location loc = world.getBlockAt(x, y, z).getLocation(); if (blockStateMap.containsKey(loc)) { return blockStateMap.get(loc); } else { return loc.getBlock().getState(); } }
@Override public void populate(World world, Random random, Chunk source) { int sourceX = (source.getX() << 4) + random.nextInt(16); int sourceZ = (source.getZ() << 4) + random.nextInt(16); int sourceY = random.nextInt(world.getSeaLevel() << 1); for (int i = 0; i < 64; i++) { int x = sourceX + random.nextInt(8) - random.nextInt(8); int z = sourceZ + random.nextInt(8) - random.nextInt(8); int y = sourceY + random.nextInt(4) - random.nextInt(4); if (world.getBlockAt(x, y, z).getType() == Material.AIR && world.getBlockAt(x, y - 1, z).getType() == Material.GRASS) { BlockState state = world.getBlockAt(x, y, z).getState(); state.setType(Material.MELON_BLOCK); state.setData(new MaterialData(Material.MELON_BLOCK)); state.update(true); } } } }
@Override public void decorate(World world, Random random, Chunk source) { int sourceX = (source.getX() << 4) + random.nextInt(16); int sourceZ = (source.getZ() << 4) + random.nextInt(16); int sourceY = random.nextInt(128); for (int i = 0; i < 64; i++) { int x = sourceX + random.nextInt(8) - random.nextInt(8); int z = sourceZ + random.nextInt(8) - random.nextInt(8); int y = sourceY + random.nextInt(4) - random.nextInt(4); Block block = world.getBlockAt(x, y, z); Block blockBelow = world.getBlockAt(x, y - 1, z); if (y < 128 && block.getType() == Material.AIR && Arrays.asList(MATERIALS) .contains(blockBelow.getType())) { BlockState state = block.getState(); state.setType(type); state.setData(new MaterialData(type)); state.update(true); } } } }
@Override public void decorate(World world, Random random, Chunk source) { int sourceX = (source.getX() << 4) + random.nextInt(16); int sourceZ = (source.getZ() << 4) + random.nextInt(16); int sourceY = world.getHighestBlockYAt(sourceX, sourceZ) - 1; while ( world.getBlockAt(sourceX, sourceY - 1, sourceZ).getType() == Material.STATIONARY_WATER || world.getBlockAt(sourceX, sourceY - 1, sourceZ).getType() == Material.WATER && sourceY > 1) { sourceY--; } Material material = world.getBlockAt(sourceX, sourceY, sourceZ).getType(); if (material == Material.STATIONARY_WATER || material == Material.WATER) { new BlockPatch(type, horizRadius, vertRadius, overridables) .generate(world, random, sourceX, sourceY, sourceZ); } } }
/** * Returns whether any of {@link #canHeightFit(int)}, {@link #canPlace(int, int, int, World)} or * {@link #canPlaceOn(BlockState)} prevent this tree from generating. * * @param baseX the X coordinate of the base of the trunk * @param baseY the Y coordinate of the base of the trunk * @param baseZ the Z coordinate of the base of the trunk * @param world the world to grow in * @return true if any of the checks prevent us from generating, false otherwise */ protected boolean cannotGenerateAt(int baseX, int baseY, int baseZ, World world) { return !canHeightFit(baseY) || !canPlaceOn(world.getBlockAt(baseX, baseY - 1, baseZ).getState()) || !canPlace(baseX, baseY, baseZ, world); }
@Override public int getBlockLightLevel(BlockVector3 pt) { return getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).getLightLevel(); }
@Override public void simulateBlockMine(BlockVector3 pt) { getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).breakNaturally(); }
@Override public com.sk89q.worldedit.world.block.BlockState getBlock(BlockVector3 position) { Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); return BukkitAdapter.adapt(bukkitBlock.getBlockData()); }
@Override public void populateOnGround(World world, Random random, Chunk chunk) { int sourceX = chunk.getX() << 4; int sourceZ = chunk.getZ() << 4; for (int i = 0; i < 7; i++) { int x = sourceX + random.nextInt(16); int z = sourceZ + random.nextInt(16); int y = world.getHighestBlockYAt(x, z); Block sourceBlock = world.getBlockAt(x, y, z); BlockStateDelegate delegate = new BlockStateDelegate(); JungleBush bush = new JungleBush(random, delegate); if (bush.generate(sourceBlock.getLocation())) { delegate.updateBlockStates(); } } super.populateOnGround(world, random, chunk); melonDecorator.populate(world, random, chunk); } }
/** * Tests if the specified location is valid for a natural spawn position * * @param world The world we're testing on * @param x X-coordinate of the block to test * @param z Z-coordinate of the block to test * @return true if the location is valid, otherwise false */ public boolean canSpawn(World world, int x, int z) { Block highest = world.getBlockAt(x, world.getHighestBlockYAt(x, z), z); switch (world.getEnvironment()) { case NETHER: return true; case THE_END: return highest.getType() != Material.AIR && highest.getType() != Material.WATER && highest.getType() != Material.LAVA; case NORMAL: default: return highest.getType() == Material.SAND || highest.getType() == Material.GRAVEL; } }
@Override public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight) throws WorldEditException { BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); if (adapter != null) { try { return adapter.setBlock(BukkitAdapter.adapt(getWorld(), position), block, notifyAndLight); } catch (Exception e) { if (block instanceof BaseBlock && ((BaseBlock) block).getNbtData() != null) { logger.warning("Tried to set a corrupt tile entity at " + position.toString()); logger.warning(((BaseBlock) block).getNbtData().toString()); } e.printStackTrace(); Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); bukkitBlock.setBlockData(BukkitAdapter.adapt(block), notifyAndLight); return true; } } else { Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); bukkitBlock.setBlockData(BukkitAdapter.adapt(block), notifyAndLight); return true; } }
@Override public boolean clearContainerBlockContents(BlockVector3 pt) { Block block = getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); if (block == null) { return false; } BlockState state = block.getState(); if (!(state instanceof org.bukkit.inventory.InventoryHolder)) { return false; } org.bukkit.inventory.InventoryHolder chest = (org.bukkit.inventory.InventoryHolder) state; Inventory inven = chest.getInventory(); if (chest instanceof Chest) { inven = getBlockInventory((Chest) chest); } inven.clear(); return true; }
/** * Builds a 1x1 column out of the given block, replacing non-solid blocks starting at a given * location and proceeding downward until a solid block is reached. * * @param pos the highest point to possibly replace, relative to this structure's root * point * @param type the block type to fill * @param data the block data */ public void setBlockDownward(Vector pos, Material type, int data) { Vector vec = translate(pos); if (boundingBox.isVectorInside(vec)) { int y = vec.getBlockY(); while (!world.getBlockAt(vec.getBlockX(), y, vec.getBlockZ()).getType().isSolid() && y > 1) { delegate.setTypeAndRawData(world, vec.getBlockX(), y, vec.getBlockZ(), type, data); y--; } } }
@Override public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) { Location loc = new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ()); if (block == null) { player.sendBlockChange(loc, player.getWorld().getBlockAt(loc).getBlockData()); } else { player.sendBlockChange(loc, BukkitAdapter.adapt(block)); if (block instanceof BaseBlock && ((BaseBlock) block).hasNbtData()) { BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); if (adapter != null) { adapter.sendFakeNBT(player, pos, ((BaseBlock) block).getNbtData()); if (block.getBlockType() == BlockTypes.STRUCTURE_BLOCK) { adapter.sendFakeOP(player); } } } } } }