Skip to content

Commit 8f446c3

Browse files
committed
BackwardsCompat: WrapperPlayServerEntityEquipment
1 parent ec3eb66 commit 8f446c3

File tree

1 file changed

+112
-32
lines changed

1 file changed

+112
-32
lines changed

PacketWrapper/src/main/java/com/comphenix/packetwrapper/WrapperPlayServerEntityEquipment.java

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818
*/
1919
package com.comphenix.packetwrapper;
2020

21-
import org.bukkit.World;
22-
import org.bukkit.entity.Entity;
23-
import org.bukkit.inventory.ItemStack;
24-
21+
import com.comphenix.packetwrapper.util.BackwardsCompatible;
2522
import com.comphenix.protocol.PacketType;
2623
import com.comphenix.protocol.events.PacketContainer;
2724
import com.comphenix.protocol.events.PacketEvent;
25+
import com.comphenix.protocol.reflect.StructureModifier;
2826
import com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot;
27+
import com.comphenix.protocol.wrappers.Pair;
28+
import org.bukkit.World;
29+
import org.bukkit.entity.Entity;
30+
import org.bukkit.inventory.ItemStack;
31+
32+
import java.util.List;
2933

34+
@BackwardsCompatible
3035
public class WrapperPlayServerEntityEquipment extends AbstractPacket {
3136
public static final PacketType TYPE = PacketType.Play.Server.ENTITY_EQUIPMENT;
3237

@@ -35,6 +40,8 @@ public class WrapperPlayServerEntityEquipment extends AbstractPacket {
3540
*/
3641
protected static final ItemSlot[] ITEM_SLOTS = ItemSlot.values();
3742

43+
protected static final boolean SUPPORTS_MULTIPLE_SLOTS = MINOR_VERSION >= 16;
44+
3845
public WrapperPlayServerEntityEquipment() {
3946
super(new PacketContainer(TYPE), TYPE);
4047
handle.getModifier().writeDefaults();
@@ -44,11 +51,20 @@ public WrapperPlayServerEntityEquipment(PacketContainer packet) {
4451
super(packet, TYPE);
4552
}
4653

54+
/**
55+
* Checks if this packet supports multiple slots.
56+
*
57+
* @return {@code true} if this packet supports multiple slots and {@code false} otherwise
58+
*/
59+
public static boolean supportMultipleSlots() {
60+
return SUPPORTS_MULTIPLE_SLOTS;
61+
}
62+
4763
/**
4864
* Retrieve Entity ID.
4965
* <p>
5066
* Notes: entity's ID
51-
*
67+
*
5268
* @return The current Entity ID
5369
*/
5470
public int getEntityID() {
@@ -57,7 +73,7 @@ public int getEntityID() {
5773

5874
/**
5975
* Set Entity ID.
60-
*
76+
*
6177
* @param value - new value.
6278
*/
6379
public void setEntityID(int value) {
@@ -66,17 +82,29 @@ public void setEntityID(int value) {
6682

6783
/**
6884
* Retrieve the entity of the painting that will be spawned.
69-
*
85+
*
7086
* @param world - the current world of the entity.
7187
* @return The spawned entity.
7288
*/
7389
public Entity getEntity(World world) {
7490
return handle.getEntityModifier(world).read(0);
7591
}
7692

93+
@BackwardsCompatible(sinceMinor = 16)
94+
public List<Pair<ItemSlot, ItemStack>> getContents() {
95+
if (MINOR_VERSION >= 16) return handle.getSlotStackPairLists().read(0);
96+
throw new UnsupportedOperationException("Unsupported on versions less than 1.16");
97+
}
98+
99+
@BackwardsCompatible(sinceMinor = 16)
100+
public void setContents(List<Pair<ItemSlot, ItemStack>> contents) {
101+
if (MINOR_VERSION >= 16) handle.getSlotStackPairLists().write(0, contents);
102+
else throw new UnsupportedOperationException("Unsupported on versions less than 1.16");
103+
}
104+
77105
/**
78106
* Retrieve the entity of the painting that will be spawned.
79-
*
107+
*
80108
* @param event - the packet event.
81109
* @return The spawned entity.
82110
*/
@@ -85,58 +113,110 @@ public Entity getEntity(PacketEvent event) {
85113
}
86114

87115
public ItemSlot getSlot() {
116+
if (MINOR_VERSION >= 16) {
117+
final List<Pair<ItemSlot, ItemStack>> slots = handle.getSlotStackPairLists().read(0);
118+
switch (slots.size()) {
119+
case 0: return null;
120+
case 1: return slots.get(0).getFirst();
121+
default: throw new UnsupportedOperationException("This packet has multiple slots specified");
122+
}
123+
}
124+
88125
if (MINOR_VERSION >= 9) return handle.getItemSlots().read(0);
89126
int slot = handle.getIntegers().read(0);
90127
if (slot >= ITEM_SLOTS.length) throw new IllegalArgumentException("Unknown item slot received: " + slot);
91128
return ITEM_SLOTS[slot];
92129
}
93130

94131
public void setSlot(ItemSlot value) {
95-
if (MINOR_VERSION >= 9) handle.getItemSlots().write(0, value);
96-
else {
97-
switch (value) {
98-
case MAINHAND: {
99-
handle.getIntegers().write(1, 0);
100-
break;
101-
}
102-
case OFFHAND: throw new IllegalArgumentException("Offhand is not available on 1.8 or less");
103-
case FEET: {
104-
handle.getIntegers().write(1, 1);
105-
break;
106-
}
107-
case LEGS: {
108-
handle.getIntegers().write(1, 2);
109-
break;
132+
if (MINOR_VERSION >= 16) {
133+
final StructureModifier<List<Pair<ItemSlot, ItemStack>>> modifier = handle.getSlotStackPairLists();
134+
List<Pair<ItemSlot, ItemStack>> slots = modifier.read(0);
135+
switch (slots.size()) {
136+
case 0: {
137+
slots.add(new Pair<>(value, null));
138+
modifier.write(0, slots);
139+
return;
110140
}
111-
case CHEST: {
112-
handle.getIntegers().write(1, 3);
113-
break;
114-
}
115-
case HEAD: {
116-
handle.getIntegers().write(1, 4);
117-
break;
141+
case 1: {
142+
slots.get(0).setFirst(value);
143+
modifier.write(0, slots);
144+
return;
118145
}
146+
default: throw new UnsupportedOperationException("This packet has multiple slots specified");
147+
}
148+
}
149+
150+
if (MINOR_VERSION >= 9) handle.getItemSlots().write(0, value);
151+
else switch (value) {
152+
case MAINHAND: {
153+
handle.getIntegers().write(1, 0);
154+
break;
155+
}
156+
case FEET: {
157+
handle.getIntegers().write(1, 1);
158+
break;
159+
}
160+
case LEGS: {
161+
handle.getIntegers().write(1, 2);
162+
break;
119163
}
164+
case CHEST: {
165+
handle.getIntegers().write(1, 3);
166+
break;
167+
}
168+
case HEAD: {
169+
handle.getIntegers().write(1, 4);
170+
break;
171+
}
172+
case OFFHAND: throw new IllegalArgumentException("Offhand is not available on 1.8 or less");
120173
}
121174
}
122175

123176
/**
124177
* Retrieve Item.
125178
* <p>
126179
* Notes: item in slot format
127-
*
180+
*
128181
* @return The current Item
129182
*/
130183
public ItemStack getItem() {
184+
if (MINOR_VERSION >= 16) {
185+
final List<Pair<ItemSlot, ItemStack>> slots = handle.getSlotStackPairLists().read(0);
186+
switch (slots.size()) {
187+
case 0: return null;
188+
case 1: return slots.get(0).getSecond();
189+
default: throw new UnsupportedOperationException("This packet has multiple slots specified");
190+
}
191+
}
192+
131193
return handle.getItemModifier().read(0);
132194
}
133195

134196
/**
135197
* Set Item.
136-
*
198+
*
137199
* @param value - new value.
138200
*/
139201
public void setItem(ItemStack value) {
202+
if (MINOR_VERSION >= 16) {
203+
final StructureModifier<List<Pair<ItemSlot, ItemStack>>> modifier = handle.getSlotStackPairLists();
204+
List<Pair<ItemSlot, ItemStack>> slots = modifier.read(0);
205+
switch (slots.size()) {
206+
case 0: {
207+
slots.add(new Pair<>(null, value));
208+
modifier.write(0, slots);
209+
return;
210+
}
211+
case 1: {
212+
slots.get(0).setSecond(value);
213+
modifier.write(0, slots);
214+
return;
215+
}
216+
default: throw new UnsupportedOperationException("This packet has multiple slots specified");
217+
}
218+
}
219+
140220
handle.getItemModifier().write(0, value);
141221
}
142222
}

0 commit comments

Comments
 (0)