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
Link to full code: MinimalExample.java · GitHub