Search in sources :

Example 6 with InvalidInputDataException

use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.

the class Blueprint method deserializeNBT.

@Override
public void deserializeNBT(NBTTagCompound nbt) throws InvalidInputDataException {
    super.deserializeNBT(nbt);
    palette.clear();
    for (NBTTagCompound schematicBlockTag : NBTUtilBC.readCompoundList(nbt.getTag("palette")).collect(Collectors.toList())) {
        // TODO: Allow reading blueprints partially - invalid elements should be replaced with air
        // (Although this needs to add a "pass-through" ISchematicBlock that will store the
        // invalid NBTTagCompound and show up in the tooltip as an error, so that we can migrate
        // schematics through mod additions/deletions)
        palette.add(SchematicBlockManager.readFromNBT(schematicBlockTag));
    }
    data = new int[Snapshot.getDataSize(size)];
    NBTTagList serializedDataList = nbt.hasKey("data", Constants.NBT.TAG_LIST) ? nbt.getTagList("data", Constants.NBT.TAG_INT) : null;
    int[] serializedDataIntArray = nbt.hasKey("data", Constants.NBT.TAG_INT_ARRAY) ? nbt.getIntArray("data") : null;
    if (serializedDataIntArray == null && serializedDataList == null) {
        throw new InvalidInputDataException("Can't read a blueprint with no data!");
    }
    int serializedDataLength = serializedDataList == null ? serializedDataIntArray.length : serializedDataList.tagCount();
    if (serializedDataLength != getDataSize()) {
        throw new InvalidInputDataException("Serialized data has length of " + serializedDataLength + ", but we expected " + getDataSize() + " (" + size.toString() + ")");
    }
    for (int z = 0; z < size.getZ(); z++) {
        for (int y = 0; y < size.getY(); y++) {
            for (int x = 0; x < size.getX(); x++) {
                data[posToIndex(x, y, z)] = serializedDataList == null ? serializedDataIntArray[posToIndex(x, y, z)] : serializedDataList.getIntAt(posToIndex(x, y, z));
            }
        }
    }
    for (NBTTagCompound schematicEntityTag : NBTUtilBC.readCompoundList(nbt.getTag("entities")).collect(Collectors.toList())) {
        entities.add(SchematicEntityManager.readFromNBT(schematicEntityTag));
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 7 with InvalidInputDataException

use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.

the class NbtSquisher method expand.

public static NBTTagCompound expand(InputStream stream) throws IOException {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    stream.mark(5);
    int byte1 = stream.read();
    int byte2 = stream.read();
    if (byte1 == NbtSquishConstants.BUILDCRAFT_MAGIC_1 && byte2 == NbtSquishConstants.BUILDCRAFT_MAGIC_2) {
        // Defiantly a BC stream
        int type = stream.read();
        if (type == TYPE_MC) {
            return CompressedStreamTools.read(new DataInputStream(stream));
        } else if (type == TYPE_MC_GZIP) {
            return CompressedStreamTools.readCompressed(stream);
        } else if (type == TYPE_BC_1) {
            return readBuildCraftV1Direct(new DataInputStream(stream));
        } else if (type == TYPE_BC_1_GZIP) {
            return readBuildCraftV1Direct(new DataInputStream(new GZIPInputStream(stream)));
        } else {
            throw new InvalidInputDataException("Cannot handle BuildCraft saved NBT type " + type);
        }
    } else if (byte1 == NbtSquishConstants.GZIP_MAGIC_1 && byte2 == NbtSquishConstants.GZIP_MAGIC_2) {
        // Defiantly a GZIP stream
        // Assume its a vanilla file
        stream.reset();
        return CompressedStreamTools.readCompressed(stream);
    }
    // Its not a new BC style nbt, try to red it as if it was an older style nbt
    // Reset + mark the same point, this time we only want to reset back 1 or 2 bytes
    stream.reset();
    stream.mark(5);
    int type = stream.read();
    if (type == TYPE_MC) {
        return CompressedStreamTools.read(new DataInputStream(stream));
    } else if (type == TYPE_MC_GZIP) {
        return CompressedStreamTools.readCompressed(stream);
    } else if (type == TYPE_BC_1) {
        return readBuildCraftV1Direct(new DataInputStream(stream));
    } else if (type == TYPE_BC_1_GZIP) {
        return readBuildCraftV1Direct(new DataInputStream(new GZIPInputStream(stream)));
    } else if (type == Constants.NBT.TAG_COMPOUND) {
        // Assume vanilla, but reset back to the first byte as vanilla needs
        stream.reset();
        return CompressedStreamTools.read(new DataInputStream(stream));
    } else {
        throw new InvalidInputDataException("Cannot handle unknown saved NBT type " + type);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream)

Example 8 with InvalidInputDataException

use of buildcraft.api.core.InvalidInputDataException 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 9 with InvalidInputDataException

use of buildcraft.api.core.InvalidInputDataException 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)

Example 10 with InvalidInputDataException

use of buildcraft.api.core.InvalidInputDataException in project BuildCraft by BuildCraft.

the class Snapshot method readFromNBT.

public static Snapshot readFromNBT(NBTTagCompound nbt) throws InvalidInputDataException {
    NBTBase tag = nbt.getTag("type");
    EnumSnapshotType type = NBTUtilBC.readEnum(tag, EnumSnapshotType.class);
    if (type == null) {
        throw new InvalidInputDataException("Unknown snapshot type " + tag);
    }
    Snapshot snapshot = Snapshot.create(type);
    snapshot.deserializeNBT(nbt);
    return snapshot;
}
Also used : InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) NBTBase(net.minecraft.nbt.NBTBase) EnumSnapshotType(buildcraft.api.enums.EnumSnapshotType)

Aggregations

InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)12 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)5 Nonnull (javax.annotation.Nonnull)4 ISchematicBlock (buildcraft.api.schematics.ISchematicBlock)3 EnumPipePart (buildcraft.api.core.EnumPipePart)2 IStatement (buildcraft.api.statements.IStatement)2 ItemStack (net.minecraft.item.ItemStack)2 EnumFacing (net.minecraft.util.EnumFacing)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 BCLog (buildcraft.api.core.BCLog)1 EnumSnapshotType (buildcraft.api.enums.EnumSnapshotType)1 ISchematicEntity (buildcraft.api.schematics.ISchematicEntity)1 SchematicBlockContext (buildcraft.api.schematics.SchematicBlockContext)1 IAction (buildcraft.api.statements.IAction)1 ITrigger (buildcraft.api.statements.ITrigger)1 IParamReaderBuf (buildcraft.api.statements.StatementManager.IParamReaderBuf)1 IFlowItems (buildcraft.api.transport.pipe.IFlowItems)1 IItemPipe (buildcraft.api.transport.pipe.IItemPipe)1 IPipe (buildcraft.api.transport.pipe.IPipe)1 PipeDefinition (buildcraft.api.transport.pipe.PipeDefinition)1