Search in sources :

Example 21 with CraftingRecipe

use of net.minecraft.world.item.crafting.CraftingRecipe in project Cyclic by Lothrazar.

the class CraftingBagContainer method slotsChanged.

@Override
public void slotsChanged(Container inventory) {
    Level world = playerInventory.player.level;
    if (!world.isClientSide) {
        ServerPlayer player = (ServerPlayer) playerInventory.player;
        ItemStack itemstack = ItemStack.EMPTY;
        Optional<CraftingRecipe> optional = world.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, craftMatrix, world);
        if (optional.isPresent()) {
            CraftingRecipe icraftingrecipe = optional.get();
            if (craftResult.setRecipeUsed(world, player, icraftingrecipe)) {
                itemstack = icraftingrecipe.assemble(craftMatrix);
            }
        }
        craftResult.setItem(0, itemstack);
        player.connection.send(new ClientboundContainerSetSlotPacket(containerId, this.getStateId(), 0, itemstack));
    }
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) Level(net.minecraft.world.level.Level) CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) ItemStack(net.minecraft.world.item.ItemStack)

Example 22 with CraftingRecipe

use of net.minecraft.world.item.crafting.CraftingRecipe in project Create by Creators-of-Create.

the class ToolboxColoringRecipeMaker method createRecipes.

// From JEI's ShulkerBoxColoringRecipeMaker
public static Stream<CraftingRecipe> createRecipes() {
    String group = "create.toolbox.color";
    ItemStack baseShulkerStack = AllBlocks.TOOLBOXES.get(DyeColor.BROWN).asStack();
    Ingredient baseShulkerIngredient = Ingredient.of(baseShulkerStack);
    return Arrays.stream(DyeColor.values()).filter(dc -> dc != DyeColor.BROWN).map(color -> {
        DyeItem dye = DyeItem.byColor(color);
        ItemStack dyeStack = new ItemStack(dye);
        Tag<Item> colorTag = color.getTag();
        Ingredient.Value dyeList = new Ingredient.ItemValue(dyeStack);
        Ingredient.Value colorList = new Ingredient.TagValue(colorTag);
        Stream<Ingredient.Value> colorIngredientStream = Stream.of(dyeList, colorList);
        Ingredient colorIngredient = Ingredient.fromValues(colorIngredientStream);
        NonNullList<Ingredient> inputs = NonNullList.of(Ingredient.EMPTY, baseShulkerIngredient, colorIngredient);
        Block coloredShulkerBox = AllBlocks.TOOLBOXES.get(color).get();
        ItemStack output = new ItemStack(coloredShulkerBox);
        ResourceLocation id = Create.asResource(group + "." + output.getDescriptionId());
        return new ShapelessRecipe(id, group, output, inputs);
    });
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Arrays(java.util.Arrays) DyeColor(net.minecraft.world.item.DyeColor) CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) Item(net.minecraft.world.item.Item) Tag(net.minecraft.tags.Tag) Ingredient(net.minecraft.world.item.crafting.Ingredient) Stream(java.util.stream.Stream) Create(com.simibubi.create.Create) ShapelessRecipe(net.minecraft.world.item.crafting.ShapelessRecipe) NonNullList(net.minecraft.core.NonNullList) DyeItem(net.minecraft.world.item.DyeItem) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) AllBlocks(com.simibubi.create.AllBlocks) Item(net.minecraft.world.item.Item) DyeItem(net.minecraft.world.item.DyeItem) Ingredient(net.minecraft.world.item.crafting.Ingredient) DyeItem(net.minecraft.world.item.DyeItem) ResourceLocation(net.minecraft.resources.ResourceLocation) ShapelessRecipe(net.minecraft.world.item.crafting.ShapelessRecipe) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack)

Example 23 with CraftingRecipe

use of net.minecraft.world.item.crafting.CraftingRecipe in project Create by Creators-of-Create.

the class BlueprintOverlayRenderer method rebuild.

