From ff9ca60a580ee2d692a951dbb6133de90b7d6861 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 22 Jul 2021 09:54:34 +0200 Subject: [PATCH] Add some inline packet constructors --- .../net/minestom/server/entity/Player.java | 21 +++---------- .../minestom/server/inventory/Inventory.java | 30 +++++-------------- .../server/inventory/PlayerInventory.java | 13 ++------ .../packet/server/play/OpenBookPacket.java | 10 +++++-- .../packet/server/play/SetSlotPacket.java | 18 ++++++----- .../packet/server/play/WindowItemsPacket.java | 13 +++++--- .../server/play/WindowPropertyPacket.java | 13 +++++--- 7 files changed, 49 insertions(+), 69 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index eec3993a7..d86f5cc9e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -926,28 +926,15 @@ public class Player extends LivingEntity implements CommandSender, Localizable, @Override public void openBook(@NotNull Book book) { - ItemStack writtenBook = ItemStack.builder(Material.WRITTEN_BOOK) + final ItemStack writtenBook = ItemStack.builder(Material.WRITTEN_BOOK) .meta(WrittenBookMeta.fromAdventure(book, this)) .build(); - // Set book in offhand - SetSlotPacket setBookPacket = new SetSlotPacket(); - setBookPacket.windowId = 0; - setBookPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT; - setBookPacket.itemStack = writtenBook; - playerConnection.sendPacket(setBookPacket); - + playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, writtenBook)); // Open the book - OpenBookPacket openBookPacket = new OpenBookPacket(); - openBookPacket.hand = Hand.OFF; - playerConnection.sendPacket(openBookPacket); - + playerConnection.sendPacket(new OpenBookPacket(Hand.OFF)); // Restore the item in offhand - SetSlotPacket restoreItemPacket = new SetSlotPacket(); - restoreItemPacket.windowId = 0; - restoreItemPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT; - restoreItemPacket.itemStack = getItemInOffHand(); - playerConnection.sendPacket(restoreItemPacket); + playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, getItemInOffHand())); } @Override diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 6f6567190..f36c62dcf 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -199,13 +199,9 @@ public class Inventory extends AbstractInventory implements Viewable { */ public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) { final ItemStack currentCursorItem = cursorPlayersItem.getOrDefault(player, ItemStack.AIR); - final boolean similar = currentCursorItem.isSimilar(cursorItem); - - if (!similar) { - final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem); - player.getPlayerConnection().sendPacket(setSlotPacket); + if (!currentCursorItem.isSimilar(cursorItem)) { + player.getPlayerConnection().sendPacket(SetSlotPacket.createCursorPacket(cursorItem)); } - if (!cursorItem.isAir()) { this.cursorPlayersItem.put(player, cursorItem); } else { @@ -225,11 +221,7 @@ public class Inventory extends AbstractInventory implements Viewable { @Override protected synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) { this.itemStacks[slot] = itemStack; - SetSlotPacket setSlotPacket = new SetSlotPacket(); - setSlotPacket.windowId = getWindowId(); - setSlotPacket.slot = (short) slot; - setSlotPacket.itemStack = itemStack; - sendPacketToViewers(setSlotPacket); + sendPacketToViewers(new SetSlotPacket(getWindowId(), 0, (short) slot, itemStack)); } /** @@ -237,12 +229,8 @@ public class Inventory extends AbstractInventory implements Viewable { * * @return a new {@link WindowItemsPacket} packet */ - @NotNull - private WindowItemsPacket createNewWindowItemsPacket() { - WindowItemsPacket windowItemsPacket = new WindowItemsPacket(); - windowItemsPacket.windowId = getWindowId(); - windowItemsPacket.items = getItemStacks(); - return windowItemsPacket; + private @NotNull WindowItemsPacket createNewWindowItemsPacket() { + return new WindowItemsPacket(getWindowId(), 0, getItemStacks(), ItemStack.AIR); } /** @@ -253,11 +241,7 @@ public class Inventory extends AbstractInventory implements Viewable { * @see https://wiki.vg/Protocol#Window_Property */ protected void sendProperty(@NotNull InventoryProperty property, short value) { - WindowPropertyPacket windowPropertyPacket = new WindowPropertyPacket(); - windowPropertyPacket.windowId = getWindowId(); - windowPropertyPacket.property = property.getProperty(); - windowPropertyPacket.value = value; - sendPacketToViewers(windowPropertyPacket); + sendPacketToViewers(new WindowPropertyPacket(getWindowId(), property.getProperty(), value)); } /** @@ -352,7 +336,7 @@ public class Inventory extends AbstractInventory implements Viewable { playerInventory.setItemStack(clickSlot, clickResult.getClicked()); } - if(clickResult.doRefresh()){ + if (clickResult.doRefresh()) { update(player); } diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 18c558d52..12acd5291 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -239,11 +239,7 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl * @param itemStack the item stack in the slot */ protected void sendSlotRefresh(short slot, ItemStack itemStack) { - SetSlotPacket setSlotPacket = new SetSlotPacket(); - setSlotPacket.windowId = 0; - setSlotPacket.slot = slot; - setSlotPacket.itemStack = itemStack; - player.getPlayerConnection().sendPacket(setSlotPacket); + player.getPlayerConnection().sendPacket(new SetSlotPacket((byte) 0, 0, slot, itemStack)); } /** @@ -253,16 +249,11 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl */ private WindowItemsPacket createWindowItemsPacket() { ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE]; - for (int i = 0; i < itemStacks.length; i++) { final int slot = convertToPacketSlot(i); convertedSlots[slot] = itemStacks[i]; } - - WindowItemsPacket windowItemsPacket = new WindowItemsPacket(); - windowItemsPacket.windowId = 0; - windowItemsPacket.items = convertedSlots; - return windowItemsPacket; + return new WindowItemsPacket((byte) 0, 0, convertedSlots, ItemStack.AIR); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java index 8bd046aa5..ad5695538 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java @@ -9,9 +9,15 @@ import org.jetbrains.annotations.NotNull; public class OpenBookPacket implements ServerPacket { - public Player.Hand hand = Player.Hand.MAIN; + public Player.Hand hand; - public OpenBookPacket() {} + public OpenBookPacket(Player.Hand hand) { + this.hand = hand; + } + + public OpenBookPacket() { + this(Player.Hand.MAIN); + } @Override public void write(@NotNull BinaryWriter writer) { diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java index e446afb73..75b91b0cd 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java @@ -14,8 +14,15 @@ public class SetSlotPacket implements ServerPacket { public short slot; public ItemStack itemStack; + public SetSlotPacket(byte windowId, int stateId, short slot, ItemStack itemStack) { + this.windowId = windowId; + this.stateId = stateId; + this.slot = slot; + this.itemStack = itemStack; + } + public SetSlotPacket() { - itemStack = ItemStack.AIR; + this((byte) 0, 0, (short) 0, ItemStack.AIR); } @Override @@ -45,12 +52,7 @@ public class SetSlotPacket implements ServerPacket { * @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; + public static @NotNull SetSlotPacket createCursorPacket(@NotNull ItemStack cursorItem) { + return new SetSlotPacket((byte) -1, -1, (short) 0, cursorItem); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java index 67f450018..60761727b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java @@ -12,12 +12,17 @@ public class WindowItemsPacket implements ServerPacket { public byte windowId; public int stateId; public ItemStack[] items; - public ItemStack carriedItem = ItemStack.AIR; + public ItemStack carriedItem; + + public WindowItemsPacket(byte windowId, int stateId, ItemStack[] items, ItemStack carriedItem) { + this.windowId = windowId; + this.stateId = stateId; + this.items = items; + this.carriedItem = carriedItem; + } - /** - * Default constructor, required for reflection operations. - */ public WindowItemsPacket() { + this((byte) 0, 0, new ItemStack[]{}, ItemStack.AIR); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java index 28a724f49..8f0451fb2 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java @@ -12,10 +12,15 @@ public class WindowPropertyPacket implements ServerPacket { public short property; public short value; - /** - * Default constructor, required for reflection operations. - */ - public WindowPropertyPacket() {} + public WindowPropertyPacket(byte windowId, short property, short value) { + this.windowId = windowId; + this.property = property; + this.value = value; + } + + public WindowPropertyPacket() { + this((byte) 0, (short) 0, (short) 0); + } @Override public void write(@NotNull BinaryWriter writer) {