Search in sources :

Example 1 with FluidVariant

use of net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant in project Primeval by devs-immortal.

the class PrimevalFluids method combineFluids.

public static Pair<FluidVariant, Integer> combineFluids(HashMap<FluidVariant, Integer> fluids) {
    if (fluids.keySet().size() == 1) {
        for (FluidVariant fluid : fluids.keySet()) {
            return new Pair<>(fluid, fluids.get(fluid));
        }
    }
    int overallFluid = 0;
    for (FluidVariant fluid : fluids.keySet()) {
        System.out.print(fluid.toNbt().getString("fluid") + " | " + fluids.get(fluid));
        overallFluid += fluids.get(fluid);
    }
    for (AlloyRatio a : alloys) {
        if (a.satisfies(fluids, overallFluid)) {
            return new Pair<>(a.getResult(), overallFluid);
        }
    }
    return new Pair<>(FluidVariant.of(MOLTEN_BOTCHED_ALLOY), overallFluid);
}
Also used : FluidVariant(net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant) Pair(net.minecraft.util.Pair)

Example 2 with FluidVariant

use of net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant in project Primeval by devs-immortal.

the class ClayMoldItem method insertFluid.

/*
     * Handle inserting fluid into a mold from a source
     * Return the amount of fluid inserted
     */
public static int insertFluid(Pair<FluidVariant, Integer> fluidPair, ItemStack mold) {
    NbtCompound nbt = mold.getOrCreateNbt();
    FluidVariant incomingFluid = fluidPair.getLeft();
    int incomingAmount = fluidPair.getRight();
    int currentStoredAmount = 0;
    if (nbt.contains("Fluid")) {
        // If already stored
        // get fluid nbt
        NbtCompound fluidNbt = nbt.getCompound("Fluid");
        FluidVariant stored = FluidVariant.fromNbt(fluidNbt);
        int amount = fluidNbt.getInt("Amount");
        if (amount != 0 && !stored.isBlank()) {
            // If not and not storing empty
            if (stored.getFluid() == incomingFluid.getFluid()) {
                currentStoredAmount = amount;
            } else {
                return 0;
            }
        }
    }
    // if not already storing, or we pass the tests above, insert 1000dp of fluid
    int remainingCapacity = CAPACITY - currentStoredAmount;
    int amountToInsert = Math.min(remainingCapacity, Math.min(incomingAmount, 1000));
    // build new nbt for mold item
    NbtCompound nbtF = fluidPair.getLeft().toNbt();
    nbtF.putInt("Amount", currentStoredAmount + amountToInsert);
    nbt.put("Fluid", nbtF);
    mold.setNbt(nbt);
    return amountToInsert;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) FluidVariant(net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant)

Example 3 with FluidVariant

use of net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant in project Primeval by devs-immortal.

the class AlloyRatio method satisfies.

public boolean satisfies(HashMap<FluidVariant, Integer> fluidsIn, double overallFluid) {
    if (fluids.keySet().size() == fluidsIn.keySet().size()) {
        double min = 1.0;
        double amount;
        for (FluidVariant f : fluids.keySet()) {
            if (fluidsIn.containsKey(f)) {
                amount = fluidsIn.get(f);
                if (amount / overallFluid < min)
                    min = amount / overallFluid;
            } else {
                return false;
            }
        }
        double ratio;
        for (FluidVariant f : fluids.keySet()) {
            ratio = (1 / min) * fluidsIn.get(f) / overallFluid;
            if (!fluids.get(f).valueIsWithin(ratio))
                return false;
        }
        return true;
    }
    return false;
}
Also used : FluidVariant(net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant)

Example 4 with FluidVariant

use of net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant 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

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