Search in sources :

Example 1 with SchematicBlock

use of buildcraft.api.blueprints.SchematicBlock in project BuildCraft by BuildCraft.

the class SchematicFactoryBlock method loadSchematicFromWorldNBT.

@Override
protected SchematicBlock loadSchematicFromWorldNBT(NBTTagCompound nbt, MappingRegistry registry) throws MappingNotFoundException {
    int blockId = nbt.getInteger("blockId");
    Block b = registry.getBlockForId(blockId);
    if (b == Blocks.AIR) {
        SchematicBlock s = new SchematicBlock();
        s.state = Blocks.AIR.getDefaultState();
        return s;
    } else {
        SchematicBlock s = SchematicRegistry.INSTANCE.createSchematicBlock(b.getStateFromMeta(nbt.getInteger("blockMeta")));
        if (s != null) {
            s.readSchematicFromNBT(nbt, registry);
            return s;
        }
    }
    return null;
}
Also used : SchematicBlock(buildcraft.api.blueprints.SchematicBlock) Block(net.minecraft.block.Block) SchematicBlock(buildcraft.api.blueprints.SchematicBlock)

Example 2 with SchematicBlock

use of buildcraft.api.blueprints.SchematicBlock in project BuildCraft by BuildCraft.

the class SchematicRegistry method createSchematicBlock.

