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);
}
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;
}
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;
}
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;
}
Aggregations