Java Code Examples for org.bukkit.Location#getBlock()

The following examples show how to use org.bukkit.Location#getBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerMovementListener.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: FaweAdapter_1_11.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: LaneMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: FlagShowBarriers.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: FaweAdapter_1_10.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: Template.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public void buildConstructionScaffolding(Location center, Player player) 
{
	//this.buildScaffolding(center);
	
	Block block = center.getBlock();
	ItemManager.setTypeIdAndData(block, ItemManager.getId(Material.CHEST), 0, false);
}
 
Example 7
Source File: BlockInteractOcclusion.java    From Hawk with GNU General Public License v3.0 5 votes vote down vote up
@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;
            }
        }
    }
}
 
Example 8
Source File: NMS_v1_11_R1.java    From Crazy-Crates with MIT License 5 votes vote down vote up
@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();
    }
}
 
Example 9
Source File: WarpSigns.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 10
Source File: Spigot_v1_15_R2.java    From worldedit-adapters with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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();
}
 
Example 11
Source File: NMS_v1_10_R1.java    From Crazy-Crates with MIT License 5 votes vote down vote up
@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();
    }
}
 
Example 12
Source File: Flag.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
  }
}
 
Example 13
Source File: FireworkMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: ForceField.java    From CombatLogX with GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example 15
Source File: Section.java    From ZombieEscape with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
                }
            }
        }
    }
}
 
Example 16
Source File: Utils.java    From ArmorStandTools with MIT License 4 votes vote down vote up
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;
}
 
Example 17
Source File: FlagFireworks.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
@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);  
		}
	}
}
 
Example 18
Source File: Materials.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
static Material materialAt(Location location) {
  Block block = location.getBlock();
  return block == null ? Material.AIR : block.getType();
}
 
Example 19
Source File: ServerUtils.java    From Hawk with GNU General Public License v3.0 4 votes vote down vote up
public static Block getBlockAsync(Location loc) {
    if (loc.getWorld().isChunkLoaded(loc.getBlockX() >> 4, loc.getBlockZ() >> 4))
        return loc.getBlock();
    return null;
}
 
Example 20
Source File: Explosive.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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);
                        }
    }

}