Java Code Examples for cn.nukkit.level.Level#BLOCK_UPDATE_RANDOM

The following examples show how to use cn.nukkit.level.Level#BLOCK_UPDATE_RANDOM . 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: BlockMycelium.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_RANDOM) {
        //TODO: light levels
        NukkitRandom random = new NukkitRandom();
        x = random.nextRange((int) x - 1, (int) x + 1);
        y = random.nextRange((int) y - 1, (int) y + 1);
        z = random.nextRange((int) z - 1, (int) z + 1);
        Block block = this.getLevel().getBlock(new Vector3(x, y, z));
        if (block.getId() == Block.DIRT) {
            if (block.up() instanceof BlockTransparent) {
                BlockSpreadEvent ev = new BlockSpreadEvent(block, this, new BlockMycelium());
                Server.getInstance().getPluginManager().callEvent(ev);
                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(block, ev.getNewState());
                }
            }
        }
    }
    return 0;
}
 
Example 2
Source File: BlockLeaves.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_RANDOM && !isPersistent() && !isCheckDecay()) {
        setCheckDecay(true);
        getLevel().setBlock(this, this, false, false);
    } else if (type == Level.BLOCK_UPDATE_RANDOM && isCheckDecay() && !isPersistent()) {
        setDamage(getDamage() & 0x03);
        int check = 0;

        LeavesDecayEvent ev = new LeavesDecayEvent(this);

        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled() || findLog(this, new LongArraySet(), 0, check)) {
            getLevel().setBlock(this, this, false, false);
        } else {
            getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example 3
Source File: BlockSapling.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().isTransparent()) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) { //Growth
        if (new NukkitRandom().nextRange(1, 7) == 1) {
            if ((this.meta & 0x08) == 0x08) {
                ObjectTree.growTree(this.getLevel(), (int) this.x, (int) this.y, (int) this.z, new NukkitRandom(), this.meta & 0x07);
            } else {
                this.meta |= 0x08;
                this.getLevel().setBlock(this, this, true);
                return Level.BLOCK_UPDATE_RANDOM;
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }
    return 1;
}
 
Example 4
Source File: BlockSnowLayer.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().isTransparent()) {
            this.getLevel().useBreakOn(this);

            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.getLevel().getBlockLightAt((int) this.x, (int) this.y, (int) this.z) >= 10) {
            this.getLevel().setBlock(this, new BlockAir(), true);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example 5
Source File: BlockSnowLayer.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().isTransparent()) {
            this.getLevel().useBreakOn(this);

            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.getLevel().getBlockLightAt((int) this.x, (int) this.y, (int) this.z) >= 10) {
            this.getLevel().setBlock(this, new BlockAir(), true);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example 6
Source File: BlockSnowLayer.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    super.onUpdate(type);
    if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.getLevel().getBlockLightAt((int) this.x, (int) this.y, (int) this.z) >= 10) {
            BlockFadeEvent event = new BlockFadeEvent(this, get(AIR));
            level.getServer().getPluginManager().callEvent(event);
            if (!event.isCancelled()) {
                level.setBlock(this, event.getNewState(), true);
            }
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example 7
Source File: BlockCrops.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().getId() != FARMLAND) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(2) == 1) {
            if (this.getDamage() < 0x07) {
                BlockCrops block = (BlockCrops) this.clone();
                block.setDamage(block.getDamage() + 1);
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), false, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 8
Source File: BlockNetherWart.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().getId() != SOUL_SAND) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(10) == 1) {
            if (this.getDamage() < 0x03) {
                BlockNetherWart block = (BlockNetherWart) this.clone();
                block.setDamage(block.getDamage() + 1);
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 9
Source File: BlockOreRedstoneGlowing.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_SCHEDULED || type == Level.BLOCK_UPDATE_RANDOM) {
        BlockFadeEvent event = new BlockFadeEvent(this, get(REDSTONE_ORE));
        level.getServer().getPluginManager().callEvent(event);
        if (!event.isCancelled()) {
            level.setBlock(this, event.getNewState(), false, false);
        }

        return Level.BLOCK_UPDATE_WEAK;
    }

    return 0;
}
 
