Search in sources :

Example 6 with BlockStateDelegate

use of net.glowstone.util.BlockStateDelegate in project Glowstone by GlowstoneMC.

the class TreeDecorator method decorate.

@Override
public void decorate(World world, Random random, Chunk source) {
    int sourceX = (source.getX() << 4) + random.nextInt(16);
    int sourceZ = (source.getZ() << 4) + random.nextInt(16);
    Block sourceBlock = world.getBlockAt(sourceX, world.getHighestBlockYAt(sourceX, sourceZ), sourceZ);
    Class<? extends GenericTree> clazz = getRandomTree(random, trees);
    if (clazz != null) {
        BlockStateDelegate delegate = new BlockStateDelegate();
        GenericTree tree;
        try {
            Constructor<? extends GenericTree> c = clazz.getConstructor(Random.class, Location.class, BlockStateDelegate.class);
            tree = c.newInstance(random, sourceBlock.getLocation(), delegate);
        } catch (Exception ex) {
            tree = new GenericTree(random, sourceBlock.getLocation(), delegate);
        }
        if (tree.generate()) {
            delegate.updateBlockStates();
        }
    }
}
Also used : BlockStateDelegate(net.glowstone.util.BlockStateDelegate) GenericTree(net.glowstone.generator.objects.trees.GenericTree) Block(org.bukkit.block.Block)

Example 7 with BlockStateDelegate

use of net.glowstone.util.BlockStateDelegate in project Glowstone by GlowstoneMC.

the class BlockSapling method generateTree.

private void generateTree(TreeType type, GlowBlock block, GlowPlayer player) {
    // get data but filters sapling age
    int data = block.getData() & 0x7;
    // replaces the sapling block(s)
    block.setType(Material.AIR);
    if (type == TreeType.JUNGLE || type == TreeType.MEGA_REDWOOD || type == TreeType.DARK_OAK) {
        block.getRelative(BlockFace.SOUTH).setType(Material.AIR);
        block.getRelative(BlockFace.EAST).setType(Material.AIR);
        block.getRelative(BlockFace.SOUTH_EAST).setType(Material.AIR);
    }
    // try to generate a tree
    Location loc = block.getLocation();
    BlockStateDelegate blockStateDelegate = new BlockStateDelegate();
    boolean canGrow = false;
    if (GlowTree.newInstance(type, random, loc, blockStateDelegate).generate()) {
        List<BlockState> blockStates = new ArrayList<>(blockStateDelegate.getBlockStates());
        StructureGrowEvent growEvent = new StructureGrowEvent(loc, type, player != null, player, blockStates);
        EventFactory.callEvent(growEvent);
        if (!growEvent.isCancelled()) {
            canGrow = true;
            for (BlockState state : blockStates) {
                state.update(true);
            }
        }
    }
    if (!canGrow) {
        // places the sapling block(s) back if the tree was not generated
        // the sapling ages are overwritten but this is an expected
        // vanilla behavior
        block.setType(Material.SAPLING);
        block.setData((byte) data);
        if (type == TreeType.JUNGLE || type == TreeType.MEGA_REDWOOD || type == TreeType.DARK_OAK) {
            block.getRelative(BlockFace.SOUTH).setType(Material.SAPLING);
            block.getRelative(BlockFace.SOUTH).setData((byte) data);
            block.getRelative(BlockFace.EAST).setType(Material.SAPLING);
            block.getRelative(BlockFace.EAST).setData((byte) data);
            block.getRelative(BlockFace.SOUTH_EAST).setType(Material.SAPLING);
            block.getRelative(BlockFace.SOUTH_EAST).setData((byte) data);
        }
    }
}
Also used : BlockStateDelegate(net.glowstone.util.BlockStateDelegate) BlockState(org.bukkit.block.BlockState) ArrayList(java.util.ArrayList) StructureGrowEvent(org.bukkit.event.world.StructureGrowEvent) Location(org.bukkit.Location)

Example 8 with BlockStateDelegate

use of net.glowstone.util.BlockStateDelegate in project Glowstone by GlowstoneMC.

the class StructurePopulator method populate.

@Override
public void populate(World world, Random random, Chunk source) {
    if (world.canGenerateStructures()) {
        int cx = source.getX();
        int cz = source.getZ();
        random.setSeed(world.getSeed());
        long xRand = random.nextLong();
        long zRand = random.nextLong();
        boolean placed = false;
        for (int x = cx - 8; x <= cx + 8 && !placed; x++) {
            for (int z = cz - 8; z <= cz + 8 && !placed; z++) {
                if (world.getChunkAt(x, z).isLoaded() || world.getChunkAt(x, z).load(true)) {
                    random.setSeed(x * xRand + z * zRand ^ world.getSeed());
                    Map<Integer, GlowStructure> structures = ((GlowWorld) world).getStructures();
                    int key = new Key(x, z).hashCode();
                    if (!structures.containsKey(key)) {
                        for (StructureStore<?> store : StructureStorage.getStructureStores()) {
                            GlowStructure structure = store.createNewStructure((GlowWorld) world, random, x, z);
                            if (structure.shouldGenerate(random)) {
                                structure.setDirty(true);
                                structures.put(key, structure);
                                GlowServer.logger.finer("structure in chunk " + x + "," + z);
                                placed = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
        int x = cx << 4;
        int z = cz << 4;
        Iterator<Entry<Integer, GlowStructure>> it = ((GlowWorld) world).getStructures().entrySet().iterator();
        while (it.hasNext()) {
            GlowStructure structure = it.next().getValue();
            if (structure.getBoundingBox().intersectsWith(x, z, x + 15, z + 15)) {
                BlockStateDelegate delegate = new BlockStateDelegate();
                if (structure.generate(random, x, z, delegate)) {
                    // maybe later trigger a StructureGeneratedEvent event and cancel
                    delegate.updateBlockStates();
                } else {
                    delegate.rollbackBlockStates();
                    it.remove();
                }
            }
        }
    }
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) Entry(java.util.Map.Entry) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) GlowWorld(net.glowstone.GlowWorld) Key(net.glowstone.chunk.GlowChunk.Key)

Aggregations

BlockStateDelegate (net.glowstone.util.BlockStateDelegate)8 Location (org.bukkit.Location)4 BlockState (org.bukkit.block.BlockState)3 ArrayList (java.util.ArrayList)2 StructureBoundingBox (net.glowstone.generator.structures.util.StructureBoundingBox)2 Block (org.bukkit.block.Block)2 StructureGrowEvent (org.bukkit.event.world.StructureGrowEvent)2 Vector (org.bukkit.util.Vector)2 Entry (java.util.Map.Entry)1 GlowWorld (net.glowstone.GlowWorld)1 GlowBlockState (net.glowstone.block.GlowBlockState)1 Key (net.glowstone.chunk.GlowChunk.Key)1 GenericTree (net.glowstone.generator.objects.trees.GenericTree)1 JungleBush (net.glowstone.generator.objects.trees.JungleBush)1 GlowDesertWell (net.glowstone.generator.structures.GlowDesertWell)1 GlowDungeon (net.glowstone.generator.structures.GlowDungeon)1 GlowStructure (net.glowstone.generator.structures.GlowStructure)1 TreeType (org.bukkit.TreeType)1 SimplexNoiseGenerator (org.bukkit.util.noise.SimplexNoiseGenerator)1