Skip to content

Commit

Permalink
Would you look at that - inserting items works!
Browse files Browse the repository at this point in the history
On the downside, TheOneProbe stopped showing the data for the Boiler for
some reason - probably because both the ItemHandler and FluidHandler
capabilities are provided in all faces. Well, another incentive to make
a nice GUI to show what's in our tanks.
  • Loading branch information
gazotti committed Mar 9, 2021
1 parent 5bb02e8 commit 9d6baf3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/main/java/gazcreations/borkler/blocks/BorklerBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ public boolean hasTileEntity(BlockState state) {
@Override
public void onNeighborChange(BlockState state, IWorldReader world, BlockPos pos, BlockPos neighbor) {
gazcreations.borkler.Borkler.LOGGER.info("A Borkler has been notified of changes to its neighbors.");
this.getTileEntity(world, pos).updateFluidConnections();
BorklerTileEntity te = this.getTileEntity(world, pos);
te.updateFluidConnections();
te.updateItemConnections();
}

/**
Expand Down Expand Up @@ -196,6 +198,7 @@ public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, Livin
te.setCustomName(stack.getDisplayName());
}
te.updateFluidConnections();
te.updateItemConnections();
}

/**
Expand Down
122 changes: 94 additions & 28 deletions src/main/java/gazcreations/borkler/entities/BorklerTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.LogicalSidedProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.registries.ForgeRegistries;
Expand Down Expand Up @@ -122,13 +123,23 @@ public class BorklerTileEntity extends LockableTileEntity implements ITickableTi
* Note that that method uses {@link Collections#synchronizedSet(Set)} to create
* the set; as such, iteration must be synchronized on it.
*/
private Set<LazyOptional<IFluidHandler>> connections;
private Set<LazyOptional<IFluidHandler>> fluidConnections;

/**
* A cacheable value for this entity's Capability<IFluidHandler>.
*/
private LazyOptional<IFluidHandler> fluidHandlerCapability;

/**
* A Set containing up to 6 item connections for this boiler.
*/
private Set<LazyOptional<IItemHandler>> itemConnections;

/**
* A cacheable value for this entity's Capability<IItemHandler>.
*/
private LazyOptional<IItemHandler> itemHandlerCapability;

/**
* A constructor. Populates the Borkler's tanks with empty FluidStacks,
* initializes its inventory and sets burnTime to zero.
Expand Down Expand Up @@ -160,10 +171,13 @@ public boolean isItemValidForSlot(int slot, ItemStack stack) {
};
this.burnTime = 0;
this.isActive = false;
this.connections = Collections.emptySet();
this.fluidConnections = Collections.emptySet();
this.itemConnections = Collections.emptySet();
this.world = (World) world;
if (world != null) // index TEs will not run this
if (world != null) { // index TEs will not run this
updateFluidConnections();
updateItemConnections();
}
Borkler.LOGGER.info("BorklerTileEntity created at " + pos);
}

Expand Down Expand Up @@ -226,25 +240,15 @@ private final void setActive(final boolean active) {
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (stack.isEmpty())
return ItemStack.EMPTY;
/*
* if (isActuallyALiquid(stack)) { // TODO this looks like it will fuck shit up.
* ItemStack copy = ItemHandlerHelper.copyStackWithSize(stack,
* stack.getCount()); IFluidHandler o = (IFluidHandler)
* copy.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY);
* fill(o.drain(o.getFluidInTank(0).getAmount(), FluidAction.EXECUTE),
* FluidAction.EXECUTE); return copy;
*
* }
*/

if (!isItemValid(0, stack))
return stack;
ItemStack existing = this.solidFuel.getStackInSlot(0);

ItemStack existing = this.solidFuel.getStackInSlot(0);
int limit = getSlotLimit(0);
if (!existing.isEmpty()) {
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
return stack;

limit -= existing.getCount();
}

Expand Down Expand Up @@ -273,7 +277,6 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
}
markDirty();
}

return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit) : ItemStack.EMPTY;
}

