Search in sources :

Example 1 with CraftingInventory

use of net.minecraft.inventory.CraftingInventory in project minecolonies by Minecolonies.

the class PrivateCraftingTeachingTransferHandler method transferRecipe.

@Nullable
@Override
public IRecipeTransferError transferRecipe(@NotNull final ContainerCrafting craftingGUIBuilding, @NotNull final Object recipe, @NotNull final IRecipeLayout recipeLayout, @NotNull final PlayerEntity player, final boolean maxTransfer, final boolean doTransfer) {
    final IGuiItemStackGroup itemStackGroup = recipeLayout.getItemStacks();
    // compact the crafting grid into a 2x2 area
    final Map<Integer, ItemStack> guiIngredients = new HashMap<>();
    guiIngredients.put(0, ItemStackUtils.EMPTY);
    guiIngredients.put(1, ItemStackUtils.EMPTY);
    guiIngredients.put(3, ItemStackUtils.EMPTY);
    guiIngredients.put(4, ItemStackUtils.EMPTY);
    // indexes that do not fit into the player crafting grid
    final Set<Integer> badIndexes;
    if (craftingGUIBuilding.isComplete()) {
        guiIngredients.put(2, ItemStackUtils.EMPTY);
        guiIngredients.put(5, ItemStackUtils.EMPTY);
        guiIngredients.put(6, ItemStackUtils.EMPTY);
        guiIngredients.put(7, ItemStackUtils.EMPTY);
        guiIngredients.put(8, ItemStackUtils.EMPTY);
        badIndexes = ImmutableSet.of();
    } else {
        badIndexes = ImmutableSet.of(2, 5, 6, 7, 8);
    }
    int inputIndex = 0;
    for (final IGuiIngredient<ItemStack> ingredient : itemStackGroup.getGuiIngredients().values()) {
        if (ingredient.isInput()) {
            if (!ingredient.getAllIngredients().isEmpty()) {
                if (badIndexes.contains(inputIndex)) {
                    final ITextComponent tooltipMessage = new TranslationTextComponent("jei.tooltip.error.recipe.transfer.too.large.player.inventory");
                    return handlerHelper.createUserErrorForSlots(tooltipMessage, badIndexes);
                }
                guiIngredients.put(inputIndex, ingredient.getDisplayedIngredient());
            }
            inputIndex++;
        }
    }
    if (doTransfer) {
        final CraftingInventory craftMatrix = craftingGUIBuilding.getInv();
        if (craftingGUIBuilding.isComplete()) {
            craftMatrix.setItem(0, guiIngredients.get(0));
            craftMatrix.setItem(1, guiIngredients.get(1));
            craftMatrix.setItem(2, guiIngredients.get(2));
            craftMatrix.setItem(3, guiIngredients.get(3));
            craftMatrix.setItem(4, guiIngredients.get(4));
            craftMatrix.setItem(5, guiIngredients.get(5));
            craftMatrix.setItem(6, guiIngredients.get(6));
            craftMatrix.setItem(7, guiIngredients.get(7));
            craftMatrix.setItem(8, guiIngredients.get(8));
        } else {
            craftMatrix.setItem(0, guiIngredients.get(0));
            craftMatrix.setItem(1, guiIngredients.get(1));
            craftMatrix.setItem(2, guiIngredients.get(3));
            craftMatrix.setItem(3, guiIngredients.get(4));
        }
        final TransferRecipeCraftingTeachingMessage message = new TransferRecipeCraftingTeachingMessage(guiIngredients, craftingGUIBuilding.isComplete());
        Network.getNetwork().sendToServer(message);
    }
    return null;
}
Also used : CraftingInventory(net.minecraft.inventory.CraftingInventory) TransferRecipeCraftingTeachingMessage(com.minecolonies.coremod.network.messages.server.TransferRecipeCraftingTeachingMessage) HashMap(java.util.HashMap) IGuiItemStackGroup(mezz.jei.api.gui.ingredient.IGuiItemStackGroup) ITextComponent(net.minecraft.util.text.ITextComponent) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 2 with CraftingInventory

use of net.minecraft.inventory.CraftingInventory in project minecolonies by Minecolonies.

the class JEIPlugin method buildVanillaRecipesMap.

