下面列出了org.bukkit.Location#getBlock ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Modify the to location of the given event to prevent the movement and
* move the player so they are standing on the center of the block at the
* from location.
*/
private static void resetPosition(final PlayerMoveEvent event) {
Location newLoc;
double yValue = event.getFrom().getY();
if(yValue <= 0 || event instanceof PlayerTeleportEvent) {
newLoc = event.getFrom();
} else {
newLoc = BlockUtils.center(event.getFrom()).subtract(new Vector(0, 0.5, 0));
if(newLoc.getBlock() != null) {
switch(newLoc.getBlock().getType()) {
case STEP:
case WOOD_STEP:
newLoc.add(new Vector(0, 0.5, 0));
break;
default: break;
}
}
}
newLoc.setPitch(event.getTo().getPitch());
newLoc.setYaw(event.getTo().getYaw());
event.setCancelled(false);
event.setTo(newLoc);
}
public BaseBlock getBlock(Location location)
{
Preconditions.checkNotNull(location);
CraftWorld craftWorld = (CraftWorld)location.getWorld();
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
Block bukkitBlock = location.getBlock();
BaseBlock block = new BaseBlock(bukkitBlock.getTypeId(), bukkitBlock.getData());
TileEntity te = craftWorld.getHandle().getTileEntity(new BlockPosition(x, y, z));
if (te != null)
{
NBTTagCompound tag = new NBTTagCompound();
readTileEntityIntoTag(te, tag);
block.setNbtData((CompoundTag)toNative(tag));
}
return block;
}
public BaseBlock getBlock(Location location)
{
Preconditions.checkNotNull(location);
CraftWorld craftWorld = (CraftWorld)location.getWorld();
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
Block bukkitBlock = location.getBlock();
BaseBlock block = new BaseBlock(bukkitBlock.getTypeId(), bukkitBlock.getData());
TileEntity te = craftWorld.getHandle().getTileEntity(new BlockPosition(x, y, z));
if (te != null)
{
NBTTagCompound tag = new NBTTagCompound();
readTileEntityIntoTag(te, tag);
block.setNbtData((CompoundTag)toNative(tag));
}
return block;
}
private void calculateSpawnLocations() {
spawningBarriers.clear();
for (Floor floor : game.getFloors()) {
Region region = floor.getRegion();
RegionIterator iterator = new RegionIterator(region);
while (iterator.hasNext()) {
BlockVector vector = iterator.next();
Location location = BukkitUtil.toLocation(game.getWorld(), vector);
Block block = location.getBlock();
if (block.getType() != Material.BARRIER) {
continue;
}
spawningBarriers.add(vector.add(0.5, 0.5, 0.5));
}
}
Collections.shuffle(spawningBarriers);
}
private static boolean isIllegallyOutsideLane(Region lane, Location loc) {
Block feet = loc.getBlock();
if(feet == null) return false;
if(isIllegalBlock(lane, feet)) {
return true;
}
Block head = feet.getRelative(BlockFace.UP);
if(head == null) return false;
if(isIllegalBlock(lane, head)) {
return true;
}
return false;
}
@Override
public void pasteSchematic(File f, Location loc) {
loc = loc.subtract(2, 1, 2);
try {
FileInputStream fis = new FileInputStream(f);
NBTTagCompound nbt = NBTCompressedStreamTools.a(fis);
short width = nbt.getShort("Width");
short height = nbt.getShort("Height");
short length = nbt.getShort("Length");
byte[] blocks = nbt.getByteArray("Blocks");
byte[] data = nbt.getByteArray("Data");
fis.close();
//paste
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
for (int z = 0; z < length; ++z) {
int index = y * width * length + z * width + x;
final Location l = new Location(loc.getWorld(), x + loc.getX(), y + loc.getY(), z + loc.getZ());
int b = blocks[index] & 0xFF;//make the block unsigned, so that blocks with an id over 127, like quartz and emerald, can be pasted
final Block block = l.getBlock();
block.setType(Material.getMaterial(b));
block.setData(data[index]);
//you can check what type the block is here, like if(m.equals(Material.BEACON)) to check if it's a beacon
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Clears any and all blocks between the corners. NOTE: This
* can be a very expensive call. However, we anticipate most
* doors to be small, and therefore inexpensive by and large.
*/
public void clear() {
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= this.maxZ; z++) {
Location loc = new Location(world, x, y, z);
Block block = loc.getBlock();
if (block != null) {
blocks.put(loc, new SectionBlock(block.getType(), block.getData()));
loc.getBlock().setType(Material.AIR);
}
}
}
}
}
@SuppressWarnings("deprecation")
private void resetBlock(Player player, Location location) {
Block block = location.getBlock();
if(VersionUtil.getMinorVersion() >= 13) {
player.sendBlockChange(location, block.getBlockData());
return;
}
player.sendBlockChange(location, block.getType(), block.getData());
}
public static Location getOpenSpaceAbove(Location location) {
Preconditions.checkNotNull(location, "location");
Location result = location.clone();
while (true) {
Block block = result.getBlock();
if (block == null || block.getType() == Material.AIR) break;
result.setY(result.getY() + 1);
}
return result;
}
public boolean canDropAt(Location location) {
Block block = location.getBlock();
Block below = block.getRelative(BlockFace.DOWN);
if (!canDropOn(below.getState())) return false;
if (block.getRelative(BlockFace.UP).getType() != Material.AIR) return false;
switch (block.getType()) {
case AIR:
case LONG_GRASS:
return true;
default:
return false;
}
}
public void buildConstructionScaffolding(Location center, Player player)
{
//this.buildScaffolding(center);
Block block = center.getBlock();
ItemManager.setTypeIdAndData(block, ItemManager.getId(Material.CHEST), 0, false);
}
@Override
public BaseBlock getBlock(Location location) {
checkNotNull(location);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
final WorldServer handle = craftWorld.getHandle();
Chunk chunk = handle.getChunkAt(x >> 4, z >> 4);
final BlockPosition blockPos = new BlockPosition(x, y, z);
final IBlockData blockData = chunk.getType(blockPos);
int internalId = Block.getCombinedId(blockData);
BlockState state = BlockStateIdAccess.getBlockStateById(internalId);
if (state == null) {
org.bukkit.block.Block bukkitBlock = location.getBlock();
state = BukkitAdapter.adapt(bukkitBlock.getBlockData());
}
// Read the NBT data
TileEntity te = chunk.a(blockPos, Chunk.EnumTileEntityState.CHECK);
if (te != null) {
NBTTagCompound tag = new NBTTagCompound();
readTileEntityIntoTag(te, tag); // Load data
return state.toBaseBlock((CompoundTag) toNative(tag));
}
return state.toBaseBlock();
}
/**
* Changes the sign to red if it exists
* @param loc
*/
private void popSign(Location loc) {
Block b = loc.getBlock();
if (b.getType().equals(Material.SIGN_POST) || b.getType().equals(Material.WALL_SIGN)) {
Sign s = (Sign) b.getState();
if (s != null) {
if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + plugin.myLocale().warpswelcomeLine)) {
s.setLine(0, ChatColor.RED + plugin.myLocale().warpswelcomeLine);
s.update(true, false);
}
}
}
}
@Override
public void pasteSchematic(File f, Location loc) {
loc = loc.subtract(2, 1, 2);
try {
FileInputStream fis = new FileInputStream(f);
NBTTagCompound nbt = NBTCompressedStreamTools.a(fis);
short width = nbt.getShort("Width");
short height = nbt.getShort("Height");
short length = nbt.getShort("Length");
byte[] blocks = nbt.getByteArray("Blocks");
byte[] data = nbt.getByteArray("Data");
fis.close();
//paste
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
for (int z = 0; z < length; ++z) {
int index = y * width * length + z * width + x;
final Location l = new Location(loc.getWorld(), x + loc.getX(), y + loc.getY(), z + loc.getZ());
int b = blocks[index] & 0xFF;//make the block unsigned, so that blocks with an id over 127, like quartz and emerald, can be pasted
final Block block = l.getBlock();
block.setType(Material.getMaterial(b));
block.setData(data[index]);
//you can check what type the block is here, like if(m.equals(Material.BEACON)) to check if it's a beacon
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void check(InteractWorldEvent e) {
HawkPlayer pp = e.getHawkPlayer();
Vector eyePos = pp.getPosition().clone().add(new Vector(0, pp.isSneaking() ? 1.54 : 1.62, 0));
Vector direction = MathPlus.getDirection(pp.getYaw(), pp.getPitch());
Location bLoc = e.getTargetedBlockLocation();
Block b = bLoc.getBlock();
WrappedBlock bNMS = WrappedBlock.getWrappedBlock(b, pp.getClientVersion());
AABB targetAABB = new AABB(bNMS.getHitBox().getMin(), bNMS.getHitBox().getMax());
double distance = targetAABB.distanceToPosition(eyePos);
BlockIterator iter = new BlockIterator(pp.getWorld(), eyePos, direction, 0, (int) distance + 2);
while (iter.hasNext()) {
Block bukkitBlock = iter.next();
if (bukkitBlock.getType() == Material.AIR || bukkitBlock.isLiquid())
continue;
if (bukkitBlock.getLocation().equals(bLoc))
break;
WrappedBlock iterBNMS = WrappedBlock.getWrappedBlock(bukkitBlock, pp.getClientVersion());
AABB checkIntersection = new AABB(iterBNMS.getHitBox().getMin(), iterBNMS.getHitBox().getMax());
Vector occludeIntersection = checkIntersection.intersectsRay(new Ray(eyePos, direction), 0, Float.MAX_VALUE);
if (occludeIntersection != null) {
if (occludeIntersection.distance(eyePos) < distance) {
Placeholder ph = new Placeholder("type", iterBNMS.getBukkitBlock().getType());
punishAndTryCancelAndBlockRespawn(pp, 1, e, ph);
return;
}
}
}
}
static Material materialAt(Location location) {
Block block = location.getBlock();
return block == null ? Material.AIR : block.getType();
}
@Subscribe
public void onPlayerWinGame(PlayerWinGameEvent event) {
for (Location location : getValue()) {
int amount = random.nextInt(3) + 3;
for (int i = 0; i < amount; i++) {
Location spawn;
int trys = 0;
do {
int x = random.nextInt(8) - 4;
int y = random.nextInt(8) - 4;
int z = random.nextInt(8) - 4;
spawn = location.clone().add(x, y, z);
Block block = spawn.getBlock();
if (!block.isLiquid() && block.getType() != Material.AIR) {
//Do another search
spawn = null;
}
} while (spawn == null && ++trys < MAX_TRYS);
if (spawn == null) {
continue;
}
Firework firework = (Firework) spawn.getWorld().spawnEntity(spawn, EntityType.FIREWORK);
FireworkMeta meta = firework.getFireworkMeta();
Type type = typeValues.get(random.nextInt(typeValues.size()));
Color c1 = colorValues.get(random.nextInt(colorValues.size()));
Color c2 = colorValues.get(random.nextInt(colorValues.size()));
FireworkEffect effect = FireworkEffect.builder()
.flicker(random.nextBoolean())
.withColor(c1)
.withFade(c2)
.with(type)
.trail(random.nextBoolean())
.build();
meta.addEffect(effect);
int rp = random.nextInt(3);
meta.setPower(rp);
firework.setFireworkMeta(meta);
}
}
}
static Block findAnAirBlock(Location l) {
while(l.getY() < 255 && l.getBlock().getType() != Material.AIR) {
l.add(0, 1, 0);
}
return l.getY() < 255 && l.getBlock().getType() == Material.AIR ? l.getBlock() : null;
}
public static Block getBlockAsync(Location loc) {
if (loc.getWorld().isChunkLoaded(loc.getBlockX() >> 4, loc.getBlockZ() >> 4))
return loc.getBlock();
return null;
}
@Override
public void effect(Event e, ItemStack item, int level) {
BlockBreakEvent event = (BlockBreakEvent) e;
Player player = event.getPlayer();
if (!isUsable(player.getItemInHand().getType().toString(), event.getBlock().getType().toString()))
return;
List<Location> locations = new ArrayList<Location>();
int locRad = Radius;
if (LargerRadius && Tools.random.nextInt(100) < level * 5)
locRad += 2;
int r = locRad - 1;
int start = r / 2;
Location sL = event.getBlock().getLocation();
player.getWorld().createExplosion(sL, 0f); // Create a fake explosion
sL.setX(sL.getX() - start);
sL.setY(sL.getY() - start);
sL.setZ(sL.getZ() - start);
for (int x = 0; x < locRad; x++)
for (int y = 0; y < locRad; y++)
for (int z = 0; z < locRad; z++)
if ((!(x == 0 && y == 0 && z == 0)) && (!(x == r && y == 0 && z == 0)) && (!(x == 0 && y == r && z == 0)) && (!(x == 0 && y == 0 && z == r)) && (!(x == r && y == r && z == 0))
&& (!(x == 0 && y == r && z == r)) && (!(x == r && y == 0 && z == r)) && (!(x == r && y == r && z == r)))
locations.add(new Location(sL.getWorld(), sL.getX() + x, sL.getY() + y, sL.getZ() + z));
for (Location loc : locations) {
String iMat = item.getType().toString();
Block b = loc.getBlock();
String bMat = b.getType().toString();
if (isUsable(iMat, bMat))
if (!loc.getBlock().getDrops(item).isEmpty())
if (Tools.checkWorldGuard(loc, player, "BUILD", false))
if (DropItems)
loc.getBlock().breakNaturally(item);
else
for (ItemStack i : loc.getBlock().getDrops(item)) {
player.getInventory().addItem(i);
loc.getBlock().setType(Material.AIR);
}
}
}