Skip to content

Chicken Variants #7868

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

Open
wants to merge 9 commits 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
21 changes: 21 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ch.njol.skript.bukkitutil.SkriptTeleportFlag;
import ch.njol.skript.classes.*;
import ch.njol.skript.classes.registry.RegistryClassInfo;
import ch.njol.skript.entity.ChickenData.ChickenVariantDummy;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.entity.PigData.PigVariantDummy;
import ch.njol.skript.entity.WolfData.WolfVariantDummy;
Expand Down Expand Up @@ -1577,6 +1578,26 @@ public String toVariableNameString(WorldBorder border) {
.requiredPlugins("Minecraft 1.21.5+")
.documentationId("PigVariant"));

ClassInfo<?> chickenVariantClassInfo = getRegistryClassInfo(
"org.bukkit.entity.Chicken$Variant",
"CHICKEN_VARIANT",
"chickenvariant",
"chicken variants"
);
if (chickenVariantClassInfo == null) {
// Registers a dummy/placeholder class to ensure working operation on MC versions that do not have 'Chicken.Variant' (1.21.4-)
chickenVariantClassInfo = new ClassInfo<>(ChickenVariantDummy.class, "chickenvariant");
}
Classes.registerClass(chickenVariantClassInfo
.user("chicken ?variants?")
.name("Chicken Variant")
.description("Represents the variant of a chicken entity.",
"NOTE: Minecraft namespaces are supported, ex: 'minecraft:warm'.")
.since("INSERT VERSION")
.requiredPlugins("Minecraft 1.21.5+")
.documentationId("ChickenVariant")
);

