Skip to content

Commit

Permalink
Finish Tank Access
Browse files Browse the repository at this point in the history
  • Loading branch information
dphaldes committed Nov 13, 2023
1 parent 215d205 commit c9fbbd5
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/core/java/com/enderio/core/CoreNBTKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CoreNBTKeys {
public static final String ITEM = "Item";
public static final String ITEMS = "Items";
public static final String FLUID = "Fluid";
public static final String FLUIDS = "Fluids";
public static final String ENERGY = "Energy";
public static final String BLOCK_ENTITY_TAG = BlockItem.BLOCK_ENTITY_TAG;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.enderio.machines.common.block.MachineBlock;
import com.enderio.machines.common.io.IOConfig;
import com.enderio.machines.common.io.fluid.MachineFluidHandler;
import com.enderio.machines.common.io.fluid.MachineTankLayout;
import com.enderio.machines.common.io.item.MachineInventory;
import com.enderio.machines.common.io.item.MachineInventoryLayout;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -47,7 +48,6 @@
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.items.IItemHandler;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -95,9 +95,6 @@ public abstract class MachineBlockEntity extends EnderBlockEntity implements Men
@Nullable
private final MachineInventory inventory;

@Nullable
private final FluidTank fluidTank;

@Nullable
private final MachineFluidHandler fluidHandler;

Expand Down Expand Up @@ -133,9 +130,9 @@ public MachineBlockEntity(BlockEntityType<?> type, BlockPos worldPosition, Block
}

// Create fluid storage
fluidTank = createFluidTank();
if (fluidTank != null) {
fluidHandler = createFluidHandler(fluidTank);
MachineTankLayout tankLayout = getTankLayout();
if (tankLayout != null) {
fluidHandler = createFluidHandler(tankLayout);
if (fluidHandler != null) {
addCapabilityProvider(fluidHandler);
}
Expand Down Expand Up @@ -381,30 +378,40 @@ protected void onInventoryContentsChanged(int slot) { }
// endregion

// region Fluid Storage

@Nullable
protected FluidTank createFluidTank() {
public MachineTankLayout getTankLayout() {
return null;
}

@Nullable
public final FluidTank getFluidTank() {
return fluidTank;
public final MachineFluidHandler getFluidHandler() {
return fluidHandler;
}

/**
* Only call this if you're sure your machine has a fluid tank.
* Only call this if you're sure your machine has an tank.
*/
protected final FluidTank getFluidTankNN() {
return Objects.requireNonNull(fluidTank);
protected final MachineFluidHandler getFluidHandlerNN() {
return Objects.requireNonNull(fluidHandler);
}

@Nullable
protected MachineFluidHandler createFluidHandler(FluidTank fluidTank) {
// We can have a default here, as if createFluidTank returns null, this is never called.
return new MachineFluidHandler(getIOConfig(), fluidTank);
protected MachineFluidHandler createFluidHandler(MachineTankLayout layout) {
return new MachineFluidHandler(getIOConfig(), layout) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
onTankContentsChanged(slot);
setChanged();
}
};
}

/**
* @apiNote Must call this on custom MachineFluid handlers!
*/
protected void onTankContentsChanged(int slot) {}

// endregion

// region Block Entity ticking
Expand Down Expand Up @@ -652,8 +659,8 @@ public void saveAdditional(CompoundTag pTag) {
pTag.put(MachineNBTKeys.ITEMS, inventory.serializeNBT());
}

if (fluidTank != null) {
pTag.put(MachineNBTKeys.FLUID, fluidTank.writeToNBT(new CompoundTag()));
if (this.fluidHandler != null) {
pTag.put(MachineNBTKeys.FLUID, fluidHandler.serializeNBT());
}

if (getMaxRange() > 0) {
Expand All @@ -675,8 +682,8 @@ public void load(CompoundTag pTag) {
inventory.deserializeNBT(pTag.getCompound(MachineNBTKeys.ITEMS));
}

if (fluidTank != null) {
fluidTank.readFromNBT(pTag.getCompound(MachineNBTKeys.FLUID));
if (this.fluidHandler != null) {
fluidHandler.deserializeNBT(pTag.getCompound(MachineNBTKeys.FLUIDS));
}

// For rendering io overlays after placed by an nbt filled block item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.enderio.api.capability.IEnderCapabilityProvider;
import com.enderio.api.io.IIOConfig;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
Expand All @@ -18,10 +23,10 @@
/**
* MachineFluidStorage takes a list of fluid tanks and handles IO for them all.
*/
public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider<IFluidHandler> {
public class MachineFluidHandler implements IFluidHandler, IEnderCapabilityProvider<IFluidHandler>, INBTSerializable<CompoundTag> {
private final IIOConfig config;
private final MachineTankLayout layout;
private final List<MachineTank> tanks;
private List<MachineTank> tanks;

private final EnumMap<Direction, LazyOptional<Sided>> sideCache = new EnumMap<>(Direction.class);
private LazyOptional<MachineFluidHandler> selfCache = LazyOptional.empty();
Expand Down Expand Up @@ -69,7 +74,7 @@ public int getTankCapacity(int tank) {

@Override
public boolean isFluidValid(int tank, FluidStack stack) {
return tanks.get(tank).isFluidValid(stack);
return layout.isFluidValid(tank, stack);
}

@Override
Expand Down Expand Up @@ -163,6 +168,35 @@ public FluidStack drain(int maxDrain, FluidAction action) {
return FluidStack.EMPTY;
}

protected void onContentsChanged(int slot) {}

@Override
public CompoundTag serializeNBT() {
ListTag nbtTagList = new ListTag();
for (int i = 0; i < tanks.size(); i++) {
CompoundTag tankTag = new CompoundTag();
tankTag.putInt("Index", i);
tanks.get(i).save(tankTag);
nbtTagList.add(tankTag);
}
CompoundTag nbt = new CompoundTag();
nbt.put("Tanks", nbtTagList);
nbt.putInt("Size", tanks.size());
return nbt;
}

@Override
public void deserializeNBT(CompoundTag nbt) {
int size = nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : tanks.size();
tanks = NonNullList.withSize(size, MachineTank.EMPTY);
ListTag tagList = nbt.getList("Tanks", Tag.TAG_COMPOUND);
for (int i = 0; i < tagList.size(); i++) {
CompoundTag tankTag = tagList.getCompound(i);
int index = tankTag.getInt("Index");
tanks.set(index, MachineTank.from(tankTag));
}
}

// Sided capability access
private static class Sided implements IFluidHandler {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package com.enderio.machines.common.io.fluid;

import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandler;
import org.jetbrains.annotations.NotNull;

import java.util.function.Predicate;

//Replace MachineFluidTank
public class MachineTank implements IFluidTank {

private final int capacity;
private final Predicate<FluidStack> validator;

private boolean canInsert;

private boolean canExtract;
@NotNull private FluidStack fluid = FluidStack.EMPTY;
public static final MachineTank EMPTY = new MachineTank(0, true, true);

public MachineTank(int capacity, Predicate<FluidStack> validator, boolean canInsert, boolean canExtract) {
public MachineTank(int capacity, boolean canInsert, boolean canExtract) {
this.capacity = capacity;
this.validator = validator;
this.canInsert = canInsert;
this.canExtract = canExtract;
}

public MachineTank(FluidStack stack, int capacity, boolean canInsert, boolean canExtract) {
this(capacity, canInsert, canExtract);
this.fluid = stack.copy();
}

public static MachineTank from(CompoundTag tag) {
FluidStack stack = FluidStack.loadFluidStackFromNBT(tag);
int capacity = tag.getInt("Capacity");
boolean canInsert = tag.getBoolean("CanInsert");
boolean canExtract = tag.getBoolean("CanExtract");
return new MachineTank(stack, capacity, canInsert, canExtract);
}

@Override
public int getCapacity() {
return capacity;
Expand All @@ -42,11 +51,11 @@ public int getFluidAmount() {

@Override
public boolean isFluidValid(FluidStack stack) {
return validator.test(stack);
return true;
}

public int fill(FluidStack resource, IFluidHandler.FluidAction action) {
if (!canInsert || resource.isEmpty() || !isFluidValid(resource)) {
if (!canInsert || resource.isEmpty()) {
return 0;
}
if (action.simulate()) {
Expand Down Expand Up @@ -103,8 +112,14 @@ public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) {
return stack;
}

protected void onContentsChanged() {
protected void onContentsChanged() {}

public CompoundTag save(CompoundTag compoundTag) {
getFluid().writeToNBT(compoundTag);
compoundTag.putInt("Capacity", getCapacity());
compoundTag.putBoolean("CanInsert", canInsert);
compoundTag.putBoolean("CanExtract", canExtract);
return compoundTag;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean isFluidValid(int slot, FluidStack stack) {
public List<MachineTank> createTanks() {
List<MachineTank> tankList = new ArrayList<>();
tanks.forEach((config -> {
tankList.add(new MachineTank(config.capacity, config.filter, config.insert, config.extract));
tankList.add(new MachineTank(config.capacity, config.insert, config.extract));
}));
return tankList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
package com.enderio.machines.common.io.fluid;

import com.enderio.machines.common.blockentity.base.MachineBlockEntity;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;

public class TankAccess {

private int index = Integer.MIN_VALUE;

public FluidStack getFluid(MachineBlockEntity blockEntity) {
return getFluid(blockEntity.getFluidHandler());
}

public FluidStack getFluid(MachineFluidHandler handler) {
return handler.getFluidInTank(index);
}

public int fill(MachineFluidHandler handler, FluidStack stack, IFluidHandler.FluidAction action) {
return handler.fill(stack, action);
}

public int fill(MachineBlockEntity machine, FluidStack stack, IFluidHandler.FluidAction action) {
return fill(machine.getFluidHandler(), stack, action);
}

public FluidStack drain(MachineFluidHandler handler, FluidStack resource, IFluidHandler.FluidAction action) {
return handler.drain(resource, action);
}

public FluidStack drain(MachineBlockEntity machine, FluidStack resource, IFluidHandler.FluidAction action) {
return drain(machine.getFluidHandler(), resource, action);
}

public FluidStack drain(MachineFluidHandler handler, int maxDrain, IFluidHandler.FluidAction action) {
return handler.drain(maxDrain, action);
}

public FluidStack drain(MachineBlockEntity machine, int maxDrain, IFluidHandler.FluidAction action) {
return drain(machine.getFluidHandler(), maxDrain, action);
}

public boolean isSlot(int slot) {
return this.index == slot;
}

public int getIndex() {
return index;
}

void init(int i) {
if (index == Integer.MIN_VALUE) {
index = i;
Expand Down

0 comments on commit c9fbbd5

Please sign in to comment.