Skip to content

Commit cd1a33a

Browse files
committed
feat: add selection direction
1 parent b894827 commit cd1a33a

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

paper-api/src/main/java/io/papermc/paper/event/inventory/PlayerBundleItemSelectEvent.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ public final class PlayerBundleItemSelectEvent extends InventoryEvent {
2929

3030
private final int previousIndex;
3131
private final int selectedIndex;
32+
private final int direction;
3233

3334
@ApiStatus.Internal
34-
public PlayerBundleItemSelectEvent(final InventoryView view, final ItemStack bundle, final int rawSlot, final ItemStack previousItem, final ItemStack selectedItem, final int previousIndex, final int selectedIndex) {
35+
public PlayerBundleItemSelectEvent(final InventoryView view, final ItemStack bundle, final int rawSlot, final ItemStack previousItem, final ItemStack selectedItem, final int previousIndex, final int selectedIndex, final int direction) {
3536
super(view);
3637

3738
this.bundle = bundle;
@@ -43,6 +44,7 @@ public PlayerBundleItemSelectEvent(final InventoryView view, final ItemStack bun
4344

4445
this.previousIndex = previousIndex;
4546
this.selectedIndex = selectedIndex;
47+
this.direction = direction;
4648
}
4749

4850
/**
@@ -119,6 +121,15 @@ public int getSelectedIndex() {
119121
return this.selectedIndex;
120122
}
121123

124+
/**
125+
* Gets the direction from the previous selection to the new one.
126+
*
127+
* @return +1 if forwards, -1 if backwards
128+
*/
129+
public int getDirection() {
130+
return this.direction;
131+
}
132+
122133
@Override
123134
public HandlerList getHandlers() {
124135
return HANDLER_LIST;

paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ public static void callPlayerBundleItemSelectEvent(final net.minecraft.world.ent
16131613
return;
16141614
}
16151615

1616-
if (slotId < 0 || slotId >= player.containerMenu.slots.size() || selectedIndex == net.minecraft.world.item.component.BundleContents.NO_SELECTED_ITEM_INDEX) {
1616+
if (slotId < 0 || slotId >= player.containerMenu.slots.size() || selectedIndex <= net.minecraft.world.item.component.BundleContents.NO_SELECTED_ITEM_INDEX) {
16171617
return;
16181618
}
16191619

@@ -1623,18 +1623,36 @@ public static void callPlayerBundleItemSelectEvent(final net.minecraft.world.ent
16231623
}
16241624

16251625
final net.minecraft.world.item.component.BundleContents contents = item.get(net.minecraft.core.component.DataComponents.BUNDLE_CONTENTS);
1626-
if (contents == null || selectedIndex < 0 || selectedIndex >= contents.size()) {
1626+
if (contents == null) {
16271627
return;
16281628
}
16291629

1630+
final int previousIndex = contents.getSelectedItemIndex();
1631+
if (selectedIndex >= contents.size() || selectedIndex == previousIndex) {
1632+
return;
1633+
}
1634+
1635+
int direction;
1636+
if (previousIndex == -1) {
1637+
direction = selectedIndex == 0 ? 1 : -1;
1638+
} else {
1639+
int diff = selectedIndex - previousIndex;
1640+
direction = diff > 0 ? 1 : -1;
1641+
1642+
if (Math.abs(diff) == contents.size() - 1) {
1643+
direction = -direction;
1644+
}
1645+
}
1646+
16301647
new io.papermc.paper.event.inventory.PlayerBundleItemSelectEvent(
16311648
player.containerMenu.getBukkitView(),
16321649
item.asBukkitMirror(),
16331650
slotId,
16341651
contents.getSelectedItem() != null ? contents.getSelectedItem().create().asBukkitMirror() : org.bukkit.inventory.ItemStack.empty(),
16351652
contents.items().get(selectedIndex).create().asBukkitMirror(),
1636-
contents.getSelectedItemIndex(),
1637-
selectedIndex
1653+
previousIndex,
1654+
selectedIndex,
1655+
direction
16381656
).callEvent();
16391657
}
16401658

0 commit comments

Comments
 (0)