diff --git a/src/main/java/ch/njol/skript/entity/SalmonData.java b/src/main/java/ch/njol/skript/entity/SalmonData.java new file mode 100644 index 00000000000..0151f31556e --- /dev/null +++ b/src/main/java/ch/njol/skript/entity/SalmonData.java @@ -0,0 +1,94 @@ +package ch.njol.skript.entity; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.coll.CollectionUtils; +import org.bukkit.entity.Salmon; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +public class SalmonData extends EntityData { + + private static final boolean SUPPORT_SALMON_VARIANTS = Skript.classExists("org.bukkit.entity.Salmon$Variant"); + private static Object[] variants; + + static { + List patternList = new ArrayList<>(); + patternList.add("salmon"); + if (SUPPORT_SALMON_VARIANTS) { + variants = Salmon.Variant.values(); + patternList.add("any salmon"); + for (Object object : variants) { + Salmon.Variant variant = (Salmon.Variant) object; + patternList.add(variant.toString().replace("_", " ").toLowerCase(Locale.ENGLISH) + " salmon"); + } + } + + EntityData.register(SalmonData.class, "salmon", Salmon.class, 0, patternList.toArray(String[]::new)); + } + + private @Nullable Object variant; + + @Override + protected boolean init(Literal[] exprs, int matchedPattern, ParseResult parseResult) { + if (matchedPattern > 1) + variant = variants[matchedPattern - 2]; + return true; + } + + @Override + protected boolean init(@Nullable Class c, @Nullable Salmon salmon) { + if (salmon != null && SUPPORT_SALMON_VARIANTS) + variant = salmon.getVariant(); + return true; + } + + @Override + public void set(Salmon entity) { + if (SUPPORT_SALMON_VARIANTS) { + Object variantSet = variant != null ? variant : CollectionUtils.getRandom(variants); + entity.setVariant((Salmon.Variant) variantSet); + } + } + + @Override + protected boolean match(Salmon entity) { + return matchedPattern <= 1 || variant == entity.getVariant(); + } + + @Override + public Class getType() { + return Salmon.class; + } + + @Override + public EntityData getSuperType() { + return new SalmonData(); + } + + @Override + protected int hashCode_i() { + return matchedPattern <= 1 ? 0 : matchedPattern; + } + + @Override + protected boolean equals_i(EntityData obj) { + if (!(obj instanceof SalmonData salmonData)) + return false; + if (matchedPattern > 1 && variant != salmonData.variant) + return false; + return true; + } + + @Override + public boolean isSupertypeOf(EntityData entity) { + if (entity instanceof SalmonData salmonData) + return matchedPattern <= 1 || variant == salmonData.variant; + return false; + } + +} diff --git a/src/main/java/ch/njol/skript/entity/SimpleEntityData.java b/src/main/java/ch/njol/skript/entity/SimpleEntityData.java index 1143d6b9db0..59bdd3e0793 100644 --- a/src/main/java/ch/njol/skript/entity/SimpleEntityData.java +++ b/src/main/java/ch/njol/skript/entity/SimpleEntityData.java @@ -159,7 +159,6 @@ private static void addSuperEntity(String codeName, Class enti addSimpleEntity("turtle", Turtle.class); addSimpleEntity("cod", Cod.class); addSimpleEntity("puffer fish", PufferFish.class); - addSimpleEntity("salmon", Salmon.class); addSimpleEntity("tropical fish", TropicalFish.class); addSimpleEntity("trident", Trident.class); diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index fd9a449be22..a7fdfc2f745 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -1303,6 +1303,19 @@ entities: bogged: name: bogged¦s pattern: bogged[1:s] + # 1.21.2 Entities + any salmon: + name: salmon¦s + pattern: any salmon[1:s] + large salmon: + name: large salmon¦s + pattern: large salmon[1:s] + medium salmon: + name: medium salmon¦s + pattern: medium salmon[1:s] + small salmon: + name: small salmon¦s + pattern: small salmon[1:s] # 1.21.3 Entities creaking: name: creaking¦s diff --git a/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk b/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk index e829053ea07..aad1d54ef10 100644 --- a/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk +++ b/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk @@ -49,6 +49,29 @@ test "spawn wolves by variant" when running minecraft "1.21.0": delete all wolves assert size of all wolves = 0 with "Size of all wolves is greater than 0 after all were deleted" +test "spawn salmon by variant" when running minecraft "1.21.2": + delete all salmons + set {_l} to test-location + spawn 5 small salmon at {_l} + assert size of all small salmons = 5 with "Size of all small salmons is not 5" + assert size of all salmons = 5 with "Size of all salmons is not 5" + spawn 3 medium salmon at {_l} + assert size of all medium salmons = 3 with "Size of all medium salmons is not 3" + assert size of all salmons = 8 with "Size of all salmons is not 8" + spawn 2 large salmon at {_l} + assert size of all large salmons = 2 with "Size of all large salmon is not 2" + assert size of all salmons = 10 with "Size of all salmon is not 10" + delete all large salmons + assert size of all large salmons = 0 with "Large salmons did not get cleared" + delete all medium salmons + assert size of all medium salmons = 0 with "Medium salmons did not get cleared" + delete all small salmons + assert size of all small salmons = 0 with "Small salmons did not get cleared" + spawn 15 of any salmon at {_l} + assert size of all salmons = 15 with "Size of all salmons is not 15" + clear all salmons + assert size of all salmons = 0 with "All salmons did not get cleared" + test "spawn entities": set {_entities::*} to "allay", "axolotl", "bat", "bee", "blaze", "cat", "cave spider", "chicken" and "cod" add "cow", "creeper", "dolphin", "donkey", "drowned", "elder guardian", "enderman" and "endermite" to {_entities::*}