fix: inventory rebase fixes

This commit is contained in:
mworzala 2024-06-07 21:32:20 -04:00
parent 05546d8806
commit ae7a80c59f
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
2 changed files with 22 additions and 26 deletions

View File

@ -184,7 +184,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
protected PlayerInventory inventory; protected PlayerInventory inventory;
private Inventory openInventory; private Inventory openInventory;
// Used internally to allow the closing of inventory within the inventory listener // Used internally to allow the closing of inventory within the inventory listener
private boolean skipClosePacket; private boolean didCloseInventory;
private byte heldSlot; private byte heldSlot;
@ -1720,19 +1720,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
return openInventory; return openInventory;
} }
private void tryCloseInventory(boolean skipClosePacket) {
var closedInventory = getOpenInventory();
if (closedInventory == null) return;
this.skipClosePacket = skipClosePacket;
if (closedInventory.removeViewer(this)) {
if (closedInventory == getOpenInventory()) {
this.openInventory = null;
}
}
}
/** /**
* Opens the specified Inventory, close the previous inventory if existing. * Opens the specified Inventory, close the previous inventory if existing.
* *
@ -1743,12 +1730,21 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this); InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this);
EventDispatcher.callCancellable(inventoryOpenEvent, () -> { EventDispatcher.callCancellable(inventoryOpenEvent, () -> {
tryCloseInventory(true); Inventory openInventory = getOpenInventory();
if (openInventory != null) {
openInventory.removeViewer(this);
}
Inventory newInventory = inventoryOpenEvent.getInventory(); Inventory newInventory = inventoryOpenEvent.getInventory();
if (newInventory.addViewer(this)) { if (newInventory == null) {
this.openInventory = newInventory; // just close the inventory
return;
} }
sendPacket(new OpenWindowPacket(newInventory.getWindowId(),
newInventory.getInventoryType().getWindowType(), newInventory.getTitle()));
newInventory.addViewer(this);
this.openInventory = newInventory;
}); });
return !inventoryOpenEvent.isCancelled(); return !inventoryOpenEvent.isCancelled();
} }
@ -1792,15 +1788,15 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
} }
if (!fromClient) sendPacket(closeWindowPacket); if (!fromClient) sendPacket(closeWindowPacket);
inventory.update(); inventory.update();
this.skipClosePacket = true; this.didCloseInventory = true;
} }
} }
/** /**
* Used internally to determine when sending the close inventory packet should be skipped. * Used internally to determine when sending the close inventory packet should be skipped.
*/ */
public boolean skipClosePacket() { public boolean didCloseInventory() {
return skipClosePacket; return didCloseInventory;
} }
/** /**
@ -1809,11 +1805,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* <p> * <p>
* Shouldn't be used externally without proper understanding of its consequence. * Shouldn't be used externally without proper understanding of its consequence.
* *
* @param skipClosePacket the new skipClosePacket field * @param didCloseInventory the new didCloseInventory field
*/ */
@ApiStatus.Internal @ApiStatus.Internal
public void UNSAFE_changeSkipClosePacket(boolean skipClosePacket) { public void UNSAFE_changeDidCloseInventory(boolean didCloseInventory) {
this.skipClosePacket = skipClosePacket; this.didCloseInventory = didCloseInventory;
} }
public int getNextTeleportId() { public int getNextTeleportId() {

View File

@ -416,7 +416,7 @@ public final class InventoryClickProcessor {
// Reset the didCloseInventory field // Reset the didCloseInventory field
// Wait for inventory conditions + events to possibly close the inventory // Wait for inventory conditions + events to possibly close the inventory
player.UNSAFE_changeSkipClosePacket(false); player.UNSAFE_changeDidCloseInventory(false);
// InventoryPreClickEvent // InventoryPreClickEvent
{ {
InventoryPreClickEvent inventoryPreClickEvent = new InventoryPreClickEvent(eventInventory, player, slot, clickType, InventoryPreClickEvent inventoryPreClickEvent = new InventoryPreClickEvent(eventInventory, player, slot, clickType,
@ -444,9 +444,9 @@ public final class InventoryClickProcessor {
} }
} }
// Cancel the click if the inventory has been closed by Player#closeInventory within an inventory listener // Cancel the click if the inventory has been closed by Player#closeInventory within an inventory listener
if (player.skipClosePacket()) { if (player.didCloseInventory()) {
clickResult.setCancel(true); clickResult.setCancel(true);
player.UNSAFE_changeSkipClosePacket(false); player.UNSAFE_changeDidCloseInventory(false);
} }
} }
} }