Example 10
Source File: BlockCocoa.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = new int[]{
                3, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 5
        };

        Block side = this.getSide(BlockFace.fromIndex(faces[this.getDamage()]));

        if (side.getId() != Block.WOOD && side.getDamage() != BlockWood.JUNGLE) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(2) == 1) {
            if (this.getDamage() / 4 < 2) {
                BlockCocoa block = (BlockCocoa) this.clone();
                block.setDamage(block.getDamage() + 4);
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 11
Source File: BlockCrops.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().getId() != FARMLAND) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (ThreadLocalRandom.current().nextInt(2) == 1) {
            if (this.getDamage() < 0x07) {
                BlockCrops block = (BlockCrops) this.clone();
                block.setDamage(block.getDamage() + 1);
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), false, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 12
Source File: BlockSugarcane.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        Block down = this.down();
        if (down.isTransparent() && down.getId() != SUGARCANE_BLOCK) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.down().getId() != SUGARCANE_BLOCK) {
            if (this.getDamage() == 0x0F) {
                for (int y = 1; y < 3; ++y) {
                    Block b = this.getLevel().getBlock(new Vector3(this.x, this.y + y, this.z));
                    if (b.getId() == AIR) {
                        BlockGrowEvent ev = new BlockGrowEvent(b, Block.get(BlockID.SUGARCANE_BLOCK));
                        Server.getInstance().getPluginManager().callEvent(ev);
                        
                        if (!ev.isCancelled()) {
                            this.getLevel().setBlock(b, Block.get(BlockID.SUGARCANE_BLOCK), false);
                        }
                        break;
                    }
                }
                this.setDamage(0);
                this.getLevel().setBlock(this, this, false);
            } else {
                this.setDamage(this.getDamage() + 1);
                this.getLevel().setBlock(this, this, false);
            }
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }
    return 0;
}
 
Example 13
Source File: BlockOreRedstoneGlowing.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_SCHEDULED || type == Level.BLOCK_UPDATE_RANDOM) {
        this.getLevel().setBlock(this, new BlockOreRedstone(), false, false);

        return Level.BLOCK_UPDATE_WEAK;
    }

    return 0;
}
 
Example 14
Source File: BlockCocoa.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        int[] faces = new int[]{
                3, 4, 2, 5, 3, 4, 2, 5, 3, 4, 2, 5
        };

        Block side = this.getSide(BlockFace.fromIndex(faces[this.getDamage()]));

        if (side.getId() != Block.WOOD && side.getDamage() != BlockWood.JUNGLE) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (ThreadLocalRandom.current().nextInt(2) == 1) {
            if (this.getDamage() / 4 < 2) {
                BlockCocoa block = (BlockCocoa) this.clone();
                block.setDamage(block.getDamage() + 4);
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 15
Source File: BlockNetherWart.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().getId() != SOUL_SAND) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(10) == 1) {
            if (this.meta < 0x03) {
                BlockNetherWart block = (BlockNetherWart) this.clone();
                ++block.meta;
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 16
Source File: BlockCactus.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        Block down = down();
        if (down.getId() != SAND && down.getId() != CACTUS) {
            this.getLevel().useBreakOn(this);
        } else {
            for (int side = 2; side <= 5; ++side) {
                Block block = getSide(BlockFace.fromIndex(side));
                if (!block.canBeFlowedInto()) {
                    this.getLevel().useBreakOn(this);
                }
            }
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (down().getId() != CACTUS) {
            if (this.getDamage() == 0x0F) {
                for (int y = 1; y < 3; ++y) {
                    Block b = this.getLevel().getBlock(new Vector3(this.x, this.y + y, this.z));
                    if (b.getId() == AIR) {
                        BlockGrowEvent event = new BlockGrowEvent(b, new BlockCactus());
                        Server.getInstance().getPluginManager().callEvent(event);
                        if (!event.isCancelled()) {
                            this.getLevel().setBlock(b, event.getNewState(), true);
                        }
                    }
                }
                this.setDamage(0);
                this.getLevel().setBlock(this, this);
            } else {
                this.setDamage(this.getDamage() + 1);
                this.getLevel().setBlock(this, this);
            }
        }
    }

    return 0;
}
 
Example 17
Source File: BlockSugarcane.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        Block down = this.down();
        if (down.isTransparent() && down.getId() != SUGARCANE_BLOCK) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.down().getId() != SUGARCANE_BLOCK) {
            if (this.getDamage() == 0x0F) {
                for (int y = 1; y < 3; ++y) {
                    Block b = this.getLevel().getBlock(new Vector3(this.x, this.y + y, this.z));
                    if (b.getId() == AIR) {
                        this.getLevel().setBlock(b, new BlockSugarcane(), false);
                        break;
                    }
                }
                this.setDamage(0);
                this.getLevel().setBlock(this, this, false);
            } else {
                this.setDamage(this.getDamage() + 1);
                this.getLevel().setBlock(this, this, false);
            }
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }
    return 0;
}
 
Example 18
Source File: BlockIce.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (this.getLevel().getBlockLightAt((int) this.x, (int) this.y, (int) this.z) >= 12) {
            this.getLevel().setBlock(this, new BlockWater(), true);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    }
    return 0;
}
 
