Get Item with the slot in Inventory


#1

Hello ! I search how can I get the Item with the slot in Inventory. I want to do the same that Bukkit in Sponge

public void saveInventory(){

    for(int slot = 0; slot < 36; slot++){
        ItemStack item = player.getInventory().getItem(slot) //That here 
        if(item != null){
            items[slot] = item;
        }
    }
    
    
    items[36] = ItemStack.of((ItemType) items[36], 1);
    items[37] = ItemStack.of((ItemType) items[37], 1);
    items[38] = ItemStack.of((ItemType) items[38], 1);
    items[39] = ItemStack.of((ItemType) items[39], 1);

    player.getInventory().clear();
    player.getInventory().offer(null);
}

Thank’s !


#2

Use slotIndex inventory propery to get the number of the slot pos.

This may help. I know there is a way of specifying which slot you want using getProperty() but can not for the life of me remeber it.


#3

I don’t find how can I use the #getProperty() to get the slot Item.
Should I do this ?

        for (Inventory i : player.getInventory().slots()) {
    	//get the slot item
    	ItemStack item = ItemStack.of(i.getProperty(property, key), 1);
        if (i.size() != 0) {
        	items[slot] = item;
            break;
        }
    }

Thank you


#4
Map<Integer, Slot> map = new HashMap<>();
for (Slot slot : player.getInventory().slots()) {
    if (slot.size() == 0) {
        break;
    }
    Optional<SlotIndex> opIndex = slot.getInventoryProperty(SlotIndex.class);
    if(!opIndex.isPresent()){
       map.put(opIndex.getValue(), i);
    }
}

ItemStack stack = map.get(35).peek().get();

That is how I would do it, like i said there is a way to use inventory property from the main inventory but can not remember it. Please note that map.get(35) if slot 35 wasnt set in then it would return null. Also note i wrote this from memory and not in a IDE such as Eclipse, so some expect typos and for it not to work without minor changes.