Search in sources :

Example 1 with Structure

use of com.wuest.prefab.StructureGen.Structure in project MC-Prefab by Brian-Wuest.

the class ModEventHandler method onServerTick.

/**
 * This event is primarily used to build 100 blocks for any queued structures for all players.
 * @param event The event object.
 */
@SubscribeEvent
public static void onServerTick(ServerTickEvent event) {
    ArrayList<EntityPlayer> playersToRemove = new ArrayList<EntityPlayer>();
    for (Entry<EntityPlayer, ArrayList<Structure>> entry : ModEventHandler.structuresToBuild.entrySet()) {
        ArrayList<Structure> structuresToRemove = new ArrayList<Structure>();
        EntityPlayer player = entry.getKey();
        // Build the first 100 blocks of each structure for this player.
        for (Structure structure : entry.getValue()) {
            for (int i = 0; i < 100; i++) {
                // Structure clearing happens before anything else.
                if (structure.clearedBlockPos.size() > 0) {
                    BlockPos currentPos = structure.clearedBlockPos.get(0);
                    structure.clearedBlockPos.remove(0);
                    IBlockState clearBlockState = structure.world.getBlockState(currentPos);
                    // This will also break other mod's logic blocks but they would probably be broken due to structure generation anyways.
                    if (clearBlockState.getBlock() != Blocks.AIR) {
                        structure.BeforeClearSpaceBlockReplaced(currentPos);
                        structure.world.setBlockToAir(currentPos);
                    } else {
                        // This is just an air block, move onto the next block don't need to wait for the next tick.
                        i--;
                    }
                    continue;
                }
                BuildBlock currentBlock = null;
                if (structure.priorityOneBlocks.size() > 0) {
                    currentBlock = structure.priorityOneBlocks.get(0);
                    structure.priorityOneBlocks.remove(0);
                } else if (structure.priorityTwoBlocks.size() > 0) {
                    currentBlock = structure.priorityTwoBlocks.get(0);
                    structure.priorityTwoBlocks.remove(0);
                } else if (structure.priorityThreeBlocks.size() > 0) {
                    currentBlock = structure.priorityThreeBlocks.get(0);
                    structure.priorityThreeBlocks.remove(0);
                } else {
                    // There are no more blocks to set.
                    structuresToRemove.add(structure);
                    break;
                }
                IBlockState state = currentBlock.getBlockState();
                BuildingMethods.ReplaceBlock(structure.world, currentBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing), state);
                // After placing the initial block, set the sub-block. This needs to happen as the list isn't always in the correct order.
                if (currentBlock.getSubBlock() != null) {
                    BuildBlock subBlock = currentBlock.getSubBlock();
                    BuildingMethods.ReplaceBlock(structure.world, subBlock.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing), subBlock.getBlockState());
                }
            }
        }
        for (Structure structure : structuresToRemove) {
            for (BuildTileEntity buildTileEntity : structure.tileEntities) {
                BlockPos tileEntityPos = buildTileEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
                TileEntity tileEntity = structure.world.getTileEntity(tileEntityPos);
                if (tileEntity == null) {
                    TileEntity.create(structure.world, buildTileEntity.getEntityDataTag());
                } else {
                    structure.world.removeTileEntity(tileEntityPos);
                    tileEntity = TileEntity.create(structure.world, buildTileEntity.getEntityDataTag());
                    structure.world.setTileEntity(tileEntityPos, tileEntity);
                    structure.world.getChunkFromBlockCoords(tileEntityPos).markDirty();
                    tileEntity.markDirty();
                    SPacketUpdateTileEntity packet = tileEntity.getUpdatePacket();
                    if (packet != null) {
                        structure.world.getMinecraftServer().getPlayerList().sendPacketToAllPlayers(tileEntity.getUpdatePacket());
                    }
                }
            }
            for (BuildEntity buildEntity : structure.entities) {
                Entity entity = EntityList.createEntityByIDFromName(buildEntity.getEntityResource(), structure.world);
                NBTTagCompound tagCompound = buildEntity.getEntityDataTag();
                BlockPos entityPos = buildEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
                if (tagCompound != null) {
                    if (tagCompound.hasUniqueId("UUID")) {
                        tagCompound.setUniqueId("UUID", UUID.randomUUID());
                    }
                    entity.readFromNBT(tagCompound);
                }
                entity.forceSpawn = true;
                float yaw = entity.rotationYaw;
                Rotation rotation = Rotation.NONE;
                double x_axis_offset = buildEntity.entityXAxisOffset;
                double z_axis_offset = buildEntity.entityZAxisOffset;
                EnumFacing facing = entity instanceof EntityHanging ? ((EntityHanging) entity).facingDirection : structure.assumedNorth;
                double y_axis_offset = entity instanceof EntityHanging ? buildEntity.entityYAxisOffset * -1 : buildEntity.entityYAxisOffset;
                if (structure.configuration.houseFacing == structure.assumedNorth.getOpposite()) {
                    rotation = Rotation.CLOCKWISE_180;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.getOpposite();
                } else if (structure.configuration.houseFacing == structure.assumedNorth.rotateY()) {
                    rotation = rotation.CLOCKWISE_90;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.rotateY();
                } else if (structure.configuration.houseFacing == structure.assumedNorth.rotateYCCW()) {
                    rotation = rotation.COUNTERCLOCKWISE_90;
                    x_axis_offset = x_axis_offset * -1;
                    z_axis_offset = z_axis_offset * -1;
                    facing = facing.rotateYCCW();
                } else {
                    x_axis_offset = 0;
                    z_axis_offset = 0;
                }
                yaw = entity.getRotatedYaw(rotation);
                if (entity instanceof EntityHanging) {
                    ((EntityHanging) entity).facingDirection = facing;
                    ModEventHandler.updateEntityHangingBoundingBox((EntityHanging) entity);
                }
                entity.setPositionAndRotation(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, entity.rotationPitch);
                if (entity instanceof EntityHanging) {
                    ModEventHandler.updateEntityHangingBoundingBox((EntityHanging) entity);
                    Chunk chunk = structure.world.getChunkFromBlockCoords(entityPos);
                    chunk.markDirty();
                }
                structure.world.spawnEntity(entity);
            }
            // This structure is done building. Do any post-building operations.
            structure.AfterBuilding(structure.configuration, structure.world, structure.originalPos, structure.assumedNorth, entry.getKey());
            entry.getValue().remove(structure);
        }
        if (entry.getValue().size() == 0) {
            playersToRemove.add(entry.getKey());
        }
    }
    // Remove each player that has their structure's built.
    for (EntityPlayer player : playersToRemove) {
        ModEventHandler.structuresToBuild.remove(player);
    }
}
Also used : SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) Entity(net.minecraft.entity.Entity) BuildEntity(com.wuest.prefab.StructureGen.BuildEntity) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) Chunk(net.minecraft.world.chunk.Chunk) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) Rotation(net.minecraft.util.Rotation) SPacketUpdateTileEntity(net.minecraft.network.play.server.SPacketUpdateTileEntity) BuildTileEntity(com.wuest.prefab.StructureGen.BuildTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BuildBlock(com.wuest.prefab.StructureGen.BuildBlock) EntityHanging(net.minecraft.entity.EntityHanging) BuildEntity(com.wuest.prefab.StructureGen.BuildEntity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) Structure(com.wuest.prefab.StructureGen.Structure) ItemBasicStructure(com.wuest.prefab.Items.Structures.ItemBasicStructure) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

ItemBasicStructure (com.wuest.prefab.Items.Structures.ItemBasicStructure)1 BuildBlock (com.wuest.prefab.StructureGen.BuildBlock)1 BuildEntity (com.wuest.prefab.StructureGen.BuildEntity)1 BuildTileEntity (com.wuest.prefab.StructureGen.BuildTileEntity)1 Structure (com.wuest.prefab.StructureGen.Structure)1 ArrayList (java.util.ArrayList)1 IBlockState (net.minecraft.block.state.IBlockState)1 Entity (net.minecraft.entity.Entity)1 EntityHanging (net.minecraft.entity.EntityHanging)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 SPacketUpdateTileEntity (net.minecraft.network.play.server.SPacketUpdateTileEntity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1 Rotation (net.minecraft.util.Rotation)1 BlockPos (net.minecraft.util.math.BlockPos)1 Chunk (net.minecraft.world.chunk.Chunk)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1