public static void rebuild(BlueprintSection sectionAt, boolean sneak) {
    cachedRenderedFilters.clear();
    ItemStackHandler items = sectionAt.getItems();
    boolean empty = true;
    for (int i = 0; i < 9; i++) {
        if (!items.getStackInSlot(i).isEmpty()) {
            empty = false;
            break;
        }
    }
    BlueprintOverlayRenderer.empty = empty;
    BlueprintOverlayRenderer.result = ItemStack.EMPTY;
    if (empty)
        return;
    boolean firstPass = true;
    boolean success = true;
    Minecraft mc = Minecraft.getInstance();
    ItemStackHandler playerInv = new ItemStackHandler(mc.player.getInventory().getContainerSize());
    for (int i = 0; i < playerInv.getSlots(); i++) playerInv.setStackInSlot(i, mc.player.getInventory().getItem(i).copy());
    int amountCrafted = 0;
    Optional<CraftingRecipe> recipe = Optional.empty();
    Map<Integer, ItemStack> craftingGrid = new HashMap<>();
    ingredients.clear();
    ItemStackHandler missingItems = new ItemStackHandler(64);
    ItemStackHandler availableItems = new ItemStackHandler(64);
    List<ItemStack> newlyAdded = new ArrayList<>();
    List<ItemStack> newlyMissing = new ArrayList<>();
    boolean invalid = false;
    do {
        craftingGrid.clear();
        newlyAdded.clear();
        newlyMissing.clear();
        Search: for (int i = 0; i < 9; i++) {
            ItemStack requestedItem = items.getStackInSlot(i);
            if (requestedItem.isEmpty()) {
                craftingGrid.put(i, ItemStack.EMPTY);
                continue;
            }
            for (int slot = 0; slot < playerInv.getSlots(); slot++) {
                if (!FilterItem.test(mc.level, playerInv.getStackInSlot(slot), requestedItem))
                    continue;
                ItemStack currentItem = playerInv.extractItem(slot, 1, false);
                craftingGrid.put(i, currentItem);
                newlyAdded.add(currentItem);
                continue Search;
            }
            success = false;
            newlyMissing.add(requestedItem);
        }
        if (success) {
            CraftingContainer craftingInventory = new BlueprintCraftingInventory(craftingGrid);
            if (!recipe.isPresent())
                recipe = mc.level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, craftingInventory, mc.level);
            ItemStack resultFromRecipe = recipe.filter(r -> r.matches(craftingInventory, mc.level)).map(r -> r.assemble(craftingInventory)).orElse(ItemStack.EMPTY);
            if (resultFromRecipe.isEmpty()) {
                if (!recipe.isPresent())
                    invalid = true;
                success = false;
            } else if (resultFromRecipe.getCount() + amountCrafted > 64) {
                success = false;
            } else {
                amountCrafted += resultFromRecipe.getCount();
                if (result.isEmpty())
                    result = resultFromRecipe.copy();
                else
                    result.grow(resultFromRecipe.getCount());
                resultCraftable = true;
                firstPass = false;
            }
        }
        if (success || firstPass) {
            newlyAdded.forEach(s -> ItemHandlerHelper.insertItemStacked(availableItems, s, false));
            newlyMissing.forEach(s -> ItemHandlerHelper.insertItemStacked(missingItems, s, false));
        }
        if (!success) {
            if (firstPass) {
                result = invalid ? ItemStack.EMPTY : items.getStackInSlot(9);
                resultCraftable = false;
            }
            break;
        }
        if (!sneak)
            break;
    } while (success);
    for (int i = 0; i < 9; i++) {
        ItemStack available = availableItems.getStackInSlot(i);
        if (available.isEmpty())
            continue;
        ingredients.add(Pair.of(available, true));
    }
    for (int i = 0; i < 9; i++) {
        ItemStack missing = missingItems.getStackInSlot(i);
        if (missing.isEmpty())
            continue;
        ingredients.add(Pair.of(missing, false));
    }
}
Also used : Items(net.minecraft.world.item.Items) BlueprintSection(com.simibubi.create.content.curiosities.tools.BlueprintEntity.BlueprintSection) FilterItem(com.simibubi.create.content.logistics.item.filter.FilterItem) Item(net.minecraft.world.item.Item) HashMap(java.util.HashMap) AllItems(com.simibubi.create.AllItems) BlueprintCraftingInventory(com.simibubi.create.content.curiosities.tools.BlueprintEntity.BlueprintCraftingInventory) IIngameOverlay(net.minecraftforge.client.gui.IIngameOverlay) ArrayList(java.util.ArrayList) PoseStack(com.mojang.blaze3d.vertex.PoseStack) Ingredient(net.minecraft.world.item.crafting.Ingredient) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Minecraft(net.minecraft.client.Minecraft) ChatFormatting(net.minecraft.ChatFormatting) Map(java.util.Map) GuiGameElement(com.simibubi.create.foundation.gui.element.GuiGameElement) WhitelistMode(com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode) AnimationTickHolder(com.simibubi.create.foundation.utility.AnimationTickHolder) RecipeType(net.minecraft.world.item.crafting.RecipeType) IdentityHashMap(java.util.IdentityHashMap) CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) ItemTags(net.minecraft.tags.ItemTags) EntityHitResult(net.minecraft.world.phys.EntityHitResult) ItemAttribute(com.simibubi.create.content.logistics.item.filter.ItemAttribute) Tag(net.minecraft.tags.Tag) Pair(com.simibubi.create.foundation.utility.Pair) AllGuiTextures(com.simibubi.create.foundation.gui.AllGuiTextures) HitResult(net.minecraft.world.phys.HitResult) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) CraftingContainer(net.minecraft.world.inventory.CraftingContainer) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) RenderSystem(com.mojang.blaze3d.systems.RenderSystem) ForgeIngameGui(net.minecraftforge.client.gui.ForgeIngameGui) Optional(java.util.Optional) ItemStack(net.minecraft.world.item.ItemStack) Type(net.minecraft.world.phys.HitResult.Type) ListTag(net.minecraft.nbt.ListTag) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) Minecraft(net.minecraft.client.Minecraft) CraftingContainer(net.minecraft.world.inventory.CraftingContainer) ItemStack(net.minecraft.world.item.ItemStack) BlueprintCraftingInventory(com.simibubi.create.content.curiosities.tools.BlueprintEntity.BlueprintCraftingInventory)

