Fix chest minecarts clearing their inventory when disassembled

This commit is contained in:
reidbhuntley 2021-05-29 15:17:24 -04:00
parent 55abcde5d5
commit 51c0e347b8
2 changed files with 37 additions and 15 deletions

View file

@ -119,7 +119,7 @@ public abstract class Contraption {
public Optional<List<AxisAlignedBB>> simplifiedEntityColliders;
public AbstractContraptionEntity entity;
public CombinedInvWrapper inventory;
public ContraptionInvWrapper inventory;
public CombinedTankWrapper fluidInventory;
public AxisAlignedBB bounds;
public BlockPos anchor;
@ -252,7 +252,7 @@ public abstract class Contraption {
.stream()
.map(MountedStorage::getItemHandler)
.collect(Collectors.toList());
inventory = new CombinedInvWrapper(Arrays.copyOf(list.toArray(), list.size(), IItemHandlerModifiable[].class));
inventory = new ContraptionInvWrapper(Arrays.copyOf(list.toArray(), list.size(), IItemHandlerModifiable[].class));
List<IFluidHandler> fluidHandlers = fluidStorage.values()
.stream()
@ -341,7 +341,7 @@ public abstract class Contraption {
// Bearings potentially create stabilized sub-contraptions
if (AllBlocks.MECHANICAL_BEARING.has(state))
moveBearing(pos, frontier, visited, state);
// WM Bearings attach their structure when moved
if (AllBlocks.WINDMILL_BEARING.has(state))
moveWindmillBearing(pos, frontier, visited, state);
@ -739,7 +739,7 @@ public abstract class Contraption {
for (MountedFluidStorage mountedStorage : fluidStorage.values())
fluidHandlers[index++] = mountedStorage.getFluidHandler();
inventory = new CombinedInvWrapper(handlers);
inventory = new ContraptionInvWrapper(handlers);
fluidInventory = new CombinedTankWrapper(fluidHandlers);
if (nbt.contains("BoundsFront"))
@ -1053,8 +1053,10 @@ public abstract class Contraption {
BlockFlags.IS_MOVING | BlockFlags.DEFAULT, 512);
}
for (int i = 0; i < inventory.getSlots(); i++)
inventory.setStackInSlot(i, ItemStack.EMPTY);
for (int i = 0; i < inventory.getSlots(); i++) {
if (!inventory.isSlotExternal(i))
inventory.setStackInSlot(i, ItemStack.EMPTY);
}
for (int i = 0; i < fluidInventory.getTanks(); i++)
fluidInventory.drain(fluidInventory.getFluidInTank(i), FluidAction.EXECUTE);
@ -1261,4 +1263,24 @@ public abstract class Contraption {
return pos.equals(te.getPos());
}
}
public static class ContraptionInvWrapper extends CombinedInvWrapper {
protected final boolean isExternal;
public ContraptionInvWrapper(boolean isExternal, IItemHandlerModifiable... itemHandler) {
super(itemHandler);
this.isExternal = isExternal;
}
public ContraptionInvWrapper(IItemHandlerModifiable... itemHandler) {
this(false, itemHandler);
}
public boolean isSlotExternal(int slot) {
if (isExternal)
return true;
IItemHandlerModifiable handler = getHandlerFromIndex(getIndexForSlot(slot));
return handler instanceof ContraptionInvWrapper && ((ContraptionInvWrapper) handler).isSlotExternal(slot);
}
}
}

View file

@ -53,7 +53,7 @@ public class MountedContraption extends Contraption {
protected ContraptionType getType() {
return ContraptionType.MOUNTED;
}
@Override
public boolean assemble(World world, BlockPos pos) throws AssemblyException {
BlockState state = world.getBlockState(pos);
@ -61,17 +61,17 @@ public class MountedContraption extends Contraption {
return false;
if (!searchMovedStructure(world, pos, null))
return false;
Axis axis = state.get(RAIL_SHAPE) == RailShape.EAST_WEST ? Axis.X : Axis.Z;
addBlock(pos, Pair.of(new BlockInfo(pos, AllBlocks.MINECART_ANCHOR.getDefaultState()
.with(BlockStateProperties.HORIZONTAL_AXIS, axis), null), null));
if (blocks.size() == 1)
return false;
return true;
}
@Override
protected boolean addToInitialFrontier(World world, BlockPos pos, Direction direction, Queue<BlockPos> frontier) {
frontier.clear();
@ -149,18 +149,18 @@ public class MountedContraption extends Contraption {
protected boolean customBlockRemoval(IWorld world, BlockPos pos, BlockState state) {
return AllBlocks.MINECART_ANCHOR.has(state);
}
@Override
public boolean canBeStabilized(Direction facing, BlockPos localPos) {
return true;
}
@Override
public void addExtraInventories(Entity cart) {
if (!(cart instanceof IInventory))
return;
IItemHandlerModifiable handlerFromInv = new InvWrapper((IInventory) cart);
inventory = new CombinedInvWrapper(handlerFromInv, inventory);
IItemHandlerModifiable handlerFromInv = new ContraptionInvWrapper(true, new InvWrapper((IInventory) cart));
inventory = new ContraptionInvWrapper(handlerFromInv, inventory);
}
@Override