Search in sources :

Example 1 with Structure

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

the class StructureEventHandler 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) {
    if (event.phase == TickEvent.Phase.START) {
        ArrayList<Player> playersToRemove = new ArrayList<>();
        if (StructureEventHandler.entitiesToGenerate.size() > 0) {
            StructureEventHandler.ticksSinceLastEntitiesGenerated++;
            if (StructureEventHandler.ticksSinceLastEntitiesGenerated > 40) {
                // Process any entities.
                StructureEventHandler.processStructureEntities();
                StructureEventHandler.ticksSinceLastEntitiesGenerated = 0;
            }
        }
        if (StructureEventHandler.structuresToBuild.size() > 0) {
            for (Entry<Player, ArrayList<Structure>> entry : StructureEventHandler.structuresToBuild.entrySet()) {
                ArrayList<Structure> structuresToRemove = new ArrayList<>();
                // Build the first 100 blocks of each structure for this player.
                for (Structure structure : entry.getValue()) {
                    if (!structure.entitiesRemoved) {
                        // If there is a player there...they will probably die anyways.....
                        for (BlockPos clearedPos : structure.clearedBlockPos) {
                            AABB axisPos = Shapes.block().bounds().move(clearedPos);
                            List<Entity> list = structure.world.getEntities(null, axisPos);
                            if (!list.isEmpty()) {
                                for (Entity entity : list) {
                                    // Don't kill living entities.
                                    if (!(entity instanceof LivingEntity)) {
                                        if (entity instanceof HangingEntity) {
                                            structure.BeforeHangingEntityRemoved((HangingEntity) entity);
                                        }
                                        structure.world.removeEntity(entity, false);
                                    }
                                }
                            }
                        }
                        structure.entitiesRemoved = true;
                    }
                    if (structure.airBlocks.size() > 0) {
                        structure.hasAirBlocks = true;
                    }
                    for (int i = 0; i < 10; i++) {
                        i = StructureEventHandler.setBlock(i, structure, structuresToRemove);
                    }
                    // After building the blocks for this tick, find waterlogged blocks and remove them.
                    StructureEventHandler.removeWaterLogging(structure);
                }
                // Update the list of structures to remove this structure since it's done building.
                StructureEventHandler.removeStructuresFromList(structuresToRemove, entry);
                if (entry.getValue().size() == 0) {
                    playersToRemove.add(entry.getKey());
                }
            }
        }
        // Remove each player that has their structure's built.
        for (Player player : playersToRemove) {
            StructureEventHandler.structuresToBuild.remove(player);
        }
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(net.minecraft.world.entity.Entity) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) LivingEntity(net.minecraft.world.entity.LivingEntity) BlockPos(net.minecraft.core.BlockPos) Structure(com.wuest.prefab.structures.base.Structure) AABB(net.minecraft.world.phys.AABB) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with Structure

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

the class StructureEventHandler method removeStructuresFromList.

private static void removeStructuresFromList(ArrayList<Structure> structuresToRemove, Entry<Player, ArrayList<Structure>> entry) {
    for (Structure structure : structuresToRemove) {
        StructureEventHandler.removeWaterLogging(structure);
        for (BuildEntity buildEntity : structure.entities) {
            Optional<EntityType<?>> entityType = EntityType.byString(buildEntity.getEntityResourceString());
            if (entityType.isPresent()) {
                StructureEventHandler.entitiesToGenerate.add(new Tuple<>(structure, buildEntity));
            }
        }
        // This structure is done building.
        entry.getValue().remove(structure);
    }
}
Also used : EntityType(net.minecraft.world.entity.EntityType) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) Structure(com.wuest.prefab.structures.base.Structure)

Example 3 with Structure

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

the class StructureEventHandler method processStructureEntities.

private static void processStructureEntities() {
    for (Tuple<Structure, BuildEntity> entityRecords : StructureEventHandler.entitiesToGenerate) {
        BuildEntity buildEntity = entityRecords.getSecond();
        Structure structure = entityRecords.getFirst();
        Optional<EntityType<?>> entityType = EntityType.byString(buildEntity.getEntityResourceString());
        if (entityType.isPresent()) {
            Entity entity = entityType.get().create(structure.world);
            if (entity != null) {
                CompoundTag tagCompound = buildEntity.getEntityDataTag();
                BlockPos entityPos = buildEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
                if (tagCompound != null) {
                    if (tagCompound.hasUUID("UUID")) {
                        tagCompound.putUUID("UUID", UUID.randomUUID());
                    }
                    ListTag nbttaglist = new ListTag();
                    nbttaglist.add(DoubleTag.valueOf(entityPos.getX()));
                    nbttaglist.add(DoubleTag.valueOf(entityPos.getY()));
                    nbttaglist.add(DoubleTag.valueOf(entityPos.getZ()));
                    tagCompound.put("Pos", nbttaglist);
                    entity.load(tagCompound);
                }
                // Set item frame facing and rotation here.
                if (entity instanceof ItemFrame) {
                    entity = StructureEventHandler.setItemFrameFacingAndRotation((ItemFrame) entity, buildEntity, entityPos, structure);
                } else if (entity instanceof Painting) {
                    entity = StructureEventHandler.setPaintingFacingAndRotation((Painting) entity, buildEntity, entityPos, structure);
                } else if (entity instanceof AbstractMinecart) {
                    // Minecarts need to be slightly higher to account for the rails; otherwise they will fall through the rail and the block below the rail.
                    buildEntity.entityYAxisOffset = buildEntity.entityYAxisOffset + .2;
                    entity = StructureEventHandler.setEntityFacingAndRotation(entity, buildEntity, entityPos, structure);
                } else {
                    // Other entities
                    entity = StructureEventHandler.setEntityFacingAndRotation(entity, buildEntity, entityPos, structure);
                }
                structure.world.addFreshEntity(entity);
            }
        }
    }
    // All entities generated; clear out the list.
    StructureEventHandler.entitiesToGenerate.clear();
}
Also used : EntityType(net.minecraft.world.entity.EntityType) LivingEntity(net.minecraft.world.entity.LivingEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Entity(net.minecraft.world.entity.Entity) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) BuildEntity(com.wuest.prefab.structures.base.BuildEntity) BlockPos(net.minecraft.core.BlockPos) ItemFrame(net.minecraft.world.entity.decoration.ItemFrame) Structure(com.wuest.prefab.structures.base.Structure) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) Painting(net.minecraft.world.entity.decoration.Painting)

Aggregations

BuildEntity (com.wuest.prefab.structures.base.BuildEntity)3 Structure (com.wuest.prefab.structures.base.Structure)3 BlockPos (net.minecraft.core.BlockPos)2 Entity (net.minecraft.world.entity.Entity)2 EntityType (net.minecraft.world.entity.EntityType)2 LivingEntity (net.minecraft.world.entity.LivingEntity)2 HangingEntity (net.minecraft.world.entity.decoration.HangingEntity)2 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 ItemFrame (net.minecraft.world.entity.decoration.ItemFrame)1 Painting (net.minecraft.world.entity.decoration.Painting)1 Player (net.minecraft.world.entity.player.Player)1 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)1 AABB (net.minecraft.world.phys.AABB)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1