private Map<IRecipeType<?>, List<IGenericRecipe>> buildVanillaRecipesMap() {
    final RecipeManager recipeManager = Minecraft.getInstance().level.getRecipeManager();
    final List<IGenericRecipe> craftingRecipes = new ArrayList<>();
    for (final IRecipe<CraftingInventory> recipe : recipeManager.byType(IRecipeType.CRAFTING).values()) {
        if (!recipe.canCraftInDimensions(3, 3))
            continue;
        tryAddingVanillaRecipe(craftingRecipes, recipe);
    }
    final List<IGenericRecipe> smeltingRecipes = new ArrayList<>();
    for (final IRecipe<IInventory> recipe : recipeManager.byType(IRecipeType.SMELTING).values()) {
        tryAddingVanillaRecipe(smeltingRecipes, recipe);
    }
    return new ImmutableMap.Builder<IRecipeType<?>, List<IGenericRecipe>>().put(IRecipeType.CRAFTING, craftingRecipes).put(IRecipeType.SMELTING, smeltingRecipes).build();
}
Also used : CraftingInventory(net.minecraft.inventory.CraftingInventory) IInventory(net.minecraft.inventory.IInventory) IRecipeType(net.minecraft.item.crafting.IRecipeType) RecipeManager(net.minecraft.item.crafting.RecipeManager) IRecipeManager(mezz.jei.api.recipe.IRecipeManager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IGenericRecipe(com.minecolonies.api.crafting.IGenericRecipe) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with CraftingInventory

use of net.minecraft.inventory.CraftingInventory in project minecolonies by Minecolonies.

the class GenericRecipe method calculateSecondaryOutputs.

@NotNull
private static List<ItemStack> calculateSecondaryOutputs(@NotNull final IRecipe<?> recipe) {
    if (recipe instanceof ICraftingRecipe) {
        final List<Ingredient> inputs = recipe.getIngredients();
        final CraftingInventory inv = new CraftingInventory(new Container(ContainerType.CRAFTING, 0) {

            @Override
            public boolean stillValid(@NotNull final PlayerEntity playerIn) {
                return false;
            }
        }, 3, 3);
        for (int slot = 0; slot < inputs.size(); ++slot) {
            final ItemStack[] stacks = inputs.get(slot).getItems();
            if (stacks.length > 0) {
                inv.setItem(slot, stacks[0]);
            }
        }
        if (((ICraftingRecipe) recipe).matches(inv, null)) {
            return ((ICraftingRecipe) recipe).getRemainingItems(inv).stream().filter(ItemStackUtils::isNotEmpty).filter(// this is filtered out of the inputs too
            stack -> stack.getItem() != buildTool.get()).collect(Collectors.toList());
        }
    }
    return Collections.emptyList();
}
Also used : CraftingInventory(net.minecraft.inventory.CraftingInventory) IRecipe(net.minecraft.item.crafting.IRecipe) Ingredient(net.minecraft.item.crafting.Ingredient) java.util(java.util) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) Container(net.minecraft.inventory.container.Container) IColonyManager(com.minecolonies.api.colony.IColonyManager) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) FurnaceRecipe(net.minecraft.item.crafting.FurnaceRecipe) Collectors(java.util.stream.Collectors) Blocks(net.minecraft.block.Blocks) ContainerType(net.minecraft.inventory.container.ContainerType) ITextComponent(net.minecraft.util.text.ITextComponent) ItemStack(net.minecraft.item.ItemStack) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) Block(net.minecraft.block.Block) CraftingInventory(net.minecraft.inventory.CraftingInventory) ResourceLocation(net.minecraft.util.ResourceLocation) ModItems.buildTool(com.ldtteam.structurize.items.ModItems.buildTool) NotNull(org.jetbrains.annotations.NotNull) OptionalPredicate(com.minecolonies.api.util.OptionalPredicate) Container(net.minecraft.inventory.container.Container) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) Ingredient(net.minecraft.item.crafting.Ingredient) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with CraftingInventory

use of net.minecraft.inventory.CraftingInventory in project MCMOD-Industria by M-Marvin.

the class TileEntityMStoringCraftingTable method tick.

