Search in sources :

Example 1 with MeltingRecipe

use of net.cr24.primeval.recipe.MeltingRecipe in project Primeval by devs-immortal.

the class VesselItem method processItems.

// 
// VESSEL-SPECIFIC METHODS
// 
public static ItemStack processItems(ItemStack vessel, World world) {
    NbtCompound nbt = vessel.getOrCreateNbt();
    NbtList nbtList = nbt.getList("Items", 10);
    // Create new hashmap to store fluids that will be inserted
    HashMap<FluidVariant, Integer> fluids = new HashMap<>();
    // Item being melted
    ItemStack itemStack;
    // Resulting fluid from melted item, being <type, quantity>
    Pair<FluidVariant, Integer> fluidResult;
    // Type of fluid result
    FluidVariant fluidType;
    // Amonut of fluid from melting
    int fluidAmount;
    for (NbtElement nbtC : nbtList) {
        // Get itemstack from item to melted
        itemStack = ItemStack.fromNbt((NbtCompound) nbtC);
        // Check if meltable recipe for the item
        Optional<MeltingRecipe> option = world.getRecipeManager().getFirstMatch(PrimevalRecipes.MELTING, new SimpleInventory(itemStack), world);
        if (option.isPresent()) {
            // if can be melted
            fluidResult = option.get().getFluidResult();
            fluidType = fluidResult.getLeft();
            fluidAmount = fluidResult.getRight();
            if (fluids.containsKey(fluidType)) {
                fluids.put(fluidType, fluids.get(fluidType) + fluidAmount * itemStack.getCount());
            } else {
                fluids.put(fluidType, fluidAmount * itemStack.getCount());
            }
        }
    }
    Pair<FluidVariant, Integer> alloyResult = PrimevalFluids.combineFluids(fluids);
    if (alloyResult.getRight() > 0) {
        NbtCompound nbtF = alloyResult.getLeft().toNbt();
        nbtF.putInt("Amount", alloyResult.getRight());
        nbt.put("Fluid", nbtF);
    }
    // Clear out items
    nbt.put("Items", new NbtList());
    vessel.setNbt(nbt);
    return vessel;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) HashMap(java.util.HashMap) FluidVariant(net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant) NbtElement(net.minecraft.nbt.NbtElement) MeltingRecipe(net.cr24.primeval.recipe.MeltingRecipe) NbtList(net.minecraft.nbt.NbtList) ItemStack(net.minecraft.item.ItemStack) SimpleInventory(net.minecraft.inventory.SimpleInventory)

Aggregations

HashMap (java.util.HashMap)1 MeltingRecipe (net.cr24.primeval.recipe.MeltingRecipe)1 FluidVariant (net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant)1 SimpleInventory (net.minecraft.inventory.SimpleInventory)1 ItemStack (net.minecraft.item.ItemStack)1 NbtCompound (net.minecraft.nbt.NbtCompound)1 NbtElement (net.minecraft.nbt.NbtElement)1 NbtList (net.minecraft.nbt.NbtList)1