Java Code Examples for cn.nukkit.block.Block#getId()

The following examples show how to use cn.nukkit.block.Block#getId() . 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: ItemTool.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean useOn(Block block) {
    if (this.isUnbreakable() || isDurable()) {
        return true;
    }

    if (block.getToolType() == ItemTool.TYPE_PICKAXE && this.isPickaxe() ||
            block.getToolType() == ItemTool.TYPE_SHOVEL && this.isShovel() ||
            block.getToolType() == ItemTool.TYPE_AXE && this.isAxe() ||
            block.getToolType() == ItemTool.TYPE_SWORD && this.isSword() ||
            block.getToolType() == ItemTool.SHEARS && this.isShears()
            ) {
        this.meta++;
    } else if (!this.isShears() && block.getBreakTime(this) > 0) {
        this.meta += 2;
    } else if (this.isHoe()) {
        if (block.getId() == GRASS || block.getId() == DIRT) {
            this.meta++;
        }
    } else {
        this.meta++;
    }
    return true;
}
 
Example 2
Source File: ItemGlassBottle.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    if (target.getId() == WATER || target.getId() == STILL_WATER) {
        Item potion = new ItemPotion();

        if (this.count == 1) {
            player.getInventory().setItemInHand(potion);
        } else if (this.count > 1) {
            this.count--;
            player.getInventory().setItemInHand(this);
            if (player.getInventory().canAddItem(potion)) {
                player.getInventory().addItem(potion);
            } else {
                player.getLevel().dropItem(player.add(0, 1.3, 0), potion, player.getDirectionVector().multiply(0.4));
            }
        }
    }
    return false;
}
 
Example 3
Source File: ItemTool.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean useOn(Block block) {
    if (this.isUnbreakable() || isDurable()) {
        return true;
    }

    if (block.getToolType() == ItemTool.TYPE_PICKAXE && this.isPickaxe() ||
            block.getToolType() == ItemTool.TYPE_SHOVEL && this.isShovel() ||
            block.getToolType() == ItemTool.TYPE_AXE && this.isAxe() ||
            block.getToolType() == ItemTool.TYPE_SWORD && this.isSword() ||
            block.getToolType() == ItemTool.SHEARS && this.isShears()
            ) {
        this.meta++;
    } else if (!this.isShears() && block.getBreakTime(this) > 0) {
        this.meta += 2;
    } else if (this.isHoe()) {
        if (block.getId() == GRASS || block.getId() == DIRT) {
            this.meta++;
        }
    } else {
        this.meta++;
    }
    return true;
}
 
Example 4
Source File: BreakCommand.java    From EssentialsNK with GNU General Public License v3.0 6 votes vote down vote up
public boolean execute(CommandSender sender, String label, String[] args) {
    if (!this.testPermission(sender)) {
        return false;
    }
    if (!this.testIngame(sender)) {
        return false;
    }
    if (args.length != 0) {
        this.sendUsage(sender);
        return false;
    }
    Player player = (Player) sender;
    Block block = player.getTargetBlock(120, new Integer[]{Block.AIR});
    if (block == null) {
        sender.sendMessage(TextFormat.RED + Language.translate("commands.break.unreachable"));
        return false;
    }
    if (block.getId() == Block.BEDROCK && !sender.hasPermission("essentialsnk.break.bedrock")) {
        sender.sendMessage(TextFormat.RED + Language.translate("commands.break.bedrock"));
        return false;
    }
    player.getLevel().setBlock(block, new BlockAir(), true, true);
    return true;
}
 
Example 5
Source File: EntityLightning.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initEntity() {
    super.initEntity();

    this.setHealth(4);
    this.setMaxHealth(4);

    this.state = 2;
    this.liveTime = this.level.rand.nextInt(3) + 1;

    if (isEffect && this.level.gameRules.getBoolean("dofiretick") && (this.server.getDifficulty() >= 2)) {
        Block block = this.getLevelBlock();
        if (block.getId() == 0 || block.getId() == Block.TALL_GRASS) {
            BlockFire fire = new BlockFire();
            fire.x = block.x;
            fire.y = block.y;
            fire.z = block.z;
            fire.level = level;
            this.getLevel().setBlock(fire, fire, true);
            if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {

                BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
                getServer().getPluginManager().callEvent(e);

                if (!e.isCancelled()) {
                    level.setBlock(fire, fire, true);
                    level.scheduleUpdate(fire, fire.tickRate() + level.rand.nextInt(10));
                }
            }
        }
    }
}
 
