/** * Backups a block state. * * @param block the block which state should be backup */ public void backupBlockState(Block block) { blockStateMap.remove(block.getLocation()); blockStateBackupMap.put(block.getLocation(), new GlowBlockState((GlowBlock) block)); }
/** * Get the Leash Hitch to which entities should be attached at the block. Useful if multiple * Leash Hitches could exist. * * @param block the Block to get the relevant Leash Hitch for * @return either an already existing Leash Hitch, or a newly spawned one */ public static LeashHitch getLeashHitchAt(Block block) { // Use the oldest leash entity as leash holder // If none found, create a new leash hitch Stream<LeashHitch> sorted = GlowLeashHitch.getExistingLeashHitches(block).sorted( comparingInt(Entity::getTicksLived) .reversed() ); Optional<LeashHitch> first = sorted.findFirst(); return first.orElseGet( () -> first.orElse(block.getWorld().spawn(block.getLocation(), LeashHitch.class))); }
/** * Get all LeashHitch Entities in the specified block. * * @param block the Block to search LeashHitch Entities in * @return a Stream of all found LeashHitch Entities */ private static Stream<LeashHitch> getExistingLeashHitches(Block block) { Location location = block.getLocation().add(0.5, 0.5, 0.5); Collection<Entity> nearbyEntities = block.getWorld() .getNearbyEntities(location, 0.49, 0.49, 0.49); return nearbyEntities.stream() .filter(e -> e instanceof LeashHitch) .map(e -> (LeashHitch) e); }
/** * 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(); } }
/** * Gets the location where the possible moving block might be if the * retracting piston is sticky. * * @return The possible location of the possibly moving block. */ public Location getRetractLocation() { return getBlock().getRelative(getDirection(), 2).getLocation(); }
/** * 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); }
/** * Convert a TNT block into a primed TNT entity with the player who ignited the TNT. * * @param tntBlock The block to ignite. * @param ignitedByExplosion True if another explosion caused this ignition. * @param player The player who ignited the TNT. */ public static void igniteBlock( Block tntBlock, boolean ignitedByExplosion, GlowPlayer player) { tntBlock.setType(Material.AIR); World world = tntBlock.getWorld(); GlowTntPrimed tnt = (GlowTntPrimed) world .spawnEntity(tntBlock.getLocation().add(0.5, 0, 0.5), EntityType.PRIMED_TNT); tnt.setSource(player); tnt.setIgnitedByExplosion(ignitedByExplosion); world.playSound(tntBlock.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1); }
/** * 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); }
/** * Gets the location that is "~ ~ ~" for a command sender. * * @param sender a command sender * @return the sender's location if the sender is a block or entity, or the default world's * coordinate origin otherwise. */ public static Location getLocation(CommandSender sender) { if (sender instanceof Entity) { return ((Entity) sender).getLocation(); } else if (sender instanceof BlockCommandSender) { return ((BlockCommandSender) sender).getBlock().getLocation(); } return new Location(getDefaultWorld(), 0, 0, 0); }
/** * Gets the lowest block at the given {@link Location} such that the block * and all blocks above it are either air or one of the given materials. * * @param location Coordinates to get the highest block * @param except Blocks to exclude in addition to air * @return Highest non-empty block */ public Block getHighestBlockAt(Location location, Material... except) { Block block = getHighestBlockAt(location); List<Material> array = Arrays.asList(except); for (int i = 0; i < 6; i++) { block = block.getLocation().clone().subtract(0, i == 0 ? 0 : 1, 0).getBlock(); if (block.getType() == Material.AIR || array.contains(block.getType())) { continue; } return block; } return getHighestBlockAt(location); }
@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); Block sourceBlock = world .getBlockAt(sourceX, world.getHighestBlockYAt(sourceX, sourceZ), sourceZ); BiFunction<Random, BlockStateDelegate, ? extends GenericTree> ctor = getRandomTree(random, trees); if (ctor != null) { BlockStateDelegate delegate = new BlockStateDelegate(); GenericTree tree; try { tree = ctor.apply(random, delegate); } catch (Exception ex) { tree = new GenericTree(random, delegate); } if (tree.generate(sourceBlock.getLocation())) { delegate.updateBlockStates(); } } }
private void increaseTimeLived() { // "The window for reeling in when a fish bites is about half a second. // If a bite is missed, the line can be left in the water to wait for another bite." // TODO: Option to give high-latency players more time? Not much abuse potential! if (lived - lifeTime > CLICK_TIMEOUT_TICKS) { lifeTime = calculateLifeTime(); lived = 0; } // "If the bobber is not directly exposed to sun or moonlight, the wait time will be // approximately doubled." Block highestBlockAt = world.getHighestBlockAt(location); if (location.getY() < highestBlockAt.getLocation().getY()) { if (ThreadLocalRandom.current().nextDouble(100) < 50) { return; } } if (GlowBiomeClimate.isRainy(location.getBlock()) && lived < lifeTime) { if (ThreadLocalRandom.current().nextDouble(100) < 20) { lived++; } } lived++; }
@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); } }
Location location = block.getLocation().clone().add(0, 1, 0); location.setYaw(random.nextFloat() * 360 - 180); if (location.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) {
return true; current = current.getBlock().getRelative(right).getLocation(); current = current.getBlock().getRelative(BlockFace.DOWN).getLocation();
private void playOutExplosion(GlowPlayer player, Iterable<Block> blocks) { Collection<Record> records = new ArrayList<>(); for (Block block : blocks) { Location blockLocation = block.getLocation(); byte x = (byte) (blockLocation.getBlockX() - (int) this.location.getX()); byte y = (byte) (blockLocation.getBlockY() - (int) this.location.getY()); byte z = (byte) (blockLocation.getBlockZ() - (int) this.location.getZ()); records.add(new Record(x, y, z)); } Vector velocity = player.getVelocity(); ExplosionMessage message = new ExplosionMessage( (float) location.getX(), (float) location.getY(), (float) location.getZ(), power, (float) velocity.getX(), (float) velocity.getY(), (float) velocity.getZ(), records); player.getSession().send(message); }
Location exitLocation = exitBlock.getLocation().add(0.5, 0.1, 0.5); // Use center of block setRawLocation(exitLocation, false); teleported = true;
private void placeBoat(GlowPlayer player, ItemStack holding) { Block targetBlock = player.getTargetBlock((Set<Material>) null, 5); if (targetBlock != null && !targetBlock.isEmpty() && targetBlock.getRelative(BlockFace.UP).isEmpty()) { Location location = targetBlock.getRelative(BlockFace.UP).getLocation(); // center boat on cursor location location.add(0.6875f, 0, 0.6875f); location.setYaw(player.getLocation().getYaw()); Boat boat = targetBlock.getWorld().spawn(location, Boat.class); boat.setWoodType(woodType); if (player.getGameMode() != GameMode.CREATIVE) { player.getInventory().removeItem(holding); } } }
@Override public Location getBedSpawnLocation() { if (bedSpawn == null) { return null; } // Find head of bed GlowBlock block = (GlowBlock) bedSpawn.getBlock(); GlowBlock head = BlockBed.getHead(block); GlowBlock foot = BlockBed.getFoot(block); if (head != null) { // If there is a bed, try to find an empty spot next to the bed if (head.getType() == Material.BED_BLOCK) { Block spawn = BlockBed.getExitLocation(head, foot); return spawn == null ? null : spawn.getLocation().add(0.5, 0.1, 0.5); } if (bedSpawnForced) { Material bottom = head.getType(); Material top = head.getRelative(BlockFace.UP).getType(); // Do not check floor when forcing spawn if (BlockBed.isValidSpawn(bottom) && BlockBed.isValidSpawn(top)) { return bedSpawn.clone().add(0.5, 0.1, 0.5); // No blocks are blocking the spawn } } } return null; }