Initial move to the fabric fluid API (#2465)

This commit is contained in:
Technici4n 2021-08-05 21:51:11 +02:00 committed by GitHub
parent 482fe4f01f
commit fd9d776b43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 239 additions and 211 deletions

View file

@ -28,6 +28,7 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
import net.fabricmc.fabric.api.event.world.WorldTickCallback;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
@ -36,6 +37,7 @@ import reborncore.api.ToolManager;
import reborncore.api.blockentity.UnloadHandler;
import reborncore.common.RebornCoreCommands;
import reborncore.common.RebornCoreConfig;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.blocks.BlockWrenchEventHandler;
import reborncore.common.config.Configuration;
import reborncore.common.crafting.ingredient.IngredientManager;
@ -48,6 +50,7 @@ import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.recipes.PaddedShapedRecipe;
import reborncore.common.util.CalenderUtils;
import reborncore.common.util.GenericWrenchHelper;
import reborncore.common.util.Tank;
import java.io.File;
import java.util.function.Supplier;
@ -115,6 +118,13 @@ public class RebornCore implements ModInitializer {
if (blockEntity instanceof UnloadHandler) ((UnloadHandler) blockEntity).onUnload();
});
FluidStorage.SIDED.registerFallback((world, pos, state, be, direction) -> {
if (be instanceof MachineBaseBlockEntity machineBase) {
return machineBase.getTank();
}
return null;
});
LOGGER.info("Reborn core is done for now, now to let other mods have their turn...");
LOADED = true;
}

View file

