Skip to content

Commit

Permalink
Fix inventory packet encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Dec 21, 2023
1 parent eb07fb2 commit b5c9f07
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 63 deletions.
19 changes: 11 additions & 8 deletions src/main/java/cn/nukkit/inventory/BaseInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cn.nukkit.item.Items;
import cn.nukkit.network.protocol.InventoryContentPacket;
import cn.nukkit.network.protocol.InventorySlotPacket;
import cn.nukkit.network.protocol.types.ContainerIds;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
Expand Down Expand Up @@ -471,18 +472,19 @@ public void sendContents(Player player) {

@Override
public void sendContents(Player... players) {
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = new Item[this.getSize()];
Item[] slots = new Item[this.getSize()];
for (int i = 0; i < this.getSize(); ++i) {
pk.slots[i] = this.getItem(i);
slots[i] = this.getItem(i);
}

for (Player player : players) {
int id = player.getWindowId(this);
if (id == -1 || !player.spawned) {
if (id == ContainerIds.NONE || !player.spawned) {
this.close(player);
continue;
}
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = slots;
pk.inventoryId = id;
player.dataPacket(pk);
}
Expand Down Expand Up @@ -548,16 +550,17 @@ public void sendSlot(int index, Player player) {

@Override
public void sendSlot(int index, Player... players) {
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = this.getItem(index).clone();
Item item = this.getItem(index);

for (Player player : players) {
int id = player.getWindowId(this);
if (id == -1) {
if (id == ContainerIds.NONE) {
this.close(player);
continue;
}
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = item;
pk.inventoryId = id;
player.dataPacket(pk);
}
Expand Down
65 changes: 35 additions & 30 deletions src/main/java/cn/nukkit/inventory/PlayerInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ public void setHeldItemSlot(int slot) {

public void sendHeldItem(Player... players) {
Item item = this.getItemInHand();

MobEquipmentPacket pk = new MobEquipmentPacket();
pk.item = item;
pk.inventorySlot = pk.hotbarSlot = this.getHeldItemIndex();
pk.eid = this.getHolder().getId();
pk.tryEncode();
long entityId = this.getHolder().getId();

for (Player player : players) {
if (player == this.getHolder()) {
Expand All @@ -152,11 +147,15 @@ public void sendHeldItem(Player... players) {
MobEquipmentPacket pk0 = new MobEquipmentPacket();
pk0.item = item;
pk0.inventorySlot = pk0.hotbarSlot = this.getHeldItemIndex();
pk0.eid = player.getId();
pk0.eid = entityId;
player.dataPacket(pk0);
continue;
}

MobEquipmentPacket pk = new MobEquipmentPacket();
pk.item = item;
pk.inventorySlot = pk.hotbarSlot = this.getHeldItemIndex();
pk.eid = entityId;
player.dataPacket(pk);
}
}
Expand Down Expand Up @@ -357,11 +356,7 @@ public void sendArmorContents(Player player) {

public void sendArmorContents(Player[] players) {
Item[] armor = this.getArmorContents();

MobArmorEquipmentPacket pk = new MobArmorEquipmentPacket();
pk.eid = this.getHolder().getId();
pk.slots = armor;
pk.tryEncode();
long entityId = this.getHolder().getId();

for (Player player : players) {
if (player == this.getHolder()) {
Expand All @@ -370,6 +365,9 @@ public void sendArmorContents(Player[] players) {
pk2.slots = armor;
player.dataPacket(pk2);
} else {
MobArmorEquipmentPacket pk = new MobArmorEquipmentPacket();
pk.eid = entityId;
pk.slots = armor;
player.dataPacket(pk);
}
}
Expand Down Expand Up @@ -405,11 +403,7 @@ public void sendArmorSlot(int index, Player player) {

public void sendArmorSlot(int index, Player[] players) {
Item[] armor = this.getArmorContents();

MobArmorEquipmentPacket pk = new MobArmorEquipmentPacket();
pk.eid = this.getHolder().getId();
pk.slots = armor;
pk.tryEncode();
long entityId = this.getHolder().getId();

for (Player player : players) {
if (player == this.getHolder()) {
Expand All @@ -419,6 +413,9 @@ public void sendArmorSlot(int index, Player[] players) {
pk2.item = this.getItem(index);
player.dataPacket(pk2);
} else {
MobArmorEquipmentPacket pk = new MobArmorEquipmentPacket();
pk.eid = entityId;
pk.slots = armor;
player.dataPacket(pk);
}
}
Expand All @@ -440,20 +437,21 @@ public void sendContents(Collection<Player> players) {

@Override
public void sendContents(Player[] players) {
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = new Item[this.getSize()];
Item[] slots = new Item[this.getSize()];
for (int i = 0; i < this.getSize(); ++i) {
pk.slots[i] = this.getItem(i);
slots[i] = this.getItem(i);
}

for (Player player : players) {
int id = player.getWindowId(this);
if (id == -1 || !player.spawned) {
if (id == ContainerIds.NONE || !player.spawned) {
if (this.getHolder() != player) this.close(player);
continue;
}
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = slots;
pk.inventoryId = id;
player.dataPacket(pk.clone());
player.dataPacket(pk);
}
}

Expand All @@ -469,22 +467,26 @@ public void sendSlot(int index, Collection<Player> players) {

@Override
public void sendSlot(int index, Player... players) {
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = this.getItem(index).clone();
Item item = this.getItem(index);

for (Player player : players) {
if (player == this.getHolder()) {
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = item;
pk.inventoryId = ContainerIds.INVENTORY;
player.dataPacket(pk);
} else {
int id = player.getWindowId(this);
if (id == -1) {
if (id == ContainerIds.NONE) {
this.close(player);
continue;
}
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = item;
pk.inventoryId = id;
player.dataPacket(pk.clone());
player.dataPacket(pk);
}
}
}
Expand Down Expand Up @@ -524,9 +526,12 @@ public void onOpen(Player who) {

@Override
public void onClose(Player who) {
ContainerClosePacket pk = new ContainerClosePacket();
pk.windowId = who.getWindowId(this);
if (pk.windowId != -1) who.dataPacket(pk);
int windowId = who.getWindowId(this);
if (windowId != ContainerIds.NONE) {
ContainerClosePacket pk = new ContainerClosePacket();
pk.windowId = windowId;
who.dataPacket(pk);
}
// player can never stop viewing their own inventory
if (who != holder) {
super.onClose(who);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/cn/nukkit/inventory/PlayerOffhandInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onSlotChange(int index, Item before, Item after, boolean send) {
@Override
public void sendContents(Player... players) {
Item item = this.getItem(0);
MobEquipmentPacket pk = this.createMobEquipmentPacket(item);
long entityId = this.getHolder().getId();

for (Player player : players) {
if (player == this.getHolder()) {
Expand All @@ -48,6 +48,11 @@ public void sendContents(Player... players) {
pk2.slots = new Item[]{item};
player.dataPacket(pk2);
} else {
MobEquipmentPacket pk = new MobEquipmentPacket();
pk.eid = entityId;
pk.item = item;
pk.inventorySlot = 1;
pk.windowId = ContainerIds.OFFHAND;
player.dataPacket(pk);
}
}
Expand All @@ -56,7 +61,7 @@ public void sendContents(Player... players) {
@Override
public void sendSlot(int index, Player... players) {
Item item = this.getItem(0);
MobEquipmentPacket pk = this.createMobEquipmentPacket(item);
long entityId = this.getHolder().getId();

for (Player player : players) {
if (player == this.getHolder()) {
Expand All @@ -65,21 +70,16 @@ public void sendSlot(int index, Player... players) {
pk2.item = item;
player.dataPacket(pk2);
} else {
MobEquipmentPacket pk = new MobEquipmentPacket();
pk.eid = entityId;
pk.item = item;
pk.inventorySlot = 1;
pk.windowId = ContainerIds.OFFHAND;
player.dataPacket(pk);
}
}
}

private MobEquipmentPacket createMobEquipmentPacket(Item item) {
MobEquipmentPacket pk = new MobEquipmentPacket();
pk.eid = this.getHolder().getId();
pk.item = item;
pk.inventorySlot = 1;
pk.windowId = ContainerIds.OFFHAND;
pk.tryEncode();
return pk;
}

@Override
public EntityHuman getHolder() {
return (EntityHuman) super.getHolder();
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/cn/nukkit/inventory/PlayerUIInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,24 @@ public void setSize(int size) {

@Override
public void sendSlot(int index, Player... target) {
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = this.getItem(index);
Item item = this.getItem(index);

for (Player p : target) {
if (p == this.getHolder()) {
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = item;
pk.inventoryId = ContainerIds.UI;
p.dataPacket(pk);
} else {
int id;

if ((id = p.getWindowId(this)) == ContainerIds.NONE) {
int id = p.getWindowId(this);
if (id == ContainerIds.NONE) {
this.close(p);
continue;
}
InventorySlotPacket pk = new InventorySlotPacket();
pk.slot = index;
pk.item = item;
pk.inventoryId = id;
p.dataPacket(pk);
}
Expand All @@ -67,23 +70,25 @@ public void sendSlot(int index, Player... target) {

@Override
public void sendContents(Player... target) {
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = new Item[this.getSize()];
Item[] slots = new Item[this.getSize()];
for (int i = 0; i < this.getSize(); ++i) {
pk.slots[i] = this.getItem(i);
slots[i] = this.getItem(i);
}

for (Player p : target) {
if (p == this.getHolder()) {
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = slots;
pk.inventoryId = ContainerIds.UI;
p.dataPacket(pk);
} else {
int id;

if ((id = p.getWindowId(this)) == ContainerIds.NONE) {
int id = p.getWindowId(this);
if (id == ContainerIds.NONE) {
this.close(p);
continue;
}
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = slots;
pk.inventoryId = id;
p.dataPacket(pk);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/network/protocol/DataPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class DataPacket extends BinaryStream implements Cloneable {

public abstract void decode();

public abstract void encode();
protected abstract void encode();

public void tryEncode() {
if (!this.isEncoded) {
Expand Down

0 comments on commit b5c9f07

Please sign in to comment.