Expand Down Expand Up @@ -305,7 +308,6 @@ public int getSlotLimit(int slot) {
@Override
public boolean isItemValid(int slot, ItemStack stack) {
return solidFuel.isItemValidForSlot(0, stack);

}

/**
Expand Down Expand Up @@ -535,9 +537,63 @@ public void accept(LazyOptional<T> t) {
return element;
}

public void updateItemConnections() {
Set<LazyOptional<IItemHandler>> consumers = Collections
.synchronizedSet(new HashSet<LazyOptional<IItemHandler>>(7, 0.99f) {
private static final long serialVersionUID = 1L;

public boolean add(LazyOptional<IItemHandler> element) {
if (element == null || !element.isPresent())
return false;
return super.add(element);
}
});
gazcreations.borkler.Borkler.LOGGER
.info("Borkler @" + world + " ," + pos + " has been politely asked to update its item connections.");
LazyOptional<IItemHandler> cap = null;
TileEntity te = null;
// Trigger warning: the following section may require subsequent use of
// eyebleach.
// up
if ((te = this.world.getTileEntity(getPos().offset(Direction.UP))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.DOWN);
// if (cap.isPresent()) override of Set.add will prevent empty Optionals from
// being added
addWithListener(consumers, cap);
}
// down
if ((te = this.world.getTileEntity(getPos().offset(Direction.DOWN))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP);
addWithListener(consumers, cap);
}
// east
if ((te = this.world.getTileEntity(getPos().offset(Direction.EAST))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.WEST);
addWithListener(consumers, cap);
}
// west
if ((te = this.world.getTileEntity(getPos().offset(Direction.WEST))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.EAST);
addWithListener(consumers, cap);
}
// north
if ((te = this.world.getTileEntity(getPos().offset(Direction.NORTH))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.SOUTH);
addWithListener(consumers, cap);
}
// south
if ((te = this.world.getTileEntity(getPos().offset(Direction.SOUTH))) != null) {
cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.NORTH);
addWithListener(consumers, cap);
}
gazcreations.borkler.Borkler.LOGGER
.info("Borkler @" + world + " ," + pos + "has updated its item connections: " + consumers.toString());
this.itemConnections = consumers;
}

/**
* This method runs a check on all sides of this TileEntity and populates this
* {@link BorklerTileEntity#connections} with a HashSet containing up to 6
* {@link BorklerTileEntity#fluidConnections} with a HashSet containing up to 6
* {@link IFluidHandler} instances. <br>
* The populated set is guaranteed not to be null and not to contain any null
* elements.
Expand All @@ -556,7 +612,7 @@ public boolean add(LazyOptional<IFluidHandler> element) {
});
gazcreations.borkler.Borkler.LOGGER
.info("Borkler @" + world + " ," + pos + " has been politely asked to update its fluid connections.");
gazcreations.borkler.Borkler.LOGGER.info("Current connections are: " + this.connections.toString());
gazcreations.borkler.Borkler.LOGGER.info("Current connections are: " + this.fluidConnections.toString());
LazyOptional<IFluidHandler> cap = null;
TileEntity te = null;
// Trigger warning: the following section may require subsequent use of
Expand Down Expand Up @@ -595,7 +651,7 @@ public boolean add(LazyOptional<IFluidHandler> element) {
}
gazcreations.borkler.Borkler.LOGGER
.info("Borkler @" + world + " ," + pos + "has updated its connections: " + consumers.toString());
this.connections = consumers;
this.fluidConnections = consumers;
}

/**
Expand Down Expand Up @@ -797,8 +853,8 @@ public void tick() {
// to do so before anything else.
if (BorklerConfig.CONFIG.THIRSTY.get()) {
// will check its connections for a water supply
synchronized (connections) {
for (LazyOptional<IFluidHandler> supplier : connections) {
synchronized (fluidConnections) {
for (LazyOptional<IFluidHandler> supplier : fluidConnections) {
// no need to check if it's null, because of the design of the
// updateFluidConnections method
if (supplier.isPresent()) {
Expand Down Expand Up @@ -962,16 +1018,17 @@ public void read(BlockState state, CompoundNBT nbt) {
@Override
public void onLoad() {
super.onLoad();
if (world != null && !world.isRemote())
if (world != null && !world.isRemote()) {
addFutureServerTask(world, () -> updateFluidConnections(), true);

addFutureServerTask(world, () -> updateItemConnections(), true);
addFutureServerTask(world, () -> this.updateContainingBlockInfo(), true);
}
}

/**
* This function was copied from the IE code because running
* updateFluidConnections in onLoad would cause minecraft to hang indefinitely.
* Let's see if this fixes it.
* EDIT: it does. Genius.
* Let's see if this fixes it. EDIT: it does. Genius.
*
* @author BluSunrize of Immersive Engineering.
* @param world
Expand Down Expand Up @@ -1012,18 +1069,27 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @javax.annotation.Nu
if (fluidHandlerCapability == null) {
fluidHandlerCapability = LazyOptional.of(() -> this);
}
return fluidHandlerCapability.cast();
return (LazyOptional<T>) fluidHandlerCapability;
}
if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
if (itemHandlerCapability == null) {
itemHandlerCapability = LazyOptional.of(() -> this);
}
return (LazyOptional<T>) itemHandlerCapability;
}
return super.getCapability(cap, side);
}

/**
* Invalidates this entity's {@link BorklerTileEntity#fluidHandlerCapability}.
* Invalidates this entity's {@link BorklerTileEntity#fluidHandlerCapability}
* and {@link BorklerTileEntity#itemHandlerCapability}.
*/
@Override
public void invalidateCaps() {
if (fluidHandlerCapability != null)
fluidHandlerCapability.invalidate();
if (itemHandlerCapability != null)
itemHandlerCapability.invalidate();
super.invalidateCaps();
}
}

0 comments on commit 9d6baf3

Please sign in to comment.