@ -74,7 +74,7 @@ public class RenderUtil {
}
public static void renderGuiTank(Tank tank, double x, double y, double zLevel, double width, double height) {
renderGuiTank(tank.getFluidInstance(), tank.getCapacity(), tank.getFluidAmount(), x, y, zLevel, width, height);
renderGuiTank(tank.getFluidInstance(), tank.getFluidValueCapacity(), tank.getFluidAmount(), x, y, zLevel, width, height);
}
public static void renderGuiTank(FluidInstance fluid, FluidValue capacity, FluidValue amount, double x, double y, double zLevel,

View file

@ -27,6 +27,7 @@ package reborncore.client.gui.guibuilder;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawableHelper;
@ -56,6 +57,7 @@ import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.powerSystem.PowerSystem.EnergySystem;
import reborncore.common.util.FluidTextHelper;
import reborncore.common.util.StringUtils;
import java.util.ArrayList;
@ -712,14 +714,14 @@ public class GuiBuilder {
* @param height int Height of fluid to draw
* @param maxCapacity int Maximum capacity of tank
*/
public void drawFluid(MatrixStack matrixStack, GuiBase<?> gui, FluidInstance fluid, int x, int y, int width, int height, int maxCapacity) {
public void drawFluid(MatrixStack matrixStack, GuiBase<?> gui, FluidInstance fluid, int x, int y, int width, int height, long maxCapacity) {
if (fluid.getFluid() == Fluids.EMPTY) {
return;
}
RenderSystem.setShaderTexture(0, SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE);
y += height;
final Sprite sprite = FluidRenderHandlerRegistry.INSTANCE.get(fluid.getFluid()).getFluidSprites(gui.getMachine().getWorld(), gui.getMachine().getPos(), fluid.getFluid().getDefaultState())[0];
int color = FluidRenderHandlerRegistry.INSTANCE.get(fluid.getFluid()).getFluidColor(gui.getMachine().getWorld(), gui.getMachine().getPos(), fluid.getFluid().getDefaultState());
final Sprite sprite = FluidVariantRendering.getSprite(fluid.getVariant());
int color = FluidVariantRendering.getColor(fluid.getVariant());
final int drawHeight = (int) (fluid.getAmount().getRawValue() / (maxCapacity * 1F) * height);
final int iconHeight = sprite.getHeight();
@ -801,7 +803,7 @@ public class GuiBuilder {
gui.drawTexture(matrixStack, x, y, 150 + 23, 122, 3, 26);
}
protected int percentage(int MaxValue, int CurrentValue) {
protected int percentage(long MaxValue, long CurrentValue) {
if (CurrentValue == 0) {
return 0;
}

View file

@ -24,11 +24,16 @@
package reborncore.common.blockentity;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import reborncore.common.fluid.FluidUtil;
import reborncore.common.util.NBTSerializable;
import reborncore.common.util.Tank;
@ -82,23 +87,21 @@ public class FluidConfiguration implements NBTSerializable {
continue;
}
Tank tank = getTank(machineBase, facing);
@Nullable
Storage<FluidVariant> tank = getTank(machineBase, facing);
if (autoInput() && fluidConfig.getIoConfig().isInsert()) {
FluidUtil.transferFluid(tank, machineBase.getTank(), machineBase.fluidTransferAmount());
StorageUtil.move(tank, machineBase.getTank(), fv -> true, machineBase.fluidTransferAmount().getRawValue(), null);
}
if (autoOutput() && fluidConfig.getIoConfig().isExtact()) {
FluidUtil.transferFluid(machineBase.getTank(), tank, machineBase.fluidTransferAmount());
StorageUtil.move(machineBase.getTank(), tank, fv -> true, machineBase.fluidTransferAmount().getRawValue(), null);
}
}
}
private Tank getTank(MachineBaseBlockEntity machine, Direction facing) {
@Nullable
private Storage<FluidVariant> getTank(MachineBaseBlockEntity machine, Direction facing) {
BlockPos pos = machine.getPos().offset(facing);
BlockEntity blockEntity = machine.getWorld().getBlockEntity(pos);
if (blockEntity instanceof MachineBaseBlockEntity) {
return ((MachineBaseBlockEntity) blockEntity).getTank();
}
return null;
return FluidStorage.SIDED.find(machine.getWorld(), pos, facing.getOpposite());
}
public boolean autoInput() {

View file

@ -54,37 +54,11 @@ public class FluidUtil {
}
public static String getFluidName(@NotNull FluidInstance fluidInstance) {
// TODO: use FluidVariantRendering
return getFluidName(fluidInstance.getFluid());
}
public static String getFluidName(@NotNull Fluid fluid) {
return StringUtils.capitalize(Registry.FLUID.getId(fluid).getPath());
}
public static void transferFluid(Tank source, Tank destination, FluidValue amount) {
if (source == null || destination == null) {
return;
}
if (source.getFluid() == Fluids.EMPTY || source.getFluidAmount().isEmpty()) {
return;
}
if (destination.getFluid() != Fluids.EMPTY && source.getFluid() != destination.getFluid()) {
return;
}
FluidValue transferAmount = source.getFluidAmount().min(amount);
if (destination.getFreeSpace().equalOrMoreThan(transferAmount)) {
FluidInstance fluidInstance = destination.getFluidInstance();
if (fluidInstance.isEmpty()) {
fluidInstance = new FluidInstance(source.getFluid(), transferAmount);
} else {
fluidInstance.addAmount(transferAmount);
}
source.setFluidAmount(source.getFluidAmount().subtract(transferAmount));
destination.setFluidInstance(fluidInstance);
if (source.getFluidAmount().equals(FluidValue.EMPTY)) {
source.setFluid(Fluids.EMPTY);
}
}
}
}

View file

@ -28,26 +28,32 @@ import com.google.common.base.Objects;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
import net.minecraft.util.JsonHelper;
import reborncore.common.util.FluidTextHelper;
public final class FluidValue {
public static final FluidValue EMPTY = new FluidValue(0);
public static final FluidValue BUCKET_QUARTER = new FluidValue(250);
public static final FluidValue BUCKET = new FluidValue(1000);
public static final FluidValue INFINITE = new FluidValue(Integer.MAX_VALUE);
public static final FluidValue BUCKET_QUARTER = new FluidValue(FluidConstants.BUCKET / 4);
public static final FluidValue BUCKET = new FluidValue(FluidConstants.BUCKET);
public static final FluidValue INFINITE = new FluidValue(Long.MAX_VALUE);
private final int rawValue;
private final long rawValue;
private FluidValue(final int rawValue) {
private static FluidValue fromMillibuckets(long millibuckets) {
return new FluidValue(millibuckets * 81);
}
private FluidValue(long rawValue) {
this.rawValue = rawValue;
}
public FluidValue multiply(int value) {
public FluidValue multiply(long value) {
return fromRaw(rawValue * value);
}
public FluidValue fraction(int divider) {return fromRaw(rawValue / divider);}
public FluidValue fraction(long divider) {return fromRaw(rawValue / divider);}
public FluidValue add(FluidValue fluidValue) {
return fromRaw(rawValue + fluidValue.rawValue);
@ -83,12 +89,12 @@ public final class FluidValue {
@Override
public String toString() {
return rawValue + " Mb";
return FluidTextHelper.getValueDisplay(this) + " Mb";
}
//TODO move away from using this
@Deprecated
public int getRawValue() {
public long getRawValue() {
return rawValue;
}
@ -106,7 +112,7 @@ public final class FluidValue {
}
@Deprecated
public static FluidValue fromRaw(int rawValue) {
public static FluidValue fromRaw(long rawValue) {
if (rawValue < 0) {
rawValue = 0;
}
@ -119,10 +125,13 @@ public final class FluidValue {
if (jsonObject.has("buckets")) {
int buckets = JsonHelper.getInt(jsonObject, "buckets");
return BUCKET.multiply(buckets);
} else if (jsonObject.has("droplets")) {
long droplets = JsonHelper.getLong(jsonObject, "droplets");
return fromRaw(droplets);
}
} else if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()) {
//TODO add a warning here
return fromRaw(jsonElement.getAsJsonPrimitive().getAsInt());
return fromMillibuckets(jsonElement.getAsJsonPrimitive().getAsInt());
}
throw new JsonSyntaxException("Could not parse fluid value");
}

View file

@ -24,6 +24,7 @@
package reborncore.common.fluid.container;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.nbt.NbtCompound;
@ -39,6 +40,7 @@ public class FluidInstance implements NBTSerializable {
public static final FluidInstance EMPTY = new FluidInstance(Fluids.EMPTY, FluidValue.EMPTY);
// TODO: move to FluidVariant
protected Fluid fluid;
protected FluidValue amount;
protected NbtCompound tag;
@ -113,7 +115,7 @@ public class FluidInstance implements NBTSerializable {
public NbtCompound write() {
NbtCompound tag = new NbtCompound();
tag.putString(FLUID_KEY, Registry.FLUID.getId(fluid).toString());
tag.putInt(AMOUNT_KEY, amount.getRawValue());
tag.putLong(AMOUNT_KEY, amount.getRawValue());
if (this.tag != null && !this.tag.isEmpty()) {
tag.put(TAG_KEY, this.tag);
}
@ -123,7 +125,7 @@ public class FluidInstance implements NBTSerializable {
@Override
public void read(NbtCompound tag) {
fluid = Registry.FLUID.get(new Identifier(tag.getString(FLUID_KEY)));
amount = FluidValue.fromRaw(tag.getInt(AMOUNT_KEY));
amount = FluidValue.fromRaw(tag.getLong(AMOUNT_KEY));
if (tag.contains(TAG_KEY)) {
this.tag = tag.getCompound(TAG_KEY);
}
@ -137,4 +139,9 @@ public class FluidInstance implements NBTSerializable {
public boolean isFluidEqual(FluidInstance instance) {
return (isEmpty() && instance.isEmpty()) || fluid.equals(instance.getFluid());
}
public FluidVariant getVariant() {
if (isEmpty()) return FluidVariant.blank();
else return FluidVariant.of(fluid, tag);
}
}

View file

@ -1,95 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reborncore.common.fluid.container;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import reborncore.common.fluid.FluidValue;
/*
* Based of Slilk's API but with some breaking changes
* Direction has been replaced with a generic type, this allows for things like ItemStack to be easily passed along
* Some methods such as getCapacity have been tweaked to also provide the type
* Some methods have got default implementations making it a lot easier to implement without the worry for bugs
* The multiple fluids thing has gone, it is still possible to one fluid per side if wanted as the type is passed around everywhere
* A lot of the "helper" methods have been removed, these should really go in boilerplate classes and not the in raw api
* removed the docs as cba to write them
*/
public interface GenericFluidContainer<T> {
@Nullable
static GenericFluidContainer<ItemStack> fromStack(@NotNull ItemStack itemStack) {
if (itemStack.getItem() instanceof GenericFluidContainer) {
//noinspection unchecked
return (GenericFluidContainer<ItemStack>) itemStack.getItem();
}
return null;
}
void setFluid(T type, @NotNull FluidInstance instance);
@NotNull
FluidInstance getFluidInstance(T type);
FluidValue getCapacity(T type);
default boolean canHold(T type, Fluid fluid) {
return true;
}
default FluidValue getCurrentFluidAmount(T type) {
return getFluidInstance(type).getAmount();
}
default boolean canInsertFluid(T type, @NotNull Fluid fluid, FluidValue amount) {
if (!canHold(type, fluid)) {
return false;
}
FluidInstance currentFluid = getFluidInstance(type);
return currentFluid.isEmpty() || currentFluid.getFluid() == fluid && currentFluid.getAmount().add(amount).lessThan(getCapacity(type));
}
default boolean canExtractFluid(T type, @NotNull Fluid fluid, FluidValue amount) {
return getFluidInstance(type).getFluid() == fluid && amount.lessThanOrEqual(getFluidInstance(type).getAmount());
}
default void insertFluid(T type, @NotNull Fluid fluid, FluidValue amount) {
if (canInsertFluid(type, fluid, amount)) {
setFluid(type, getFluidInstance(type).addAmount(amount));
}
}
default void extractFluid(T type, @NotNull Fluid fluid, FluidValue amount) {
if (canExtractFluid(type, fluid, amount)) {
setFluid(type, getFluidInstance(type).subtractAmount(amount));
}
}
}

View file

@ -72,9 +72,9 @@ public enum ObjectBufferUtils {
}),
FLUID_VALUE(FluidValue.class, (value, buffer) -> {
buffer.writeInt(value.getRawValue());
buffer.writeLong(value.getRawValue());
}, buffer -> {
return FluidValue.fromRaw(buffer.readInt());
return FluidValue.fromRaw(buffer.readLong());
}),
COMPOUND_TAG(NbtCompound.class, (value, buffer) -> {

View file

@ -0,0 +1,73 @@
package reborncore.common.util;
import com.google.common.math.LongMath;
import reborncore.common.fluid.FluidValue;
/**
* A few helpers to display fluids.
*/
public class FluidTextHelper {
public static String getValueDisplay(FluidValue value) {
return getUnicodeMillibuckets(value.getRawValue(), false);
}
/**
* Return a unicode string representing a fraction, like ¹.
*/
public static String getUnicodeFraction(long numerator, long denominator, boolean simplify) {
if (numerator < 0 || denominator < 0)
throw new IllegalArgumentException("Numerator and denominator must be non negative.");
if (simplify && denominator != 0) {
long g = LongMath.gcd(numerator, denominator);
numerator /= g;
denominator /= g;
}
StringBuilder numString = new StringBuilder();
while (numerator > 0) {
numString.append(SUPERSCRIPT[(int) (numerator % 10)]);
numerator /= 10;
}
StringBuilder denomString = new StringBuilder();
while (denominator > 0) {
denomString.append(SUBSCRIPT[(int) (denominator % 10)]);
denominator /= 10;
}
return numString.reverse().toString() + FRACTION_BAR + denomString.reverse().toString();
}
/**
* Convert a non negative fluid amount in droplets to a unicode string
* representing the amount in millibuckets. For example, passing 163 will result
* in
*
* <pre>
* 2 ¹
* </pre>
*
* .
*/
public static String getUnicodeMillibuckets(long droplets, boolean simplify) {
String result = "" + droplets / 81;
if (droplets % 81 != 0) {
result += " " + getUnicodeFraction(droplets % 81, 81, simplify);
}
return result;
}
private static final char[] SUPERSCRIPT = new char[] { '\u2070', '\u00b9', '\u00b2', '\u00b3', '\u2074', '\u2075', '\u2076', '\u2077', '\u2078',
'\u2079' };
private static final char FRACTION_BAR = '\u2044';
private static final char[] SUBSCRIPT = new char[] { '\u2080', '\u2081', '\u2082', '\u2083', '\u2084', '\u2085', '\u2086', '\u2087', '\u2088',
'\u2089' };
private FluidTextHelper() {
}
}

View file

@ -24,36 +24,36 @@
package reborncore.common.util;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import reborncore.client.screen.builder.Syncable;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.fluid.container.GenericFluidContainer;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class Tank implements GenericFluidContainer<Direction>, Syncable {
@SuppressWarnings({"deprecation", "UnstableApiUsage"})
public class Tank extends SnapshotParticipant<FluidInstance> implements Syncable, SingleSlotStorage<FluidVariant> {
private final String name;
@NotNull
private FluidInstance fluidInstance = new FluidInstance();
private final FluidValue capacity;
@Nullable
private Direction side = null;
private final MachineBaseBlockEntity blockEntity;
public Tank(String name, FluidValue capacity, MachineBaseBlockEntity blockEntity) {
@ -65,7 +65,7 @@ public class Tank implements GenericFluidContainer<Direction>, Syncable {
@NotNull
public FluidInstance getFluidInstance() {
return getFluidInstance(side);
return fluidInstance;
}
@NotNull
@ -73,12 +73,12 @@ public class Tank implements GenericFluidContainer<Direction>, Syncable {
return getFluidInstance().getFluid();
}
public FluidValue getCapacity() {
public FluidValue getFluidValueCapacity() {
return capacity;
}
public FluidValue getFreeSpace() {
return getCapacity().subtract(getFluidAmount());
return getFluidValueCapacity().subtract(getFluidAmount());
}
public boolean canFit(Fluid fluid, FluidValue amount) {
@ -90,7 +90,7 @@ public class Tank implements GenericFluidContainer<Direction>, Syncable {
}
public boolean isFull() {
return !getFluidInstance().isEmpty() && getFluidInstance().getAmount().equalOrMoreThan(getCapacity());
return !getFluidInstance().isEmpty() && getFluidInstance().getAmount().equalOrMoreThan(getFluidValueCapacity());
}
public final NbtCompound write(NbtCompound nbt) {
@ -121,17 +121,6 @@ public class Tank implements GenericFluidContainer<Direction>, Syncable {
fluidInstance.setFluid(f);
}
@Nullable
public Direction getSide() {
return side;
}
public void setSide(
@Nullable
Direction side) {
this.side = side;
}
@Override
public void getSyncPair(List<Pair<Supplier<?>, Consumer<?>>> pairList) {
pairList.add(Pair.of(() -> Registry.FLUID.getId(fluidInstance.getFluid()).toString(), (Consumer<String>) o -> fluidInstance.setFluid(Registry.FLUID.get(new Identifier(o)))));
@ -142,25 +131,86 @@ public class Tank implements GenericFluidContainer<Direction>, Syncable {
return getFluidInstance().getAmount();
}
@Override
public void setFluid(@Nullable Direction type, @NotNull FluidInstance instance) {
public void setFluid(@NotNull FluidInstance instance) {
fluidInstance = instance;
}
@NotNull
@Override
public FluidInstance getFluidInstance(@Nullable Direction type) {
return fluidInstance;
}
public void setFluidInstance(@NotNull FluidInstance fluidInstance) {
this.fluidInstance = fluidInstance;
}
@Override
public FluidValue getCapacity(@Nullable Direction type) {
return capacity;
public long insert(FluidVariant insertedVariant, long maxAmount, TransactionContext transaction) {
StoragePreconditions.notBlankNotNegative(insertedVariant, maxAmount);
FluidVariant currentVariant = getResource();
if (currentVariant.equals(insertedVariant) || currentVariant.isBlank()) {
long insertedAmount = Math.min(maxAmount, getCapacity() - getAmount());
if (insertedAmount > 0) {
updateSnapshots(transaction);
// Just in case.
if (currentVariant.isBlank()) fluidInstance.setAmount(FluidValue.EMPTY);
fluidInstance.setFluid(insertedVariant.getFluid());
fluidInstance.setTag(insertedVariant.getNbt());
fluidInstance.addAmount(FluidValue.fromRaw(insertedAmount));
}
return insertedAmount;
}
return 0;
}
@Override
public long extract(FluidVariant extractedVariant, long maxAmount, TransactionContext transaction) {
StoragePreconditions.notBlankNotNegative(extractedVariant, maxAmount);
FluidVariant currentVariant = getResource();
if (extractedVariant.equals(currentVariant)) {
long extractedAmount = Math.min(maxAmount, getAmount());
if (extractedAmount > 0) {
updateSnapshots(transaction);
fluidInstance.subtractAmount(FluidValue.fromRaw(extractedAmount));
}
return extractedAmount;
}
return 0;
}
@Override
public boolean isResourceBlank() {
return getResource().isBlank();
}
@Override
public FluidVariant getResource() {
return fluidInstance.getVariant();
}
@Override
public long getAmount() {
return fluidInstance.getAmount().getRawValue();
}
@Override
public long getCapacity() {
return getFluidValueCapacity().getRawValue();
}
@Override
protected FluidInstance createSnapshot() {
return fluidInstance.copy();
}
@Override
protected void readSnapshot(FluidInstance snapshot) {
setFluidInstance(snapshot);
}
}

View file

@ -46,6 +46,7 @@ import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.fluid.FluidUtil;
import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.util.FluidTextHelper;
import reborncore.common.util.RebornInventory;
import reborncore.common.util.Tank;
import techreborn.init.TRBlockEntities;
@ -56,7 +57,7 @@ import java.util.List;
public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider {
protected Tank tank;
private int serverMaxCapacity = -1;
private long serverMaxCapacity = -1;
protected RebornInventory<TankUnitBaseBlockEntity> inventory = new RebornInventory<>(2, "TankInventory", 64, this);
@ -169,7 +170,7 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I
info.add(
new TranslatableText("techreborn.tooltip.unit.capacity")
.formatted(Formatting.GRAY)
.append(new LiteralText(String.valueOf(this.tank.getCapacity()))
.append(new LiteralText(String.valueOf(this.tank.getFluidValueCapacity()))
.formatted(Formatting.GOLD))
);
}
@ -186,11 +187,11 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I
}
// Sync between server/client if configs are mis-matched.
public int getMaxCapacity() {
return this.tank.getCapacity().getRawValue();
public long getMaxCapacity() {
return this.tank.getFluidValueCapacity().getRawValue();
}
public void setMaxCapacity(int maxCapacity) {
public void setMaxCapacity(long maxCapacity) {
FluidInstance instance = tank.getFluidInstance();
this.tank = new Tank("TankStorage", FluidValue.fromRaw(maxCapacity), this);
this.tank.setFluidInstance(instance);
@ -202,9 +203,4 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I
public Tank getTank() {
return tank;
}
@Deprecated
public void setTank(Tank tank) {
this.tank.setFluid(null, tank.getFluidInstance());
}
}

View file

@ -91,7 +91,7 @@ public class TankUnitBlock extends BlockMachineBase {
if(amountInTank.equalOrMoreThan(FluidValue.BUCKET)){
// Amount to transfer is whatever is lower (stack count or tank level)
int amountTransferBuckets = Math.min(amountInTank.getRawValue() / FluidValue.BUCKET.getRawValue(), stackInHand.getCount());
int amountTransferBuckets = (int) Math.min(amountInTank.getRawValue() / FluidValue.BUCKET.getRawValue(), stackInHand.getCount());
// Remove items from player
stackInHand.decrement(amountTransferBuckets);

View file

@ -60,7 +60,7 @@ public class GuiDieselGenerator extends GuiBase<BuiltScreenHandler> {
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(10), 100, 83, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 130, 28, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
}

View file

@ -68,7 +68,7 @@ public class GuiFluidReplicator extends GuiBase<BuiltScreenHandler> {
super.drawForeground(matrixStack, mouseX, mouseY);
final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
builder.drawTank(matrixStack, this, 99, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 99, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(100), 100, 76, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
if (blockEntity.isMultiblockValid()) {
addHologramButton(6, 4, 212, layer).clickHandler(this::onClick);

View file

@ -57,7 +57,7 @@ public class GuiGasTurbine extends GuiBase<BuiltScreenHandler> {
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(10), 100, 83, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 130, 28, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
}
}

View file

@ -72,7 +72,7 @@ public class GuiIndustrialGrinder extends GuiBase<BuiltScreenHandler> {
final Layer layer = Layer.FOREGROUND;
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(100), 100, 105, 47, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawTank(matrixStack, this, 53, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 53, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
if (blockEntity.isMultiblockValid()) {
addHologramButton(6, 4, 212, layer).clickHandler(this::onClick);
} else {

View file

@ -71,7 +71,7 @@ public class GuiIndustrialSawmill extends GuiBase<BuiltScreenHandler> {
final Layer layer = Layer.FOREGROUND;
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(100), 100, 105, 47, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawTank(matrixStack, this, 53, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 53, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
if (blockEntity.isMultiblockValid()) {
addHologramButton(6, 4, 212, layer).clickHandler(this::onClick);
} else {

View file

@ -63,7 +63,7 @@ public class GuiPlasmaGenerator extends GuiBase<BuiltScreenHandler> {
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(10), 100, 83, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 130, 28, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
}

View file

@ -57,7 +57,7 @@ public class GuiSemifluidGenerator extends GuiBase<BuiltScreenHandler> {
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(10), 100, 83, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 130, 28, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
}
}

View file

@ -73,7 +73,7 @@ public class GuiTankUnit extends GuiBase<BuiltScreenHandler> {
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.tank.amount"), 10, 50, 4210752);
textRenderer.draw(matrixStack, fluid.getAmount().toString(), 10, 60, 4210752);
String percentFilled = String.valueOf((int) ((double) fluid.getAmount().getRawValue() / (double) tankEntity.getTank().getCapacity().getRawValue() * 100));
String percentFilled = String.valueOf((int) ((double) fluid.getAmount().getRawValue() / (double) tankEntity.getTank().getFluidValueCapacity().getRawValue() * 100));
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.unit.used").append(percentFilled + "%"), 10, 70, 4210752);

View file

@ -57,7 +57,7 @@ public class GuiThermalGenerator extends GuiBase<BuiltScreenHandler> {
builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(10), 100, 83, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 130, 28, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getCapacity(), blockEntity.tank.isEmpty(), layer);
builder.drawTank(matrixStack, this, 44, 25, mouseX, mouseY, blockEntity.tank.getFluidInstance(), blockEntity.tank.getFluidValueCapacity(), blockEntity.tank.isEmpty(), layer);
}
}

View file

@ -29,13 +29,12 @@ import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import org.jetbrains.annotations.NotNull;
import reborncore.common.fluid.FluidValue;
import reborncore.common.fluid.container.FluidInstance;
import reborncore.common.fluid.container.GenericFluidContainer;
import reborncore.common.fluid.container.ItemFluidInfo;
import reborncore.common.util.Tank;
import reborncore.mixin.common.AccessorFluidBlock;
import java.util.List;
@ -55,11 +54,11 @@ public class FluidUtils {
return Registry.FLUID.stream().collect(Collectors.toList());
}
public static boolean drainContainers(GenericFluidContainer<Direction> tank, Inventory inventory, int inputSlot, int outputSlot) {
public static boolean drainContainers(Tank tank, Inventory inventory, int inputSlot, int outputSlot) {
return drainContainers(tank, inventory, inputSlot, outputSlot, false);
}
public static boolean drainContainers(GenericFluidContainer<Direction> tank, Inventory inventory, int inputSlot, int outputSlot, boolean voidFluid) {
public static boolean drainContainers(Tank tank, Inventory inventory, int inputSlot, int outputSlot, boolean voidFluid) {
ItemStack inputStack = inventory.getStack(inputSlot);
ItemStack outputStack = inventory.getStack(outputSlot);
@ -71,11 +70,11 @@ public class FluidUtils {
if (!outputStack.isEmpty() && !outputStack.isItemEqual(itemFluidInfo.getEmpty())) return false;
FluidInstance tankFluidInstance = tank.getFluidInstance(null);
FluidInstance tankFluidInstance = tank.getFluidInstance();
Fluid tankFluid = tankFluidInstance.getFluid();
if (tankFluidInstance.isEmpty() || tankFluid == itemFluidInfo.getFluid(inputStack)) {
FluidValue freeSpace = tank.getCapacity(null).subtract(tankFluidInstance.getAmount());
FluidValue freeSpace = tank.getFluidValueCapacity().subtract(tankFluidInstance.getAmount());
if (freeSpace.equalOrMoreThan(FluidValue.BUCKET) || voidFluid) {
inputStack.decrement(1);
@ -94,13 +93,13 @@ public class FluidUtils {
return true;
}
public static boolean fillContainers(GenericFluidContainer<Direction> source, Inventory inventory, int inputSlot, int outputSlot) {
public static boolean fillContainers(Tank source, Inventory inventory, int inputSlot, int outputSlot) {
ItemStack inputStack = inventory.getStack(inputSlot);
ItemStack outputStack = inventory.getStack(outputSlot);
if (!FluidUtils.isContainerEmpty(inputStack)) return false;
ItemFluidInfo itemFluidInfo = (ItemFluidInfo) inputStack.getItem();
FluidInstance sourceFluid = source.getFluidInstance(null);
FluidInstance sourceFluid = source.getFluidInstance();
if (sourceFluid.getFluid() == Fluids.EMPTY || sourceFluid.getAmount().lessThan(FluidValue.BUCKET)) {
return false;