Skip to content

Commit 8fc5e50

Browse files
authored
Added factory method for initializing WrappedDataValue (#2523)
1 parent 9e9b39a commit 8fc5e50

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

src/main/java/com/comphenix/protocol/events/AbstractStructure.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.comphenix.protocol.events;
22

3-
import javax.annotation.Nonnull;
4-
import java.lang.reflect.Array;
5-
import java.time.Instant;
6-
import java.util.*;
7-
83
import com.comphenix.protocol.PacketType;
94
import com.comphenix.protocol.reflect.EquivalentConverter;
105
import com.comphenix.protocol.reflect.StructureModifier;
@@ -17,7 +12,6 @@
1712
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
1813
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
1914
import com.google.common.base.Preconditions;
20-
2115
import org.apache.commons.lang.Validate;
2216
import org.bukkit.Material;
2317
import org.bukkit.Sound;
@@ -31,6 +25,11 @@
3125
import org.bukkit.util.Vector;
3226
import org.jetbrains.annotations.NotNull;
3327

28+
import javax.annotation.Nonnull;
29+
import java.lang.reflect.Array;
30+
import java.time.Instant;
31+
import java.util.*;
32+
3433
public abstract class AbstractStructure {
3534
protected transient Object handle;
3635
protected transient StructureModifier<Object> structureModifier;
@@ -410,7 +409,7 @@ public StructureModifier<List<BlockPosition>> getBlockPositionCollectionModifier
410409
}
411410

412411
/**
413-
* Retrieves a read/write structure for collections of watchable objects.
412+
* Retrieves a read/write structure for collections of watchable objects before Minecraft 1.19.3.
414413
* <p>
415414
* This modifier will automatically marshal between the visible WrappedWatchableObject and the
416415
* internal Minecraft WatchableObject.
@@ -424,7 +423,7 @@ public StructureModifier<List<WrappedWatchableObject>> getWatchableCollectionMod
424423
}
425424

426425
/**
427-
* Retrieves a read/write structure for collections of data values.
426+
* Retrieves a read/write structure for collections of data values for Minecraft 1.19.3 or later.
428427
* @return A modifier for data values.
429428
*/
430429
public StructureModifier<List<WrappedDataValue>> getDataValueCollectionModifier() {

src/main/java/com/comphenix/protocol/wrappers/WrappedDataValue.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* Represents a DataValue in 1.19.3+.
12+
* Use {@link WrappedWatchableObject} before 1.19.3.
1213
*/
1314
public class WrappedDataValue extends AbstractWrapper {
1415

@@ -29,10 +30,28 @@ public WrappedDataValue(Object handle) {
2930
this.modifier = new StructureModifier<>(this.handleType).withTarget(handle);
3031
}
3132

33+
/**
34+
* Creates a new WrappedDataValue from a NMS value.
35+
* ProtocolLib wrappers are not supported as arguments.
36+
* If implicit unwrapping of wrappers is required, use {@link WrappedDataValue#fromWrappedValue(int, Serializer, Object)}.
37+
* @param index the index of the metadata value
38+
* @param serializer the serializer corresponding for serializing. Can be null.
39+
* @param value The raw value for the DataValue. Can be null.
40+
*/
3241
public WrappedDataValue(int index, Serializer serializer, Object value) {
3342
this(newHandle(index, serializer, value));
3443
}
3544

45+
/**
46+
* Creates a new WrappedDataValue from a possibly wrapped value and implicitly unwrap value if possible.
47+
* @param index the index of the metadata value
48+
* @param serializer the serializer corresponding for serializing. Can be null.
49+
* @param value The value for the DataValue. Can be null.
50+
*/
51+
public static WrappedDataValue fromWrappedValue(int index, Serializer serializer, Object value) {
52+
return new WrappedDataValue(index, serializer, value == null ? null : WrappedWatchableObject.getUnwrapped(value));
53+
}
54+
3655
private static Object newHandle(int index, Serializer serializer, Object value) {
3756
if (constructor == null) {
3857
constructor = Accessors.getConstructorAccessor(HANDLE_TYPE.getConstructors()[0]);
@@ -41,14 +60,26 @@ private static Object newHandle(int index, Serializer serializer, Object value)
4160
return constructor.invoke(index, serializer.getHandle(), value);
4261
}
4362

63+
/**
64+
* Returns the entity-type specific index of this DataValue
65+
* @return index of the DataValue
66+
*/
4467
public int getIndex() {
4568
return this.modifier.<Integer>withType(int.class).read(0);
4669
}
4770

71+
/**
72+
* Sets the entity-type specific index of this DataValue
73+
* @param index New index of the DataValue
74+
*/
4875
public void setIndex(int index) {
4976
this.modifier.withType(int.class).write(0, index);
5077
}
5178

79+
/**
80+
* Returns the current serializer for this DataValue.
81+
* @return serializer
82+
*/
5283
public Serializer getSerializer() {
5384
Object serializer = this.modifier.readSafely(1);
5485
if (serializer != null) {
@@ -63,22 +94,42 @@ public Serializer getSerializer() {
6394
}
6495
}
6596

97+
/**
98+
* Changes the serializer for this DataValue
99+
* @param serializer serializer
100+
*/
66101
public void setSerializer(Serializer serializer) {
67102
this.modifier.writeSafely(1, serializer == null ? null : serializer.getHandle());
68103
}
69104

105+
/**
106+
* Returns the current value associated and implicitly wraps it to corresponding ProtocolLib wrappers if possible.
107+
* @return Current value
108+
*/
70109
public Object getValue() {
71110
return WrappedWatchableObject.getWrapped(getRawValue());
72111
}
73112

113+
/**
114+
* Sets the current value associated and implicitly unwraps it to NMS types if a ProtocolLib wrapper is provided.
115+
* @param value New value for this DataValue
116+
*/
74117
public void setValue(Object value) {
75118
setRawValue(WrappedWatchableObject.getUnwrapped(value));
76119
}
77120

121+
/**
122+
* Returns the current, raw value.
123+
* @return Raw value (not wrapped)
124+
*/
78125
public Object getRawValue() {
79126
return this.modifier.readSafely(2);
80127
}
81128

129+
/**
130+
* Updates the raw value for this DataValue. No unwrapping will be applied.
131+
* @param value NMS value
132+
*/
82133
public void setRawValue(Object value) {
83134
this.modifier.writeSafely(2, value);
84135
}

src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515
package com.comphenix.protocol.wrappers;
1616

17-
import static com.comphenix.protocol.utility.MinecraftReflection.is;
18-
1917
import com.comphenix.protocol.reflect.StructureModifier;
2018
import com.comphenix.protocol.reflect.accessors.Accessors;
2119
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
@@ -25,12 +23,15 @@
2523
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
2624
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
2725
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
28-
import java.util.Optional;
2926
import org.bukkit.inventory.ItemStack;
3027

28+
import java.util.Optional;
29+
30+
import static com.comphenix.protocol.utility.MinecraftReflection.is;
31+
3132
/**
32-
* Represents a DataWatcher Item in 1.8 to 1.10.
33-
*
33+
* Represents a DataWatcher Item in 1.8 to 1.19.2.
34+
* Use {@link WrappedDataValue} for 1.19.3 or later.
3435
* @author dmulloy2
3536
*/
3637
public class WrappedWatchableObject extends AbstractWrapper {
@@ -97,6 +98,9 @@ private static Object newHandle(WrappedDataWatcherObject watcherObject, Object v
9798
* @return The wrapped object.
9899
*/
99100
static Object getWrapped(Object value) {
101+
if(value == null) {
102+
return null;
103+
}
100104
// Handle watcher items first
101105
if (is(MinecraftReflection.getDataWatcherItemClass(), value)) {
102106
return getWrapped(new WrappedWatchableObject(value).getRawValue());
@@ -135,6 +139,9 @@ static Object getWrapped(Object value) {
135139
*/
136140
// Must be kept in sync with getWrapped!
137141
static Object getUnwrapped(Object wrapped) {
142+
if(wrapped == null) {
143+
return null;
144+
}
138145
if (wrapped instanceof Optional<?>) {
139146
return ((Optional<?>) wrapped).map(WrappedWatchableObject::getUnwrapped);
140147
}

0 commit comments

Comments
 (0)