-
-
Notifications
You must be signed in to change notification settings - Fork 446
Add entity size expression #7702
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
erenkarakal
merged 16 commits into
SkriptLang:dev/feature
from
Fusezion:feature/entity-size
Mar 31, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ce7a051
ExprEntitySize - Add entity size for slimes and phantoms
Fusezion ab581b4
ExprEntitySize - Add null and nan to test
Fusezion b508cfe
ExprEntitySize - Follow nbt format for slimes
Fusezion 9eef88d
ExprEntitySize.java - description change
Fusezion 1f68b48
Apply suggestions from code review
Fusezion 65be1d1
ExprEntitySize.java - deltaSize
Fusezion f421b4a
Merge remote-tracking branch 'origin/feature/entity-size' into featur…
Fusezion 9aec0cc
ExprEntitySize.java - Fix integer overflow
Fusezion 33b9c6a
ExprEntitySize.sk - Expand test cases
Fusezion 6bcd30a
ExprEntitySize.java - deltaSize -> sizeDelta
Fusezion 2921298
"I" Helped
Fusezion 261604e
Merge branch 'dev/feature' into feature/entity-size
sovdeeth 3bc4fe4
Update src/main/java/ch/njol/skript/expressions/ExprEntitySize.java
Fusezion 4d0fa90
ExprEntitySize.java - Update description to include default sizes
Fusezion 99dd536
Merge branch 'dev/feature' into feature/entity-size
Fusezion 86c45b2
Merge branch 'dev/feature' into feature/entity-size
erenkarakal 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
107 changes: 107 additions & 0 deletions
107
src/main/java/ch/njol/skript/expressions/ExprEntitySize.java
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,107 @@ | ||
| package ch.njol.skript.expressions; | ||
|
|
||
| import ch.njol.skript.classes.Changer.ChangeMode; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Example; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
| import ch.njol.util.Math2; | ||
| import ch.njol.util.coll.CollectionUtils; | ||
| import org.bukkit.entity.LivingEntity; | ||
| import org.bukkit.entity.Phantom; | ||
| import org.bukkit.entity.Slime; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Entity Size") | ||
| @Description({ | ||
| "Changes the entity size of slimes and phantoms. This is not the same as changing the scale attribute of an entity.", | ||
| "When changing the size of a slime, its health is fully resorted and will have changes done to its max health, movement speed and attack damage.", | ||
| "The default minecraft size of a slime is anywhere between 0 and 2, with a maximum of 126.", | ||
| "The default minecraft size of a phantom is 0 with a maximum size of 64." | ||
| }) | ||
| @Example(""" | ||
| spawn a slime at player: | ||
| set entity size of event-entity to 5 | ||
| set name of event-entity to "King Slime Jorg" | ||
| """) | ||
| @Since("INSERT VERSION") | ||
| public class ExprEntitySize extends SimplePropertyExpression<LivingEntity, Integer> { | ||
|
|
||
| private static final int MAXIMUM_SLIME_SIZE = 127; | ||
| private static final int MAXIMUM_PHANTOM_SIZE = 64; | ||
|
|
||
| static { | ||
| register(ExprEntitySize.class, Integer.class, "entity size", "livingentities"); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Integer convert(LivingEntity from) { | ||
| if (from instanceof Phantom phantom) { | ||
| return phantom.getSize(); | ||
| } else if (from instanceof Slime slime) { | ||
| // Skript follows the nbt format of 0-126 for slimes, as bukkit uses a 1-127 value | ||
| return slime.getSize()-1; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
| return switch (mode) { | ||
| case ADD, REMOVE, SET -> CollectionUtils.array(Number.class); | ||
| case RESET -> CollectionUtils.array(); | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
| if (delta == null && mode != ChangeMode.RESET) | ||
| return; | ||
|
|
||
| double deltaSizeDouble = delta != null ? ((Number) delta[0]).doubleValue() : 0; | ||
| if (Double.isNaN(deltaSizeDouble) || Double.isInfinite(deltaSizeDouble)) | ||
| return; | ||
| // Due to how double to int conversions happen, we are required to reassign from delta to prevent integer overflows | ||
| int sizeDelta = delta != null ? ((Number) delta[0]).intValue() : 0; | ||
| if (mode == ChangeMode.REMOVE) | ||
| sizeDelta = -sizeDelta; | ||
|
|
||
| switch (mode) { | ||
| case ADD, REMOVE -> { | ||
| for (LivingEntity entity : getExpr().getArray(event)) { | ||
| if (entity instanceof Phantom phantom) { | ||
| int newSize = Math2.fit(0, (phantom.getSize() + sizeDelta), MAXIMUM_PHANTOM_SIZE); | ||
| phantom.setSize(newSize); | ||
| } else if (entity instanceof Slime slime) { | ||
| int newSize = Math2.fit(1, (slime.getSize() + sizeDelta), MAXIMUM_SLIME_SIZE); | ||
| slime.setSize(newSize); | ||
| } | ||
| } | ||
| } | ||
| case SET, RESET -> { | ||
| for (LivingEntity entity : getExpr().getArray(event)) { | ||
| if (entity instanceof Phantom phantom) { | ||
| phantom.setSize(Math2.fit(0, sizeDelta, MAXIMUM_PHANTOM_SIZE)); | ||
| } else if (entity instanceof Slime slime) { | ||
| // Skript follows the nbt format of 0-126 for slimes, as bukkit uses a 1-127 value | ||
| slime.setSize(Math2.fit(1, sizeDelta+1, MAXIMUM_SLIME_SIZE)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends Integer> getReturnType() { | ||
| return Integer.class; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "entity size"; | ||
| } | ||
|
|
||
| } | ||
121 changes: 121 additions & 0 deletions
121
src/test/skript/tests/syntaxes/expressions/ExprEntitySize.sk
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,121 @@ | ||
|
|
||
| test "ExprEntitySize - Slimes": | ||
|
|
||
| spawn a slime at test location: | ||
| make entity invulnerable | ||
| set {_entity} to entity | ||
|
|
||
| # Changer - Set | ||
| set entity size of {_entity} to 5 | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to nan value | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to infinity value | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to -5 | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Changer - Add | ||
| add 10 to entity size of {_entity} | ||
| assert entity size of {_entity} is 10 | ||
|
|
||
| add -5 to entity size of {_entity} | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| add 5000000000 to entity size of {_entity} | ||
| assert entity size of {_entity} is 126 | ||
|
|
||
| add nan value to entity size of {_entity} | ||
| assert entity size of {_entity} is 126 | ||
|
|
||
| add infinity value to entity size of {_entity} | ||
| assert entity size of {_entity} is 126 | ||
|
|
||
| # Changer - Remove | ||
| remove 100 from entity size of {_entity} | ||
| assert entity size of {_entity} is 26 | ||
|
|
||
| remove -10 from entity size of {_entity} | ||
| assert entity size of {_entity} is 36 | ||
|
|
||
| remove nan value from entity size of {_entity} | ||
| assert entity size of {_entity} is 36 | ||
|
|
||
| remove infinity value from entity size of {_entity} | ||
| assert entity size of {_entity} is 36 | ||
|
|
||
| remove 100 from entity size of {_entity} | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Changer - Reset | ||
|
|
||
| add 3 to entity size of {_entity} | ||
| assert entity size of {_entity} is 3 | ||
|
|
||
| reset entity size of {_entity} | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Cleanup | ||
| delete entity within {_entity} | ||
|
|
||
| test "ExprEntitySize - Phantoms": | ||
| spawn a phantom at test location: | ||
| make entity invulnerable | ||
| set {_entity} to entity | ||
|
|
||
| set entity size of {_entity} to 5 | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to nan value | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to infinity value | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| set entity size of {_entity} to -5 | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Changer - Add | ||
| add 10 to entity size of {_entity} | ||
| assert entity size of {_entity} is 10 | ||
|
|
||
| add -5 to entity size of {_entity} | ||
| assert entity size of {_entity} is 5 | ||
|
|
||
| add 5000000000 to entity size of {_entity} | ||
| assert entity size of {_entity} is 64 | ||
|
|
||
| add nan value to entity size of {_entity} | ||
| assert entity size of {_entity} is 64 | ||
|
|
||
| add infinity value to entity size of {_entity} | ||
| assert entity size of {_entity} is 64 | ||
|
|
||
| # Changer - Remove | ||
| remove 40 from entity size of {_entity} | ||
| assert entity size of {_entity} is 24 | ||
|
|
||
| remove -10 from entity size of {_entity} | ||
| assert entity size of {_entity} is 34 | ||
|
|
||
| remove nan value from entity size of {_entity} | ||
| assert entity size of {_entity} is 34 | ||
|
|
||
| remove infinity value from entity size of {_entity} | ||
| assert entity size of {_entity} is 34 | ||
|
|
||
| remove 100 from entity size of {_entity} | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Changer - Reset | ||
| add 3 to entity size of {_entity} | ||
| assert entity size of {_entity} is 3 | ||
|
|
||
| reset entity size of {_entity} | ||
| assert entity size of {_entity} is 0 | ||
|
|
||
| # Cleanup | ||
| delete entity within {_entity} |
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.