Search in sources :

Example 1 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockFire method updateBlock.

@Override
public void updateBlock(GlowBlock block) {
    if (!block.getWorld().getGameRuleMap().getBoolean("doFireTick")) {
        return;
    }
    GlowWorld world = block.getWorld();
    Material type = block.getRelative(BlockFace.DOWN).getType();
    boolean isInfiniteFire;
    switch(type) {
        case NETHERRACK:
            isInfiniteFire = true;
            break;
        case BEDROCK:
            if (world.getEnvironment() == Environment.THE_END) {
                isInfiniteFire = true;
                break;
            }
        default:
            isInfiniteFire = false;
            break;
    }
    if (!isInfiniteFire && world.hasStorm() && isRainingAround(block)) {
        // if it's raining around, stop fire
        block.breakNaturally();
        return;
    }
    // increase fire age
    GlowBlockState state = block.getState();
    int age = state.getRawData();
    if (age < MAX_FIRE_AGE) {
        // increase fire age
        state.setRawData((byte) (age + random.nextInt(3) / 2));
        state.update(true);
    }
    if (!isInfiniteFire) {
        if (!hasNearFlammableBlock(block)) {
            // there's no flammable blocks around, stop fire
            if (age > 3 || block.getRelative(BlockFace.DOWN).isEmpty()) {
                block.breakNaturally();
                world.cancelPulse(block);
            }
        } else if (age == MAX_FIRE_AGE && !block.getRelative(BlockFace.DOWN).isFlammable() && random.nextInt(4) == 0) {
            // if fire reached max age, bottom block is not flammable, 25% chance to stop fire
            block.breakNaturally();
            world.cancelPulse(block);
        } else {
            // fire propagation / block burning
            // burn blocks around
            boolean isWet = GlowBiomeClimate.isWet(block);
            for (Entry<BlockFace, Integer> entry : BURNRESISTANCE_MAP.entrySet()) {
                burnBlock(block.getRelative(entry.getKey()), entry.getValue() - (isWet ? 50 : 0), age);
            }
            Difficulty difficulty = world.getDifficulty();
            int difficultyModifier;
            switch(difficulty) {
                case EASY:
                    difficultyModifier = 7;
                    break;
                case NORMAL:
                    difficultyModifier = 14;
                    break;
                case HARD:
                    difficultyModifier = 21;
                    break;
                default:
                    difficultyModifier = 0;
                    break;
            }
            // try to propagate fire in a 3x3x6 box
            for (int x = -1; x <= 1; x++) {
                for (int z = -1; z <= 1; z++) {
                    for (int y = -1; y <= 4; y++) {
                        if (x != 0 || z != 0 || y != 0) {
                            GlowBlock propagationBlock = world.getBlockAt(block.getLocation().add(x, y, z));
                            int flameResistance = getFlameResistance(propagationBlock);
                            if (flameResistance > 0) {
                                int resistance = (40 + difficultyModifier + flameResistance) / (30 + age);
                                if (isWet) {
                                    resistance /= 2;
                                }
                                if ((!world.hasStorm() || !isRainingAround(propagationBlock)) && resistance > 0 && random.nextInt(y > 1 ? 100 + 100 * (y - 1) : 100) <= resistance) {
                                    BlockIgniteEvent igniteEvent = new BlockIgniteEvent(propagationBlock, IgniteCause.SPREAD, block);
                                    EventFactory.callEvent(igniteEvent);
                                    if (!igniteEvent.isCancelled()) {
                                        if (propagationBlock.getType() == Material.TNT) {
                                            BlockTNT.igniteBlock(propagationBlock, false);
                                        } else {
                                            int increasedAge = increaseFireAge(age);
                                            state = propagationBlock.getState();
                                            state.setType(Material.FIRE);
                                            state.setRawData((byte) (increasedAge > MAX_FIRE_AGE ? MAX_FIRE_AGE : increasedAge));
                                            state.update(true);
                                            world.requestPulse(propagationBlock);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) Difficulty(org.bukkit.Difficulty) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) BlockIgniteEvent(org.bukkit.event.block.BlockIgniteEvent)

Example 2 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockGrass method grow.

@Override
public void grow(GlowPlayer player, GlowBlock block) {
    GlowWorld world = block.getWorld();
    int i = 0;
    do {
        int j = 0;
        while (true) {
            // if there's available space
            if (block.getRelative(BlockFace.UP).getType() == Material.AIR) {
                GlowBlock b = block.getRelative(BlockFace.UP);
                GlowBlockState blockState = b.getState();
                if (random.nextFloat() < 0.125D) {
                    // sometimes grow random flower
                    // would be better to call a method that choose a random
                    // flower depending on the biome
                    FlowerType[] flowers = FlowerForestPopulator.FLOWERS;
                    Material flower = flowers[random.nextInt(flowers.length)].getType();
                    if (ItemTable.instance().getBlock(flower).canPlaceAt(b, BlockFace.DOWN)) {
                        blockState.setType(flower);
                    }
                } else {
                    Material tallGrass = Material.LONG_GRASS;
                    if (ItemTable.instance().getBlock(tallGrass).canPlaceAt(b, BlockFace.DOWN)) {
                        // grow tall grass if possible
                        blockState.setType(tallGrass);
                        blockState.setData(new LongGrass(GrassSpecies.NORMAL));
                    }
                }
                BlockGrowEvent growEvent = new BlockGrowEvent(b, blockState);
                EventFactory.callEvent(growEvent);
                if (!growEvent.isCancelled()) {
                    blockState.update(true);
                }
            } else if (j < i / 16) {
                // look around for grass block
                int x = block.getX();
                int y = block.getY();
                int z = block.getZ();
                x += random.nextInt(3) - 1;
                y += random.nextInt(3) * random.nextInt(3) / 2;
                z += random.nextInt(3) - 1;
                if (world.getBlockAt(x, y, z).getType() == Material.GRASS) {
                    j++;
                    continue;
                }
            }
            i++;
            break;
        }
    } while (i < 128);
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) FlowerType(net.glowstone.generator.objects.FlowerType) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) LongGrass(org.bukkit.material.LongGrass) BlockGrowEvent(org.bukkit.event.block.BlockGrowEvent)

Example 3 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockLog2 method blockDestroy.

@Override
public void blockDestroy(GlowPlayer player, GlowBlock block, BlockFace face) {
    // vanilla set leaf decay check in a 9x9x9 neighboring when a log block is removed
    GlowWorld world = block.getWorld();
    for (int x = 0; x < 9; x++) {
        for (int z = 0; z < 9; z++) {
            for (int y = 0; y < 9; y++) {
                GlowBlock b = world.getBlockAt(block.getLocation().add(x - 4, y - 4, z - 4));
                if (b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) {
                    GlowBlockState state = b.getState();
                    if ((state.getRawData() & 0x08) == 0 && (state.getRawData() & 0x04) == 0) {
                        // check decay is off and decay is on
                        // set decay check on for this leaves block
                        state.setRawData((byte) (state.getRawData() | 0x08));
                        state.update(true);
                    }
                }
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld)

Example 4 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockMushroom method updateBlock.

@Override
public void updateBlock(GlowBlock block) {
    if (random.nextInt(25) == 0) {
        GlowWorld world = block.getWorld();
        int x, y, z;
        int i = 0;
        for (x = block.getX() - 4; x <= block.getX() + 4; x++) {
            for (z = block.getZ() - 4; z <= block.getZ() + 4; z++) {
                for (y = block.getY() - 1; y <= block.getY() + 1; y++) {
                    if (world.getBlockAt(x, y, z).getType() == mushroomType) {
                        if (++i > 4) {
                            return;
                        }
                    }
                }
            }
        }
        int nX, nY, nZ;
        nX = block.getX() + random.nextInt(3) - 1;
        nY = block.getY() + random.nextInt(2) - random.nextInt(2);
        nZ = block.getZ() + random.nextInt(3) - 1;
        x = block.getX();
        y = block.getY();
        z = block.getZ();
        for (i = 0; i < 4; i++) {
            if (world.getBlockAt(nX, nY, nZ).getType() == Material.AIR && canPlaceAt(world.getBlockAt(nX, nY, nZ), BlockFace.DOWN)) {
                x = nX;
                y = nY;
                z = nZ;
            }
            nX = x + random.nextInt(3) - 1;
            nY = y + random.nextInt(2) - random.nextInt(2);
            nZ = z + random.nextInt(3) - 1;
        }
        if (world.getBlockAt(nX, nY, nZ).getType() == Material.AIR && canPlaceAt(world.getBlockAt(nX, nY, nZ), BlockFace.DOWN)) {
            GlowBlockState state = world.getBlockAt(nX, nY, nZ).getState();
            state.setType(mushroomType);
            BlockSpreadEvent spreadEvent = new BlockSpreadEvent(state.getBlock(), block, state);
            EventFactory.callEvent(spreadEvent);
            if (!spreadEvent.isCancelled()) {
                state.update(true);
            }
        }
    }
    // it is stated in the wiki
    if (!canPlaceAt(block, BlockFace.DOWN)) {
        block.breakNaturally();
    }
}
Also used : BlockSpreadEvent(org.bukkit.event.block.BlockSpreadEvent) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld)

Example 5 with GlowWorld

use of net.glowstone.GlowWorld in project Glowstone by GlowstoneMC.

the class BlockLog method blockDestroy.

@Override
public void blockDestroy(GlowPlayer player, GlowBlock block, BlockFace face) {
    // vanilla set leaf decay check in a 9x9x9 neighboring when a log block is removed
    GlowWorld world = block.getWorld();
    for (int x = 0; x < 9; x++) {
        for (int z = 0; z < 9; z++) {
            for (int y = 0; y < 9; y++) {
                GlowBlock b = world.getBlockAt(block.getLocation().add(x - 4, y - 4, z - 4));
                if (b.getType() == Material.LEAVES || b.getType() == Material.LEAVES_2) {
                    GlowBlockState state = b.getState();
                    if ((state.getRawData() & 0x08) == 0 && (state.getRawData() & 0x04) == 0) {
                        // check decay is off and decay is on
                        // set decay check on for this leaves block
                        state.setRawData((byte) (state.getRawData() | 0x08));
                        state.update(true);
                    }
                }
            }
        }
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlockState(net.glowstone.block.GlowBlockState) GlowWorld(net.glowstone.GlowWorld)

Aggregations

GlowWorld (net.glowstone.GlowWorld)23 GlowBlock (net.glowstone.block.GlowBlock)14 GlowBlockState (net.glowstone.block.GlowBlockState)10 GlowPlayer (net.glowstone.entity.GlowPlayer)3 Location (org.bukkit.Location)3 BlockSpreadEvent (org.bukkit.event.block.BlockSpreadEvent)3 Entry (java.util.Map.Entry)2 BlockType (net.glowstone.block.blocktype.BlockType)2 Material (org.bukkit.Material)2 BlockFace (org.bukkit.block.BlockFace)2 BlockFadeEvent (org.bukkit.event.block.BlockFadeEvent)2 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)2 ItemStack (org.bukkit.inventory.ItemStack)2 MaterialData (org.bukkit.material.MaterialData)2 Vector (org.bukkit.util.Vector)2 Message (com.flowpowered.networking.Message)1 Arrays (java.util.Arrays)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1 ItemTimedUsage (net.glowstone.block.itemtype.ItemTimedUsage)1