Example 24 with CraftingRecipe

use of net.minecraft.world.item.crafting.CraftingRecipe in project Create by Creators-of-Create.

the class BlueprintContainer method onCraftMatrixChanged.

public void onCraftMatrixChanged() {
    if (contentHolder.getBlueprintWorld().isClientSide)
        return;
    ServerPlayer serverplayerentity = (ServerPlayer) player;
    CraftingContainer craftingInventory = new BlueprintCraftingInventory(this, ghostInventory);
    Optional<CraftingRecipe> optional = player.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, craftingInventory, player.getCommandSenderWorld());
    if (!optional.isPresent()) {
        if (ghostInventory.getStackInSlot(9).isEmpty())
            return;
        if (!contentHolder.inferredIcon)
            return;
        ghostInventory.setStackInSlot(9, ItemStack.EMPTY);
        serverplayerentity.connection.send(new ClientboundContainerSetSlotPacket(containerId, incrementStateId(), 36 + 9, ItemStack.EMPTY));
        contentHolder.inferredIcon = false;
        return;
    }
    CraftingRecipe icraftingrecipe = optional.get();
    ItemStack itemstack = icraftingrecipe.assemble(craftingInventory);
    ghostInventory.setStackInSlot(9, itemstack);
    contentHolder.inferredIcon = true;
    ItemStack toSend = itemstack.copy();
    toSend.getOrCreateTag().putBoolean("InferredFromRecipe", true);
    serverplayerentity.connection.send(new ClientboundContainerSetSlotPacket(containerId, incrementStateId(), 36 + 9, toSend));
}
Also used : CraftingContainer(net.minecraft.world.inventory.CraftingContainer) ServerPlayer(net.minecraft.server.level.ServerPlayer) ClientboundContainerSetSlotPacket(net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket) CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) ItemStack(net.minecraft.world.item.ItemStack)

