Skip to content

Commit

Permalink
Fallback to the current side when getting item cap
Browse files Browse the repository at this point in the history
Fixes #1764
  • Loading branch information
SquidDev committed Mar 25, 2024
1 parent ad0f551 commit 9af1aa1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.platform.ForgeContainerTransfer;
import dan200.computercraft.shared.util.CapabilityUtil;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
Expand Down Expand Up @@ -73,7 +75,7 @@ public int pushItems(
var location = computer.getAvailablePeripheral(toName);
if (location == null) throw new LuaException("Target '" + toName + "' does not exist");

var to = extractHandler(location.getTarget());
var to = extractHandler(location);
if (to == null) throw new LuaException("Target '" + toName + "' is not an inventory");

// Validate slots
Expand All @@ -95,7 +97,7 @@ public int pullItems(
var location = computer.getAvailablePeripheral(fromName);
if (location == null) throw new LuaException("Source '" + fromName + "' does not exist");

var from = extractHandler(location.getTarget());
var from = extractHandler(location);
if (from == null) throw new LuaException("Source '" + fromName + "' is not an inventory");

// Validate slots
Expand All @@ -108,11 +110,14 @@ public int pullItems(
}

@Nullable
private static IItemHandler extractHandler(@Nullable Object object) {
private static IItemHandler extractHandler(IPeripheral peripheral) {
var object = peripheral.getTarget();
var direction = peripheral instanceof dan200.computercraft.shared.peripheral.generic.GenericPeripheral sided ? sided.side() : null;

if (object instanceof BlockEntity blockEntity && blockEntity.isRemoved()) return null;

if (object instanceof ICapabilityProvider provider) {
var cap = provider.getCapability(ForgeCapabilities.ITEM_HANDLER);
var cap = CapabilityUtil.getCapability(provider, ForgeCapabilities.ITEM_HANDLER, direction);
if (cap.isPresent()) return cap.orElseThrow(NullPointerException::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static <T> T unwrap(LazyOptional<T> p, InvalidateCallback invalidate) {
* @param <T> The type of the underlying capability.
* @return The extracted capability, if present.
*/
public static <T> LazyOptional<T> getCapability(ICapabilityProvider provider, Capability<T> capability, Direction side) {
public static <T> LazyOptional<T> getCapability(ICapabilityProvider provider, Capability<T> capability, @Nullable Direction side) {
var cap = provider.getCapability(capability);
return cap.isPresent() ? cap : provider.getCapability(capability, side);
return !cap.isPresent() && side != null ? provider.getCapability(capability, side) : cap;
}
}

0 comments on commit 9af1aa1

Please sign in to comment.