Search in sources :

Example 1 with Recipe

use of net.minecraft.recipe.Recipe in project nbt-crafting by Siphalor.

the class NbtCraftingClient method onInitializeClient.

@Override
public void onInitializeClient() {
    ClientLoginNetworking.registerGlobalReceiver(NbtCrafting.PRESENCE_CHANNEL, (client, handler, buf, listenerAdder) -> {
        return CompletableFuture.completedFuture(new PacketByteBuf(Unpooled.buffer()));
    });
    ClientSidePacketRegistry.INSTANCE.register(NbtCrafting.UPDATE_ANVIL_TEXT_S2C_PACKET_ID, (packetContext, packetByteBuf) -> {
        if (MinecraftClient.getInstance().currentScreen instanceof AnvilScreen) {
            ((AnvilScreenAccessor) MinecraftClient.getInstance().currentScreen).getNameField().setText(packetByteBuf.readString());
        } else
            packetByteBuf.readString();
    });
    ClientPlayNetworking.registerGlobalReceiver(NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, (client, handler, buf, responseSender) -> {
        RecipeManager recipeManager = handler.getRecipeManager();
        Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipeMap = ((RecipeManagerAccessor) recipeManager).getRecipes();
        recipeMap = new HashMap<>(recipeMap);
        int recipeCount = buf.readVarInt();
        NbtCrafting.advancedIngredientSerializationEnabled.set(true);
        for (int i = 0; i < recipeCount; i++) {
            RecipeSerializer<?> serializer = Registry.RECIPE_SERIALIZER.get(buf.readIdentifier());
            Identifier id = buf.readIdentifier();
            Recipe<?> recipe = serializer.read(id, buf);
            Map<Identifier, Recipe<?>> recipeType = recipeMap.computeIfAbsent(recipe.getType(), rt -> new HashMap<>());
            recipeType.put(id, recipe);
        }
        NbtCrafting.advancedIngredientSerializationEnabled.set(false);
        ((RecipeManagerAccessor) recipeManager).setRecipes(ImmutableMap.copyOf(recipeMap));
    });
}
Also used : Recipe(net.minecraft.recipe.Recipe) AnvilScreen(net.minecraft.client.gui.screen.ingame.AnvilScreen) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) Identifier(net.minecraft.util.Identifier) RecipeType(net.minecraft.recipe.RecipeType) PacketByteBuf(net.minecraft.util.PacketByteBuf) RecipeManager(net.minecraft.recipe.RecipeManager) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Recipe

use of net.minecraft.recipe.Recipe in project nbt-crafting by Siphalor.

the class MixinBrewingSlotPotion method canInsert.

@Inject(method = "canInsert(Lnet/minecraft/item/ItemStack;)Z", at = @At("RETURN"), cancellable = true)
public void canInsert(ItemStack stack, CallbackInfoReturnable<Boolean> callbackInfoReturnable) {
    if (callbackInfoReturnable.getReturnValue() || matches(stack)) {
        callbackInfoReturnable.setReturnValue(true);
        return;
    }
    RecipeManagerAccessor recipeManager;
    if (inventory instanceof BrewingStandBlockEntity) {
        recipeManager = (RecipeManagerAccessor) ((BrewingStandBlockEntity) inventory).getWorld().getRecipeManager();
    } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
        recipeManager = (RecipeManagerAccessor) NbtCraftingClient.getClientRecipeManager();
    } else {
        NbtCrafting.logError("Failed to get recipe manager in brewing stand container class!");
        return;
    }
    Map<Identifier, Recipe<Inventory>> recipes = recipeManager.callGetAllOfType(NbtCrafting.BREWING_RECIPE_TYPE);
    callbackInfoReturnable.setReturnValue(recipes.values().stream().anyMatch(recipe -> recipe instanceof BrewingRecipe && ((BrewingRecipe) recipe).getBase().test(stack)));
}
Also used : FabricLoader(net.fabricmc.loader.api.FabricLoader) BrewingStandBlockEntity(net.minecraft.block.entity.BrewingStandBlockEntity) NbtCraftingClient(de.siphalor.nbtcrafting.client.NbtCraftingClient) Inject(org.spongepowered.asm.mixin.injection.Inject) Slot(net.minecraft.container.Slot) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) ItemStack(net.minecraft.item.ItemStack) Mixin(org.spongepowered.asm.mixin.Mixin) Map(java.util.Map) EnvType(net.fabricmc.api.EnvType) Identifier(net.minecraft.util.Identifier) Shadow(org.spongepowered.asm.mixin.Shadow) NbtCrafting(de.siphalor.nbtcrafting.NbtCrafting) At(org.spongepowered.asm.mixin.injection.At) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) Identifier(net.minecraft.util.Identifier) Recipe(net.minecraft.recipe.Recipe) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) BrewingStandBlockEntity(net.minecraft.block.entity.BrewingStandBlockEntity) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with Recipe

