18
18
*/
19
19
package ch .njol .skript .entity ;
20
20
21
+ import ch .njol .skript .bukkitutil .BukkitUtils ;
22
+ import ch .njol .skript .registrations .Classes ;
23
+ import ch .njol .util .coll .CollectionUtils ;
24
+ import com .google .common .collect .Iterators ;
21
25
import org .bukkit .DyeColor ;
22
26
import org .bukkit .entity .Wolf ;
23
27
import org .jetbrains .annotations .Nullable ;
24
28
29
+ import ch .njol .skript .Skript ;
25
30
import ch .njol .skript .lang .Literal ;
26
31
import ch .njol .skript .lang .SkriptParser .ParseResult ;
27
32
import ch .njol .skript .util .Color ;
28
33
34
+ import java .util .Objects ;
35
+
29
36
public class WolfData extends EntityData <Wolf > {
30
37
38
+ private static boolean variantsEnabled = false ;
39
+
31
40
static {
32
41
EntityData .register (WolfData .class , "wolf" , Wolf .class , 1 ,
33
42
"peaceful wolf" , "wolf" , "angry wolf" ,
34
43
"wild wolf" , "tamed wolf" );
44
+ if (Skript .classExists ("org.bukkit.entity.Wolf$Variant" ) && BukkitUtils .registryExists ("WOLF_VARIANT" )) {
45
+ variantsEnabled = true ;
46
+ variants = Iterators .toArray (Classes .getExactClassInfo (Wolf .Variant .class ).getSupplier ().get (), Wolf .Variant .class );
47
+ }
35
48
}
36
49
37
- @ Nullable
38
- private DyeColor collarColor ;
50
+
51
+ private static Object [] variants ;
52
+
53
+ private @ Nullable Object variant ;
54
+ private @ Nullable DyeColor collarColor ;
39
55
40
56
private int angry = 0 ;
41
57
private int tamed = 0 ;
@@ -47,8 +63,10 @@ protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parse
47
63
angry = matchedPattern - 1 ;
48
64
else
49
65
tamed = matchedPattern == 3 ? -1 : 1 ;
50
- if (exprs [0 ] != null )
51
- collarColor = ((Literal <Color >) exprs [0 ]).getSingle ().asDyeColor ();
66
+ if (exprs [0 ] != null && variantsEnabled )
67
+ variant = ((Literal <Wolf .Variant >) exprs [0 ]).getSingle ();
68
+ if (exprs [1 ] != null )
69
+ collarColor = ((Literal <Color >) exprs [1 ]).getSingle ().asDyeColor ();
52
70
return true ;
53
71
}
54
72
@@ -58,6 +76,8 @@ protected boolean init(@Nullable Class<? extends Wolf> c, @Nullable Wolf wolf) {
58
76
angry = wolf .isAngry () ? 1 : -1 ;
59
77
tamed = wolf .isTamed () ? 1 : -1 ;
60
78
collarColor = wolf .getCollarColor ();
79
+ if (variantsEnabled )
80
+ variant = wolf .getVariant ();
61
81
}
62
82
return true ;
63
83
}
@@ -70,11 +90,17 @@ public void set(Wolf entity) {
70
90
entity .setTamed (tamed == 1 );
71
91
if (collarColor != null )
72
92
entity .setCollarColor (collarColor );
93
+ Object variantSet = null ;
94
+ if (variantsEnabled ) {
95
+ variantSet = variant != null ? variant : CollectionUtils .getRandom (variants );
96
+ entity .setVariant ((Wolf .Variant ) variantSet );
97
+ }
73
98
}
74
99
75
100
@ Override
76
101
public boolean match (Wolf entity ) {
77
- return (angry == 0 || entity .isAngry () == (angry == 1 )) && (tamed == 0 || entity .isTamed () == (tamed == 1 )) && (collarColor == null ? true : entity .getCollarColor () == collarColor );
102
+ return (angry == 0 || entity .isAngry () == (angry == 1 )) && (tamed == 0 || entity .isTamed () == (tamed == 1 )) &&
103
+ (collarColor == null || entity .getCollarColor () == collarColor ) && (variant == null || entity .getVariant () == variant );
78
104
}
79
105
80
106
@ Override
@@ -88,6 +114,8 @@ protected int hashCode_i() {
88
114
result = prime * result + angry ;
89
115
result = prime * result + tamed ;
90
116
result = prime * result + (collarColor == null ? 0 : collarColor .hashCode ());
117
+ if (variantsEnabled )
118
+ result = prime * result + (variant == null ? 0 : Objects .hashCode (variant ));
91
119
return result ;
92
120
}
93
121
@@ -102,6 +130,8 @@ protected boolean equals_i(EntityData<?> obj) {
102
130
return false ;
103
131
if (collarColor != other .collarColor )
104
132
return false ;
133
+ if (variantsEnabled && variant != other .variant )
134
+ return false ;
105
135
return true ;
106
136
}
107
137
@@ -127,7 +157,7 @@ protected boolean deserialize(String s) {
127
157
public boolean isSupertypeOf (EntityData <?> entityData ) {
128
158
if (entityData instanceof WolfData ) {
129
159
WolfData wolfData = (WolfData ) entityData ;
130
- return (angry == 0 || wolfData .angry == angry ) && (tamed == 0 || wolfData .tamed == tamed ) && (wolfData .collarColor == collarColor );
160
+ return (angry == 0 || wolfData .angry == angry ) && (tamed == 0 || wolfData .tamed == tamed ) && (wolfData .collarColor == collarColor ) && (! variantsEnabled || wolfData . variant == variant ) ;
131
161
}
132
162
return false ;
133
163
}
@@ -137,4 +167,9 @@ public EntityData<Wolf> getSuperType() {
137
167
return new WolfData ();
138
168
}
139
169
170
+ /**
171
+ * A dummy/placeholder class to ensure working operation on MC versions that do not have `Wolf.Variant`
172
+ */
173
+ public static class VariantDummy {};
174
+
140
175
}
0 commit comments