Search in sources :

Example 1 with IStorageScanner

use of mcjty.rftools.api.storage.IStorageScanner in project XNet by McJty.

the class RFToolsSupport method tickStorageScanner.

public static void tickStorageScanner(IControllerContext context, ItemConnectorSettings settings, TileEntity te, ItemChannelSettings channelSettings) {
    IStorageScanner scanner = (IStorageScanner) te;
    Predicate<ItemStack> extractMatcher = settings.getMatcher();
    Integer count = settings.getCount();
    int amount = 0;
    if (count != null) {
        amount = scanner.countItems(extractMatcher, true, count);
        if (amount < count) {
            return;
        }
    }
    ItemStack stack = scanner.requestItem(extractMatcher, true, settings.getStackMode() == ItemConnectorSettings.StackMode.STACK ? 64 : 1, true);
    if (!stack.isEmpty()) {
        // Now that we have a stack we first reduce the amount of the stack if we want to keep a certain
        // number of items
        int toextract = stack.getCount();
        if (count != null) {
            int canextract = amount - count;
            if (canextract <= 0) {
                return;
            }
            if (canextract < toextract) {
                toextract = canextract;
                if (toextract <= 0) {
                    stack.setCount(0);
                } else {
                    stack.setCount(toextract);
                }
            }
        }
        List<Pair<SidedConsumer, ItemConnectorSettings>> inserted = new ArrayList<>();
        int remaining = channelSettings.insertStackSimulate(inserted, context, stack);
        if (!inserted.isEmpty()) {
            if (context.checkAndConsumeRF(GeneralConfiguration.controllerOperationRFT)) {
                channelSettings.insertStackReal(context, inserted, scanner.requestItem(extractMatcher, false, toextract - remaining, true));
            }
        }
    }
}
Also used : IStorageScanner(mcjty.rftools.api.storage.IStorageScanner) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with IStorageScanner

use of mcjty.rftools.api.storage.IStorageScanner in project RFToolsControl by McJty.

the class ProcessorTileEntity method getStorageScanner.

private IStorageScanner getStorageScanner() {
    int card = getStorageCard();
    if (card == -1) {
        throw new ProgException(EXCEPT_MISSINGSTORAGECARD);
    }
    ItemStack storageStack = getStackInSlot(card);
    if (!storageStack.hasTagCompound()) {
        throw new ProgException(EXCEPT_MISSINGSTORAGECARD);
    }
    NBTTagCompound tagCompound = storageStack.getTagCompound();
    BlockPos c = new BlockPos(tagCompound.getInteger("monitorx"), tagCompound.getInteger("monitory"), tagCompound.getInteger("monitorz"));
    int dim = tagCompound.getInteger("monitordim");
    World world = DimensionManager.getWorld(dim);
    if (world == null) {
        throw new ProgException(EXCEPT_MISSINGSTORAGE);
    }
    if (!WorldTools.chunkLoaded(world, c)) {
        throw new ProgException(EXCEPT_MISSINGSTORAGE);
    }
    TileEntity te = world.getTileEntity(c);
    if (te == null) {
        throw new ProgException(EXCEPT_MISSINGSTORAGE);
    }
    if (!(te instanceof IStorageScanner)) {
        throw new ProgException(EXCEPT_MISSINGSTORAGE);
    }
    return (IStorageScanner) te;
}
Also used : ProgException(mcjty.rftoolscontrol.logic.running.ProgException) GenericEnergyReceiverTileEntity(mcjty.lib.entity.GenericEnergyReceiverTileEntity) CraftingStationTileEntity(mcjty.rftoolscontrol.blocks.craftingstation.CraftingStationTileEntity) WorkbenchTileEntity(mcjty.rftoolscontrol.blocks.workbench.WorkbenchTileEntity) NodeTileEntity(mcjty.rftoolscontrol.blocks.node.NodeTileEntity) MultiTankTileEntity(mcjty.rftoolscontrol.blocks.multitank.MultiTankTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IStorageScanner(mcjty.rftools.api.storage.IStorageScanner) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) World(net.minecraft.world.World)

Example 3 with IStorageScanner

use of mcjty.rftools.api.storage.IStorageScanner in project RFToolsControl by McJty.

the class ProcessorTileEntity method getIngredientsSmart.

public int getIngredientsSmart(IProgram program, Inventory inv, @Nonnull Inventory cardInv, ItemStack item, int slot1, int slot2, @Nonnull Inventory destInv) {
    IStorageScanner scanner = getScannerForInv(inv);
    IItemHandler handler = getHandlerForInv(inv);
    if (item.isEmpty()) {
        item = getCraftResult(program);
    }
    if (item.isEmpty()) {
        throw new ProgException(EXCEPT_MISSINGCRAFTRESULT);
    }
    IItemHandler destHandler = getHandlerForInv(destInv);
    if (destHandler == null) {
        throw new ProgException(EXCEPT_INVALIDINVENTORY);
    }
    IItemHandler cardHandler = getItemHandlerAt(cardInv);
    ItemStack card = findCraftingCard(cardHandler, item);
    if (card.isEmpty()) {
        throw new ProgException(EXCEPT_MISSINGCRAFTINGCARD);
    }
    CardInfo info = this.cardInfo[((RunningProgram) program).getCardIndex()];
    List<ItemStack> ingredients;
    if (CraftingCardItem.fitsGrid(card) && (slot2 - slot1 >= 8)) {
        // We have something that fits a crafting grid and we have enough room for a 3x3 grid
        ingredients = CraftingCardItem.getIngredientsGrid(card);
    } else {
        ingredients = CraftingCardItem.getIngredients(card);
    }
    boolean strictnbt = CraftingCardItem.isStrictNBT(card);
    List<ItemStack> needed = combineIngredients(ingredients);
    int requested = checkAvailableItemsAndRequestMissing(destInv, scanner, handler, needed);
    if (requested != 0) {
        return requested;
    }
    // We got everything;
    IItemHandler itemHandler = getItemHandler();
    int slot = slot1;
    for (ItemStack ingredient : ingredients) {
        int realSlot = info.getRealSlot(slot);
        if (!ingredient.isEmpty()) {
            ItemStack stack = InventoryTools.extractItem(handler, scanner, ingredient.getCount(), true, false, strictnbt, ingredient, null);
            if (!stack.isEmpty()) {
                itemHandler.insertItem(realSlot, stack, false);
            }
        }
        slot++;
    }
    return 0;
}
Also used : ProgException(mcjty.rftoolscontrol.logic.running.ProgException) IItemHandler(net.minecraftforge.items.IItemHandler) IStorageScanner(mcjty.rftools.api.storage.IStorageScanner) ItemStack(net.minecraft.item.ItemStack)

