Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Cognitive Complexity of Cargo Utils #4235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.github.bakedlibs.dough.collections.Pair;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
Expand Down Expand Up @@ -289,22 +290,13 @@ static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> in
continue;
}

if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
if (currentAmount < maxStackSize) {
int amount = currentAmount + stack.getAmount();

itemInSlot.setAmount(Math.min(amount, maxStackSize));
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
} else {
stack = null;
}

menu.replaceExistingItem(slot, itemInSlot);
return stack;
} else if (smartFill) {
return stack;
}
if (!SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
continue;
}

var result = handleStack(stack, smartFill, itemInSlot, menu, slot);
if (Boolean.TRUE.equals(result.getSecondValue())) {
return result.getFirstValue();
}
}

Expand Down Expand Up @@ -333,37 +325,63 @@ private static ItemStack insertIntoVanillaInventory(@Nonnull ItemStack stack, @N
if (itemInSlot == null) {
inv.setItem(slot, stack);
return null;
} else {
int currentAmount = itemInSlot.getAmount();
int maxStackSize = itemInSlot.getType().getMaxStackSize();
}

if (!smartFill && currentAmount == maxStackSize) {
// Skip full stacks - Performance optimization for non-smartfill nodes
continue;
}
int currentAmount = itemInSlot.getAmount();
int maxStackSize = itemInSlot.getType().getMaxStackSize();

if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
if (currentAmount < maxStackSize) {
int amount = currentAmount + stack.getAmount();

if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
itemInSlot.setAmount(maxStackSize);
return stack;
} else {
itemInSlot.setAmount(Math.min(amount, maxStackSize));
return null;
}
} else if (smartFill) {
return stack;
}
}
if (!smartFill && currentAmount == maxStackSize) {
// Skip full stacks - Performance optimization for non-smartfill nodes
continue;
}

if (!SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
continue;
}

var result = handleStack(stack, smartFill, itemInSlot, null, 0);
if (Boolean.TRUE.equals(result.getSecondValue())) {
return result.getFirstValue();
}
}

return stack;
}

/**
*
* @param stack ItemStack to insert
* @param smartFill if smart fill enabled
* @param itemInSlot ItemStack in slot
* @param menu If not null replaces ihe item in the slot of specified in the parameter slot with itemInSlot
* @param slot Slot to replace if menu is not null
* @return Pair of itemstack and boolean, the boolean tells you if the operation was successful or not
*/
private static Pair<ItemStack, Boolean> handleStack(ItemStack stack, boolean smartFill, ItemStack itemInSlot, @Nullable DirtyChestMenu menu, int slot) {
int currentAmount = itemInSlot.getAmount();
int maxStackSize = itemInSlot.getType().getMaxStackSize();

if (currentAmount < maxStackSize) {
int amount = currentAmount + stack.getAmount();

itemInSlot.setAmount(Math.min(amount, maxStackSize));
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
} else {
stack = null;
}

if (menu != null)
menu.replaceExistingItem(slot, itemInSlot);

return new Pair<>(stack, true);
} else if (smartFill) {
return new Pair<>(stack, true);
}

return new Pair<>(null, false);
}

@Nullable
static DirtyChestMenu getChestMenu(@Nonnull Block block) {
if (BlockStorage.hasInventory(block)) {
Expand Down
Loading