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();
}
}
}
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);
}
}
}
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();
}
}
}
}
}
Aggregations