-
-
Notifications
You must be signed in to change notification settings - Fork 398
Cow Variants #7866
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
Merged
APickledWalrus
merged 12 commits into
SkriptLang:dev/feature
from
Absolutionism:dev/CowVariants
Jun 30, 2025
Merged
Cow Variants #7866
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2217a53
Initial Commit
Absolutionism 4e04bae
Update CowData.java
Absolutionism 3a3d102
Update src/main/resources/lang/default.lang
Absolutionism bea6ada
Update
Absolutionism b928c2c
Merge branch 'dev/feature' into dev/CowVariants
Absolutionism c18fe4e
Merge branch 'dev/feature' into dev/CowVariants
Absolutionism 93f9474
Merge branch 'dev/feature' into dev/CowVariants
sovdeeth fb8a4ac
Update src/main/java/ch/njol/skript/entity/CowData.java
Absolutionism f382653
Merge branch 'dev/feature' into dev/CowVariants
Absolutionism 54d93ab
Pickle's Changes
Absolutionism 2cad32f
Update CowData.java
Absolutionism 59d4e2b
Merge remote-tracking branch 'upstream/dev/feature' into dev/CowVariants
Absolutionism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
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.Cow; | ||
import org.bukkit.entity.Entity; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Objects; | ||
|
||
public class CowData extends EntityData<Cow> { | ||
|
||
private static final boolean VARIANTS_ENABLED; | ||
private static final Object[] variants; | ||
private static final Class<Cow> COW_CLASS; | ||
private static final @Nullable Method getVariantMethod; | ||
private static final @Nullable Method setVariantMethod; | ||
|
||
static { | ||
Class<Cow> cowClass = null; | ||
|
||
try { | ||
//noinspection unchecked | ||
cowClass = (Class<Cow>) Class.forName("org.bukkit.entity.Cow"); | ||
} catch (Exception ignored) {} | ||
|
||
COW_CLASS = cowClass; | ||
register(CowData.class, "cow", COW_CLASS, 0, "cow"); | ||
if (Skript.classExists("org.bukkit.entity.Cow$Variant")) { | ||
VARIANTS_ENABLED = true; | ||
variants = Iterators.toArray(Classes.getExactClassInfo(Cow.Variant.class).getSupplier().get(), Cow.Variant.class); | ||
try { | ||
getVariantMethod = COW_CLASS.getDeclaredMethod("getVariant"); | ||
setVariantMethod = COW_CLASS.getDeclaredMethod("setVariant", Cow.Variant.class); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not retrieve get/set variant methods for Cow.", e); | ||
} | ||
} else { | ||
VARIANTS_ENABLED = false; | ||
variants = null; | ||
getVariantMethod = null; | ||
setVariantMethod = null; | ||
} | ||
} | ||
|
||
private @Nullable Object variant = null; | ||
|
||
public CowData() {} | ||
|
||
// TODO: When the api-version is 1.21.5, 'variant' should have the type changed to 'Cow.Variant' and reflection can be removed | ||
public CowData(@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) { // cow | ||
expr = exprs[0]; | ||
} else if (exprs[1] != null) { // calf | ||
expr = exprs[1]; | ||
} | ||
if (expr != null) | ||
//noinspection unchecked | ||
variant = ((Literal<Cow.Variant>) expr).getSingle(); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
protected boolean init(@Nullable Class<? extends Cow> entityClass, @Nullable Cow cow) { | ||
if (cow != null && VARIANTS_ENABLED) { | ||
variant = getVariant(cow); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void set(Cow entity) { | ||
if (!VARIANTS_ENABLED) | ||
return; | ||
Object finalVariant = variant != null ? variant : CollectionUtils.getRandom(variants); | ||
assert finalVariant != null; | ||
setVariant(entity, finalVariant); | ||
} | ||
|
||
@Override | ||
protected boolean match(Cow entity) { | ||
return variant == null || getVariant(entity) == variant; | ||
} | ||
|
||
@Override | ||
public Class<Cow> getType() { | ||
return COW_CLASS; | ||
} | ||
|
||
@Override | ||
public EntityData<Cow> getSuperType() { | ||
return new CowData(); | ||
} | ||
|
||
@Override | ||
protected int hashCode_i() { | ||
return Objects.hashCode(variant); | ||
} | ||
|
||
@Override | ||
protected boolean equals_i(EntityData<?> obj) { | ||
if (!(obj instanceof CowData other)) | ||
return false; | ||
return variant == other.variant; | ||
} | ||
|
||
@Override | ||
public boolean isSupertypeOf(EntityData<?> entityData) { | ||
if (!(entityData instanceof CowData other)) | ||
return false; | ||
return variant == null || variant == other.variant; | ||
} | ||
|
||
/** | ||
* Due to the addition of 'AbstractCow' and 'api-version' being '1.19' | ||
* This helper method is required in order to set the {@link #variant} of the {@link Cow} | ||
* @param cow The {@link Cow} to set the variant | ||
*/ | ||
public void setVariant(Cow cow) { | ||
setVariant(cow, variant); | ||
} | ||
|
||
/** | ||
* Due to the addition of 'AbstractCow' and 'api-version' being '1.19' | ||
* This helper method is required in order to set the {@code object} of the {@link Cow} | ||
* @param cow The {@link Cow} to set the variant | ||
* @param object The 'Cow.Variant' | ||
*/ | ||
public void setVariant(Cow cow, Object object) { | ||
if (!VARIANTS_ENABLED || setVariantMethod == null) | ||
return; | ||
Entity entity = COW_CLASS.cast(cow); | ||
try { | ||
setVariantMethod.invoke(entity, (Cow.Variant) object); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Due to the addition of 'AbstractCow' and 'api-version' being '1.19' | ||
* This helper method is required in order to get the 'Cow.Variant' of the {@link Cow} | ||
* @param cow The {@link Cow} to get the variant | ||
* @return The 'Cow.Variant' | ||
*/ | ||
public @Nullable Object getVariant(Cow cow) { | ||
if (!VARIANTS_ENABLED || getVariantMethod == null) | ||
return null; | ||
Entity entity = COW_CLASS.cast(cow); | ||
try { | ||
return getVariantMethod.invoke(entity); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* A dummy/placeholder class to ensure working operation on MC versions that do not have 'Cow.Variant' | ||
*/ | ||
public static class CowVariantDummy {} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.