@Override
public void tick() {
    if (!this.level.isClientSide()) {
        ElectricityNetworkHandler.getHandlerForWorld(level).updateNetwork(level, worldPosition);
        ElectricityNetwork network = ElectricityNetworkHandler.getHandlerForWorld(level).getNetwork(worldPosition);
        CraftingInventory craftMatrix = makeCraftMatrix();
        ICraftingRecipe recipe = findRecipe(craftMatrix);
        canWork = false;
        if (recipe != null) {
            ItemStack result = recipe.assemble(craftMatrix);
            ItemStack stack = this.getItem(9);
            if (stack.isEmpty()) {
                canWork = true;
            } else if (stack.getItem() == result.getItem() && stack.getCount() + result.getCount() <= result.getMaxStackSize()) {
                canWork = true;
            }
        }
        this.hasPower = network.canMachinesRun() == Voltage.NormalVoltage;
        this.isWorking = this.canWork && hasPower;
        if (this.isWorking) {
            if (this.progress >= 100) {
                ItemStack result = recipe.assemble(craftMatrix);
                ItemStack stack = this.getItem(9);
                this.remainingItems = recipe.getRemainingItems(craftMatrix);
                if (stack.isEmpty()) {
                    this.inventory.set(9, result.copy());
                } else if (stack.getItem() == result.getItem() && stack.getCount() + result.getCount() <= result.getMaxStackSize()) {
                    stack.grow(result.getCount());
                }
                for (int i = 0; i < 9; i++) {
                    if (this.inventory.get(i).getItem() != this.inventory.get(10).getItem())
                        this.inventory.get(i).shrink(1);
                }
                for (int i = 0; i < 9; i++) {
                    ItemStack remainingItem = remainingItems.get(i);
                    if (!remainingItem.isEmpty())
                        this.inventory.set(i, remainingItem.copy());
                }
                this.progress = 0;
            } else {
                this.progress++;
            }
        }
    }
}
Also used : CraftingInventory(net.minecraft.inventory.CraftingInventory) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) ItemStack(net.minecraft.item.ItemStack) ElectricityNetwork(de.industria.util.handler.ElectricityNetworkHandler.ElectricityNetwork)

Example 5 with CraftingInventory

use of net.minecraft.inventory.CraftingInventory in project SophisticatedBackpacks by P3pp3rF1y.

the class RecipeHelper method getCompactingResult.

private static CompactingResult getCompactingResult(Item item, World w, int width, int height) {
    CompactedItem compactedItem = new CompactedItem(item, width, height);
    if (COMPACTING_RESULTS.containsKey(compactedItem)) {
        return COMPACTING_RESULTS.get(compactedItem);
    }
    CraftingInventory craftingInventory = getFilledCraftingInventory(item, width, height);
    List<ItemStack> remainingItems = new ArrayList<>();
    ItemStack result = w.getRecipeManager().getRecipeFor(IRecipeType.CRAFTING, craftingInventory, w).map(r -> {
        r.getRemainingItems(craftingInventory).forEach(stack -> {
            if (!stack.isEmpty()) {
                remainingItems.add(stack);
            }
        });
        return r.assemble(craftingInventory);
    }).orElse(ItemStack.EMPTY);
    CompactingResult compactingResult = new CompactingResult(result, remainingItems);
    if (!result.isEmpty()) {
        COMPACTING_RESULTS.put(compactedItem, compactingResult);
    }
    return compactingResult;
}
Also used : CraftingInventory(net.minecraft.inventory.CraftingInventory) LoadingCache(com.google.common.cache.LoadingCache) Item(net.minecraft.item.Item) StonecuttingRecipe(net.minecraft.item.crafting.StonecuttingRecipe) Container(net.minecraft.inventory.container.Container) AbstractCookingRecipe(net.minecraft.item.crafting.AbstractCookingRecipe) HashMap(java.util.HashMap) IRecipeType(net.minecraft.item.crafting.IRecipeType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map) CraftingInventory(net.minecraft.inventory.CraftingInventory) NonNullList(net.minecraft.util.NonNullList) WeakReference(java.lang.ref.WeakReference) LinkedList(java.util.LinkedList) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) Set(java.util.Set) CacheLoader(com.google.common.cache.CacheLoader) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) IInventory(net.minecraft.inventory.IInventory) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) Queue(java.util.Queue) RecipeWrapper(net.minecraftforge.items.wrapper.RecipeWrapper) Collections(java.util.Collections) CompactingShape(net.p3pp3rf1y.sophisticatedbackpacks.util.RecipeHelper.CompactingShape) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack)

Aggregations

CraftingInventory (net.minecraft.inventory.CraftingInventory)17 ItemStack (net.minecraft.item.ItemStack)12 ArrayList (java.util.ArrayList)6 List (java.util.List)6 PlayerEntity (net.minecraft.entity.player.PlayerEntity)5 TransferRecipeCraftingTeachingMessage (com.minecolonies.coremod.network.messages.server.TransferRecipeCraftingTeachingMessage)4 HashMap (java.util.HashMap)4 ICraftingRecipe (net.minecraft.item.crafting.ICraftingRecipe)4 Map (java.util.Map)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 IInventory (net.minecraft.inventory.IInventory)3 IRecipeType (net.minecraft.item.crafting.IRecipeType)3 ITextComponent (net.minecraft.util.text.ITextComponent)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ModItems.buildTool (com.ldtteam.structurize.items.ModItems.buildTool)2 IColonyManager (com.minecolonies.api.colony.IColonyManager)2 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)2 IGenericRecipe (com.minecolonies.api.crafting.IGenericRecipe)2 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)2