18
18
*/
19
19
package com .comphenix .packetwrapper ;
20
20
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 ;
25
22
import com .comphenix .protocol .PacketType ;
26
23
import com .comphenix .protocol .events .PacketContainer ;
27
24
import com .comphenix .protocol .events .PacketEvent ;
25
+ import com .comphenix .protocol .reflect .StructureModifier ;
28
26
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 ;
29
33
34
+ @ BackwardsCompatible
30
35
public class WrapperPlayServerEntityEquipment extends AbstractPacket {
31
36
public static final PacketType TYPE = PacketType .Play .Server .ENTITY_EQUIPMENT ;
32
37
@@ -35,6 +40,8 @@ public class WrapperPlayServerEntityEquipment extends AbstractPacket {
35
40
*/
36
41
protected static final ItemSlot [] ITEM_SLOTS = ItemSlot .values ();
37
42
43
+ protected static final boolean SUPPORTS_MULTIPLE_SLOTS = MINOR_VERSION >= 16 ;
44
+
38
45
public WrapperPlayServerEntityEquipment () {
39
46
super (new PacketContainer (TYPE ), TYPE );
40
47
handle .getModifier ().writeDefaults ();
@@ -44,11 +51,20 @@ public WrapperPlayServerEntityEquipment(PacketContainer packet) {
44
51
super (packet , TYPE );
45
52
}
46
53
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
+
47
63
/**
48
64
* Retrieve Entity ID.
49
65
* <p>
50
66
* Notes: entity's ID
51
- *
67
+ *
52
68
* @return The current Entity ID
53
69
*/
54
70
public int getEntityID () {
@@ -57,7 +73,7 @@ public int getEntityID() {
57
73
58
74
/**
59
75
* Set Entity ID.
60
- *
76
+ *
61
77
* @param value - new value.
62
78
*/
63
79
public void setEntityID (int value ) {
@@ -66,17 +82,29 @@ public void setEntityID(int value) {
66
82
67
83
/**
68
84
* Retrieve the entity of the painting that will be spawned.
69
- *
85
+ *
70
86
* @param world - the current world of the entity.
71
87
* @return The spawned entity.
72
88
*/
73
89
public Entity getEntity (World world ) {
74
90
return handle .getEntityModifier (world ).read (0 );
75
91
}
76
92
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
+
77
105
/**
78
106
* Retrieve the entity of the painting that will be spawned.
79
- *
107
+ *
80
108
* @param event - the packet event.
81
109
* @return The spawned entity.
82
110
*/
@@ -85,58 +113,110 @@ public Entity getEntity(PacketEvent event) {
85
113
}
86
114
87
115
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
+
88
125
if (MINOR_VERSION >= 9 ) return handle .getItemSlots ().read (0 );
89
126
int slot = handle .getIntegers ().read (0 );
90
127
if (slot >= ITEM_SLOTS .length ) throw new IllegalArgumentException ("Unknown item slot received: " + slot );
91
128
return ITEM_SLOTS [slot ];
92
129
}
93
130
94
131
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 ;
110
140
}
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 ;
118
145
}
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 ;
119
163
}
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" );
120
173
}
121
174
}
122
175
123
176
/**
124
177
* Retrieve Item.
125
178
* <p>
126
179
* Notes: item in slot format
127
- *
180
+ *
128
181
* @return The current Item
129
182
*/
130
183
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
+
131
193
return handle .getItemModifier ().read (0 );
132
194
}
133
195
134
196
/**
135
197
* Set Item.
136
- *
198
+ *
137
199
* @param value - new value.
138
200
*/
139
201
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
+
140
220
handle .getItemModifier ().write (0 , value );
141
221
}
142
222
}
0 commit comments