Search in sources :

Example 1 with PlayerInventory

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);
}
Also used : ItemDropper(com.skelril.nitro.item.ItemDropper) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Clause(com.skelril.nitro.Clause) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Aggregations

Clause (com.skelril.nitro.Clause)1 ItemDropper (com.skelril.nitro.item.ItemDropper)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 PlayerInventory (org.spongepowered.api.item.inventory.entity.PlayerInventory)1 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)1