Example 4 with IStorageScanner

use of mcjty.rftools.api.storage.IStorageScanner in project RFToolsControl by McJty.

the class ProcessorTileEntity method pushItemsMulti.

public int pushItemsMulti(IProgram program, @Nullable Inventory inv, int slot1, int slot2, @Nullable Integer extSlot) {
    IItemHandler handler = getHandlerForInv(inv);
    IStorageScanner scanner = getScannerForInv(inv);
    CardInfo info = this.cardInfo[((RunningProgram) program).getCardIndex()];
    IItemHandler itemHandler = getItemHandler();
    int e = 0;
    if (extSlot != null) {
        e = extSlot;
    }
    int failed = 0;
    for (int slot = slot1; slot <= slot2; slot++) {
        int realSlot = info.getRealSlot(slot);
        ItemStack stack = itemHandler.getStackInSlot(realSlot);
        if (!stack.isEmpty()) {
            ItemStack remaining = InventoryTools.insertItem(handler, scanner, stack, extSlot == null ? null : e);
            if (!remaining.isEmpty()) {
                failed++;
            }
            inventoryHelper.setStackInSlot(realSlot, remaining);
        }
        e++;
    }
    return failed;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IStorageScanner(mcjty.rftools.api.storage.IStorageScanner) ItemStack(net.minecraft.item.ItemStack)

Example 5 with IStorageScanner

use of mcjty.rftools.api.storage.IStorageScanner in project RFToolsControl by McJty.

the class ProcessorTileEntity method getIngredients.

public int getIngredients(IProgram program, Inventory inv, Inventory cardInv, ItemStack item, int slot1, int slot2) {
    IStorageScanner scanner = getScannerForInv(inv);
    IItemHandler handler = getHandlerForInv(inv);
    if (item.isEmpty()) {
        item = getCraftResult(program);
    }
    if (item.isEmpty()) {
        throw new ProgException(EXCEPT_MISSINGCRAFTRESULT);
    }
    IItemHandler cardHandler = getItemHandlerAt(cardInv);
    ItemStack card = findCraftingCard(cardHandler, item);
    if (card.isEmpty()) {
        throw new ProgException(EXCEPT_MISSINGCRAFTINGCARD);
    }
    CardInfo info = this.cardInfo[((RunningProgram) program).getCardIndex()];
    IItemHandler itemHandler = getItemHandler();
    int slot = slot1;
    List<ItemStack> ingredients;
    if (CraftingCardItem.fitsGrid(card) && (slot2 - slot1 >= 8)) {
        // We have something that fits a crafting grid and we have enough room for a 3x3 grid
        ingredients = CraftingCardItem.getIngredientsGrid(card);
    } else {
        ingredients = CraftingCardItem.getIngredients(card);
    }
    boolean strictnbt = CraftingCardItem.isStrictNBT(card);
    int failed = 0;
    for (ItemStack ingredient : ingredients) {
        int realSlot = info.getRealSlot(slot);
        if (!ingredient.isEmpty()) {
            ItemStack stack = InventoryTools.extractItem(handler, scanner, ingredient.getCount(), true, false, strictnbt, ingredient, null);
            if (!stack.isEmpty()) {
                ItemStack remainder = itemHandler.insertItem(realSlot, stack, false);
                if (!remainder.isEmpty()) {
                    InventoryTools.insertItem(handler, scanner, remainder, null);
                }
            } else {
                failed++;
            }
        }
        slot++;
    }
    return failed;
}
Also used : ProgException(mcjty.rftoolscontrol.logic.running.ProgException) IItemHandler(net.minecraftforge.items.IItemHandler) IStorageScanner(mcjty.rftools.api.storage.IStorageScanner) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IStorageScanner (mcjty.rftools.api.storage.IStorageScanner)7 ItemStack (net.minecraft.item.ItemStack)7 IItemHandler (net.minecraftforge.items.IItemHandler)5 ProgException (mcjty.rftoolscontrol.logic.running.ProgException)4 ArrayList (java.util.ArrayList)1 GenericEnergyReceiverTileEntity (mcjty.lib.entity.GenericEnergyReceiverTileEntity)1 CraftingStationTileEntity (mcjty.rftoolscontrol.blocks.craftingstation.CraftingStationTileEntity)1 MultiTankTileEntity (mcjty.rftoolscontrol.blocks.multitank.MultiTankTileEntity)1 NodeTileEntity (mcjty.rftoolscontrol.blocks.node.NodeTileEntity)1 WorkbenchTileEntity (mcjty.rftoolscontrol.blocks.workbench.WorkbenchTileEntity)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1 Pair (org.apache.commons.lang3.tuple.Pair)1