Example 6
Source File: EntityLiving.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public Block[] getLineOfSight(int maxDistance, int maxLength, Integer[] transparent) {
    if (maxDistance > 120) {
        maxDistance = 120;
    }

    if (transparent != null && transparent.length == 0) {
        transparent = null;
    }

    List<Block> blocks = new ArrayList<>();

    BlockIterator itr = new BlockIterator(this.level, this.getPosition(), this.getDirectionVector(), this.getEyeHeight(), maxDistance);

    while (itr.hasNext()) {
        Block block = itr.next();
        blocks.add(block);

        if (maxLength != 0 && blocks.size() > maxLength) {
            blocks.remove(0);
        }

        int id = block.getId();

        if (transparent == null) {
            if (id != 0) {
                break;
            }
        } else {
            if (Arrays.binarySearch(transparent, id) < 0) {
                break;
            }
        }
    }

    return blocks.stream().toArray(Block[]::new);
}
 
Example 7
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void scheduleUpdate(Block block, Vector3 pos, int delay, int priority, boolean checkArea) {
    if (block.getId() == 0 || (checkArea && !this.isChunkLoaded(block.getFloorX() >> 4, block.getFloorZ() >> 4))) {
        return;
    }

    BlockUpdateEntry entry = new BlockUpdateEntry(pos.floor(), block, ((long) delay) + getCurrentTick(), priority);

    if (!this.updateQueue.contains(entry)) {
        this.updateQueue.add(entry);
    }
}
 
Example 8
Source File: EntityLightning.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initEntity() {
    super.initEntity();

    this.setHealth(4);
    this.setMaxHealth(4);

    this.state = 2;
    this.liveTime = ThreadLocalRandom.current().nextInt(3) + 1;

    if (isEffect && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK) && (this.server.getDifficulty() >= 2)) {
        Block block = this.getLevelBlock();
        if (block.getId() == 0 || block.getId() == Block.TALL_GRASS) {
            BlockFire fire = (BlockFire) Block.get(BlockID.FIRE);
            fire.x = block.x;
            fire.y = block.y;
            fire.z = block.z;
            fire.level = level;
            this.getLevel().setBlock(fire, fire, true);
            if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {

                BlockIgniteEvent e = new BlockIgniteEvent(block, null, this, BlockIgniteEvent.BlockIgniteCause.LIGHTNING);
                getServer().getPluginManager().callEvent(e);

                if (!e.isCancelled()) {
                    level.setBlock(fire, fire, true);
                    level.scheduleUpdate(fire, fire.tickRate() + ThreadLocalRandom.current().nextInt(10));
                }
            }
        }
    }
}
 
Example 9
Source File: EntityLiving.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public Block[] getLineOfSight(int maxDistance, int maxLength, Integer[] transparent) {
    if (maxDistance > 120) {
        maxDistance = 120;
    }

    if (transparent != null && transparent.length == 0) {
        transparent = null;
    }

    List<Block> blocks = new ArrayList<>();

    BlockIterator itr = new BlockIterator(this.level, this.getPosition(), this.getDirectionVector(), this.getEyeHeight(), maxDistance);

    while (itr.hasNext()) {
        Block block = itr.next();
        blocks.add(block);

        if (maxLength != 0 && blocks.size() > maxLength) {
            blocks.remove(0);
        }

        int id = block.getId();

        if (transparent == null) {
            if (id != 0) {
                break;
            }
        } else {
            if (Arrays.binarySearch(transparent, id) < 0) {
                break;
            }
        }
    }

    return blocks.toArray(new Block[0]);
}
 
Example 10
Source File: AdvancedRouteFinder.java    From Actaeon with MIT License 4 votes vote down vote up
private boolean canWalkOn(Block block){
	return !(block.getId() == Block.LAVA || block.getId() == Block.STILL_LAVA);
}
 
Example 11
Source File: ItemBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ItemBlock(Block block, Integer meta, int count) {
    super(block.getId(), meta, count, block.getName());
    this.block = block;
}
 
Example 12
Source File: EmptyChunkSection.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Block getAndSetBlock(int x, int y, int z, Block block) {
    if (block.getId() != 0) throw new ChunkException("Tried to modify an empty Chunk");
    return Block.get(0);
}
 
