Potentially fix cursor ghost item when opening a new inventory

This commit is contained in:
TheMode 2021-04-24 19:14:19 +02:00
parent b662e331f0
commit 4eaa15477b
2 changed files with 11 additions and 10 deletions

View File

@ -1928,9 +1928,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this); InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this);
callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> { callCancellableEvent(InventoryOpenEvent.class, inventoryOpenEvent, () -> {
Inventory openInventory = getOpenInventory();
if (getOpenInventory() != null) { if (openInventory != null) {
getOpenInventory().removeViewer(this); openInventory.removeViewer(this);
} }
Inventory newInventory = inventoryOpenEvent.getInventory(); Inventory newInventory = inventoryOpenEvent.getInventory();

View File

@ -174,7 +174,7 @@ public class Inventory extends AbstractInventory implements Viewable {
@Override @Override
public boolean removeViewer(@NotNull Player player) { public boolean removeViewer(@NotNull Player player) {
final boolean result = this.viewers.remove(player); final boolean result = this.viewers.remove(player);
this.cursorPlayersItem.remove(player); setCursorItem(player, ItemStack.AIR);
this.clickProcessor.clearCache(player); this.clickProcessor.clearCache(player);
return result; return result;
} }
@ -198,18 +198,19 @@ public class Inventory extends AbstractInventory implements Viewable {
* @param cursorItem the new player cursor item * @param cursorItem the new player cursor item
*/ */
public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) { public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) {
if (!isViewer(player)) final ItemStack currentCursorItem = cursorPlayersItem.getOrDefault(player, ItemStack.AIR);
return; final boolean similar = currentCursorItem.isSimilar(cursorItem);
final ItemStack currentCursorItem = cursorPlayersItem.get(player);
final boolean similar = currentCursorItem != null && currentCursorItem.isSimilar(cursorItem);
if (!similar) { if (!similar) {
final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem); final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
player.getPlayerConnection().sendPacket(setSlotPacket); player.getPlayerConnection().sendPacket(setSlotPacket);
} }
this.cursorPlayersItem.put(player, cursorItem); if (!cursorItem.isAir()) {
this.cursorPlayersItem.put(player, cursorItem);
} else {
this.cursorPlayersItem.remove(player);
}
} }
/** /**