use of org.spongepowered.api.item.inventory.entity.PlayerInventory in project Skree by Skelril.
the class MarketImplUtil method giveItems.
public static Clause<Boolean, List<Clause<ItemStack, Integer>>> giveItems(Player player, Collection<ItemStack> stacks, Cause cause) {
List<Clause<ItemStack, Integer>> transactions = new ArrayList<>(stacks.size());
List<ItemStackSnapshot> itemBuffer = new ArrayList<>();
itemBuffer.addAll(stacks.stream().map(ItemStack::createSnapshot).collect(Collectors.toList()));
PlayerInventory playerInventory = player.getInventory().query(PlayerInventory.class);
List<Inventory> inventories = new ArrayList<>();
inventories.add(playerInventory.getHotbar());
inventories.add(playerInventory.getMain());
// Loop through replacing empty space with the requested items
for (Inventory inventory : inventories) {
List<ItemStackSnapshot> newBuffer = new ArrayList<>();
for (ItemStackSnapshot snapshot : itemBuffer) {
ItemStack stack = snapshot.createStack();
InventoryTransactionResult result = inventory.offer(stack);
newBuffer.addAll(result.getRejectedItems());
transactions.add(new Clause<>(stack, stack.getQuantity()));
}
itemBuffer = newBuffer;
}
// Drop remaining items
new ItemDropper(player.getLocation()).dropStackSnapshots(itemBuffer, SpawnTypes.PLUGIN);
return new Clause<>(true, transactions);
}
Aggregations