Example 13
Source File: EntityFallingBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {

    if (closed) {
        return false;
    }

    this.timing.startTiming();

    int tickDiff = currentTick - lastUpdate;
    if (tickDiff <= 0 && !justCreated) {
        return true;
    }

    lastUpdate = currentTick;

    boolean hasUpdate = entityBaseTick(tickDiff);

    if (isAlive()) {
        motionY -= getGravity();

        move(motionX, motionY, motionZ);

        float friction = 1 - getDrag();

        motionX *= friction;
        motionY *= 1 - getDrag();
        motionZ *= friction;

        Vector3 pos = (new Vector3(x - 0.5, y, z - 0.5)).round();

        if (onGround) {
            close();
            Block block = level.getBlock(pos);
            if (block.getId() > 0 && block.isTransparent() && !block.canBeReplaced()) {
                if (this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                    getLevel().dropItem(this, Item.get(this.getBlock(), this.getDamage(), 1));
                }
            } else {
                EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, block, Block.get(getBlock(), getDamage()));
                server.getPluginManager().callEvent(event);
                if (!event.isCancelled()) {
                    getLevel().setBlock(pos, event.getTo(), true);

                    if (event.getTo().getId() == Item.ANVIL) {
                        getLevel().addSound(pos, Sound.RANDOM_ANVIL_LAND);
                    }
                }
            }
            hasUpdate = true;
        }

        updateMovement();
    }

    this.timing.stopTiming();

    return hasUpdate || !onGround || Math.abs(motionX) > 0.00001 || Math.abs(motionY) > 0.00001 || Math.abs(motionZ) > 0.00001;
}
 
Example 14
Source File: BlockEntityMobSpawner.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate() {
    if (this.closed) {
        return false;
    }

    this.timing.startTiming();

    if (!(this.chunk instanceof Chunk)) {
        return false;
    }

    if (!(this.canUpdate())) {
        return false;
    }

    if (this.namedTag.getInt("Delay") <= 0) {
        int success = 0;
        NukkitRandom random = new NukkitRandom();
        for (int i = 0; i < this.namedTag.getInt("SpawnCount"); ++i) {
            Vector3 pos = this.add(
                    this.getRandomSpawn(), 
                    NukkitMath.randomRange(random, -1, 1), 
                    this.getRandomSpawn());
            Block target = this.getLevel().getBlock(pos);
            Block ground = target.down();
            if (target.getId() == Block.AIR && ground.isSolid()) {
                ++success;
                CompoundTag nbt = new CompoundTag()
                        .putList(new ListTag<DoubleTag>("Pos")
                                .add(new DoubleTag("", pos.x))
                                .add(new DoubleTag("", pos.y))
                                .add(new DoubleTag("", pos.z)))
                        .putList(new ListTag<DoubleTag>("Motion")
                                .add(new DoubleTag("", 0))
                                .add(new DoubleTag("", 0))
                                .add(new DoubleTag("", 0)))
                        .putList(new ListTag<FloatTag>("Rotation")
                                .add(new FloatTag("", (float) Math.random() * 360))
                                .add(new FloatTag("", (float) 0)));

                Entity entity = Entity.createEntity(this.getNetworkId(), this.chunk, nbt);
                entity.spawnToAll();
            }
        }

        if (success > 0) {
            this.namedTag.putInt("Delay", NukkitMath.randomRange(random, this.namedTag.getInt("MinSpawnDelay"), this.namedTag.getInt("MaxSpawnDelay")));
        }

    } else {
        this.namedTag.putInt("Delay", this.namedTag.getInt("Delay") - 1);
    }

    this.timing.stopTiming();

    return true;
}
 
Example 15
Source File: EmptyChunkSection.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Block getAndSetBlock(int x, int y, int z, Block block) {
    if (block.getId() != 0) throw new ChunkException("Tried to modify an empty Chunk");
    return Block.get(0);
}
 
Example 16
Source File: DestroyBlockParticle.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public DestroyBlockParticle(Vector3 pos, Block block) {
    super(pos.x, pos.y, pos.z);
    this.data = block.getId() | (block.getDamage() << 8);
}
 
Example 17
Source File: TerrainParticle.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public TerrainParticle(Vector3 pos, Block block) {
    super(pos, Particle.TYPE_TERRAIN, (block.getDamage() << 8) | block.getId());
}
 
Example 18
Source File: ItemBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ItemBlock(Block block, Integer meta, int count) {
    super(block.getId(), meta, count, block.getName());
    this.block = block;
}
 
