Search in sources :

Example 1 with ISchematicBlock

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

the class TileReplacer method update.

@Override
public void update() {
    if (world.isRemote) {
        return;
    }
    if (!invSnapshot.getStackInSlot(0).isEmpty() && !invSchematicFrom.getStackInSlot(0).isEmpty() && !invSchematicTo.getStackInSlot(0).isEmpty()) {
        Header header = BCBuildersItems.snapshot.getHeader(invSnapshot.getStackInSlot(0));
        if (header != null) {
            Snapshot snapshot = GlobalSavedDataSnapshots.get(world).getSnapshot(header.key);
            if (snapshot instanceof Blueprint) {
                Blueprint blueprint = (Blueprint) snapshot;
                try {
                    ISchematicBlock from = SchematicBlockManager.readFromNBT(NBTUtilBC.getItemData(invSchematicFrom.getStackInSlot(0)).getCompoundTag(ItemSchematicSingle.NBT_KEY));
                    ISchematicBlock to = SchematicBlockManager.readFromNBT(NBTUtilBC.getItemData(invSchematicTo.getStackInSlot(0)).getCompoundTag(ItemSchematicSingle.NBT_KEY));
                    Blueprint newBlueprint = blueprint.copy();
                    newBlueprint.replace(from, to);
                    newBlueprint.computeKey();
                    GlobalSavedDataSnapshots.get(world).addSnapshot(newBlueprint);
                    invSnapshot.setStackInSlot(0, BCBuildersItems.snapshot.getUsed(EnumSnapshotType.BLUEPRINT, new Header(blueprint.key, getOwner().getId(), new Date(), header.name)));
                    invSchematicFrom.setStackInSlot(0, ItemStack.EMPTY);
                    invSchematicTo.setStackInSlot(0, ItemStack.EMPTY);
                } catch (InvalidInputDataException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) ItemSnapshot(buildcraft.builders.item.ItemSnapshot) Snapshot(buildcraft.builders.snapshot.Snapshot) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) Header(buildcraft.builders.snapshot.Snapshot.Header) Blueprint(buildcraft.builders.snapshot.Blueprint) Date(java.util.Date)

Example 2 with ISchematicBlock

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

the class SchematicBlockManager method readFromNBT.

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

Example 3 with ISchematicBlock

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

the class SchematicBlockManager method getSchematicBlock.

@SuppressWarnings("WeakerAccess")
public static ISchematicBlock getSchematicBlock(SchematicBlockContext context) {
    for (SchematicBlockFactory<?> schematicBlockFactory : Lists.reverse(SchematicBlockFactoryRegistry.getFactories())) {
        if (schematicBlockFactory.predicate.test(context)) {
            ISchematicBlock schematicBlock = schematicBlockFactory.supplier.get();
            schematicBlock.init(context);
            return schematicBlock;
        }
    }
    throw new UnsupportedOperationException();
}
Also used : ISchematicBlock(buildcraft.api.schematics.ISchematicBlock)

Example 4 with ISchematicBlock

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

the class ItemSchematicSingle method onItemUseFirst.

@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    if (world.isRemote) {
        return EnumActionResult.PASS;
    }
    ItemStack stack = player.getHeldItem(hand);
    if (player.isSneaking()) {
        NBTTagCompound itemData = NBTUtilBC.getItemData(StackUtil.asNonNull(stack));
        itemData.removeTag(NBT_KEY);
        if (itemData.hasNoTags()) {
            stack.setTagCompound(null);
        }
        stack.setItemDamage(DAMAGE_CLEAN);
        return EnumActionResult.SUCCESS;
    }
    int damage = stack.getItemDamage();
    if (damage != DAMAGE_USED) {
        IBlockState state = world.getBlockState(pos);
        ISchematicBlock schematicBlock = SchematicBlockManager.getSchematicBlock(new SchematicBlockContext(world, pos, pos, state, state.getBlock()));
        if (schematicBlock.isAir()) {
            return EnumActionResult.FAIL;
        }
        NBTUtilBC.getItemData(stack).setTag(NBT_KEY, SchematicBlockManager.writeToNBT(schematicBlock));
        stack.setItemDamage(DAMAGE_USED);
        return EnumActionResult.SUCCESS;
    } else {
        BlockPos placePos = pos;
        boolean replaceable = world.getBlockState(pos).getBlock().isReplaceable(world, pos);
        if (!replaceable) {
            placePos = placePos.offset(side);
        }
        if (!world.mayPlace(world.getBlockState(pos).getBlock(), placePos, false, side, null)) {
            return EnumActionResult.FAIL;
        }
        if (replaceable && !world.isAirBlock(placePos)) {
            world.setBlockToAir(placePos);
        }
        try {
            ISchematicBlock schematicBlock = getSchematic(stack);
            if (schematicBlock != null) {
                if (!schematicBlock.isBuilt(world, placePos) && schematicBlock.canBuild(world, placePos)) {
                    List<FluidStack> requiredFluids = schematicBlock.computeRequiredFluids();
                    List<ItemStack> requiredItems = schematicBlock.computeRequiredItems();
                    if (requiredFluids.isEmpty()) {
                        InventoryWrapper itemTransactor = new InventoryWrapper(player.inventory);
                        if (StackUtil.mergeSameItems(requiredItems).stream().noneMatch(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), true).isEmpty())) {
                            if (schematicBlock.build(world, placePos)) {
                                StackUtil.mergeSameItems(requiredItems).forEach(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), false));
                                SoundUtil.playBlockPlace(world, placePos);
                                player.swingArm(hand);
                                return EnumActionResult.SUCCESS;
                            }
                        } else {
                            player.sendStatusMessage(new TextComponentString("Not enough items. Total needed: " + StackUtil.mergeSameItems(requiredItems).stream().map(s -> s.getTextComponent().getFormattedText() + " x " + s.getCount()).collect(Collectors.joining(", "))), true);
                        }
                    } else {
                        player.sendStatusMessage(new TextComponentString("Schematic requires fluids"), true);
                    }
                }
            }
        } catch (InvalidInputDataException e) {
            player.sendStatusMessage(new TextComponentString("Invalid schematic: " + e.getMessage()), true);
            e.printStackTrace();
        }
        return EnumActionResult.FAIL;
    }
}
Also used : TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) EnumHand(net.minecraft.util.EnumHand) ItemBC_Neptune(buildcraft.lib.item.ItemBC_Neptune) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ItemStack(net.minecraft.item.ItemStack) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) SchematicBlockManager(buildcraft.builders.snapshot.SchematicBlockManager) Side(net.minecraftforge.fml.relauncher.Side) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Nonnull(javax.annotation.Nonnull) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BCLog(buildcraft.api.core.BCLog) World(net.minecraft.world.World) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) ActionResult(net.minecraft.util.ActionResult) TextComponentString(net.minecraft.util.text.TextComponentString) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EnumActionResult(net.minecraft.util.EnumActionResult) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) StackUtil(buildcraft.lib.misc.StackUtil) FluidStack(net.minecraftforge.fluids.FluidStack) InventoryWrapper(buildcraft.lib.inventory.InventoryWrapper) SoundUtil(buildcraft.lib.misc.SoundUtil) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) IBlockState(net.minecraft.block.state.IBlockState) InventoryWrapper(buildcraft.lib.inventory.InventoryWrapper) FluidStack(net.minecraftforge.fluids.FluidStack) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) TextComponentString(net.minecraft.util.text.TextComponentString) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 5 with ISchematicBlock

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

