Fix SetSlotPacket

This commit is contained in:
themode 2020-12-15 05:39:28 +01:00
parent 0658d1659c
commit e1a886aa3a
4 changed files with 31 additions and 44 deletions

View File

@ -304,11 +304,13 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
if (!isViewer(player)) if (!isViewer(player))
return; return;
SetSlotPacket setSlotPacket = new SetSlotPacket(); final ItemStack currentCursorItem = cursorPlayersItem.get(player);
setSlotPacket.windowId = -1; final boolean similar = currentCursorItem != null && currentCursorItem.isSimilar(cursorItem);
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem; if (!similar) {
player.getPlayerConnection().sendPacket(setSlotPacket); final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
player.getPlayerConnection().sendPacket(setSlotPacket);
}
this.cursorPlayersItem.put(player, cursorItem); this.cursorPlayersItem.put(player, cursorItem);
} }

View File

@ -221,7 +221,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
* @param slot the slot to refresh * @param slot the slot to refresh
*/ */
public void refreshSlot(short slot) { public void refreshSlot(short slot) {
sendSlotRefresh((short) convertToPacketSlot(slot), getItemStack(slot)); final int packetSlot = convertToPacketSlot(slot);
sendSlotRefresh((short) packetSlot, getItemStack(slot));
} }
/** /**
@ -240,12 +241,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
* @param cursorItem the new cursor item * @param cursorItem the new cursor item
*/ */
public void setCursorItem(@NotNull ItemStack cursorItem) { public void setCursorItem(@NotNull ItemStack cursorItem) {
final boolean similar = this.cursorItem.isSimilar(cursorItem);
this.cursorItem = cursorItem; this.cursorItem = cursorItem;
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1; if (!similar) {
setSlotPacket.slot = -1; final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
setSlotPacket.itemStack = cursorItem; player.getPlayerConnection().sendPacket(setSlotPacket);
player.getPlayerConnection().sendPacket(setSlotPacket); }
} }
/** /**
@ -340,7 +342,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
*/ */
protected void sendSlotRefresh(short slot, ItemStack itemStack) { protected void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket(); SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = (byte) (MathUtils.isBetween(slot, 35, INVENTORY_SIZE) ? 0 : -2); setSlotPacket.windowId = 0;
setSlotPacket.slot = slot; setSlotPacket.slot = slot;
setSlotPacket.itemStack = itemStack; setSlotPacket.itemStack = itemStack;
player.getPlayerConnection().sendPacket(setSlotPacket); player.getPlayerConnection().sendPacket(setSlotPacket);

View File

@ -4,12 +4,9 @@ import net.minestom.server.entity.Player;
import net.minestom.server.event.inventory.InventoryCloseEvent; import net.minestom.server.event.inventory.InventoryCloseEvent;
import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryClickHandler; import net.minestom.server.inventory.InventoryClickHandler;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.play.ClientClickWindowPacket; import net.minestom.server.network.packet.client.play.ClientClickWindowPacket;
import net.minestom.server.network.packet.client.play.ClientCloseWindow; import net.minestom.server.network.packet.client.play.ClientCloseWindow;
import net.minestom.server.network.packet.client.play.ClientWindowConfirmationPacket; import net.minestom.server.network.packet.client.play.ClientWindowConfirmationPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowConfirmationPacket; import net.minestom.server.network.packet.server.play.WindowConfirmationPacket;
public class WindowListener { public class WindowListener {
@ -80,8 +77,6 @@ public class WindowListener {
break; break;
} }
refreshCursorItem(player, inventory);
WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket(); WindowConfirmationPacket windowConfirmationPacket = new WindowConfirmationPacket();
windowConfirmationPacket.windowId = windowId; windowConfirmationPacket.windowId = windowId;
windowConfirmationPacket.actionNumber = actionNumber; windowConfirmationPacket.actionNumber = actionNumber;
@ -90,33 +85,6 @@ public class WindowListener {
player.getPlayerConnection().sendPacket(windowConfirmationPacket); player.getPlayerConnection().sendPacket(windowConfirmationPacket);
} }
/**
* @param player the player to refresh the cursor item
* @param inventory the player open inventory, null if not any (could be player inventory)
*/
private static void refreshCursorItem(Player player, Inventory inventory) {
PlayerInventory playerInventory = player.getInventory();
ItemStack cursorItem;
if (inventory != null) {
cursorItem = inventory.getCursorItem(player);
} else {
cursorItem = playerInventory.getCursorItem();
}
// Error occurred while retrieving the cursor item, stop here
if (cursorItem == null) {
return;
}
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
player.getPlayerConnection().sendPacket(setSlotPacket);
}
public static void closeWindowListener(ClientCloseWindow packet, Player player) { public static void closeWindowListener(ClientCloseWindow packet, Player player) {
// if windowId == 0 then it is player's inventory, meaning that they hadn't been any open inventory packet // if windowId == 0 then it is player's inventory, meaning that they hadn't been any open inventory packet
InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player, player.getOpenInventory()); InventoryCloseEvent inventoryCloseEvent = new InventoryCloseEvent(player, player.getOpenInventory());

View File

@ -23,4 +23,19 @@ public class SetSlotPacket implements ServerPacket {
public int getId() { public int getId() {
return ServerPacketIdentifier.SET_SLOT; return ServerPacketIdentifier.SET_SLOT;
} }
/**
* Returns a {@link SetSlotPacket} used to change a player cursor item.
*
* @param cursorItem the cursor item
* @return a set slot packet to change a player cursor item
*/
@NotNull
public static SetSlotPacket createCursorPacket(@NotNull ItemStack cursorItem) {
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
return setSlotPacket;
}
} }