Skip to content

Commit 819971b

Browse files
committed
Implement PlayerEquipmentData
- Fix client not grouping PlayerInfoPayload component changes due to logical OR short-circuiting
1 parent 6ac51c8 commit 819971b

4 files changed

Lines changed: 74 additions & 7 deletions

File tree

src/main/java/dev/hintsystem/playerrelay/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Config {
4848
public boolean showPingsFromOtherServers = false;
4949

5050
public int peerConnectionTimeout = 6000;
51-
public int tcpSendIntervalMs = 400;
51+
public int tcpSendIntervalMs = 500;
5252
public int udpSendIntervalMs = 100;
5353
public int udpPingIntervalMs = 5000;
5454
public int udpPingTimeoutMs = 2000;

src/main/java/dev/hintsystem/playerrelay/payload/PlayerInfoPayload.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class PlayerInfoPayload implements IPayload {
2727
registerComponent(PlayerWorldData.class, PlayerWorldData::new);
2828
registerComponent(PlayerPositionData.class, PlayerPositionData::new);
2929
registerComponent(PlayerStatsData.class, PlayerStatsData::new);
30+
registerComponent(PlayerEquipmentData.class, PlayerEquipmentData::new);
3031
registerComponent(PlayerStatusEffectsData.class, PlayerStatusEffectsData::new);
3132
}
3233

@@ -99,13 +100,11 @@ public <T extends PlayerDataComponent> T getComponentOrEmpty(Class<T> componentC
99100
}
100101

101102
public <T extends PlayerDataComponent> PlayerInfoPayload setComponent(T component) {
102-
ComponentInfo<?> info = COMPONENT_REGISTRY.get(component.getClass());
103-
if (info == null) throw new IllegalArgumentException("Unknown component class: " + component.getClass());
104-
105-
int index = getComponentIndex(info.flag);
103+
byte flag = getComponentInfo(component.getClass()).flag;
104+
int index = getComponentIndex(flag);
106105
components[index] = component;
107106

108-
this.flags |= info.flag;
107+
this.flags |= flag;
109108
return this;
110109
}
111110

src/main/java/dev/hintsystem/playerrelay/payload/PlayerInventoryPayload.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.hintsystem.playerrelay.networking.message.P2PMessageType;
44

55
import net.minecraft.entity.player.PlayerEntity;
6+
import net.minecraft.entity.player.PlayerInventory;
67
import net.minecraft.item.ItemStack;
78
import net.minecraft.network.RegistryByteBuf;
89

@@ -23,7 +24,7 @@ public PlayerInventoryPayload(PlayerEntity player) {
2324
this.isRequest = false;
2425
this.playerId = player.getUuid();
2526

26-
for (int i = 0; i < 36; i++) {
27+
for (int i = 0; i < PlayerInventory.MAIN_SIZE; i++) {
2728
ItemStack stack = player.getInventory().getStack(i);
2829

2930
this.inventoryItems.add(stack.copy());
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package dev.hintsystem.playerrelay.payload.player;
2+
3+
import net.minecraft.entity.EquipmentSlot;
4+
import net.minecraft.entity.player.PlayerEntity;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.network.RegistryByteBuf;
7+
import net.minecraft.util.collection.DefaultedList;
8+
9+
public class PlayerEquipmentData implements PlayerDataComponent {
10+
public static final EquipmentSlot[] EQUIPMENT_SLOT_ORDER = new EquipmentSlot[] {
11+
EquipmentSlot.MAINHAND,
12+
EquipmentSlot.OFFHAND,
13+
EquipmentSlot.FEET,
14+
EquipmentSlot.LEGS,
15+
EquipmentSlot.CHEST,
16+
EquipmentSlot.HEAD,
17+
};
18+
19+
public final DefaultedList<ItemStack> equipment = DefaultedList.ofSize(EQUIPMENT_SLOT_ORDER.length, ItemStack.EMPTY);
20+
21+
public PlayerEquipmentData() {}
22+
23+
public PlayerEquipmentData(PlayerEntity player) {
24+
for (int i = 0; i < EQUIPMENT_SLOT_ORDER.length; i++) {
25+
EquipmentSlot slot = EQUIPMENT_SLOT_ORDER[i];
26+
ItemStack stack = player.getEquippedStack(slot);
27+
this.equipment.set(i, stack.copy());
28+
}
29+
}
30+
31+
@Override
32+
public void write(RegistryByteBuf buf) {
33+
for (int i = 0; i < EQUIPMENT_SLOT_ORDER.length; i++) {
34+
ItemStack.OPTIONAL_PACKET_CODEC.encode(buf, equipment.get(i));
35+
}
36+
}
37+
38+
@Override
39+
public void read(RegistryByteBuf buf) {
40+
for (int i = 0; i < EQUIPMENT_SLOT_ORDER.length; i++) {
41+
ItemStack stack = ItemStack.OPTIONAL_PACKET_CODEC.decode(buf);
42+
this.equipment.set(i, stack);
43+
}
44+
}
45+
46+
@Override
47+
public boolean hasChanged(PlayerDataComponent other) {
48+
if (!(other instanceof PlayerEquipmentData otherEquipment)) return true;
49+
50+
for (int i = 0; i < EQUIPMENT_SLOT_ORDER.length; i++) {
51+
if (!ItemStack.areEqual(this.equipment.get(i), otherEquipment.equipment.get(i))) {
52+
return true;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
59+
@Override
60+
public PlayerEquipmentData copy() {
61+
PlayerEquipmentData copy = new PlayerEquipmentData();
62+
for (int i = 0; i < this.equipment.size(); i++) {
63+
copy.equipment.set(i, this.equipment.get(i).copy());
64+
}
65+
return copy;
66+
}
67+
}

0 commit comments

Comments
 (0)