Search in sources :

Example 6 with RecipePair

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

the class AbstractStackRecipeData method readNbt.

@Override
public void readNbt(NbtCompound pCompound) {
    if (pCompound.contains("SelectedRecipe")) {
        this.loadedRecipe = new Identifier(pCompound.getString("SelectedRecipe"));
    }
    if (pCompound.contains("RecipeDataSet")) {
        Set<RecipePair> dataset = this.getRecipesList();
        dataset.clear();
        NbtList list = pCompound.getList("RecipeDataSet", NbtType.COMPOUND);
        for (NbtElement inbt : list) {
            NbtCompound tag = (NbtCompound) inbt;
            Identifier id = Identifier.tryParse(tag.getString("Id"));
            ItemStack stack = ItemStack.fromNbt(tag.getCompound("ItemStack"));
            dataset.add(new RecipePairImpl(id, stack));
        }
    }
}
Also used : Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) NbtCompound(net.minecraft.nbt.NbtCompound) RecipePairImpl(top.theillusivec4.polymorph.common.impl.RecipePairImpl) NbtList(net.minecraft.nbt.NbtList) NbtElement(net.minecraft.nbt.NbtElement) ItemStack(net.minecraft.item.ItemStack)

Example 7 with RecipePair

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

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

the class CommonEventsListener method openScreenHandler.

public static void openScreenHandler(ServerPlayerEntity player) {
    ScreenHandler screenHandler = player.currentScreenHandler;
    PolymorphCommon commonApi = PolymorphApi.common();
    commonApi.getRecipeDataFromBlockEntity(screenHandler).ifPresent(recipeData -> {
        PolymorphPacketDistributor packetDistributor = commonApi.getPacketDistributor();
        if (recipeData.isFailing() || recipeData.isEmpty(null)) {
            packetDistributor.sendRecipesListS2C(player);
        } else {
            Pair<SortedSet<RecipePair>, Identifier> data = recipeData.getPacketData();
            packetDistributor.sendRecipesListS2C(player, data.getLeft(), data.getRight());
        }
    });
    for (AbstractCompatibilityModule integration : PolymorphIntegrations.get()) {
        if (integration.openScreenHandler(screenHandler, player)) {
            return;
        }
    }
}
Also used : Identifier(net.minecraft.util.Identifier) PolymorphPacketDistributor(top.theillusivec4.polymorph.api.common.base.PolymorphPacketDistributor) ScreenHandler(net.minecraft.screen.ScreenHandler) AbstractCompatibilityModule(top.theillusivec4.polymorph.common.integration.AbstractCompatibilityModule) PolymorphCommon(top.theillusivec4.polymorph.api.common.base.PolymorphCommon) SortedSet(java.util.SortedSet)

Example 9 with RecipePair

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

the class SPacketPlayerRecipeSync method decode.

public static SPacketPlayerRecipeSync decode(FriendlyByteBuf pBuffer) {
    SortedSet<IRecipePair> recipeDataset = new TreeSet<>();
    ResourceLocation selected = null;
    if (pBuffer.isReadable()) {
        int size = pBuffer.readInt();
        for (int i = 0; i < size; i++) {
            recipeDataset.add(new RecipePair(pBuffer.readResourceLocation(), pBuffer.readItem()));
        }
        if (pBuffer.isReadable()) {
            selected = pBuffer.readResourceLocation();
        }
    }
    return new SPacketPlayerRecipeSync(recipeDataset, selected);
}
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) TreeSet(java.util.TreeSet) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 10 with RecipePair

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

the class AbstractHighlightedRecipeData method getPacketData.

@Override
public Pair<SortedSet<RecipePair>, Identifier> getPacketData() {
    SortedSet<RecipePair> recipesList = this.getRecipesList();
    Identifier selected = null;
    if (!recipesList.isEmpty()) {
        selected = this.getSelectedRecipe().map(Recipe::getId).orElse(recipesList.first().getIdentifier());
    }
    return new Pair<>(recipesList, selected);
}
Also used : Identifier(net.minecraft.util.Identifier) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair) Recipe(net.minecraft.recipe.Recipe) Pair(net.minecraft.util.Pair) RecipePair(top.theillusivec4.polymorph.api.common.base.RecipePair)

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