Search in sources :

Example 1 with AutoCraftingInventory

use of logisticspipes.blocks.crafting.AutoCraftingInventory in project LogisticsPipes by RS485.

the class PipeBlockRequestTable method cycleRecipe.

public void cycleRecipe(boolean down) {
    cacheRecipe();
    if (targetType == null) {
        return;
    }
    cache = null;
    AutoCraftingInventory craftInv = new AutoCraftingInventory(null);
    for (int i = 0; i < 9; i++) {
        craftInv.setInventorySlotContents(i, matrix.getStackInSlot(i));
    }
    List<IRecipe> list = new ArrayList<>();
    for (IRecipe r : CraftingUtil.getRecipeList()) {
        if (r.matches(craftInv, getWorld())) {
            list.add(r);
        }
    }
    if (list.size() > 1) {
        boolean found = false;
        IRecipe prev = null;
        for (IRecipe recipe : list) {
            if (found) {
                cache = recipe;
                break;
            }
            craftInv = new AutoCraftingInventory(null);
            for (int i = 0; i < 9; i++) {
                craftInv.setInventorySlotContents(i, matrix.getStackInSlot(i));
            }
            if (targetType == ItemIdentifier.get(recipe.getCraftingResult(craftInv))) {
                if (down) {
                    found = true;
                } else {
                    if (prev == null) {
                        cache = list.get(list.size() - 1);
                    } else {
                        cache = prev;
                    }
                    break;
                }
            }
            prev = recipe;
        }
        if (cache == null) {
            cache = list.get(0);
        }
        craftInv = new AutoCraftingInventory(null);
        for (int i = 0; i < 9; i++) {
            craftInv.setInventorySlotContents(i, matrix.getStackInSlot(i));
        }
        targetType = ItemIdentifier.get(cache.getCraftingResult(craftInv));
    }
    if (!localGuiWatcher.isEmpty() && getWorld() != null && MainProxy.isServer(getWorld())) {
        MainProxy.sendToPlayerList(PacketHandler.getPacket(CraftingSetType.class).setTargetType(targetType).setTilePos(container), localGuiWatcher);
    }
    cacheRecipe();
}
Also used : CraftingSetType(logisticspipes.network.packets.block.CraftingSetType) IRecipe(net.minecraft.item.crafting.IRecipe) AutoCraftingInventory(logisticspipes.blocks.crafting.AutoCraftingInventory) ArrayList(java.util.ArrayList)

Example 2 with AutoCraftingInventory

use of logisticspipes.blocks.crafting.AutoCraftingInventory in project LogisticsPipes by RS485.

the class PipeBlockRequestTable method getOutput.

public ItemStack getOutput(boolean oreDict) {
    if (cache == null) {
        cacheRecipe();
        if (cache == null) {
            return null;
        }
    }
    if (resultInv.getIDStackInSlot(0) == null) {
        return null;
    }
    int[] toUse = new int[9];
    int[] used = new int[inv.getSizeInventory()];
    outer: for (int i = 0; i < 9; i++) {
        ItemStack item = matrix.getStackInSlot(i);
        if (item == null) {
            toUse[i] = -1;
            continue;
        }
        ItemIdentifier ident = ItemIdentifier.get(item);
        for (int j = 0; j < inv.getSizeInventory(); j++) {
            item = inv.getStackInSlot(j);
            if (item == null) {
                continue;
            }
            ItemIdentifier withIdent = ItemIdentifier.get(item);
            if (ident.equalsForCrafting(withIdent)) {
                if (item.stackSize > used[j]) {
                    used[j]++;
                    toUse[i] = j;
                    continue outer;
                }
            }
            if (oreDict) {
                if (ident.getDictIdentifiers() != null && withIdent.getDictIdentifiers() != null && ident.getDictIdentifiers().canMatch(withIdent.getDictIdentifiers(), true, false)) {
                    if (item.stackSize > used[j]) {
                        used[j]++;
                        toUse[i] = j;
                        continue outer;
                    }
                }
            }
        }
        //Not enough material
        return null;
    }
    //TODO
    AutoCraftingInventory crafter = new AutoCraftingInventory(null);
    for (int i = 0; i < 9; i++) {
        int j = toUse[i];
        if (j != -1) {
            crafter.setInventorySlotContents(i, inv.getStackInSlot(j));
        }
    }
    if (!cache.matches(crafter, getWorld())) {
        //Fix MystCraft
        return null;
    }
    ItemStack result = cache.getCraftingResult(crafter);
    if (result == null) {
        return null;
    }
    if (!resultInv.getIDStackInSlot(0).getItem().equalsWithoutNBT(ItemIdentifier.get(result))) {
        return null;
    }
    //TODO
    crafter = new AutoCraftingInventory(null);
    for (int i = 0; i < 9; i++) {
        int j = toUse[i];
        if (j != -1) {
            crafter.setInventorySlotContents(i, inv.decrStackSize(j, 1));
        }
    }
    result = cache.getCraftingResult(crafter);
    if (fake == null) {
        fake = MainProxy.getFakePlayer(container);
    }
    result = result.copy();
    SlotCrafting craftingSlot = new SlotCrafting(fake, crafter, resultInv, 0, 0, 0);
    craftingSlot.onPickupFromSlot(fake, result);
    for (int i = 0; i < 9; i++) {
        ItemStack left = crafter.getStackInSlot(i);
        crafter.setInventorySlotContents(i, null);
        if (left != null) {
            left.stackSize = inv.addCompressed(left, false);
            if (left.stackSize > 0) {
                ItemIdentifierInventory.dropItems(getWorld(), left, getX(), getY(), getZ());
            }
        }
    }
    for (int i = 0; i < fake.inventory.getSizeInventory(); i++) {
        ItemStack left = fake.inventory.getStackInSlot(i);
        fake.inventory.setInventorySlotContents(i, null);
        if (left != null) {
            left.stackSize = inv.addCompressed(left, false);
            if (left.stackSize > 0) {
                ItemIdentifierInventory.dropItems(getWorld(), left, getX(), getY(), getZ());
            }
        }
    }
    return result;
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) AutoCraftingInventory(logisticspipes.blocks.crafting.AutoCraftingInventory) ItemStack(net.minecraft.item.ItemStack) SlotCrafting(net.minecraft.inventory.SlotCrafting)

