/** * Returns the {@link BlockState} of a block at the given location. * * @param loc the location which contains the block * @return The {@link BlockState} state. */ public BlockState getBlockState(Location loc) { if (blockStateMap.containsKey(loc)) { return blockStateMap.get(loc); } else { return loc.getBlock().getState(); } } }
/** * Returns true if the player is inside a water block. * * @return True if entity is in water. */ public boolean isInWater() { Material mat = getLocation().getBlock().getType(); return mat == Material.WATER || mat == Material.STATIONARY_WATER; }
@Override public Block getAttachedBlock() { if (isInBlock()) { return getLocation().getBlock(); } return null; }
private boolean isValidMovement(Entity vehicle, Location oldLocation, Location newLocation) { if (!(vehicle instanceof Boat)) { return true; } boolean workOnLand = ((Boat) vehicle).getWorkOnLand(); if (workOnLand) { return true; } Material type = oldLocation.getBlock().getType(); if (type != Material.WATER && type != Material.STATIONARY_WATER) { return true; } return !newLocation.getBlock().getType().isSolid(); } }
/** * 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 pulse() { super.pulse(); // TODO: Particles // TODO: Bopper movement if (location.getBlock().getType() == Material.WATER) { increaseTimeLived(); } }
/** * Returns whether the entity's eye location is within a solid block. * * @return true if the entity is in a solid block; false otherwise */ public boolean isWithinSolidBlock() { return getEyeLocation().getBlock().getType().isOccluding(); }
@Override public void pulse() { super.pulse(); // seems like every hanging entity only checks every 5 Seconds if it can still survive if (ticksLived % (5 * 20) == 0) { Block block = location.getBlock(); BlockType blockType = ItemTable.instance().getBlock(block.getType()); if (!(blockType instanceof BlockFence)) { getExistingLeashHitches(block).forEach(Entity::remove); } } }
private void placeFallingBlock() { location.getBlock().setTypeIdAndData(material.getId(), getBlockData(), true); if (getBlockEntityCompoundTag() != null) { if (location.getBlock() instanceof GlowBlock) { GlowBlock block = (GlowBlock) location.getBlock(); BlockEntity blockEntity = block.getBlockEntity(); if (blockEntity != null) { blockEntity.loadNbt(getBlockEntityCompoundTag()); } } } if (material == Material.ANVIL) { ThreadLocalRandom random = ThreadLocalRandom.current(); world.playSound(location, Sound.BLOCK_ANVIL_FALL, 4, (1.0F + (random.nextFloat() - random.nextFloat()) * 0.2F) * 0.7F); } }
@Override public InventoryView openWorkbench(Location location, boolean force) { if (location == null) { location = getLocation(); } if (!force && location.getBlock().getType() != Material.WORKBENCH) { return null; } return openInventory(new GlowCraftingInventory(this, InventoryType.WORKBENCH)); }
@Override public void pulse() { super.pulse(); // "While in the End, [..] will continually generate fire, [..] replacing any other block" if (world.getEnvironment() == Environment.THE_END) { Block block = location.getBlock(); if (block.getType() != Material.FIRE) { block.setType(Material.FIRE); } } }
@Override public InventoryView openEnchanting(Location location, boolean force) { if (location == null) { location = getLocation(); } if (!force && location.getBlock().getType() != Material.ENCHANTMENT_TABLE) { return null; } return openInventory(new GlowEnchantingInventory(location, (GlowPlayer) this)); }
protected void jump() { if (location.getBlock().isLiquid()) { // jump out more when you breach the surface of the liquid if (location.getBlock().getRelative(BlockFace.UP).isEmpty()) { velocity.setY(velocity.getY() + 0.3); } // less jumping in liquid velocity.setY(velocity.getY() + 0.04); } else { // jump normally velocity.setY(velocity.getY() + 0.42); } }
/** * 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); }
/** * Process teleportation when collide with an entity. * * @param entity the eneity that the ender pearl collides with */ @Override public void collide(LivingEntity entity) { ProjectileSource source = getShooter(); // the entity receives fake damage. if (source instanceof Entity) { entity.damage(0, (Entity) source, EntityDamageEvent.DamageCause.PROJECTILE); } else { entity.damage(0, EntityDamageEvent.DamageCause.PROJECTILE); } collide(entity.getLocation().getBlock()); }
/** * Process random spawn chicks when collide with a living entity. * * @param entity the eneity that the egg collides with */ @Override public void collide(LivingEntity entity) { ProjectileSource source = getShooter(); // the entity receives fake damage. if (entity instanceof Entity) { entity.damage(0, (Entity) source, EntityDamageEvent.DamageCause.PROJECTILE); } else { entity.damage(0, EntityDamageEvent.DamageCause.PROJECTILE); } collide(entity.getLocation().getBlock()); }
private boolean canHoldPainting(Location where) { Block block = where.getBlock(); if (block.getType().isSolid()) { return false; } Block behind = block.getRelative(getAttachedFace()); return behind.getType().isSolid(); }
@Override protected void pulsePhysics() { if (location.getBlock().getType().isSolid()) { setRawLocation(location.clone().add(0, 0.2, 0), false); } super.pulsePhysics(); }
/** * Removes this fishing hook. Drops loot and xp if a player is fishing. */ public void reelIn() { if (location.getBlock().getType() == Material.WATER) { ProjectileSource shooter = getShooter(); if (shooter instanceof Player) { PlayerFishEvent fishEvent = new PlayerFishEvent((Player) shooter, this, null, CAUGHT_FISH); fishEvent.setExpToDrop(ThreadLocalRandom.current().nextInt(1, 7)); fishEvent = EventFactory.getInstance().callEvent(fishEvent); if (!fishEvent.isCancelled()) { // TODO: Item should "fly" towards player world.dropItemNaturally(((Player) getShooter()).getLocation(), getRewardItem()); ((Player) getShooter()).giveExp(fishEvent.getExpToDrop()); } } } remove(); }
@Override public void pulse() { super.pulse(); if (ticksLived % (20 * 5) == 0) { if (location.getBlock().getRelative(getAttachedFace()).getType() == Material.AIR) { if (EventFactory.getInstance() .callEvent(new HangingBreakEvent(this, RemoveCause.PHYSICS)) .isCancelled()) { return; } world.dropItemNaturally(location, new ItemStack(Material.ITEM_FRAME)); if (!isEmpty()) { world.dropItemNaturally(location, getItem().clone()); } remove(); } } }