Search in sources :

Example 1 with RecipePair

use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.

the class AbstractRecipeData method getRecipe.

@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Container> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, Level pWorld, List<T> pRecipes) {
    this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().byKey(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<IRecipePair> newDataset = new TreeSet<>();
    List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getRecipesFor(pType, pInventory, pWorld) : pRecipes;
    if (recipes.isEmpty()) {
        this.setFailing(true);
        this.sendRecipesListToListeners(true);
        return Optional.empty();
    }
    for (T entry : recipes) {
        ResourceLocation id = entry.getId();
        if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
            ref.set(entry);
        }
        newDataset.add(new RecipePair(id, entry.assemble(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 : ResourceLocation(net.minecraft.resources.ResourceLocation) Tag(net.minecraft.nbt.Tag) SortedSet(java.util.SortedSet) Set(java.util.Set) PolymorphMod(top.theillusivec4.polymorph.common.PolymorphMod) AtomicReference(java.util.concurrent.atomic.AtomicReference) Pair(com.mojang.datafixers.util.Pair) TreeSet(java.util.TreeSet) ServerPlayer(net.minecraft.server.level.ServerPlayer) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Container(net.minecraft.world.Container) IRecipePair(top.theillusivec4.polymorph.api.common.base.IRecipePair) Recipe(net.minecraft.world.item.crafting.Recipe) RecipePair(top.theillusivec4.polymorph.common.impl.RecipePair) Optional(java.util.Optional) IRecipeData(top.theillusivec4.polymorph.api.common.capability.IRecipeData) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) PolymorphApi(top.theillusivec4.polymorph.api.PolymorphApi) Nonnull(javax.annotation.Nonnull) RecipeType(net.minecraft.world.item.crafting.RecipeType) ListTag(net.minecraft.nbt.ListTag) IRecipePair(top.theillusivec4.polymorph.api.common.base.IRecipePair) IRecipePair(top.theillusivec4.polymorph.api.common.base.IRecipePair) RecipePair(top.theillusivec4.polymorph.common.impl.RecipePair) TreeSet(java.util.TreeSet) ResourceLocation(net.minecraft.resources.ResourceLocation) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with RecipePair

use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.

the class AbstractRecipeData method readNBT.

@Override
public void readNBT(CompoundTag pCompound) {
    if (pCompound.contains("SelectedRecipe")) {
        this.loadedRecipe = new ResourceLocation(pCompound.getString("SelectedRecipe"));
    }
    if (pCompound.contains("RecipeDataSet")) {
        Set<IRecipePair> dataset = this.getRecipesList();
        dataset.clear();
        ListTag list = pCompound.getList("RecipeDataSet", Tag.TAG_COMPOUND);
        for (Tag inbt : list) {
            CompoundTag tag = (CompoundTag) inbt;
            ResourceLocation id = ResourceLocation.tryParse(tag.getString("Id"));
            ItemStack stack = ItemStack.of(tag.getCompound("ItemStack"));
            dataset.add(new RecipePair(id, stack));
        }
    }
}
Also used : IRecipePair(top.theillusivec4.polymorph.api.common.base.IRecipePair) IRecipePair(top.theillusivec4.polymorph.api.common.base.IRecipePair) RecipePair(top.theillusivec4.polymorph.common.impl.RecipePair) ResourceLocation(net.minecraft.resources.ResourceLocation) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 3 with RecipePair

use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.

the class PolymorphClientNetwork method sendRecipesList.

private static void sendRecipesList(MinecraftClient pClient, ClientPlayNetworkHandler pHandler, PacketByteBuf pBuf, PacketSender pSender) {
    SortedSet<RecipePair> recipeDataset = new TreeSet<>();
    Identifier selected = null;
    if (pBuf.isReadable()) {
        int size = pBuf.readInt();
        for (int i = 0; i < size; i++) {
            recipeDataset.add(new RecipePairImpl(pBuf.readIdentifier(), pBuf.readItemStack()));
        }
        if (pBuf.isReadable()) {
            selected = pBuf.readIdentifier();
        }
    }
    Identifier finalSelected = selected;
    pClient.execute(() -> {
        ClientPlayerEntity player = pClient.player;
        if (player != null) {
            Optional<RecipesWidget> maybeWidget = RecipesWidgetControl.get();
            maybeWidget.ifPresent(widget -> widget.setRecipesList(recipeDataset, finalSelected));
            if (!maybeWidget.isPresent()) {
                RecipesWidgetControl.enqueueRecipesList(recipeDataset, finalSelected);
            }
        }
    });
}
Also used : Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) TreeSet(java.util.TreeSet) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) RecipesWidget(top.theillusivec4.polymorph.api.client.base.RecipesWidget) ClientPlayerEntity(net.minecraft.client.network.ClientPlayerEntity)

Example 4 with RecipePair

use of top.theillusivec4.polymorph.api.common.base.RecipePair in project Polymorph by TheIllusiveC4.

the class PolymorphPacketDistributorImpl method sendPlayerSyncS2C.

@Override
public void sendPlayerSyncS2C(ServerPlayerEntity pPlayer, SortedSet<RecipePair> pRecipesList, Identifier pSelected) {
    PacketByteBuf buf = PacketByteBufs.create();
    if (!pRecipesList.isEmpty()) {
        buf.writeInt(pRecipesList.size());
        for (RecipePair data : pRecipesList) {
            buf.writeIdentifier(data.getIdentifier());
            buf.writeItemStack(data.getOutput());
        }
        if (pSelected != null) {
            buf.writeIdentifier(pSelected);
        }
    }
    ServerPlayNetworking.send(pPlayer, PolymorphPackets.RECIPE_SYNC, buf);
}
Also used : RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) PacketByteBuf(net.minecraft.network.PacketByteBuf)

Example 5 with RecipePair

use of top.theillusivec4.polymorph.api.common.base.RecipePair 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)

Aggregations

RecipePair (top.theillusivec4.polymorph.api.common.base.RecipePair)11 Identifier (net.minecraft.util.Identifier)8 TreeSet (java.util.TreeSet)7 NbtCompound (net.minecraft.nbt.NbtCompound)6 NbtList (net.minecraft.nbt.NbtList)6 RecipePairImpl (top.theillusivec4.polymorph.common.impl.RecipePairImpl)6 SortedSet (java.util.SortedSet)4 ItemStack (net.minecraft.item.ItemStack)4 NbtElement (net.minecraft.nbt.NbtElement)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 IRecipePair (top.theillusivec4.polymorph.api.common.base.IRecipePair)4 RecipePair (top.theillusivec4.polymorph.common.impl.RecipePair)4 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Recipe (net.minecraft.recipe.Recipe)3 Pair (net.minecraft.util.Pair)3 PolymorphApi (top.theillusivec4.polymorph.api.PolymorphApi)3 PolymorphMod (top.theillusivec4.polymorph.common.PolymorphMod)3