Example 19
Source File: ItemFlintSteel.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    if (block.getId() == AIR && (target instanceof BlockSolid)) {
        if (target.getId() == OBSIDIAN) {
            int targetX = target.getFloorX();
            int targetY = target.getFloorY();
            int targetZ = target.getFloorZ();
            int x_max = targetX;
            int x_min = targetX;
            int x;
            for (x = targetX + 1; level.getBlock(new Vector3(x, targetY, targetZ)).getId() == OBSIDIAN; x++) {
                x_max++;
            }
            for (x = targetX - 1; level.getBlock(new Vector3(x, targetY, targetZ)).getId() == OBSIDIAN; x--) {
                x_min--;
            }
            int count_x = x_max - x_min + 1;
            int z_max = targetZ;
            int z_min = targetZ;
            int z;
            for (z = targetZ + 1; level.getBlock(new Vector3(targetX, targetY, z)).getId() == OBSIDIAN; z++) {
                z_max++;
            }
            for (z = targetZ - 1; level.getBlock(new Vector3(targetX, targetY, z)).getId() == OBSIDIAN; z--) {
                z_min--;
            }
            int count_z = z_max - z_min + 1;
            int z_max_y = targetY;
            int z_min_y = targetY;
            int y;
            for (y = targetY; level.getBlock(new Vector3(targetX, y, z_max)).getId() == OBSIDIAN; y++) {
                z_max_y++;
            }
            for (y = targetY; level.getBlock(new Vector3(targetX, y, z_min)).getId() == OBSIDIAN; y++) {
                z_min_y++;
            }
            int y_max = Math.min(z_max_y, z_min_y) - 1;
            int count_y = y_max - targetY + 2;
            if ((count_x >= 4 && count_x <= 23 || count_z >= 4 && count_z <= 23) && count_y >= 5 && count_y <= 23) {
                int count_up = 0;
                for (int up_z = z_min; level.getBlock(new Vector3(targetX, y_max, up_z)).getId() == OBSIDIAN && up_z <= z_max; up_z++) {
                    count_up++;
                }
                if (count_up == count_z) {
                    for (int block_z = z_min + 1; block_z < z_max; block_z++) {
                        for (int block_y = targetY + 1; block_y < y_max; block_y++) {
                            level.setBlock(new Vector3(targetX, block_y, block_z), new BlockNetherPortal());
                        }
                    }
                    return true;
                }
            }
        }
        BlockFire fire = new BlockFire();
        fire.x = block.x;
        fire.y = block.y;
        fire.z = block.z;
        fire.level = level;

        if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {
            BlockIgniteEvent e = new BlockIgniteEvent(block, null, player, BlockIgniteEvent.BlockIgniteCause.FLINT_AND_STEEL);
            block.getLevel().getServer().getPluginManager().callEvent(e);

            if (!e.isCancelled()) {
                level.setBlock(fire, fire, true);
                level.scheduleUpdate(fire, fire.tickRate() + level.rand.nextInt(10));
            }
            return true;
        }
        if ((player.gamemode & 0x01) == 0 && this.useOn(block)) {
            if (this.getDamage() >= this.getMaxDurability()) {
                player.getInventory().setItemInHand(new Item(Item.AIR, 0, 0));
            } else {
                this.meta++;
                player.getInventory().setItemInHand(this);
            }
        }
        return true;
    }
    return false;
}
 
Example 20
Source File: EntityFallingBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {

    if (closed) {
        return false;
    }

    this.timing.startTiming();

    int tickDiff = currentTick - lastUpdate;
    if (tickDiff <= 0 && !justCreated) {
        return true;
    }

    lastUpdate = currentTick;

    boolean hasUpdate = entityBaseTick(tickDiff);

    if (isAlive()) {
        motionY -= getGravity();

        move(motionX, motionY, motionZ);

        float friction = 1 - getDrag();

        motionX *= friction;
        motionY *= 1 - getDrag();
        motionZ *= friction;

        Vector3 pos = (new Vector3(x - 0.5, y, z - 0.5)).round();

        if (onGround) {
            kill();
            Block block = level.getBlock(pos);
            if (block.getId() > 0 && block.isTransparent() && !block.canBeReplaced()) {
                if (this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                    getLevel().dropItem(this, Item.get(this.getBlock(), this.getDamage(), 1));
                }
            } else {
                EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, block, Block.get(getBlock(), getDamage()));
                server.getPluginManager().callEvent(event);
                if (!event.isCancelled()) {
                    getLevel().setBlock(pos, event.getTo(), true);

                    if (event.getTo().getId() == Item.ANVIL) {
                        getLevel().addSound(pos, Sound.RANDOM_ANVIL_LAND);
                    }
                }
            }
            hasUpdate = true;
        }

        updateMovement();
    }

    this.timing.stopTiming();

    return hasUpdate || !onGround || Math.abs(motionX) > 0.00001 || Math.abs(motionY) > 0.00001 || Math.abs(motionZ) > 0.00001;
}