Skip to content

Commit 0e581fb

Browse files
Deduplicate inventory drag slots
1 parent c4f2b31 commit 0e581fb

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/inventory/ReferencingInventory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.BiFunction;
2323
import java.util.function.Function;
2424

25+
// TODO: equals+hashCode for normal referencing inventories
2526
/**
2627
* A {@link xyz.xenondevs.invui.inventory.Inventory} which is backed by a bukkit {@link Inventory}.
2728
* <p>
@@ -271,6 +272,17 @@ protected void setDirectBackingItem(int slot, @Nullable ItemStack itemStack) {
271272
public PlayerInventory getReferencedInventory() {
272273
return (PlayerInventory) super.getReferencedInventory();
273274
}
275+
276+
@Override
277+
public int hashCode() {
278+
return getReferencedInventory().hashCode();
279+
}
280+
281+
@Override
282+
public boolean equals(Object obj) {
283+
return obj instanceof ReferencingInventory.PlayerStorageContents other &&
284+
getReferencedInventory().equals(other.getReferencedInventory());
285+
}
274286
}
275287

276288
}

invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public void handleDrag(IntSet slots, ClickType mode) {
295295
if (mode == ClickType.MIDDLE && viewer.getGameMode() != GameMode.CREATIVE)
296296
return;
297297

298-
List<InventorySlot> invSlots = getActiveInventorySlots(slots);
298+
SequencedSet<InventorySlot> invSlots = getActiveInventorySlots(slots);
299299
if (invSlots.isEmpty())
300300
return;
301301

@@ -349,8 +349,8 @@ public void handleDrag(IntSet slots, ClickType mode) {
349349
* @param slots The window slots
350350
* @return A list of all active inventory slots referenced by the window slots
351351
*/
352-
private List<InventorySlot> getActiveInventorySlots(IntSet slots) {
353-
List<InventorySlot> invSlots = new ArrayList<>();
352+
private SequencedSet<InventorySlot> getActiveInventorySlots(IntSet slots) {
353+
SequencedSet<InventorySlot> invSlots = new LinkedHashSet<>();
354354

355355
slotLoop:
356356
for (int slot : slots) {
@@ -375,7 +375,7 @@ private List<InventorySlot> getActiveInventorySlots(IntSet slots) {
375375
return invSlots;
376376
}
377377

378-
private boolean distributeItems(UpdateReason updateReason, List<InventorySlot> slots) {
378+
private boolean distributeItems(UpdateReason updateReason, Set<InventorySlot> slots) {
379379
ItemStack cursor = viewer.getItemOnCursor();
380380
int amount = cursor.getAmount();
381381
int itemsPerSlot = amount / slots.size();

0 commit comments

Comments
 (0)