Example 25 with CraftingRecipe

use of net.minecraft.world.item.crafting.CraftingRecipe in project excavated_variants by lukebemish.

the class ExcavatedVariantsClientPlugin method registerDisplays.

@Override
public void registerDisplays(DisplayRegistry registry) {
    if (ExcavatedVariants.getConfig().add_conversion_recipes && ExcavatedVariants.getConfig().jei_rei_compat) {
        List<CraftingRecipe> recipes = new ArrayList<>();
        OreConversionRecipe.assembleOrNull();
        for (Pair<BaseOre, HashSet<BaseStone>> p : ExcavatedVariants.oreStoneList) {
            ArrayList<Item> items = new ArrayList<>();
            for (BaseStone stone : p.last()) {
                ResourceLocation rl = new ResourceLocation(ExcavatedVariants.MOD_ID, stone.id + "_" + p.first().id);
                Item item = Services.REGISTRY_UTIL.getItemById(rl);
                if (item != null) {
                    items.add(item);
                }
            }
            Item outItem = Services.REGISTRY_UTIL.getItemById(p.first().block_id.get(0));
            if (items.size() > 0 && outItem != null) {
                Ingredient input = Ingredient.of(items.stream().map(ItemStack::new));
                ItemStack output = new ItemStack(outItem);
                NonNullList<Ingredient> inputs = NonNullList.of(Ingredient.EMPTY, input);
                String ore_id = p.first().id;
                recipes.add(new ShapelessRecipe(new ResourceLocation(ExcavatedVariants.MOD_ID, ore_id + "_conversion"), "excavated_variants.ore_conversion", output, inputs));
            }
        }
        CategoryIdentifier<Display> categoryIdentifier = CategoryIdentifier.of("minecraft", "plugins/crafting");
        for (CraftingRecipe recipe : recipes) {
            Collection<Display> displays = registry.tryFillDisplay(recipe);
            for (Display display : displays) {
                if (Objects.equals(display.getCategoryIdentifier(), categoryIdentifier)) {
                    registry.add(display, recipe);
                }
            }
        }
    }
}
Also used : CraftingRecipe(net.minecraft.world.item.crafting.CraftingRecipe) BaseStone(io.github.lukebemish.excavated_variants.data.BaseStone) Item(net.minecraft.world.item.Item) BaseOre(io.github.lukebemish.excavated_variants.data.BaseOre) Ingredient(net.minecraft.world.item.crafting.Ingredient) ResourceLocation(net.minecraft.resources.ResourceLocation) ShapelessRecipe(net.minecraft.world.item.crafting.ShapelessRecipe) ItemStack(net.minecraft.world.item.ItemStack) Display(me.shedaniel.rei.api.common.display.Display)

Aggregations

CraftingRecipe (net.minecraft.world.item.crafting.CraftingRecipe)25 ItemStack (net.minecraft.world.item.ItemStack)22 ServerPlayer (net.minecraft.server.level.ServerPlayer)10 ClientboundContainerSetSlotPacket (net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket)9 Ingredient (net.minecraft.world.item.crafting.Ingredient)7 NonNullList (net.minecraft.core.NonNullList)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 CraftingContainer (net.minecraft.world.inventory.CraftingContainer)4 Item (net.minecraft.world.item.Item)4 ShapelessRecipe (net.minecraft.world.item.crafting.ShapelessRecipe)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RecipeManager (net.minecraft.world.item.crafting.RecipeManager)3 AllItems (com.simibubi.create.AllItems)2 Create (com.simibubi.create.Create)2 FilterItem (com.simibubi.create.content.logistics.item.filter.FilterItem)2 Arrays (java.util.Arrays)2 List (java.util.List)2 ItemTags (net.minecraft.tags.ItemTags)2