use of com.wuest.prefab.StructureGen.BuildTileEntity 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);
}
}
Aggregations