Example 3 with AutoCraftingInventory

use of logisticspipes.blocks.crafting.AutoCraftingInventory in project LogisticsPipes by RS485.

the class PipeBlockRequestTable method cacheRecipe.

public void cacheRecipe() {
    ItemIdentifier oldTargetType = targetType;
    cache = null;
    resultInv.clearInventorySlotContents(0);
    AutoCraftingInventory craftInv = new AutoCraftingInventory(null);
    for (int i = 0; i < 9; i++) {
        craftInv.setInventorySlotContents(i, matrix.getStackInSlot(i));
    }
    List<IRecipe> list = new ArrayList<>();
    for (IRecipe r : CraftingUtil.getRecipeList()) {
        if (r.matches(craftInv, getWorld())) {
            list.add(r);
        }
    }
    if (list.size() == 1) {
        cache = list.get(0);
        resultInv.setInventorySlotContents(0, cache.getCraftingResult(craftInv));
        targetType = null;
    } else if (list.size() > 1) {
        if (targetType != null) {
            for (IRecipe recipe : list) {
                craftInv = new AutoCraftingInventory(null);
                for (int i = 0; i < 9; i++) {
                    craftInv.setInventorySlotContents(i, matrix.getStackInSlot(i));
                }
                ItemStack result = recipe.getCraftingResult(craftInv);
                if (targetType == ItemIdentifier.get(result)) {
                    resultInv.setInventorySlotContents(0, result);
                    cache = recipe;
                    break;
                }
            }
        }
        if (cache == null) {
            cache = list.get(0);
            ItemStack result = cache.getCraftingResult(craftInv);
            resultInv.setInventorySlotContents(0, result);
            targetType = ItemIdentifier.get(result);
        }
    } else {
        targetType = null;
    }
    if (targetType != oldTargetType && !localGuiWatcher.isEmpty() && getWorld() != null && MainProxy.isServer(getWorld())) {
        MainProxy.sendToPlayerList(PacketHandler.getPacket(CraftingSetType.class).setTargetType(targetType).setTilePos(container), localGuiWatcher);
    }
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) CraftingSetType(logisticspipes.network.packets.block.CraftingSetType) IRecipe(net.minecraft.item.crafting.IRecipe) AutoCraftingInventory(logisticspipes.blocks.crafting.AutoCraftingInventory) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack)

Aggregations

AutoCraftingInventory (logisticspipes.blocks.crafting.AutoCraftingInventory)3 ArrayList (java.util.ArrayList)2 CraftingSetType (logisticspipes.network.packets.block.CraftingSetType)2 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)2 ItemStack (net.minecraft.item.ItemStack)2 IRecipe (net.minecraft.item.crafting.IRecipe)2 SlotCrafting (net.minecraft.inventory.SlotCrafting)1