Skip to content

Commit

Permalink
Fix side interactions (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNijjar committed Jan 16, 2024
1 parent fb0d0ca commit 64608ef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- Fixed REI crash (#421)
- Fixed cheese not being edible.
- Fixed etrionic blast furnace layer not being cutout.
- Fixed etrionic blast furnace skipping cooking progress when items are taken out and put back in.
- Fixed etrionic blast furnace skipping cooking progress when items are taken out and put back in.
- Fixed side interactions putting items in wrong slot and not working for blocks like furnaces (#423)
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@

public class ItemUtils {

/**
* Pushes items from one container to another.
*
* @param from The container to pull items from.
* @param to The container to push items to.
* @param fromSlots The slots to pull items from.
* @param direction The direction to push items to.
*/
public static void push(Container from, Container to, int[] fromSlots, Direction direction) {
if (inventoryFull(to)) return;
for (int slot : fromSlots) {
ItemStack stack = from.getItem(slot);
if (stack.isEmpty()) continue;
if (!addItem(from, to, stack.copyWithCount(1), IntStream.range(0, to.getContainerSize()).toArray(), direction)) {
if (!addItem(to, stack.copyWithCount(1), IntStream.range(0, to.getContainerSize()).toArray(), direction)) {
continue;
}
from.removeItem(slot, 1);
Expand All @@ -23,18 +31,32 @@ public static void push(Container from, Container to, int[] fromSlots, Direction
}
}

/**
* Pulls items from one container to another.
*
* @param from The container to pull items from.
* @param to The container to push items to.
* @param toSlots The slots to push items to.
* @param direction The direction to pull items from.
*/
public static void pull(Container from, Container to, int[] toSlots, Direction direction) {
if (inventoryFull(to)) return;
for (int i = 0; i < from.getContainerSize(); i++) {
ItemStack stack = from.getItem(i);
if (stack.isEmpty()) continue;
if (!addItem(from, to, stack.copyWithCount(1), toSlots, direction)) continue;
if (!addItem(to, stack.copyWithCount(1), toSlots, direction)) continue;
from.removeItem(i, 1);
to.setChanged();
return;
}
}

/**
* Checks if a container is full.
*
* @param container The container to check.
* @return True if the container is full, false otherwise.
*/
public static boolean inventoryFull(Container container) {
for (int i = 0; i < container.getContainerSize(); i++) {
ItemStack stack = container.getItem(i);
Expand All @@ -44,40 +66,40 @@ public static boolean inventoryFull(Container container) {
return true;
}

private static boolean addItem(Container from, Container to, ItemStack stack, int[] toSlots, Direction direction) {
/**
* Adds an item to a container.
*
* @param to The container to push items to.
* @param stack The stack to add.
* @param toSlots The slots to push items to.
* @param direction The direction to push items to.
* @return True if the item was added, false otherwise.
*/
private static boolean addItem(Container to, ItemStack stack, int[] toSlots, Direction direction) {
for (int slot : toSlots) {
if (from instanceof WorldlyContainer worldlyContainer) {
if (!worldlyContainer.canTakeItemThroughFace(slot, stack, direction)) continue;
}
if (to instanceof WorldlyContainer worldlyContainer) {
int[] slots = worldlyContainer.getSlotsForFace(direction);
if (IntStream.of(slots).noneMatch(i -> i == slot)) continue;
if (!worldlyContainer.canPlaceItemThroughFace(slot, stack, direction)) continue;
}
ItemStack toStack = to.getItem(slot);
var toStack = to.getItem(slot);
if (toStack.isEmpty()) {
to.setItem(slot, stack);
return true;
} else if (toStack.getCount() < toStack.getMaxStackSize() && ItemStack.isSameItemSameTags(toStack, stack)) {
} else if (canAddItem(toStack, stack)) {
toStack.grow(1);
return true;
}
}
return false;
}

public static boolean canAddItem(ItemStack input, ItemStack output) {
return input.isEmpty() || (ItemStack.isSameItemSameTags(input, output) && output.getCount() + input.getCount() <= input.getMaxStackSize());
}

public static boolean canAddItem(Container container, ItemStack output, int... slots) {
for (int slot : slots) {
ItemStack input = container.getItem(slot);
if (canAddItem(input, output)) return true;
}
return false;
}

/**
* Adds an item to a container.
*
* @param container The container to add the item to.
* @param output The output item.
* @param slots The potential slots to add the item to. The item will be added to the first available slot.
*/
public static void addItem(Container container, ItemStack output, int... slots) {
for (int slot : slots) {
ItemStack input = container.getItem(slot);
Expand All @@ -90,4 +112,34 @@ public static void addItem(Container container, ItemStack output, int... slots)
}
}
}

/**
* Checks if an item can be added to another item.
* An item can be added if the output item is empty or the stacks can be merged.
*
* @param input The input item.
* @param output The output item.
* @return True if the item can be added, false otherwise.
*/
public static boolean canAddItem(ItemStack input, ItemStack output) {
return input.isEmpty()
|| (ItemStack.isSameItemSameTags(input, output)
&& output.getCount() + input.getCount() <= input.getMaxStackSize());
}

/**
* Checks if an item can be added to a container.
*
* @param container The container to check.
* @param output The output item.
* @param slots The potential slots to add the item to.
* @return True if the item can be added, false otherwise.
*/
public static boolean canAddItem(Container container, ItemStack output, int... slots) {
for (int slot : slots) {
ItemStack input = container.getItem(slot);
if (canAddItem(input, output)) return true;
}
return false;
}
}

0 comments on commit 64608ef

Please sign in to comment.