public SchematicBlock createSchematicBlock(IBlockState state) {
    if (state == null) {
        return null;
    }
    SchematicConstructor c = schematicBlocks.get(toStringKey(state));
    if (c == null) {
        return null;
    }
    try {
        SchematicBlock s = (SchematicBlock) c.newInstance();
        s.state = state;
        return s;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SchematicBlock(buildcraft.api.blueprints.SchematicBlock) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with SchematicBlock

use of buildcraft.api.blueprints.SchematicBlock in project BuildCraft by BuildCraft.

the class BuildingSlotBlock method writeToWorld.

@Override
public boolean writeToWorld(IBuilderContext context) {
    if (internalRequirementRemovalListener != null) {
        internalRequirementRemovalListener.remove(this);
    }
    if (mode == Mode.ClearIfInvalid) {
        if (!getSchematic().isAlreadyBuilt(context, pos)) {
            context.world().sendBlockBreakProgress(pos.hashCode(), pos, -1);
            if (BuildCraftBuilders.dropBrokenBlocks) {
                BlockUtil.breakBlock((WorldServer) context.world(), pos, pos);
                return false;
            } else {
                context.world().setBlockToAir(pos);
                return true;
            }
        }
    } else {
        try {
            getSchematic().placeInWorld(context, pos, stackConsumed);
            // you're unable to break an API too much.
            if (!getSchematic().isAlreadyBuilt(context, pos)) {
                if (context.world().isAirBlock(pos)) {
                    return false;
                } else if (!(getSchematic() instanceof SchematicBlock) || !context.world().getBlockState(pos).getBlock().isAssociatedBlock(((SchematicBlock) getSchematic()).state.getBlock())) {
                    BCLog.logger.warn("Placed block does not match expectations! Most likely a bug in BuildCraft or a supported mod. Removed mismatched block.");
                    IBlockState state = context.world().getBlockState(pos);
                    BCLog.logger.warn("Location: " + pos + " - Block: " + Block.REGISTRY.getNameForObject(state.getBlock()) + "@" + state);
                    context.world().removeTileEntity(pos);
                    context.world().setBlockToAir(pos);
                    return false;
                } else {
                    return true;
                }
            }
            // the stored requirements for anti-cheating purposes.
            if (!context.world().isAirBlock(pos) && getSchematic().getBuildingPermission() == BuildingPermission.ALL && getSchematic() instanceof SchematicBlock) {
                SchematicBlock sb = (SchematicBlock) getSchematic();
                // Copy the old array of stored requirements.
                ItemStack[] oldRequirementsArray = sb.storedRequirements;
                List<ItemStack> oldRequirements = Arrays.asList(oldRequirementsArray);
                sb.storedRequirements = new ItemStack[0];
                sb.storeRequirements(context, pos);
                for (ItemStack s : sb.storedRequirements) {
                    boolean contains = false;
                    for (ItemStack ss : oldRequirements) {
                        if (getSchematic().isItemMatchingRequirement(s, ss)) {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains) {
                        BCLog.logger.warn("Blueprint has MISMATCHING REQUIREMENTS! Potential corrupted/hacked blueprint! Removed mismatched block.");
                        BCLog.logger.warn("Location: " + pos + " - ItemStack: " + s.toString());
                        context.world().removeTileEntity(pos);
                        context.world().setBlockToAir(pos);
                        return true;
                    }
                }
                // Restore the stored requirements.
                sb.storedRequirements = oldRequirementsArray;
            }
            // Once the schematic has been written, we're going to issue
            // calls
            // to various functions, in particular updating the tile entity.
            // If these calls issue problems, in order to avoid corrupting
            // the world, we're logging the problem and setting the block to
            // air.
            TileEntity e = context.world().getTileEntity(pos);
            if (e != null && e instanceof ITickable) {
                ((ITickable) e).update();
            }
            return true;
        } catch (Throwable t) {
            t.printStackTrace();
            context.world().setBlockToAir(pos);
            return false;
        }
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) ITickable(net.minecraft.util.ITickable) SchematicBlock(buildcraft.api.blueprints.SchematicBlock) ItemStack(net.minecraft.item.ItemStack)

Example 4 with SchematicBlock

use of buildcraft.api.blueprints.SchematicBlock in project BuildCraft by BuildCraft.

the class Blueprint method readFromWorld.

@Override
public void readFromWorld(IBuilderContext context, TileEntity anchorTile, BlockPos pos) {
    BptContext bptContext = (BptContext) context;
    IBlockState state = anchorTile.getWorld().getBlockState(pos);
    if (context.world().isAirBlock(pos)) {
        // will make sure that they don't get recorded.
        return;
    }
    SchematicBlock slot = SchematicRegistry.INSTANCE.createSchematicBlock(state);
    if (slot == null) {
        return;
    }
    BlockPos contextPos = pos.subtract(context.surroundingBox().min());
    slot.state = state;
    if (!SchematicRegistry.INSTANCE.isSupported(state)) {
        return;
    }
    try {
        slot.initializeFromObjectAt(context, pos);
        slot.storeRequirements(context, pos);
        set(contextPos, slot);
    } catch (Throwable t) {
        // Defensive code against errors in implementers
        t.printStackTrace();
        BCLog.logger.throwing(t);
    }
    switch(slot.getBuildingPermission()) {
        case ALL:
            break;
        case CREATIVE_ONLY:
            if (bptContext.readConfiguration.allowCreative) {
                if (buildingPermission == BuildingPermission.ALL) {
                    buildingPermission = BuildingPermission.CREATIVE_ONLY;
                }
            } else {
                set(contextPos, null);
            }
            break;
        case NONE:
            buildingPermission = BuildingPermission.NONE;
            break;
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) SchematicBlock(buildcraft.api.blueprints.SchematicBlock) BlockPos(net.minecraft.util.math.BlockPos)

Example 5 with SchematicBlock

use of buildcraft.api.blueprints.SchematicBlock in project BuildCraft by BuildCraft.

the class BptBuilderBlueprint method internalInit.

@Override
protected void internalInit() {
    BlockPos worldOffset = pos.subtract(blueprint.anchor);
    BlockPos bptMin = BlockPos.ORIGIN;
    if (worldOffset.getY() < 0)
        bptMin = VecUtil.replaceValue(bptMin, Axis.Y, -worldOffset.getY());
    BlockPos bptMax = blueprint.size.subtract(Utils.POS_ONE);
    if (worldOffset.add(bptMax).getY() > context.world().getHeight()) {
        bptMax = VecUtil.replaceValue(bptMax, Axis.Y, context.world().getHeight() - worldOffset.getY());
    }
    /* Check to make sure the max is bigger than the min- if its not it means that the size was 0 for one of the
         * axis */
    if (Utils.min(bptMin, bptMax).equals(bptMin) && Utils.max(bptMin, bptMax).equals(bptMax)) {
        try {
            for (BlockPos bptOffset : Utils.getAllInBox(bptMin, bptMax, getOrder())) {
                BlockPos pointWorldOffset = worldOffset.add(bptOffset);
                if (!isLocationUsed(pointWorldOffset)) {
                    SchematicBlock slot = (SchematicBlock) blueprint.get(bptOffset);
                    if (slot == null && !blueprint.excavate) {
                        continue;
                    }
                    if (slot == null) {
                        slot = new SchematicBlock();
                        slot.state = Blocks.AIR.getDefaultState();
                    }
                    if (!SchematicRegistry.INSTANCE.isAllowedForBuilding(slot.state)) {
                        continue;
                    }
                    BuildingSlotBlock b = new BuildingSlotBlock();
                    b.schematic = slot;
                    b.pos = pointWorldOffset;
                    b.mode = Mode.ClearIfInvalid;
                    b.buildStage = 0;
                    addToBuildList(b);
                }
            }
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            BCLog.logger.warn("Attempted to use the positions " + bptMin + ", " + bptMax + " to access a blueprint with a size of " + blueprint.size);
            throw BCLog.logger.throwing(aioobe);
        }
        LinkedList<BuildingSlotBlock> tmpStandalone = new LinkedList<>();
        LinkedList<BuildingSlotBlock> tmpExpanding = new LinkedList<>();
        for (BlockPos bptOffset : Utils.getAllInBox(bptMin, bptMax, getOrder())) {
            BlockPos pointWorldOffset = worldOffset.add(bptOffset);
            SchematicBlock slot = (SchematicBlock) blueprint.get(bptOffset);
            if (slot == null) {
                continue;
            }
            if (!SchematicRegistry.INSTANCE.isAllowedForBuilding(slot.state)) {
                continue;
            }
            BuildingSlotBlock b = new BuildingSlotBlock();
            b.schematic = slot;
            b.pos = pointWorldOffset;
            b.mode = Mode.Build;
            if (!isLocationUsed(pointWorldOffset)) {
                switch(slot.getBuildStage()) {
                    case STANDALONE:
                        tmpStandalone.add(b);
                        b.buildStage = 1;
                        break;
                    case EXPANDING:
                        tmpExpanding.add(b);
                        b.buildStage = 2;
                        break;
                }
            } else {
                postProcessing.add(b);
            }
        }
        for (BuildingSlotBlock b : tmpStandalone) {
            addToBuildList(b);
        }
        for (BuildingSlotBlock b : tmpExpanding) {
            addToBuildList(b);
        }
    }
    int seqId = 0;
    for (SchematicEntity e : ((Blueprint) blueprint).entities) {
        BuildingSlotEntity b = new BuildingSlotEntity();
        b.schematic = e;
        b.sequenceNumber = seqId;
        if (!builtEntities.contains(seqId)) {
            entityList.add(b);
        } else {
            postProcessing.add(b);
        }
        seqId++;
    }
    recomputeNeededItems();
}
Also used : BuildingSlotBlock(buildcraft.core.builders.BuildingSlotBlock) SchematicEntity(buildcraft.api.blueprints.SchematicEntity) BuildingSlotEntity(buildcraft.core.builders.BuildingSlotEntity) SchematicBlock(buildcraft.api.blueprints.SchematicBlock) BlockPos(net.minecraft.util.math.BlockPos) LinkedList(java.util.LinkedList)

Aggregations

SchematicBlock (buildcraft.api.blueprints.SchematicBlock)6 BlockPos (net.minecraft.util.math.BlockPos)3 IBlockState (net.minecraft.block.state.IBlockState)2 SchematicEntity (buildcraft.api.blueprints.SchematicEntity)1 BuildingSlotBlock (buildcraft.core.builders.BuildingSlotBlock)1 BuildingSlotEntity (buildcraft.core.builders.BuildingSlotEntity)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedList (java.util.LinkedList)1 Block (net.minecraft.block.Block)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 ITickable (net.minecraft.util.ITickable)1