1
1
package com .comphenix .protocol .wrappers ;
2
2
3
+ import java .lang .reflect .Constructor ;
4
+ import java .util .Collection ;
5
+ import java .util .Collections ;
6
+ import java .util .HashSet ;
7
+ import java .util .Set ;
8
+ import java .util .UUID ;
9
+ import javax .annotation .Nonnull ;
10
+ import javax .annotation .Nullable ;
11
+
3
12
import com .comphenix .protocol .PacketType ;
4
13
import com .comphenix .protocol .events .PacketContainer ;
5
14
import com .comphenix .protocol .reflect .FuzzyReflection ;
9
18
import com .comphenix .protocol .utility .MinecraftVersion ;
10
19
import com .comphenix .protocol .wrappers .collection .CachedSet ;
11
20
import com .comphenix .protocol .wrappers .collection .ConvertedSet ;
21
+
12
22
import com .google .common .base .Objects ;
13
23
import com .google .common .base .Preconditions ;
14
24
import com .google .common .collect .Sets ;
15
25
16
- import javax .annotation .Nonnull ;
17
- import javax .annotation .Nullable ;
18
- import java .lang .reflect .Constructor ;
19
- import java .util .*;
20
-
21
26
/**
22
27
* Represents a single attribute sent in packet 44.
23
28
*
24
29
* @author Kristian
25
30
*/
26
- public class WrappedAttribute extends AbstractWrapper {
27
- public static boolean KEY_WRAPPED = MinecraftVersion .NETHER_UPDATE .atOrAbove ();
28
- public static boolean IS_STATIC = MinecraftVersion .CAVES_CLIFFS_1 .atOrAbove ();
29
- public static boolean IS_IN_HOLDER = MinecraftVersion .v1_20_5 .atOrAbove ();
31
+ public final class WrappedAttribute extends AbstractWrapper {
32
+ static final boolean KEY_WRAPPED = MinecraftVersion .NETHER_UPDATE .atOrAbove ();
33
+ static final boolean IS_STATIC = MinecraftVersion .CAVES_CLIFFS_1 .atOrAbove ();
34
+ static final boolean IS_IN_HOLDER = MinecraftVersion .v1_20_5 .atOrAbove ();
30
35
31
36
// Shared structure modifier
32
- private static StructureModifier <Object > ATTRIBUTE_MODIFIER ;
37
+ static StructureModifier <Object > ATTRIBUTE_MODIFIER ;
33
38
34
39
// The one constructor
35
- private static Constructor <?> ATTRIBUTE_CONSTRUCTOR ;
40
+ static Constructor <?> ATTRIBUTE_CONSTRUCTOR ;
36
41
37
42
/**
38
43
* Remaps attributes if needed.
39
44
*
40
45
* @return the remapped attribute or the attribute itself if no remapping was necessary.
41
46
*/
42
- private static String remap (String string ) {
47
+ static String remap (String string ) {
43
48
switch (string ) {
44
49
case "generic.maxHealth" : return "generic.max_health" ;
45
50
case "generic.followRange" : return "generic.follow_range" ;
@@ -55,11 +60,7 @@ private static String remap(String string) {
55
60
}
56
61
}
57
62
58
- /**
59
- * Reference to the underlying attribute snapshot.
60
- */
61
- protected Object handle ;
62
- protected StructureModifier <Object > modifier ;
63
+ StructureModifier <Object > modifier ;
63
64
64
65
// Cached computed value
65
66
private double computedValue = Double .NaN ;
@@ -72,7 +73,7 @@ private static String remap(String string) {
72
73
*
73
74
* @param handle - the NMS instance.
74
75
*/
75
- private WrappedAttribute (@ Nonnull Object handle ) {
76
+ WrappedAttribute (@ Nonnull Object handle ) {
76
77
super (MinecraftReflection .getAttributeSnapshotClass ());
77
78
setHandle (handle );
78
79
@@ -83,7 +84,6 @@ private WrappedAttribute(@Nonnull Object handle) {
83
84
this .modifier = ATTRIBUTE_MODIFIER .withTarget (handle );
84
85
}
85
86
86
-
87
87
/**
88
88
* Construct a new wrapped attribute around a specific NMS instance.
89
89
*
@@ -205,7 +205,7 @@ public PacketContainer getParentPacket() {
205
205
* @return TRUE if it does, FALSE otherwise.
206
206
*/
207
207
public boolean hasModifier (UUID id ) {
208
- return getModifiers (). contains ( WrappedAttributeModifier . newBuilder ( id ). build ()) ;
208
+ return getModifierByUUID ( id ) != null ;
209
209
}
210
210
211
211
/**
@@ -214,14 +214,14 @@ public boolean hasModifier(UUID id) {
214
214
* @param id - the id to look for.
215
215
* @return The single attribute modifier with the given ID.
216
216
*/
217
+ @ Nullable
217
218
public WrappedAttributeModifier getModifierByUUID (UUID id ) {
218
- if (hasModifier (id )) {
219
- for (WrappedAttributeModifier modifier : getModifiers ()) {
220
- if (Objects .equal (modifier .getUUID (), id )) {
221
- return modifier ;
222
- }
219
+ for (WrappedAttributeModifier modifier : getModifiers ()) {
220
+ if (Objects .equal (modifier .getUUID (), id )) {
221
+ return modifier ;
223
222
}
224
223
}
224
+
225
225
return null ;
226
226
}
227
227
@@ -283,7 +283,7 @@ public boolean equals(Object obj) {
283
283
public int hashCode () {
284
284
if (attributeModifiers == null )
285
285
getModifiers ();
286
- return Objects .hashCode (getAttributeKey (), getBaseValue (), attributeModifiers );
286
+ return Objects .hashCode (getAttributeKey (), getBaseValue (), attributeModifiers . hashCode () );
287
287
}
288
288
289
289
/**
@@ -365,7 +365,7 @@ public static class Builder {
365
365
private PacketContainer packet ;
366
366
private Collection <WrappedAttributeModifier > modifiers = Collections .emptyList ();
367
367
368
- private Builder (WrappedAttribute template ) {
368
+ Builder (WrappedAttribute template ) {
369
369
if (template != null ) {
370
370
baseValue = template .getBaseValue ();
371
371
attributeKey = template .getAttributeKey ();
@@ -431,7 +431,7 @@ public Builder packet(PacketContainer packet) {
431
431
* @return Unwrapped modifiers.
432
432
*/
433
433
private Set <Object > getUnwrappedModifiers () {
434
- final Set <Object > output = new HashSet <>();
434
+ Set <Object > output = new HashSet <>();
435
435
436
436
for (WrappedAttributeModifier modifier : modifiers ) {
437
437
output .add (modifier .getHandle ());
@@ -476,7 +476,9 @@ public WrappedAttribute build() {
476
476
throw new IllegalArgumentException ("Invalid attribute name: " + this .attributeKey );
477
477
}
478
478
479
- attributeKey = registry .getHolder (attributeKey );
479
+ if (IS_IN_HOLDER ) {
480
+ attributeKey = registry .getHolder (attributeKey );
481
+ }
480
482
} else {
481
483
attributeKey = this .attributeKey ;
482
484
}
0 commit comments