The magical bucket

- Fixed basins consuming container items with certain output inventories
- Potentially fixed item duplication with stale contraption inventories
This commit is contained in:
simibubi 2020-12-22 13:18:44 +01:00
parent 6b5e223508
commit 32b0a01c3a
3 changed files with 70 additions and 8 deletions

View File

@ -63,6 +63,7 @@ import net.minecraft.block.SlimeBlock;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.fluid.IFluidState; import net.minecraft.fluid.IFluidState;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT; import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.NBTUtil; import net.minecraft.nbt.NBTUtil;
@ -85,6 +86,7 @@ import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import net.minecraftforge.fluids.capability.templates.FluidTank; import net.minecraftforge.fluids.capability.templates.FluidTank;
import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.CombinedInvWrapper; import net.minecraftforge.items.wrapper.CombinedInvWrapper;
@ -825,6 +827,11 @@ public abstract class Contraption {
} }
} }
for (int i = 0; i < inventory.getSlots(); i++)
inventory.setStackInSlot(i, ItemStack.EMPTY);
for (int i = 0; i < fluidInventory.getTanks(); i++)
fluidInventory.drain(fluidInventory.getFluidInTank(i), FluidAction.EXECUTE);
for (Pair<BlockPos, Direction> pair : superglue) { for (Pair<BlockPos, Direction> pair : superglue) {
BlockPos targetPos = transform.apply(pair.getKey()); BlockPos targetPos = transform.apply(pair.getKey());
Direction targetFacing = transform.transformFacing(pair.getValue()); Direction targetFacing = transform.transformFacing(pair.getValue());

View File

@ -158,6 +158,7 @@ public class BasinBlock extends Block implements ITE<BasinTileEntity>, IWrenchab
withTileEntityDo(worldIn, pos, te -> { withTileEntityDo(worldIn, pos, te -> {
ItemHelper.dropContents(worldIn, pos, te.inputInventory); ItemHelper.dropContents(worldIn, pos, te.inputInventory);
ItemHelper.dropContents(worldIn, pos, te.outputInventory); ItemHelper.dropContents(worldIn, pos, te.outputInventory);
te.spoutputBuffer.forEach(is -> Block.spawnAsEntity(worldIn, pos, is));
}); });
worldIn.removeTileEntity(pos); worldIn.removeTileEntity(pos);
} }

View File

