Skip to content

Commit

Permalink
Merge pull request #1 from BluSunrize/voidsong/fix-minecart-interaction
Browse files Browse the repository at this point in the history
Fix synching errors with minecarts & fluid pumps
  • Loading branch information
voidsong-dragonfly authored Jun 14, 2024
2 parents 9ba081b + de0a151 commit 6834130
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -53,7 +52,6 @@
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.Capabilities.EnergyStorage;
import net.neoforged.neoforge.capabilities.Capabilities.FluidHandler;
import net.neoforged.neoforge.energy.IEnergyStorage;
Expand Down Expand Up @@ -129,7 +127,9 @@ public void tickServer()
if(sideConfig.get(f)==IOSideConfig.INPUT)
{
IFluidHandler input = blockFluidHandlers.get(f).getCapability();
List<Entity> entities;
// attempt to find an adjacent entity fluid handler
if(input==null)
input = getEntityFluidHandler(f);
if(input!=null)
{
int drainAmount = IFluidPipe.getTransferableAmount(this.canOutputPressurized(false));
Expand All @@ -139,21 +139,6 @@ public void tickServer()
int out = this.outputFluid(drain, FluidAction.EXECUTE);
input.drain(out, FluidAction.EXECUTE);
}
else if (!(entities=level.getEntities(null, new AABB(getBlockPos().offset(f.getNormal())))).isEmpty())
{
for (Entity toCheck : entities) {
input = toCheck.getCapability(FluidHandler.ENTITY, f);
if (input!=null)
{
int drainAmount = IFluidPipe.getTransferableAmount(this.canOutputPressurized(false));
FluidStack drain = input.drain(drainAmount, FluidAction.SIMULATE);
if(drain.isEmpty())
continue;
int out = this.outputFluid(drain, FluidAction.EXECUTE);
input.drain(out, FluidAction.EXECUTE);
}
}
}
else
gatherInfiniteFluidFromWorld(f);
}
Expand Down Expand Up @@ -284,7 +269,6 @@ public int outputFluid(FluidStack fs, FluidAction action)
if(sideConfig.get(f)==IOSideConfig.OUTPUT)
{
IFluidHandler handler = blockFluidHandlers.get(f).getCapability();
List<Entity> entities;
if(handler!=null)
{
// TODO check if there are bad BE assumptions here
Expand All @@ -299,19 +283,17 @@ public int outputFluid(FluidStack fs, FluidAction action)
sum += temp;
}
}
else if (!(entities=level.getEntities(null, new AABB(getBlockPos().offset(f.getNormal())))).isEmpty())
else
{
for (Entity toCheck : entities) {
handler = toCheck.getCapability(FluidHandler.ENTITY, f);
if (handler!=null)
handler = getEntityFluidHandler(f);
if(handler!=null)
{
FluidStack insertResource = Utils.copyFluidStackWithAmount(fs, fs.getAmount(), true);
int temp = handler.fill(insertResource, FluidAction.SIMULATE);
if(temp > 0)
{
FluidStack insertResource = Utils.copyFluidStackWithAmount(fs, fs.getAmount(), true);
int temp = handler.fill(insertResource, FluidAction.SIMULATE);
if(temp > 0)
{
sorting.put(new DirectionalFluidOutput(handler, f, null, worldPosition.relative(f)), temp);
sum += temp;
}
sorting.put(new DirectionalFluidOutput(handler, f, null, worldPosition.relative(f)), temp);
sum += temp;
}
}
}
Expand Down Expand Up @@ -343,6 +325,16 @@ else if (!(entities=level.getEntities(null, new AABB(getBlockPos().offset(f.getN
return 0;
}

@Nullable
private IFluidHandler getEntityFluidHandler(Direction direction)
{
return level.getEntities(null, new AABB(getBlockPos().relative(direction))).stream()
.map(entity -> entity.getCapability(FluidHandler.ENTITY, direction.getOpposite()))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}


@Override
public void readCustomNBT(CompoundTag nbt, boolean descPacket)
Expand Down
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 6834130

Please sign in to comment.