Add some inline packet constructors

This commit is contained in:
TheMode 2021-07-22 09:54:34 +02:00
parent 690c5b7614
commit ff9ca60a58
7 changed files with 49 additions and 69 deletions

View File

@ -926,28 +926,15 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
@Override @Override
public void openBook(@NotNull Book book) { 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)) .meta(WrittenBookMeta.fromAdventure(book, this))
.build(); .build();
// Set book in offhand // Set book in offhand
SetSlotPacket setBookPacket = new SetSlotPacket(); playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, writtenBook));
setBookPacket.windowId = 0;
setBookPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT;
setBookPacket.itemStack = writtenBook;
playerConnection.sendPacket(setBookPacket);
// Open the book // Open the book
OpenBookPacket openBookPacket = new OpenBookPacket(); playerConnection.sendPacket(new OpenBookPacket(Hand.OFF));
openBookPacket.hand = Hand.OFF;
playerConnection.sendPacket(openBookPacket);
// Restore the item in offhand // Restore the item in offhand
SetSlotPacket restoreItemPacket = new SetSlotPacket(); playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, getItemInOffHand()));
restoreItemPacket.windowId = 0;
restoreItemPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT;
restoreItemPacket.itemStack = getItemInOffHand();
playerConnection.sendPacket(restoreItemPacket);
} }
@Override @Override

View File

