Skip to content

Commit

Permalink
Implement material payment
Browse files Browse the repository at this point in the history
  • Loading branch information
sarhatabaot committed Nov 21, 2024
1 parent 2a98d99 commit 9577647
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
38 changes: 26 additions & 12 deletions src/main/java/me/tychsen/enchantgui/economy/MaterialPayment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,18 +20,31 @@ public boolean withdraw(@NotNull Player player, int amount) {
if (!hasSufficientFunds(player, amount)) {
return false;
}
//todo
// final HashMap<Integer, ? extends ItemStack> 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<ItemStack> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, ? extends ItemStack> 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;
}

Expand Down

0 comments on commit 9577647

Please sign in to comment.