9
9
10
10
/**
11
11
* Represents a DataValue in 1.19.3+.
12
+ * Use {@link WrappedWatchableObject} before 1.19.3.
12
13
*/
13
14
public class WrappedDataValue extends AbstractWrapper {
14
15
@@ -29,10 +30,28 @@ public WrappedDataValue(Object handle) {
29
30
this .modifier = new StructureModifier <>(this .handleType ).withTarget (handle );
30
31
}
31
32
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
+ */
32
41
public WrappedDataValue (int index , Serializer serializer , Object value ) {
33
42
this (newHandle (index , serializer , value ));
34
43
}
35
44
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
+
36
55
private static Object newHandle (int index , Serializer serializer , Object value ) {
37
56
if (constructor == null ) {
38
57
constructor = Accessors .getConstructorAccessor (HANDLE_TYPE .getConstructors ()[0 ]);
@@ -41,14 +60,26 @@ private static Object newHandle(int index, Serializer serializer, Object value)
41
60
return constructor .invoke (index , serializer .getHandle (), value );
42
61
}
43
62
63
+ /**
64
+ * Returns the entity-type specific index of this DataValue
65
+ * @return index of the DataValue
66
+ */
44
67
public int getIndex () {
45
68
return this .modifier .<Integer >withType (int .class ).read (0 );
46
69
}
47
70
71
+ /**
72
+ * Sets the entity-type specific index of this DataValue
73
+ * @param index New index of the DataValue
74
+ */
48
75
public void setIndex (int index ) {
49
76
this .modifier .withType (int .class ).write (0 , index );
50
77
}
51
78
79
+ /**
80
+ * Returns the current serializer for this DataValue.
81
+ * @return serializer
82
+ */
52
83
public Serializer getSerializer () {
53
84
Object serializer = this .modifier .readSafely (1 );
54
85
if (serializer != null ) {
@@ -63,22 +94,42 @@ public Serializer getSerializer() {
63
94
}
64
95
}
65
96
97
+ /**
98
+ * Changes the serializer for this DataValue
99
+ * @param serializer serializer
100
+ */
66
101
public void setSerializer (Serializer serializer ) {
67
102
this .modifier .writeSafely (1 , serializer == null ? null : serializer .getHandle ());
68
103
}
69
104
105
+ /**
106
+ * Returns the current value associated and implicitly wraps it to corresponding ProtocolLib wrappers if possible.
107
+ * @return Current value
108
+ */
70
109
public Object getValue () {
71
110
return WrappedWatchableObject .getWrapped (getRawValue ());
72
111
}
73
112
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
+ */
74
117
public void setValue (Object value ) {
75
118
setRawValue (WrappedWatchableObject .getUnwrapped (value ));
76
119
}
77
120
121
+ /**
122
+ * Returns the current, raw value.
123
+ * @return Raw value (not wrapped)
124
+ */
78
125
public Object getRawValue () {
79
126
return this .modifier .readSafely (2 );
80
127
}
81
128
129
+ /**
130
+ * Updates the raw value for this DataValue. No unwrapping will be applied.
131
+ * @param value NMS value
132
+ */
82
133
public void setRawValue (Object value ) {
83
134
this .modifier .writeSafely (2 , value );
84
135
}
0 commit comments