Search in sources :

Example 1 with PacketRecipeTransfer

use of mezz.jei.network.packets.PacketRecipeTransfer in project AstralSorcery by HellFirePvP.

the class TieredAltarRecipeTransferHandler method transferRecipe.

@Nullable
@Override
public IRecipeTransferError transferRecipe(C container, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer, boolean doTransfer) {
    if (!ServerInfo.isJeiOnServer()) {
        String tooltipMessage = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.no.server");
        return handlerHelper.createUserErrorWithTooltip(tooltipMessage);
    }
    if (!containerClass.isAssignableFrom(container.getClass())) {
        return handlerHelper.createInternalError();
    }
    IRecipeCategory<?> category = recipeLayout.getRecipeCategory();
    if (!(category instanceof CategoryAltar)) {
        return handlerHelper.createInternalError();
    }
    AltarType recipeTier = ((CategoryAltar) category).getAltarType();
    AltarType altarTier = container.getTileEntity().getAltarType();
    Map<Integer, Slot> inventorySlots = new HashMap<>();
    for (Slot slot : container.inventorySlots.subList(0, 36)) {
        inventorySlots.put(slot.slotNumber, slot);
    }
    Map<Integer, Slot> craftingSlots = new HashMap<>();
    for (Slot slot : container.inventorySlots.subList(36, 36 + maxListSize)) {
        craftingSlots.put(slot.slotNumber, slot);
    }
    int inputCount = 0;
    IGuiItemStackGroup itemStackGroup = recipeLayout.getItemStacks();
    // Remove relay inputs from the input grid.
    Map<Integer, IGuiIngredient<ItemStack>> itemStacks = new HashMap<>();
    for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : itemStackGroup.getGuiIngredients().entrySet()) {
        if (entry.getKey() < 25) {
            itemStacks.put(entry.getKey(), entry.getValue());
        }
    }
    for (IGuiIngredient<ItemStack> ingredient : itemStacks.values()) {
        if (ingredient.isInput() && !ingredient.getAllIngredients().isEmpty()) {
            inputCount++;
        }
    }
    if (inputCount > craftingSlots.size()) {
        LOGGER.error("Recipe Transfer does not work for container {}", container.getClass());
        return handlerHelper.createInternalError();
    }
    Map<Integer, ItemStack> availableItemStacks = new HashMap<>();
    int filledCraftSlotCount = 0;
    int emptySlotCount = 0;
    for (Slot slot : craftingSlots.values()) {
        final ItemStack stack = slot.getStack();
        if (!stack.isEmpty()) {
            if (!slot.canTakeStack(player)) {
                LOGGER.error("Recipe Transfer does not work for container {}. Player can't move item out of Crafting Slot number {}", container.getClass(), slot.slotNumber);
                return handlerHelper.createInternalError();
            }
            filledCraftSlotCount++;
            availableItemStacks.put(slot.slotNumber, stack.copy());
        }
    }
    for (Slot slot : inventorySlots.values()) {
        final ItemStack stack = slot.getStack();
        if (!stack.isEmpty()) {
            availableItemStacks.put(slot.slotNumber, stack.copy());
        } else {
            emptySlotCount++;
        }
    }
    // check if we have enough inventory space to shuffle items around to their final locations
    if (filledCraftSlotCount - inputCount > emptySlotCount) {
        String message = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.inventory.full");
        return handlerHelper.createUserErrorWithTooltip(message);
    }
    RecipeTransferUtil.MatchingItemsResult matchingItemsResult = RecipeTransferUtil.getMatchingItems(stackHelper, availableItemStacks, itemStacks);
    if (matchingItemsResult.missingItems.size() > 0) {
        String message = Translator.translateToLocal("jei.tooltip.error.recipe.transfer.missing");
        return handlerHelper.createUserErrorForSlots(message, matchingItemsResult.missingItems);
    }
    List<Integer> craftingSlotIndexes = new ArrayList<>(craftingSlots.keySet());
    Collections.sort(craftingSlotIndexes);
    List<Integer> inventorySlotIndexes = new ArrayList<>(inventorySlots.keySet());
    Collections.sort(inventorySlotIndexes);
    Map<Integer, Integer> matchIndices = MapStream.of(matchingItemsResult.matchingItems).mapKey(container::translateIndex).toMap();
    // check that the slots exist and can be altered
    for (Map.Entry<Integer, Integer> entry : matchIndices.entrySet()) {
        int craftNumber = entry.getKey();
        int slotNumber = craftingSlotIndexes.get(craftNumber);
        if (slotNumber < 0 || slotNumber >= container.inventorySlots.size()) {
            LOGGER.error("Recipes Transfer references slot {} outside of the inventory's size {}", slotNumber, container.inventorySlots.size());
            return handlerHelper.createInternalError();
        }
    }
    if (doTransfer) {
        PacketRecipeTransfer packet = new PacketRecipeTransfer(matchIndices, craftingSlotIndexes, inventorySlotIndexes, maxTransfer, true);
        Network.sendPacketToServer(packet);
    }
    return null;
}
Also used : IGuiItemStackGroup(mezz.jei.api.gui.ingredient.IGuiItemStackGroup) AltarType(hellfirepvp.astralsorcery.common.block.tile.altar.AltarType) RecipeTransferUtil(mezz.jei.transfer.RecipeTransferUtil) IGuiIngredient(mezz.jei.api.gui.ingredient.IGuiIngredient) PacketRecipeTransfer(mezz.jei.network.packets.PacketRecipeTransfer) Slot(net.minecraft.inventory.container.Slot) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Aggregations

AltarType (hellfirepvp.astralsorcery.common.block.tile.altar.AltarType)1 Nullable (javax.annotation.Nullable)1 IGuiIngredient (mezz.jei.api.gui.ingredient.IGuiIngredient)1 IGuiItemStackGroup (mezz.jei.api.gui.ingredient.IGuiItemStackGroup)1 PacketRecipeTransfer (mezz.jei.network.packets.PacketRecipeTransfer)1 RecipeTransferUtil (mezz.jei.transfer.RecipeTransferUtil)1 Slot (net.minecraft.inventory.container.Slot)1 ItemStack (net.minecraft.item.ItemStack)1