org.bukkit.block.Block.getLocation()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中org.bukkit.block.Block.getLocation()方法的一些代码示例,展示了Block.getLocation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Block.getLocation()方法的具体详情如下:
包路径:org.bukkit.block.Block
类名称:Block
方法名:getLocation

Block.getLocation介绍

[英]Gets the Location of the block
[中]

代码示例

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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));
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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)));
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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();
  }
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * 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();
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * 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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

@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();
    }
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

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++;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@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);
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

Location location = block.getLocation().clone().add(0, 1, 0);
location.setYaw(random.nextFloat() * 360 - 180);
if (location.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) {

代码示例来源:origin: GlowstoneMC/Glowstone

return true;
  current = current.getBlock().getRelative(right).getLocation();
current = current.getBlock().getRelative(BlockFace.DOWN).getLocation();

代码示例来源:origin: GlowstoneMC/Glowstone

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);
}

代码示例来源:origin: GlowstoneMC/Glowstone

Location exitLocation = exitBlock.getLocation().add(0.5, 0.1, 0.5); // Use center of block
setRawLocation(exitLocation, false);
teleported = true;

代码示例来源:origin: GlowstoneMC/Glowstone

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);
    }
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@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;
}

相关文章