Skip to content

Commit b4887d7

Browse files
Wolf Variants (#7041)
* Starter Files * Notes * Typo :) * Fixxy Fix Fixed, inability to operate on MC versions not containing 'Wolf.Variant' Added, required data/types into lang Added, test to spawn variant wolves Added, spawning a wolf without a provided variant, will choose a random one (Like Cats) * Requested Changes * Requested Changes - 2 * New Line + No Spacing * Requested Change * Conflict --------- Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
1 parent 99731b0 commit b4887d7

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

src/main/java/ch/njol/skript/classes/data/BukkitClasses.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import ch.njol.skript.classes.Serializer;
3333
import ch.njol.skript.classes.registry.RegistryClassInfo;
3434
import ch.njol.skript.entity.EntityData;
35+
import ch.njol.skript.entity.WolfData;
3536
import ch.njol.skript.expressions.ExprDamageCause;
3637
import ch.njol.skript.expressions.base.EventValueExpression;
3738
import ch.njol.skript.lang.ParseContext;
@@ -67,6 +68,7 @@
6768
import org.bukkit.enchantments.Enchantment;
6869
import org.bukkit.enchantments.EnchantmentOffer;
6970
import org.bukkit.entity.Cat;
71+
import org.bukkit.entity.Wolf;
7072
import org.bukkit.entity.Entity;
7173
import org.bukkit.entity.Item;
7274
import org.bukkit.entity.LivingEntity;
@@ -1523,12 +1525,30 @@ public String toVariableNameString(EnchantmentOffer eo) {
15231525
.name("Transform Reason")
15241526
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
15251527
.since("2.8.0"));
1526-
1528+
15271529
Classes.registerClass(new EnumClassInfo<>(EntityPotionEffectEvent.Cause.class, "entitypotioncause", "entity potion causes")
15281530
.user("(entity )?potion ?effect ?cause")
15291531
.name("Entity Potion Cause")
15301532
.description("Represents the cause of the action of a potion effect on an entity, e.g. arrow, command")
15311533
.since("INSERT VERSION"));
1534+
1535+
ClassInfo<?> wolfVariantClassInfo;
1536+
if (Skript.classExists("org.bukkit.entity.Wolf$Variant") && BukkitUtils.registryExists("WOLF_VARIANT")) {
1537+
wolfVariantClassInfo = new RegistryClassInfo<>(Wolf.Variant.class, Registry.WOLF_VARIANT, "wolfvariant", "wolf variants");
1538+
} else {
1539+
/*
1540+
* Registers a dummy/placeholder class to ensure working operation on MC versions that do not have `Wolf.Variant`
1541+
*/
1542+
wolfVariantClassInfo = new ClassInfo<>(WolfData.VariantDummy.class, "wolfvariant");
1543+
}
1544+
Classes.registerClass(wolfVariantClassInfo
1545+
.user("wolf ?variants?")
1546+
.name("Wolf Variant")
1547+
.description("Represents the variant of a wolf entity.",
1548+
"NOTE: Minecraft namespaces are supported, ex: 'minecraft:ashen'.")
1549+
.since("@VERSION")
1550+
.requiredPlugins("Minecraft 1.21+")
1551+
.documentationId("WolfVariant"));
15321552
}
15331553

15341554
}

src/main/java/ch/njol/skript/entity/WolfData.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,40 @@
1818
*/
1919
package ch.njol.skript.entity;
2020

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;
2125
import org.bukkit.DyeColor;
2226
import org.bukkit.entity.Wolf;
2327
import org.jetbrains.annotations.Nullable;
2428

29+
import ch.njol.skript.Skript;
2530
import ch.njol.skript.lang.Literal;
2631
import ch.njol.skript.lang.SkriptParser.ParseResult;
2732
import ch.njol.skript.util.Color;
2833

34+
import java.util.Objects;
35+
2936
public class WolfData extends EntityData<Wolf> {
3037

38+
private static boolean variantsEnabled = false;
39+
3140
static {
3241
EntityData.register(WolfData.class, "wolf", Wolf.class, 1,
3342
"peaceful wolf", "wolf", "angry wolf",
3443
"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+
}
3548
}
3649

37-
@Nullable
38-
private DyeColor collarColor;
50+
51+
private static Object[] variants;
52+
53+
private @Nullable Object variant;
54+
private @Nullable DyeColor collarColor;
3955

4056
private int angry = 0;
4157
private int tamed = 0;
@@ -47,8 +63,10 @@ protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parse
4763
angry = matchedPattern - 1;
4864
else
4965
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();
5270
return true;
5371
}
5472

@@ -58,6 +76,8 @@ protected boolean init(@Nullable Class<? extends Wolf> c, @Nullable Wolf wolf) {
5876
angry = wolf.isAngry() ? 1 : -1;
5977
tamed = wolf.isTamed() ? 1 : -1;
6078
collarColor = wolf.getCollarColor();
79+
if (variantsEnabled)
80+
variant = wolf.getVariant();
6181
}
6282
return true;
6383
}
@@ -70,11 +90,17 @@ public void set(Wolf entity) {
7090
entity.setTamed(tamed == 1);
7191
if (collarColor != null)
7292
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+
}
7398
}
7499