the class TileArchitectTable method scanSingleBlock.

private void scanSingleBlock() {
    BlockPos size = box.size();
    if (templateScannedBlocks == null || blueprintScannedData == null) {
        boxIterator = new BoxIterator(box, EnumAxisOrder.XZY.getMinToMaxOrder(), true);
        templateScannedBlocks = new BitSet(Snapshot.getDataSize(size));
        blueprintScannedData = new int[Snapshot.getDataSize(size)];
    }
    // Read from world
    BlockPos worldScanPos = boxIterator.getCurrent();
    BlockPos schematicPos = worldScanPos.subtract(box.min());
    if (snapshotType == EnumSnapshotType.TEMPLATE) {
        templateScannedBlocks.set(Snapshot.posToIndex(box.size(), schematicPos), !world.isAirBlock(worldScanPos));
    }
    if (snapshotType == EnumSnapshotType.BLUEPRINT) {
        ISchematicBlock schematicBlock = readSchematicBlock(worldScanPos);
        int index = blueprintScannedPalette.indexOf(schematicBlock);
        if (index == -1) {
            index = blueprintScannedPalette.size();
            blueprintScannedPalette.add(schematicBlock);
        }
        blueprintScannedData[Snapshot.posToIndex(box.size(), schematicPos)] = index;
    }
    createAndSendMessage(NET_SCAN, buffer -> MessageUtil.writeBlockPos(buffer, worldScanPos));
    sendNetworkUpdate(NET_RENDER_DATA);
    // Move scanPos along
    boxIterator.advance();
    if (boxIterator.hasFinished()) {
        scanning = false;
        boxIterator = null;
    }
}
Also used : ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) BoxIterator(buildcraft.lib.misc.data.BoxIterator) BitSet(java.util.BitSet) BlockPos(net.minecraft.util.math.BlockPos) Blueprint(buildcraft.builders.snapshot.Blueprint)

Aggregations

ISchematicBlock (buildcraft.api.schematics.ISchematicBlock)5 InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)3 Blueprint (buildcraft.builders.snapshot.Blueprint)2 Nonnull (javax.annotation.Nonnull)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 BlockPos (net.minecraft.util.math.BlockPos)2 BCLog (buildcraft.api.core.BCLog)1 SchematicBlockContext (buildcraft.api.schematics.SchematicBlockContext)1 ItemSnapshot (buildcraft.builders.item.ItemSnapshot)1 SchematicBlockManager (buildcraft.builders.snapshot.SchematicBlockManager)1 Snapshot (buildcraft.builders.snapshot.Snapshot)1 Header (buildcraft.builders.snapshot.Snapshot.Header)1 InventoryWrapper (buildcraft.lib.inventory.InventoryWrapper)1 ItemBC_Neptune (buildcraft.lib.item.ItemBC_Neptune)1 NBTUtilBC (buildcraft.lib.misc.NBTUtilBC)1 SoundUtil (buildcraft.lib.misc.SoundUtil)1 StackUtil (buildcraft.lib.misc.StackUtil)1 BoxIterator (buildcraft.lib.misc.data.BoxIterator)1 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)1 BitSet (java.util.BitSet)1