mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Add some inline packet constructors
This commit is contained in:
parent
690c5b7614
commit
ff9ca60a58
@ -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
|
||||
|
@ -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 <a href="https://wiki.vg/Protocol#Window_Property">https://wiki.vg/Protocol#Window_Property</a>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user