Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Salmon Variants #7233

Open
wants to merge 1 commit into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/main/java/ch/njol/skript/entity/SalmonData.java
Original file line number Diff line number Diff line change
@@ -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<Salmon> {

private static final boolean SUPPORT_SALMON_VARIANTS = Skript.classExists("org.bukkit.entity.Salmon$Variant");
private static Object[] variants;

static {
List<String> 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<? extends Salmon> 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<? extends Salmon> 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;
}

}
1 change: 0 additions & 1 deletion src/main/java/ch/njol/skript/entity/SimpleEntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ private static void addSuperEntity(String codeName, Class<? extends Entity> 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);

Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk
Original file line number Diff line number Diff line change
Expand Up @@ -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::*}
Expand Down