use of net.minecraft.recipe.Recipe in project Polymorph by TheIllusiveC4.

the class AbstractRecipeData method getRecipe.

@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
    this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
        try {
            if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
                this.setSelectedRecipe(selected);
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
        }
        this.loadedRecipe = null;
    });
    if (this.isEmpty(pInventory)) {
        this.setFailing(false);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    AtomicReference<T> ref = new AtomicReference<>(null);
    this.getLastRecipe().ifPresent(recipe -> {
        try {
            if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
                this.getSelectedRecipe().ifPresent(selected -> {
                    try {
                        if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
                            ref.set((T) selected);
                        }
                    } catch (ClassCastException e) {
                        PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
                    }
                });
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
        }
    });
    T result = ref.get();
    if (result != null) {
        this.setFailing(false);
        this.sendRecipesListToListeners(false);
        return Optional.of(result);
    }
    SortedSet<RecipePair> newDataset = new TreeSet<>();
    List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
    if (recipes.isEmpty()) {
        this.setFailing(true);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    for (T entry : recipes) {
        Identifier id = entry.getId();
        if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
            ref.set(entry);
        }
        newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
    }
    this.setRecipesList(newDataset);
    result = ref.get();
    result = result != null ? result : recipes.get(0);
    this.lastRecipe = result;
    this.setSelectedRecipe(result);
    this.setFailing(false);
    this.sendRecipesListToListeners(false);
    return Optional.of(result);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) RecipeData(top.theillusivec4.polymorph.api.common.component.RecipeData) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 4 with Recipe

use of net.minecraft.recipe.Recipe in project Polymorph by TheIllusiveC4.

the class AbstractStackRecipeData method getRecipe.

@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
    this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
        try {
            if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
                this.setSelectedRecipe(selected);
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
        }
        this.loadedRecipe = null;
    });
    if (this.isEmpty(pInventory)) {
        this.setFailing(false);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    AtomicReference<T> ref = new AtomicReference<>(null);
    this.getLastRecipe().ifPresent(recipe -> {
        try {
            if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
                this.getSelectedRecipe().ifPresent(selected -> {
                    try {
                        if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
                            ref.set((T) selected);
                        }
                    } catch (ClassCastException e) {
                        PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
                    }
                });
            }
        } catch (ClassCastException e) {
            PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
        }
    });
    T result = ref.get();
    if (result != null) {
        this.setFailing(false);
        this.sendRecipesListToListeners(false);
        return Optional.of(result);
    }
    SortedSet<RecipePair> newDataset = new TreeSet<>();
    List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
    if (recipes.isEmpty()) {
        this.setFailing(true);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    for (T entry : recipes) {
        Identifier id = entry.getId();
        if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
            ref.set(entry);
        }
        newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
    }
    this.setRecipesList(newDataset);
    result = ref.get();
    result = result != null ? result : recipes.get(0);
    this.lastRecipe = result;
    this.setSelectedRecipe(result);
    this.setFailing(false);
    this.sendRecipesListToListeners(false);
    return Optional.of(result);
}
Also used : Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) SortedSet(java.util.SortedSet) NbtElement(net.minecraft.nbt.NbtElement) World(net.minecraft.world.World) NbtList(net.minecraft.nbt.NbtList) Set(java.util.Set) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) NbtType(net.fabricmc.fabric.api.util.NbtType) ItemComponent(dev.onyxstudios.cca.api.v3.item.ItemComponent) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) StackRecipeData(top.theillusivec4.polymorph.api.common.component.StackRecipeData) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) RecipeType(net.minecraft.recipe.RecipeType) Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 5 with Recipe

