use of ivorius.reccomplex.block.GeneratingTileEntity in project RecurrentComplex by Ivorforce.
the class GenericStructure method generate.
@Override
public void generate(@Nonnull final StructureSpawnContext context, @Nonnull InstanceData instanceData, @Nonnull TransformerMulti foreignTransformer) {
WorldServer world = context.environment.world;
IvWorldData worldData = constructWorldData();
boolean asSource = context.generateAsSource;
RunTransformer transformer = getRunTransformer(instanceData, foreignTransformer, asSource);
instanceData.variableDomain.fill(context.environment.variables);
// The world initializes the block event array after it generates the world - in the constructor
// This hackily sets the field to a temporary value. Yay.
// Hax
RCAccessorWorldServer.ensureBlockEventArray(world);
IvBlockCollection blockCollection = worldData.blockCollection;
int[] areaSize = new int[] { blockCollection.width, blockCollection.height, blockCollection.length };
BlockPos origin = StructureBoundingBoxes.min(context.boundingBox);
Map<BlockPos, GeneratingTileEntity<?>> generatingTEs = new HashMap<>();
Map<BlockPos, NBTTagCompound> tileEntityCompounds = new HashMap<>();
for (NBTTagCompound tileEntityCompound : worldData.tileEntities) {
BlockPos src = RCMover.getTileEntityPos(tileEntityCompound);
tileEntityCompounds.put(src, tileEntityCompound);
asGeneratingTileEntity(context, world, areaSize, origin, tileEntityCompound, generatingTEs::put);
}
if (transformer != null)
transformer.transformer.transform(transformer.instanceData, Transformer.Phase.BEFORE, context, worldData, transformer);
StructureBoundingBox relevantSourceArea = context.intersection(BlockAreas.toBoundingBox(blockCollection.area()));
if (// Why did we get asked to generate again?
relevantSourceArea != null) {
BlockPos.MutableBlockPos worldPos = new BlockPos.MutableBlockPos();
for (int pass = 0; pass < 2; pass++) {
for (BlockPos sourcePos : RCStructureBoundingBoxes.mutablePositions(relevantSourceArea)) {
IvMutableBlockPos.add(context.transform.applyOn(sourcePos, worldPos, areaSize), origin);
if (context.includesComplex(worldPos)) {
IBlockState state = PosTransformer.transformBlockState(blockCollection.getBlockState(sourcePos), context.transform);
if (RecurrentComplex.specialRegistry.isSafe(state.getBlock()) && pass == getPass(state) && (transformer == null || !transformer.transformer.skipGeneration(transformer.instanceData, context, worldPos, state, worldData, sourcePos))) {
GeneratingTileEntity<?> generatingTE = generatingTEs.get(sourcePos);
//noinspection unchecked
if (asSource || generatingTE == null || ((GeneratingTileEntity) generatingTE).shouldPlaceInWorld(context, instanceData.tileEntities.get(sourcePos)))
setBlock(context, areaSize, worldPos, state, () -> tileEntityCompounds.get(sourcePos));
else
// Replace with air
context.setBlock(worldPos, Blocks.AIR.getDefaultState(), 2);
}
}
}
}
}
if (transformer != null)
transformer.transformer.transform(transformer.instanceData, Transformer.Phase.AFTER, context, worldData, transformer);
for (NBTTagCompound entityCompound : worldData.entities) {
double[] transformedEntityPos = context.transform.applyOn(getEntityPos(entityCompound), areaSize);
if (context.includes(new Vec3i(transformedEntityPos[0] + origin.getX(), transformedEntityPos[1] + origin.getY(), transformedEntityPos[2] + origin.getZ()))) {
Entity entity = EntityList.createEntityFromNBT(entityCompound, world);
if (entity != null) {
PosTransformer.transformEntityPos(entity, context.transform, areaSize);
Mover.moveEntity(entity, origin);
RCAccessorEntity.setEntityUniqueID(entity, UUID.randomUUID());
generateEntityContents(context, entity);
world.spawnEntity(entity);
} else {
RecurrentComplex.logger.error("Error loading entity with ID '" + entityCompound.getString("id") + "'");
}
}
}
if (!asSource) {
if (context.generationLayer < MAX_GENERATING_LAYERS) {
generatingTEs.entrySet().forEach(entry -> {
NBTStorable teData = instanceData.tileEntities.get(entry.getKey());
if (teData != null)
((GeneratingTileEntity) entry.getValue()).generate(context, teData);
});
} else {
RecurrentComplex.logger.warn("Structure generated with over " + MAX_GENERATING_LAYERS + " layers; most likely infinite loop!");
}
}
}
use of ivorius.reccomplex.block.GeneratingTileEntity in project RecurrentComplex by Ivorforce.
the class GenericStructure method asGeneratingTileEntity.
protected static void asGeneratingTileEntity(@Nonnull StructureContext context, World world, int[] areaSize, BlockPos origin, NBTTagCompound tileEntityCompound, BiConsumer<BlockPos, GeneratingTileEntity> consumer) {
BlockPos src = RCMover.getTileEntityPos(tileEntityCompound);
BlockPos dest = context.transform.apply(src, areaSize).add(origin);
tileEntityCompound = RCMover.setTileEntityPos(tileEntityCompound, dest);
TileEntity tileEntity = RecurrentComplex.specialRegistry.loadTileEntity(world, tileEntityCompound);
if (tileEntity instanceof GeneratingTileEntity) {
RCPosTransformer.transformAdditionalData(tileEntity, context.transform, areaSize);
RCMover.moveAdditionalData(tileEntity, origin);
consumer.accept(src, (GeneratingTileEntity) tileEntity);
}
}
Aggregations