Java Code Examples for org.spongepowered.api.item.inventory.Inventory#peek()

The following examples show how to use org.spongepowered.api.item.inventory.Inventory#peek() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FactionChestImpl.java    From EagleFactions with MIT License 6 votes vote down vote up
private List<SlotItem> toSlotItems(final Inventory inventory)
{
    final List<FactionChest.SlotItem> slotItemList = new ArrayList<>();
    final Iterable<Inventory> slots = inventory.slots();
    int column = 1;
    int row = 1;
    for(Inventory slot : slots)
    {
        Optional<ItemStack> optionalItemStack = slot.peek();
        if(optionalItemStack.isPresent())
        {
            slotItemList.add(new FactionChestImpl.SlotItemImpl(column, row, optionalItemStack.get()));
        }

        column++;
        if(column > 9)
        {
            row++;
            column = 1;
        }

        if(row > 3)
            break;
    }
    return slotItemList;
}
 
Example 2
Source File: InventorySerializer.java    From EagleFactions with MIT License 6 votes vote down vote up
public static List<DataView> serializeInventory(Inventory inventory) {
    DataContainer container;
    List<DataView> slots = new LinkedList<>();

    int i = 0;
    Optional<ItemStack> stack;

    for (Inventory inv : inventory.slots()) {
        stack = inv.peek();

        if (stack.isPresent()) {
            container = DataContainer.createNew(SafetyMode.ALL_DATA_CLONED);

            container.set(SLOT, i);
            container.set(STACK, serializeItemStack(stack.get()));

            slots.add(container);
        }

        i++;
    }

    return slots;
}
 
Example 3
Source File: VersionHelper8.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Override
public long getInvValue(Iterable<Inventory> inv) {
    long value = 0;
    for (Inventory item : inv) {
        if (item.peek().isEmpty()) {
            continue;
        }
        ItemStack stack = item.peek();
        value += ((RedProtect.get().config.ecoRoot().items.values.get(stack.getType().getName()) * stack.getQuantity()));
        if (stack.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
            for (Enchantment enchant : stack.get(Keys.ITEM_ENCHANTMENTS).get()) {
                value += ((RedProtect.get().config.ecoRoot().enchantments.values.get(enchant.getType().getName()) * enchant.getLevel()));
            }
        }
    }
    return value;
}
 
Example 4
Source File: ItemListener.java    From UltimateCore with MIT License 6 votes vote down vote up
@Listener
public void onChange(ChangeInventoryEvent event, @First Player p) {
    ModuleConfig config = Modules.BLACKLIST.get().getConfig().get();
    CommentedConfigurationNode hnode = config.get();
    try {
        for (Inventory s : event.getTargetInventory().slots()) {
            ItemStack item = s.peek();
            CommentedConfigurationNode node = hnode.getNode("items", item.getType().getKey());
            if (!node.isVirtual()) {
                if (node.getNode("deny-possession").getBoolean()) {
                    s.poll();
                }
            }
        }
    } catch (Exception ignore) {
    }
}