use of net.minecraft.recipe.Recipe in project nbt-crafting by Siphalor.

the class MixinBrewingSlotIngredient method canInsert.

@Inject(method = "canInsert(Lnet/minecraft/item/ItemStack;)Z", at = @At("RETURN"), cancellable = true)
public void canInsert(ItemStack stack, CallbackInfoReturnable<Boolean> callbackInfoReturnable) {
    if (callbackInfoReturnable.getReturnValue() || BrewingRecipeRegistry.isValidIngredient(stack)) {
        callbackInfoReturnable.setReturnValue(true);
        return;
    }
    RecipeManagerAccessor recipeManager;
    if (inventory instanceof BrewingStandBlockEntity) {
        recipeManager = (RecipeManagerAccessor) ((BrewingStandBlockEntity) inventory).getWorld().getRecipeManager();
    } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
        recipeManager = (RecipeManagerAccessor) NbtCraftingClient.getClientRecipeManager();
    } else {
        NbtCrafting.logError("Failed to get recipe manager in brewing stand container class!");
        return;
    }
    Map<Identifier, Recipe<Inventory>> recipes = recipeManager.callGetAllOfType(NbtCrafting.BREWING_RECIPE_TYPE);
    callbackInfoReturnable.setReturnValue(recipes.values().stream().anyMatch(recipe -> recipe instanceof BrewingRecipe && ((BrewingRecipe) recipe).getIngredient().test(stack)));
}
Also used : FabricLoader(net.fabricmc.loader.api.FabricLoader) BrewingStandBlockEntity(net.minecraft.block.entity.BrewingStandBlockEntity) NbtCraftingClient(de.siphalor.nbtcrafting.client.NbtCraftingClient) Inject(org.spongepowered.asm.mixin.injection.Inject) Slot(net.minecraft.container.Slot) Inventory(net.minecraft.inventory.Inventory) Recipe(net.minecraft.recipe.Recipe) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) ItemStack(net.minecraft.item.ItemStack) BrewingRecipeRegistry(net.minecraft.recipe.BrewingRecipeRegistry) Mixin(org.spongepowered.asm.mixin.Mixin) Map(java.util.Map) EnvType(net.fabricmc.api.EnvType) Identifier(net.minecraft.util.Identifier) NbtCrafting(de.siphalor.nbtcrafting.NbtCrafting) At(org.spongepowered.asm.mixin.injection.At) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) Identifier(net.minecraft.util.Identifier) Recipe(net.minecraft.recipe.Recipe) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) BrewingStandBlockEntity(net.minecraft.block.entity.BrewingStandBlockEntity) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Recipe (net.minecraft.recipe.Recipe)6 Identifier (net.minecraft.util.Identifier)6 Inventory (net.minecraft.inventory.Inventory)4 ItemStack (net.minecraft.item.ItemStack)4 RecipeManagerAccessor (de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor)3 Map (java.util.Map)3 RecipeType (net.minecraft.recipe.RecipeType)3 Pair (net.minecraft.util.Pair)3 RecipePair (top.theillusivec4.polymorph.api.common.base.RecipePair)3 NbtCrafting (de.siphalor.nbtcrafting.NbtCrafting)2 NbtCraftingClient (de.siphalor.nbtcrafting.client.NbtCraftingClient)2 BrewingRecipe (de.siphalor.nbtcrafting.recipe.BrewingRecipe)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EnvType (net.fabricmc.api.EnvType)2 NbtType (net.fabricmc.fabric.api.util.NbtType)2