@ -199,13 +199,9 @@ public class Inventory extends AbstractInventory implements Viewable {
*/ */
public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) { public void setCursorItem(@NotNull Player player, @NotNull ItemStack cursorItem) {
final ItemStack currentCursorItem = cursorPlayersItem.getOrDefault(player, ItemStack.AIR); final ItemStack currentCursorItem = cursorPlayersItem.getOrDefault(player, ItemStack.AIR);
final boolean similar = currentCursorItem.isSimilar(cursorItem); if (!currentCursorItem.isSimilar(cursorItem)) {
player.getPlayerConnection().sendPacket(SetSlotPacket.createCursorPacket(cursorItem));
if (!similar) {
final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
player.getPlayerConnection().sendPacket(setSlotPacket);
} }
if (!cursorItem.isAir()) { if (!cursorItem.isAir()) {
this.cursorPlayersItem.put(player, cursorItem); this.cursorPlayersItem.put(player, cursorItem);
} else { } else {
@ -225,11 +221,7 @@ public class Inventory extends AbstractInventory implements Viewable {
@Override @Override
protected synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) { protected synchronized void safeItemInsert(int slot, @NotNull ItemStack itemStack) {
this.itemStacks[slot] = itemStack; this.itemStacks[slot] = itemStack;
SetSlotPacket setSlotPacket = new SetSlotPacket(); sendPacketToViewers(new SetSlotPacket(getWindowId(), 0, (short) slot, itemStack));
setSlotPacket.windowId = getWindowId();
setSlotPacket.slot = (short) slot;
setSlotPacket.itemStack = itemStack;
sendPacketToViewers(setSlotPacket);
} }
/** /**
@ -237,12 +229,8 @@ public class Inventory extends AbstractInventory implements Viewable {
* *
* @return a new {@link WindowItemsPacket} packet * @return a new {@link WindowItemsPacket} packet
*/ */
@NotNull private @NotNull WindowItemsPacket createNewWindowItemsPacket() {
private WindowItemsPacket createNewWindowItemsPacket() { return new WindowItemsPacket(getWindowId(), 0, getItemStacks(), ItemStack.AIR);
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
windowItemsPacket.windowId = getWindowId();
windowItemsPacket.items = getItemStacks();
return windowItemsPacket;
} }
/** /**
@ -253,11 +241,7 @@ public class Inventory extends AbstractInventory implements Viewable {
* @see <a href="https://wiki.vg/Protocol#Window_Property">https://wiki.vg/Protocol#Window_Property</a> * @see <a href="https://wiki.vg/Protocol#Window_Property">https://wiki.vg/Protocol#Window_Property</a>
*/ */
protected void sendProperty(@NotNull InventoryProperty property, short value) { protected void sendProperty(@NotNull InventoryProperty property, short value) {
WindowPropertyPacket windowPropertyPacket = new WindowPropertyPacket(); sendPacketToViewers(new WindowPropertyPacket(getWindowId(), property.getProperty(), value));
windowPropertyPacket.windowId = getWindowId();
windowPropertyPacket.property = property.getProperty();
windowPropertyPacket.value = value;
sendPacketToViewers(windowPropertyPacket);
} }
/** /**
@ -352,7 +336,7 @@ public class Inventory extends AbstractInventory implements Viewable {
playerInventory.setItemStack(clickSlot, clickResult.getClicked()); playerInventory.setItemStack(clickSlot, clickResult.getClicked());
} }
if(clickResult.doRefresh()){ if (clickResult.doRefresh()) {
update(player); update(player);
} }

View File

@ -239,11 +239,7 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
* @param itemStack the item stack in the slot * @param itemStack the item stack in the slot
*/ */
protected void sendSlotRefresh(short slot, ItemStack itemStack) { protected void sendSlotRefresh(short slot, ItemStack itemStack) {
SetSlotPacket setSlotPacket = new SetSlotPacket(); player.getPlayerConnection().sendPacket(new SetSlotPacket((byte) 0, 0, slot, itemStack));
setSlotPacket.windowId = 0;
setSlotPacket.slot = slot;
setSlotPacket.itemStack = itemStack;
player.getPlayerConnection().sendPacket(setSlotPacket);
} }
/** /**
@ -253,16 +249,11 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
*/ */
private WindowItemsPacket createWindowItemsPacket() { private WindowItemsPacket createWindowItemsPacket() {
ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE]; ItemStack[] convertedSlots = new ItemStack[INVENTORY_SIZE];
for (int i = 0; i < itemStacks.length; i++) { for (int i = 0; i < itemStacks.length; i++) {
final int slot = convertToPacketSlot(i); final int slot = convertToPacketSlot(i);
convertedSlots[slot] = itemStacks[i]; convertedSlots[slot] = itemStacks[i];
} }
return new WindowItemsPacket((byte) 0, 0, convertedSlots, ItemStack.AIR);
WindowItemsPacket windowItemsPacket = new WindowItemsPacket();
windowItemsPacket.windowId = 0;
windowItemsPacket.items = convertedSlots;
return windowItemsPacket;
} }
@Override @Override

View File

@ -9,9 +9,15 @@ import org.jetbrains.annotations.NotNull;
public class OpenBookPacket implements ServerPacket { 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 @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -14,8 +14,15 @@ public class SetSlotPacket implements ServerPacket {
public short slot; public short slot;
public ItemStack itemStack; 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() { public SetSlotPacket() {
itemStack = ItemStack.AIR; this((byte) 0, 0, (short) 0, ItemStack.AIR);
} }
@Override @Override
@ -45,12 +52,7 @@ public class SetSlotPacket implements ServerPacket {
* @param cursorItem the cursor item * @param cursorItem the cursor item
* @return a set slot packet to change a player cursor item * @return a set slot packet to change a player cursor item
*/ */
@NotNull public static @NotNull SetSlotPacket createCursorPacket(@NotNull ItemStack cursorItem) {
public static SetSlotPacket createCursorPacket(@NotNull ItemStack cursorItem) { return new SetSlotPacket((byte) -1, -1, (short) 0, cursorItem);
SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = -1;
setSlotPacket.slot = -1;
setSlotPacket.itemStack = cursorItem;
return setSlotPacket;
} }
} }

View File

@ -12,12 +12,17 @@ public class WindowItemsPacket implements ServerPacket {
public byte windowId; public byte windowId;
public int stateId; public int stateId;
public ItemStack[] items; 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() { public WindowItemsPacket() {
this((byte) 0, 0, new ItemStack[]{}, ItemStack.AIR);
} }
@Override @Override

View File

@ -12,10 +12,15 @@ public class WindowPropertyPacket implements ServerPacket {
public short property; public short property;
public short value; public short value;
/** public WindowPropertyPacket(byte windowId, short property, short value) {
* Default constructor, required for reflection operations. this.windowId = windowId;
*/ this.property = property;
public WindowPropertyPacket() {} this.value = value;
}
public WindowPropertyPacket() {
this((byte) 0, (short) 0, (short) 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {