diff --git a/src/main/java/blusunrize/immersiveengineering/common/entities/BarrelMinecartEntity.java b/src/main/java/blusunrize/immersiveengineering/common/entities/BarrelMinecartEntity.java index 517516a181..c111754998 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/entities/BarrelMinecartEntity.java +++ b/src/main/java/blusunrize/immersiveengineering/common/entities/BarrelMinecartEntity.java @@ -26,13 +26,17 @@ import net.minecraft.world.level.block.state.BlockState; import net.neoforged.neoforge.capabilities.Capabilities.FluidHandler; import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; +import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidUtil; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; import javax.annotation.Nonnull; import java.util.function.Supplier; public class BarrelMinecartEntity extends IEMinecartEntity { + public MinecartFluidHandler minecartFluidHandler = new MinecartFluidHandler(this); + public BarrelMinecartEntity(Level world, double x, double y, double z) { this(IEEntityTypes.BARREL_MINECART.get(), world, x, y, z); @@ -51,7 +55,7 @@ public BarrelMinecartEntity(EntityType type, Level world) public static void registerCapabilities(RegisterCapabilitiesEvent ev, Supplier> type) { - ev.registerEntity(FluidHandler.ENTITY, type.get(), (e, $) -> e.containedBlockEntity.tank); + ev.registerEntity(FluidHandler.ENTITY, type.get(), (e, $) -> e.minecartFluidHandler); } @Override @@ -102,4 +106,67 @@ public BlockState getDisplayBlockState() return IEBlocks.WoodenDevices.WOODEN_BARREL.defaultBlockState(); } + + static class MinecartFluidHandler implements IFluidHandler + { + final BarrelMinecartEntity minecart; + + public MinecartFluidHandler(BarrelMinecartEntity minecart) + { + this.minecart = minecart; + } + + @Override + public int getTanks() + { + return 1; + } + + @Override + public FluidStack getFluidInTank(int tank) + { + return this.minecart.containedBlockEntity.tank.getFluidInTank(tank); + } + + @Override + public int getTankCapacity(int tank) + { + return this.minecart.containedBlockEntity.tank.getTankCapacity(tank); + } + + @Override + public boolean isFluidValid(int tank, FluidStack stack) + { + return this.minecart.containedBlockEntity.tank.isFluidValid(tank, stack); + } + + @Override + public int fill(FluidStack resource, FluidAction action) + { + int filled = this.minecart.containedBlockEntity.tank.fill(resource, action); + updateContainingEntity(); + return filled; + } + + @Override + public FluidStack drain(FluidStack resource, FluidAction action) + { + FluidStack drained = this.minecart.containedBlockEntity.tank.drain(resource, action); + updateContainingEntity(); + return drained; + } + + @Override + public FluidStack drain(int maxDrain, FluidAction action) + { + FluidStack drained = this.minecart.containedBlockEntity.tank.drain(maxDrain, action); + updateContainingEntity(); + return drained; + } + + private void updateContainingEntity() + { + this.minecart.updateSynchedData(); + } + } } diff --git a/src/main/java/blusunrize/immersiveengineering/common/entities/IEMinecartEntity.java b/src/main/java/blusunrize/immersiveengineering/common/entities/IEMinecartEntity.java index b3ff31d791..d6866627f8 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/entities/IEMinecartEntity.java +++ b/src/main/java/blusunrize/immersiveengineering/common/entities/IEMinecartEntity.java @@ -12,6 +12,9 @@ import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IComparatorOverride; import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IInteractionObjectIE; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.syncher.EntityDataAccessor; +import net.minecraft.network.syncher.EntityDataSerializers; +import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -36,6 +39,7 @@ public abstract class IEMinecartEntity extends AbstractMinecart implements MenuProvider { + private static final EntityDataAccessor DATA_ID_BE_DATA = SynchedEntityData.defineId(IEMinecartEntity.class, EntityDataSerializers.COMPOUND_TAG); protected T containedBlockEntity; protected IEMinecartEntity(EntityType type, Level world, double x, double y, double z) @@ -114,6 +118,14 @@ public AbstractContainerMenu createMenu(int id, @Nonnull Inventory playerInvento return null; } + @Override + protected void defineSynchedData() + { + super.defineSynchedData(); + this.entityData.define(DATA_ID_BE_DATA, new CompoundTag()); + } + + @Override protected void addAdditionalSaveData(@Nonnull CompoundTag compound) { @@ -131,6 +143,20 @@ protected void readAdditionalSaveData(@Nonnull CompoundTag compound) super.readAdditionalSaveData(compound); this.containedBlockEntity = getTileProvider().get(); this.containedBlockEntity.load(compound); + this.updateSynchedData(); + } + + public void updateSynchedData() + { + this.getEntityData().set(DATA_ID_BE_DATA, this.containedBlockEntity.saveWithoutMetadata()); + } + + @Override + public void onSyncedDataUpdated(EntityDataAccessor p_38527_) + { + super.onSyncedDataUpdated(p_38527_); + if(DATA_ID_BE_DATA.equals(p_38527_)) + this.containedBlockEntity.load(this.getEntityData().get(DATA_ID_BE_DATA)); } // This is only used by the super impl of destroy, which does not allow attaching NBT to the drop. So it's actually