diff --git a/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java b/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java index 1d913a9..e94b829 100644 --- a/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java +++ b/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.stream.Stream; public class MaterialPayment implements PaymentStrategy { private final Material material; @@ -19,18 +20,31 @@ public boolean withdraw(@NotNull Player player, int amount) { if (!hasSufficientFunds(player, amount)) { return false; } - //todo - // final HashMap items = player.getInventory().all(material).entrySet() - // .stream() - // .sorted(Comparator.comparing(e -> e.getValue().getAmount())); - // - // Arrays.stream(player.getInventory().getContents()) - // .filter(Objects::nonNull) - // .filter(itemStack -> itemStack.getType() == material) - // .sorted(Comparator.comparing(ItemStack::getAmount)) - // .map(, ); - // map the slots of the itemstacks, remove from the smallest ones first - //todo do stuff + + // Collect matching stacks from the inventory, sorted by stack size (ascending) + List matchingStacks = Stream.of(player.getInventory().getContents()) + .filter(stack -> stack != null && stack.isSimilar(new ItemStack(material))) + .sorted(Comparator.comparingInt(ItemStack::getAmount)) + .toList(); + + int remaining = amount; + + for (ItemStack stack : matchingStacks) { + int stackAmount = stack.getAmount(); + + if (remaining <= stackAmount) { + // Deduct the remaining amount and break + stack.setAmount(stackAmount - remaining); + break; + } else { + // Remove the entire stack and reduce the remaining amount + remaining -= stackAmount; + stack.setAmount(0); + } + } + + // Update inventory to reflect changes + player.updateInventory(); return true; } diff --git a/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java~ b/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java~ index 69aa3c8..1de1ef1 100644 --- a/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java~ +++ b/src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java~ @@ -16,19 +16,20 @@ public class MaterialPayment implements PaymentStrategy { @Override public boolean withdraw(@NotNull Player player, int amount) { - if(!hasSufficientFunds(player,amount)) return false; + if (!hasSufficientFunds(player, amount)) { + return false; + } + final HashMap items = player.getInventory().all(material).entrySet() .stream() - .sorted(Comparator.comparing(e -> e.getValue().getAmount())) - ; + .sorted(Comparator.comparing(e -> e.getValue().getAmount())); Arrays.stream(player.getInventory().getContents()) .filter(Objects::nonNull) .filter(itemStack -> itemStack.getType() == material) .sorted(Comparator.comparing(ItemStack::getAmount)) - .map(,); - // map the slots of the itemstacks, remove from the smallest ones first - //todo do stuff + .map(, ); + // map the slots of the itemstacks, remove from the smallest ones first return true; }