@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.processing;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
@ -33,6 +34,7 @@ import com.simibubi.create.foundation.utility.LerpedFloat.Chaser;
import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.NBTHelper;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
@ -82,6 +84,7 @@ public class BasinTileEntity extends SmartTileEntity {
List<Direction> disabledSpoutputs; List<Direction> disabledSpoutputs;
Direction preferredSpoutput; Direction preferredSpoutput;
protected List<ItemStack> spoutputBuffer;
public static final int OUTPUT_ANIMATION_TIME = 10; public static final int OUTPUT_ANIMATION_TIME = 10;
List<IntAttached<ItemStack>> visualizedOutputItems; List<IntAttached<ItemStack>> visualizedOutputItems;
@ -106,6 +109,7 @@ public class BasinTileEntity extends SmartTileEntity {
visualizedOutputFluids = Collections.synchronizedList(new ArrayList<>()); visualizedOutputFluids = Collections.synchronizedList(new ArrayList<>());
disabledSpoutputs = new ArrayList<>(); disabledSpoutputs = new ArrayList<>();
preferredSpoutput = null; preferredSpoutput = null;
spoutputBuffer = new ArrayList<>();
} }
@Override @Override
@ -143,6 +147,7 @@ public class BasinTileEntity extends SmartTileEntity {
disabledSpoutputs.clear(); disabledSpoutputs.clear();
ListNBT disabledList = compound.getList("DisabledSpoutput", NBT.TAG_STRING); ListNBT disabledList = compound.getList("DisabledSpoutput", NBT.TAG_STRING);
disabledList.forEach(d -> disabledSpoutputs.add(Direction.valueOf(((StringNBT) d).getString()))); disabledList.forEach(d -> disabledSpoutputs.add(Direction.valueOf(((StringNBT) d).getString())));
spoutputBuffer = NBTHelper.readItemList(compound.getList("Overflow", NBT.TAG_COMPOUND));
if (!clientPacket) if (!clientPacket)
return; return;
@ -165,6 +170,7 @@ public class BasinTileEntity extends SmartTileEntity {
ListNBT disabledList = new ListNBT(); ListNBT disabledList = new ListNBT();
disabledSpoutputs.forEach(d -> disabledList.add(StringNBT.of(d.name()))); disabledSpoutputs.forEach(d -> disabledList.add(StringNBT.of(d.name())));
compound.put("DisabledSpoutput", disabledList); compound.put("DisabledSpoutput", disabledList);
compound.put("Overflow", NBTHelper.writeItemList(spoutputBuffer));
if (!clientPacket) if (!clientPacket)
return; return;
@ -269,6 +275,10 @@ public class BasinTileEntity extends SmartTileEntity {
ingredientRotationSpeed.tickChaser(); ingredientRotationSpeed.tickChaser();
ingredientRotation.setValue(ingredientRotation.getValue() + ingredientRotationSpeed.getValue()); ingredientRotation.setValue(ingredientRotation.getValue() + ingredientRotationSpeed.getValue());
} }
if (!spoutputBuffer.isEmpty() && !world.isRemote)
tryClearingSpoutputOverflow();
if (!contentsChanged) if (!contentsChanged)
return; return;
contentsChanged = false; contentsChanged = false;
@ -287,6 +297,46 @@ public class BasinTileEntity extends SmartTileEntity {
} }
} }
private void tryClearingSpoutputOverflow() {
BlockState blockState = getBlockState();
if (!(blockState.getBlock() instanceof BasinBlock))
return;
Direction direction = blockState.get(BasinBlock.FACING);
TileEntity te = world.getTileEntity(pos.down()
.offset(direction));
IItemHandler targetInv = te == null ? null
: te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite())
.orElse(null);
boolean update = false;
for (Iterator<ItemStack> iterator = spoutputBuffer.iterator(); iterator.hasNext();) {
ItemStack itemStack = iterator.next();
if (direction == Direction.DOWN) {
Block.spawnAsEntity(world, pos, itemStack);
iterator.remove();
update = true;
continue;
}
if (targetInv == null)
return;
if (!ItemHandlerHelper.insertItemStacked(targetInv, itemStack, true)
.isEmpty())
continue;
update = true;
ItemHandlerHelper.insertItemStacked(targetInv, itemStack.copy(), false);
iterator.remove();
visualizedOutputItems.add(IntAttached.withZero(itemStack));
}
if (update) {
notifyChangeOfContents();
sendData();
}
}
public float getTotalFluidUnits(float partialTicks) { public float getTotalFluidUnits(float partialTicks) {
int renderedFluids = 0; int renderedFluids = 0;
float totalUnits = 0; float totalUnits = 0;
@ -370,6 +420,8 @@ public class BasinTileEntity extends SmartTileEntity {
} else { } else {
// Output basin, try moving items to it // Output basin, try moving items to it
if (!spoutputBuffer.isEmpty())
return false;
TileEntity te = world.getTileEntity(pos.down() TileEntity te = world.getTileEntity(pos.down()
.offset(direction)); .offset(direction));
if (te == null) if (te == null)
@ -382,12 +434,14 @@ public class BasinTileEntity extends SmartTileEntity {
if (targetInv == null && !outputItems.isEmpty()) if (targetInv == null && !outputItems.isEmpty())
return false; return false;
for (ItemStack itemStack : outputItems) for (ItemStack itemStack : outputItems) {
if (simulate) {
if (!ItemHandlerHelper.insertItemStacked(targetInv, itemStack.copy(), simulate) if (!ItemHandlerHelper.insertItemStacked(targetInv, itemStack.copy(), simulate)
.isEmpty()) .isEmpty())
return false; return false;
else if (!simulate) } else
visualizedOutputItems.add(IntAttached.withZero(itemStack)); spoutputBuffer.add(itemStack.copy());
}
if (outputFluids.isEmpty()) if (outputFluids.isEmpty())
return true; return true;