Unable to obtain SlotPos on ClickInventory Event

I’m trying to obtain the SlotPos from a ClickInventoryEvent but I have been unable to obtain it.

I’m using Sponge 7.2.0

I first create the inventory when a specific item is clicked and fill it with glass panes:
(link to full code at the bottom)

@Listener
public void onRightClick(InteractItemEvent event, @First Player player) {
    // Obtain item
    Optional<ItemStack> item = player.getItemInHand(HandTypes.MAIN_HAND);
    if (!item.isPresent()) {
        return;
    }
    String id = item.get().getType().getId();
    if (!id.equals("projectred-expansion:plan")) {
        return;
    }

    // Create an inventory
    Inventory craftingMenu = Inventory.builder()
        .of(InventoryArchetypes.DOUBLE_CHEST)
        .property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Configure Recipe Plan")))
        .withCarrier(player)
        .listener(ClickInventoryEvent.class, this::callback)
        .build(this);
    player.openInventory(craftingMenu);

    // Fill chest with black glass panes
    Iterable<Slot> slots = craftingMenu.slots();
    for (Slot slot : slots) {
        ItemStack stackToInsert = ItemStack.of(ItemTypes.STAINED_GLASS_PANE, 1);
        stackToInsert.offer(Keys.DYE_COLOR, DyeColors.BLACK);
        stackToInsert.offer(Keys.DISPLAY_NAME, Text.of(""));
        slot.set(stackToInsert);
    }

When the player interacts with items in the inventory I need to know what position the item is in. This is that part that I can’t seem to get working. Relevant code:

public void callback(ClickInventoryEvent event) {
    Sponge.getServer().getBroadcastChannel().send(Text.of("======[ Slot values ]======"));
    Optional<Slot> slot = event.getSlot();
    String msgString;
    if (!slot.isPresent()) {
        msgString = "No slot value";
    } else {
        Optional<SlotPos> pos = slot.get().getInventoryProperty(SlotPos.class);
        if (!pos.isPresent()) {
            msgString = "[event.getSlot()] No position value was given";
        } else {
            msgString = "[event.getSlot()] Slot position (x, y): (" + String.valueOf(pos.get().getX()) + ", " + String.valueOf(pos.get().getY()) + ")";
        }
    }        
    Sponge.getServer().getBroadcastChannel().send(Text.of(msgString));
}

The Optional<SlotPos> never seems to have a value.

How would I go about getting the slot coordinates? Thanks for the help :slight_smile:

Link to full code: MinimalExample.java · GitHub

So when it comes to inventory properties, by default they will take a single parent up. This may not be the inventory’s root.

SlotPos is the x and y coord of the slot, therefore the parent needs to be a gridInventory which im guessing isnt the case here.

Personally what i would do is assume that the event#getInventory() is the root. And then on slot, get the inventory position of the slot though the root inventory using this method

https://jd.spongepowered.org/spongeapi/7.4.0/org/spongepowered/api/item/inventory/Inventory.html#getInventoryProperty-org.spongepowered.api.item.inventory.Inventory-java.lang.Class-

Hope that helps

Edit:

I would stick .root() on the end of the getInventory to ensure it is the root inventory

Thank you, I’ll have a go of it this weekend :slight_smile: