Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Intybyte committed Sep 8, 2024
1 parent f1363ce commit 203339b
Showing 1 changed file with 57 additions and 39 deletions.
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

0 comments on commit 203339b

Please sign in to comment.