Search in sources :

Example 1 with ISchematicEntity

use of buildcraft.api.schematics.ISchematicEntity in project BuildCraft by BuildCraft.

the class BlueprintBuilder method tick.

@Override
public boolean tick() {
    if (tile.getWorldBC().isRemote) {
        return super.tick();
    }
    tile.getWorldBC().profiler.startSection("entitiesWithinBox");
    List<Entity> entitiesWithinBox = tile.getWorldBC().getEntitiesWithinAABB(Entity.class, getBuildingInfo().box.getBoundingBox(), Objects::nonNull);
    tile.getWorldBC().profiler.endSection();
    tile.getWorldBC().profiler.startSection("toSpawn");
    List<ISchematicEntity> toSpawn = getBuildingInfo().entities.stream().filter(schematicEntity -> entitiesWithinBox.stream().map(Entity::getPositionVector).map(schematicEntity.getPos().add(new Vec3d(getBuildingInfo().offsetPos))::distanceTo).noneMatch(distance -> distance < MAX_ENTITY_DISTANCE)).collect(Collectors.toList());
    tile.getWorldBC().profiler.endSection();
    // Compute needed stacks
    tile.getWorldBC().profiler.startSection("remainingDisplayRequired");
    remainingDisplayRequired.clear();
    remainingDisplayRequired.addAll(StackUtil.mergeSameItems(Stream.concat(remainingDisplayRequiredBlocksConcat.stream(), toSpawn.stream().flatMap(schematicEntity -> getDisplayRequired(getBuildingInfo().entitiesRequiredItems.get(schematicEntity), getBuildingInfo().entitiesRequiredFluids.get(schematicEntity)))).collect(Collectors.toList())));
    tile.getWorldBC().profiler.endSection();
    // Kill not needed entities
    tile.getWorldBC().profiler.startSection("toKill");
    List<Entity> toKill = entitiesWithinBox.stream().filter(entity -> entity != null && getBuildingInfo().entities.stream().map(ISchematicEntity::getPos).map(new Vec3d(getBuildingInfo().offsetPos)::add).map(entity.getPositionVector()::distanceTo).noneMatch(distance -> distance < MAX_ENTITY_DISTANCE) && SchematicEntityManager.getSchematicEntity(new SchematicEntityContext(tile.getWorldBC(), BlockPos.ORIGIN, entity)) != null).collect(Collectors.toList());
    if (!toKill.isEmpty()) {
        if (!tile.getBattery().isFull()) {
            return false;
        } else {
            tile.getWorldBC().profiler.startSection("kill");
            toKill.forEach(Entity::setDead);
            tile.getWorldBC().profiler.endSection();
        }
    }
    tile.getWorldBC().profiler.endSection();
    // Call superclass method
    if (super.tick()) {
        // Spawn needed entities
        if (!toSpawn.isEmpty()) {
            if (!tile.getBattery().isFull()) {
                return false;
            } else {
                tile.getWorldBC().profiler.startSection("spawn");
                toSpawn.stream().filter(schematicEntity -> tryExtractRequired(getBuildingInfo().entitiesRequiredItems.get(schematicEntity), getBuildingInfo().entitiesRequiredFluids.get(schematicEntity), true).isPresent()).filter(schematicEntity -> schematicEntity.build(tile.getWorldBC(), getBuildingInfo().offsetPos) != null).forEach(schematicEntity -> tryExtractRequired(getBuildingInfo().entitiesRequiredItems.get(schematicEntity), getBuildingInfo().entitiesRequiredFluids.get(schematicEntity), false));
                tile.getWorldBC().profiler.endSection();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : IntStream(java.util.stream.IntStream) SchematicEntityContext(buildcraft.api.schematics.SchematicEntityContext) Arrays(java.util.Arrays) FluidUtil(net.minecraftforge.fluids.FluidUtil) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Pair(org.apache.commons.lang3.tuple.Pair) Vec3d(net.minecraft.util.math.Vec3d) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) Map(java.util.Map) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Entity(net.minecraft.entity.Entity) FluidUtilBC(buildcraft.lib.misc.FluidUtilBC) PacketBufferBC(buildcraft.lib.net.PacketBufferBC) Collection(java.util.Collection) IOException(java.io.IOException) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) StackUtil(buildcraft.lib.misc.StackUtil) Optional(java.util.Optional) FluidStack(net.minecraftforge.fluids.FluidStack) Collections(java.util.Collections) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) Entity(net.minecraft.entity.Entity) Objects(java.util.Objects) SchematicEntityContext(buildcraft.api.schematics.SchematicEntityContext) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) Vec3d(net.minecraft.util.math.Vec3d)

Example 2 with ISchematicEntity

use of buildcraft.api.schematics.ISchematicEntity in project BuildCraft by BuildCraft.

the class SchematicEntityManager method getSchematicEntity.

@SuppressWarnings("WeakerAccess")
public static ISchematicEntity getSchematicEntity(SchematicEntityContext context) {
    for (SchematicEntityFactory<?> schematicEntityFactory : Lists.reverse(SchematicEntityFactoryRegistry.getFactories())) {
        if (schematicEntityFactory.predicate.test(context)) {
            ISchematicEntity schematicEntity = schematicEntityFactory.supplier.get();
            schematicEntity.init(context);
            return schematicEntity;
        }
    }
    return null;
}
Also used : ISchematicEntity(buildcraft.api.schematics.ISchematicEntity)

Example 3 with ISchematicEntity

use of buildcraft.api.schematics.ISchematicEntity in project BuildCraft by BuildCraft.

the class SchematicEntityManager method readFromNBT.

@Nonnull
public static ISchematicEntity readFromNBT(NBTTagCompound schematicEntityTag) throws InvalidInputDataException {
    ResourceLocation name = new ResourceLocation(schematicEntityTag.getString("name"));
    SchematicEntityFactory<?> factory = SchematicEntityFactoryRegistry.getFactoryByName(name);
    if (factory == null) {
        throw new InvalidInputDataException("Unknown schematic type " + name);
    }
    ISchematicEntity schematicEntity = factory.supplier.get();
    NBTTagCompound data = schematicEntityTag.getCompoundTag("data");
    try {
        schematicEntity.deserializeNBT(data);
        return schematicEntity;
    } catch (InvalidInputDataException e) {
        throw new InvalidInputDataException("Failed to load the schematic from " + data, e);
    }
}
Also used : InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ISchematicEntity(buildcraft.api.schematics.ISchematicEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

ISchematicEntity (buildcraft.api.schematics.ISchematicEntity)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)1 ISchematicBlock (buildcraft.api.schematics.ISchematicBlock)1 SchematicEntityContext (buildcraft.api.schematics.SchematicEntityContext)1 FluidUtilBC (buildcraft.lib.misc.FluidUtilBC)1 StackUtil (buildcraft.lib.misc.StackUtil)1 PacketBufferBC (buildcraft.lib.net.PacketBufferBC)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1