75100
@Override
76101
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);
78104
}
79105

80106
@Override
@@ -88,6 +114,8 @@ protected int hashCode_i() {
88114
result = prime * result + angry;
89115
result = prime * result + tamed;
90116
result = prime * result + (collarColor == null ? 0 : collarColor.hashCode());
117+
if (variantsEnabled)
118+
result = prime * result + (variant == null ? 0 : Objects.hashCode(variant));
91119
return result;
92120
}
93121

@@ -102,6 +130,8 @@ protected boolean equals_i(EntityData<?> obj) {
102130
return false;
103131
if (collarColor != other.collarColor)
104132
return false;
133+
if (variantsEnabled && variant != other.variant)
134+
return false;
105135
return true;
106136
}
107137

@@ -127,7 +157,7 @@ protected boolean deserialize(String s) {
127157
public boolean isSupertypeOf(EntityData<?> entityData) {
128158
if (entityData instanceof WolfData) {
129159
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);
131161
}
132162
return false;
133163
}
@@ -137,4 +167,9 @@ public EntityData<Wolf> getSuperType() {
137167
return new WolfData();
138168
}
139169

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+
140175
}

src/main/resources/lang/default.lang

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,19 +813,19 @@ entities:
813813
pattern: wither skull((|1¦s)| projectile(|1¦s))
814814
wolf:
815815
name: wol¦f¦ves
816-
pattern: <age> wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
816+
pattern: <age> [%-wolfvariant%] wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
817817
tamed wolf:
818818
name: tamed wol¦f¦ves
819-
pattern: (<age> dog(|1¦s)|tamed <age> wol(f|1¦ves)|(4¦)pupp(y|1¦ies)) [[with collar] colo[u]r[ed] %-color%]
819+
pattern: (<age> [%-wolfvariant%] dog(|1¦s)|tamed <age> [%-wolfvariant%] wol(f|1¦ves)| [%-wolfvariant%] (4¦)pupp(y|1¦ies)) [[with collar] colo[u]r[ed] %-color%]
820820
wild wolf:
821821
name: wild wol¦f¦ves
822-
pattern: (wild|untamed) <age> wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
822+
pattern: (wild|untamed) <age> [%-wolfvariant%] wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
823823
angry wolf:
824824
name: angry wol¦f¦ves @an
825-
pattern: (angry|aggressive) <age> wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
825+
pattern: (angry|aggressive) <age> [%-wolfvariant%] wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
826826
peaceful wolf:
827827
name: peaceful wol¦f¦ves
828-
pattern: (peaceful|neutral|unaggressive) <age> wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
828+
pattern: (peaceful|neutral|unaggressive) <age> [%-wolfvariant%] wol(f|1¦ves) [[with collar] colo[u]r[ed] %-color%]
829829
zombie:
830830
name: zombie¦s
831831
pattern: <age> zombie(|1¦s)|(4¦)zombie (kid(|1¦s)|child(|1¦ren))
@@ -1312,6 +1312,18 @@ cat types:
13121312
jellie: jellie
13131313
all_black: all black
13141314

1315+
# -- Wolf Variants --
1316+
wolf variants:
1317+
ashen: ashen
1318+
black: black
1319+
chestnut: chestnut
1320+
pale: pale
1321+
rusty: rusty
1322+
snowy: snowy
1323+
spotted: spotted
1324+
striped: striped
1325+
woods: woods
1326+
13151327
# -- Damage Causes --
13161328
damage causes:
13171329
contact: contact
@@ -2415,6 +2427,7 @@ types:
24152427
blockdata: block data¦s @a
24162428
healreason: heal reason¦s @a
24172429
cattype: cat type¦s @a
2430+
wolfvariant: wolf variant¦s @a
24182431
gamerule: gamerule¦s @a
24192432
attributetype: attribute type¦s @a
24202433
enchantmentoffer: enchantment offer¦s @an

src/test/skript/tests/syntaxes/effects/EffSpawn.sk

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,18 @@ test "spawn cats by type" when running minecraft "1.15.2":
1010
delete all siamese cats
1111
assert size of all cats = 5 with "Size of all cats is not 5 after delete 2 siamese cats"
1212
delete all cats
13-
assert size of all cats = 0 with "Size of all cats is greater than 0 after all were deleted"
13+
assert size of all cats = 0 with "Size of all cats is greater than 0 after all were deleted"
14+
15+
test "spawn wolves by variant" when running minecraft "1.21.0":
16+
delete all wolves
17+
set {_l} to location of spawn of world "world"
18+
spawn 5 ashen wolves at {_l}
19+
assert size of all wolves = 5 with "Size of all wolves is not 5"
20+
assert size of all ashen wolves = 5 with "Size of all ashen wolves is not 5"
21+
spawn 2 rusty wolves at {_l}
22+
assert size of all wolves = 7 with "Size of all wolves is not 7"
23+
assert size of all rusty wolves = 2 with "Size of all rusty wolves is not 2"
24+
delete all rusty wolves
25+
assert size of all wolves = 5 with "Size of all wolves is not 5 after delete 2 rusty wolves"
26+
delete all wolves
27+
assert size of all wolves = 0 with "Size of all wolves is greater than 0 after all were deleted"

0 commit comments

Comments
 (0)