Skip to content

Commit

Permalink
Force barrel minecarts to properly sync their stored fluid when acces…
Browse files Browse the repository at this point in the history
…sed by capabilities
  • Loading branch information
BluSunrize committed Jun 14, 2024
1 parent 5a4a582 commit de0a151
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<WoodenBarrelBlockEntity>
{
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);
Expand All @@ -51,7 +55,7 @@ public BarrelMinecartEntity(EntityType<?> type, Level world)
public static <T extends BarrelMinecartEntity>
void registerCapabilities(RegisterCapabilitiesEvent ev, Supplier<EntityType<T>> type)
{
ev.registerEntity(FluidHandler.ENTITY, type.get(), (e, $) -> e.containedBlockEntity.tank);
ev.registerEntity(FluidHandler.ENTITY, type.get(), (e, $) -> e.minecartFluidHandler);
}

@Override
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +39,7 @@

public abstract class IEMinecartEntity<T extends BlockEntity> extends AbstractMinecart implements MenuProvider
{
private static final EntityDataAccessor<CompoundTag> 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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down

0 comments on commit de0a151

Please sign in to comment.