Example 19
Source File: BlockCrops.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL) {
        if (this.down().getId() != FARMLAND) {
            this.getLevel().useBreakOn(this);
            return Level.BLOCK_UPDATE_NORMAL;
        }
    } else if (type == Level.BLOCK_UPDATE_RANDOM) {
        if (new Random().nextInt(2) == 1) {
            if (this.meta < 0x07) {
                BlockCrops block = (BlockCrops) this.clone();
                ++block.meta;
                BlockGrowEvent ev = new BlockGrowEvent(this, block);
                Server.getInstance().getPluginManager().callEvent(ev);

                if (!ev.isCancelled()) {
                    this.getLevel().setBlock(this, ev.getNewState(), true, true);
                } else {
                    return Level.BLOCK_UPDATE_RANDOM;
                }
            }
        } else {
            return Level.BLOCK_UPDATE_RANDOM;
        }
    }

    return 0;
}
 
Example 20
Source File: BlockFire.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type == Level.BLOCK_UPDATE_NORMAL || type == Level.BLOCK_UPDATE_RANDOM) {
        if (!this.isBlockTopFacingSurfaceSolid(this.down()) && !this.canNeighborBurn()) {
            this.getLevel().setBlock(this, new BlockAir(), true);
        }

        return Level.BLOCK_UPDATE_NORMAL;
    } else if (type == Level.BLOCK_UPDATE_SCHEDULED && this.level.gameRules.getBoolean(GameRule.DO_FIRE_TICK)) {
        boolean forever = this.down().getId() == Block.NETHERRACK;

        ThreadLocalRandom random = ThreadLocalRandom.current();

        //TODO: END

        if (!this.isBlockTopFacingSurfaceSolid(this.down()) && !this.canNeighborBurn()) {
            this.getLevel().setBlock(this, new BlockAir(), true);
        }

        if (!forever && this.getLevel().isRaining() &&
                (this.getLevel().canBlockSeeSky(this) ||
                        this.getLevel().canBlockSeeSky(this.east()) ||
                        this.getLevel().canBlockSeeSky(this.west()) ||
                        this.getLevel().canBlockSeeSky(this.south()) ||
                        this.getLevel().canBlockSeeSky(this.north()))
                ) {
            this.getLevel().setBlock(this, new BlockAir(), true);
        } else {
            int meta = this.getDamage();

            if (meta < 15) {
                this.setDamage(meta + random.nextInt(3));
                this.getLevel().setBlock(this, this, true);
            }

            this.getLevel().scheduleUpdate(this, this.tickRate() + random.nextInt(10));

            if (!forever && !this.canNeighborBurn()) {
                if (!this.isBlockTopFacingSurfaceSolid(this.down()) || meta > 3) {
                    this.getLevel().setBlock(this, new BlockAir(), true);
                }
            } else if (!forever && !(this.down().getBurnAbility() > 0) && meta == 15 && random.nextInt(4) == 0) {
                this.getLevel().setBlock(this, new BlockAir(), true);
            } else {
                int o = 0;

                //TODO: decrease the o if the rainfall values are high

                this.tryToCatchBlockOnFire(this.east(), 300 + o, meta);
                this.tryToCatchBlockOnFire(this.west(), 300 + o, meta);
                this.tryToCatchBlockOnFire(this.down(), 250 + o, meta);
                this.tryToCatchBlockOnFire(this.up(), 250 + o, meta);
                this.tryToCatchBlockOnFire(this.south(), 300 + o, meta);
                this.tryToCatchBlockOnFire(this.north(), 300 + o, meta);

                for (int x = (int) (this.x - 1); x <= (int) (this.x + 1); ++x) {
                    for (int z = (int) (this.z - 1); z <= (int) (this.z + 1); ++z) {
                        for (int y = (int) (this.y - 1); y <= (int) (this.y + 4); ++y) {
                            if (x != (int) this.x || y != (int) this.y || z != (int) this.z) {
                                int k = 100;

                                if (y > this.y + 1) {
                                    k += (y - (this.y + 1)) * 100;
                                }

                                Block block = this.getLevel().getBlock(new Vector3(x, y, z));
                                int chance = this.getChanceOfNeighborsEncouragingFire(block);

                                if (chance > 0) {
                                    int t = (chance + 40 + this.getLevel().getServer().getDifficulty() * 7) / (meta + 30);

                                    //TODO: decrease the t if the rainfall values are high

                                    if (t > 0 && random.nextInt(k) <= t) {
                                        int damage = meta + random.nextInt(5) / 4;

                                        if (damage > 15) {
                                            damage = 15;
                                        }

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

                                        if (!e.isCancelled()) {
                                            this.getLevel().setBlock(block, new BlockFire(damage), true);
                                            this.getLevel().scheduleUpdate(block, this.tickRate());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}