Classes.registerClass(new EnumClassInfo<>(VillagerCareerChangeEvent.ChangeReason.class, "villagercareerchangereason", "villager career change reasons")
.user("(villager )?career ?change ?reasons?")
.name("Villager Career Change Reason")
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/ch/njol/skript/entity/ChickenData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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.skript.registrations.Classes;
import ch.njol.util.coll.CollectionUtils;
import com.google.common.collect.Iterators;
import org.bukkit.entity.Chicken;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class ChickenData extends EntityData<Chicken> {

private static final boolean VARIANTS_ENABLED;
private static final Object[] variants;

static {
register(ChickenData.class, "chicken", Chicken.class, "chicken");
if (Skript.classExists("org.bukkit.entity.Chicken$Variant")) {
VARIANTS_ENABLED = true;
variants = Iterators.toArray(Classes.getExactClassInfo(Chicken.Variant.class).getSupplier().get(), Chicken.Variant.class);
} else {
VARIANTS_ENABLED = false;
variants = null;
}
}

private @Nullable Object variant = null;

public ChickenData() {}

// TODO: When safe, 'variant' should have the type changed to 'Chicken.Variant'
public ChickenData(@Nullable Object variant) {
this.variant = variant;
}

@Override
protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
if (VARIANTS_ENABLED) {
Literal<?> expr = null;
if (exprs[0] != null) { // chicken
expr = exprs[0];
} else if (exprs[1] != null) { // chicks
expr = exprs[1];
}
if (expr != null)
//noinspection unchecked
variant = ((Literal<Chicken.Variant>) expr).getSingle();
}
return true;
}

@Override
protected boolean init(@Nullable Class<? extends Chicken> entityClass, @Nullable Chicken chicken) {
if (chicken != null) {
if (VARIANTS_ENABLED)
variant = chicken.getVariant();
}
return true;
}

@Override
public void set(Chicken chicken) {
if (VARIANTS_ENABLED) {
Object finalVariant = variant != null ? variant : CollectionUtils.getRandom(variants);
assert finalVariant != null;
chicken.setVariant((Chicken.Variant) finalVariant);
}
}

@Override
protected boolean match(Chicken chicken) {
return variant == null || variant == chicken.getVariant();
}

@Override
public Class<? extends Chicken> getType() {
return Chicken.class;
}

@Override
public EntityData<Chicken> getSuperType() {
return new ChickenData();
}

@Override
protected int hashCode_i() {
return Objects.hashCode(variant);
}

@Override
protected boolean equals_i(EntityData<?> obj) {
if (!(obj instanceof ChickenData other))
return false;
return variant == other.variant;
}

@Override
public boolean isSupertypeOf(EntityData<?> entityData) {
if (!(entityData instanceof ChickenData other))
return false;
return variant == null || variant == other.variant;
}

/**
* A dummy/placeholder class to ensure working operation on MC versions that do not have `Chicken.Variant`
*/
public static class ChickenVariantDummy {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ private static void addSuperEntity(String codeName, Class<? extends Entity> enti
addSimpleEntity("spectral arrow", SpectralArrow.class);
addSimpleEntity("tipped arrow", TippedArrow.class);
addSimpleEntity("blaze", Blaze.class);
addSimpleEntity("chicken", Chicken.class);
addSimpleEntity("mooshroom", MushroomCow.class);
addSimpleEntity("cow", Cow.class);
addSimpleEntity("cave spider", CaveSpider.class);
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ entities:
pattern: blaze(|1¦s)
chicken:
name: chicken¦s
pattern: <age> chicken(|1¦s)|(4¦)chick(|1¦s)
pattern: <age> [%-chickenvariant%] chicken[1:s]|(4:)[%-chickenvariant%] chick[1:s]
cow:
name: cow¦s
pattern: <age> cow(|1¦s)|(4¦)cal(f|1¦ves)
Expand Down Expand Up @@ -2592,6 +2592,12 @@ pig variants:
temperate: temperate
warm: warm

# -- Chicken Variants --
chicken variants:
cold: cold
temperate: temperate
warm: warm

# -- Villager Career Change Reasons
villager career change reasons:
employed: employment
Expand Down Expand Up @@ -2701,6 +2707,7 @@ types:
fishingstate: fishing state¦s @a
equipmentslot: equipment slot¦s @an
pigvariant: pig variant¦s @a
chickenvariant: chicken variant¦s @a
villagercareerchangereason: villager career change reason¦s @a

# Skript
Expand Down
42 changes: 42 additions & 0 deletions src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,45 @@ test "spawn pig by variant" when running minecraft "1.21.5":
assert size of all pigs is 5 with "Only temperate pigs should have been deleted"
delete all warm pigs
assert size of all warm pigs is 0 with "Warm pigs were not deleted"

test "spawn chicken by variant" when running minecraft "1.21.5":
delete all chickens
set {_l} to test-location
spawn 5 warm chickens at {_l}
assert size of all warm chickens is 5 with "Size of all warm chickens is not 5"
assert size of all chickens (entity type) is 5 with "Size of all chickens is not 5"
spawn 3 temperate chickens at {_l}
assert size of all temperate chickens is 3 with "Size of all temperate chickens is not 3"
assert size of all chickens (entity type) is 8 with "Size of all chickens is not 8"
spawn 2 cold chickens at {_l}
assert size of all cold chickens is 2 with "Size of all cold chickens is not 2"
assert size of all chickens (entity type) is 10 with "Size of all chickens is not 10"
delete all cold chickens
assert size of all cold chickens is 0 with "Cold chickens were not deleted"
assert size of all chickens (entity type) is 8 with "Only cold chickens should have been deleted"
delete all temperate chickens
assert size of all temperate chickens is 0 with "Temperate chickens were not deleted"
assert size of all chickens (entity type) is 5 with "Only temperate chickens should have been deleted"
delete all warm chickens
assert size of all warm chickens is 0 with "Warm chickens were not deleted"

test "spawn baby chicken by variant" when running minecraft "1.21.5":
delete all chicks
set {_l} to test-location
spawn 5 warm chicks at {_l}
assert size of all warm chicks is 5 with "Size of all warm chicks is not 5"
assert size of all chicks is 5 with "Size of all chicks is not 5"
spawn 3 temperate chicks at {_l}
assert size of all temperate chicks is 3 with "Size of all temperate chicks is not 3"
assert size of all chicks is 8 with "Size of all chicks is not 8"
spawn 2 cold chicks at {_l}
assert size of all cold chicks is 2 with "Size of all cold chicks is not 2"
assert size of all chicks is 10 with "Size of all chicks is not 10"
delete all cold chicks
assert size of all cold chicks is 0 with "Cold chicks were not deleted"
assert size of all chicks is 8 with "Only cold chicks should have been deleted"
delete all temperate chicks
assert size of all temperate chicks is 0 with "Temperate chicks were not deleted"
assert size of all chicks is 5 with "Only temperate chicks should have been deleted"
delete all warm chicks
assert size of all warm chicks is 0 with "Warm chicks were not deleted"