-
-
Notifications
You must be signed in to change notification settings - Fork 416
Entity/Block Persistence + Entity Despawnable #7564
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
Efnilite
merged 9 commits into
SkriptLang:dev/feature
from
Absolutionism:dev/Persistence+Despawnable
Mar 20, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a78dcb7
Initial Commit
Absolutionism 1332d35
Remove JUnit Test
Absolutionism 76416eb
Fix Example
Absolutionism 2baf136
Update Docs
Absolutionism 92f2b50
Requested Changes
Absolutionism 3c72525
Readd "not"
Absolutionism 76b13e2
Fixxy Fix
Absolutionism 65ee4bb
Merge branch 'dev/feature' into dev/Persistence+Despawnable
Absolutionism 17f9c10
Merge branch 'dev/feature' into dev/Persistence+Despawnable
Efnilite 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
37 changes: 37 additions & 0 deletions
37
src/main/java/ch/njol/skript/conditions/CondEntityUnload.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,37 @@ | ||
| package ch.njol.skript.conditions; | ||
|
|
||
| import ch.njol.skript.conditions.base.PropertyCondition; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Examples; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import org.bukkit.entity.LivingEntity; | ||
|
|
||
| @Name("Can Despawn") | ||
| @Description({ | ||
| "Check if an entity can despawn when the chunk they're located at is unloaded.", | ||
| "More information on what and when entities despawn can be found at " | ||
| + "<a href=\"https://minecraft.wiki/w/Mob_spawning#Despawning\">reference</a>." | ||
| }) | ||
| @Examples({ | ||
| "if last spawned entity can despawn on chunk unload:", | ||
| "\tmake last spawned entity not despawn on chunk unload" | ||
| }) | ||
| @Since("INSERT VERSION") | ||
| public class CondEntityUnload extends PropertyCondition<LivingEntity> { | ||
|
|
||
| static { | ||
| register(CondEntityUnload.class, PropertyType.CAN, "despawn (on chunk unload|when far away)", "livingentities"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean check(LivingEntity entity) { | ||
| return entity.getRemoveWhenFarAway(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "despawn on chunk unload"; | ||
| } | ||
|
|
||
| } |
50 changes: 50 additions & 0 deletions
50
src/main/java/ch/njol/skript/conditions/CondIsPersistent.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,50 @@ | ||
| package ch.njol.skript.conditions; | ||
|
|
||
| import ch.njol.skript.conditions.base.PropertyCondition; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Examples; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.block.data.type.Leaves; | ||
| import org.bukkit.entity.Entity; | ||
|
|
||
| @Name("Is Persistent") | ||
| @Description({ | ||
| "Whether entities, players, or leaves are persistent.", | ||
| "Persistence of entities is whether they are retained through server restarts.", | ||
| "Persistence of leaves is whether they should decay when not connected to a log block within 6 meters.", | ||
| "Persistence of players is if the player's playerdata should be saved when they leave the server. " | ||
| + "Players' persistence is reset back to 'true' when they join the server.", | ||
| "Passengers inherit the persistence of their vehicle, meaning a persistent zombie put on a " | ||
| + "non-persistent chicken will become non-persistent. This does not apply to players.", | ||
| "By default, all entities are persistent." | ||
| }) | ||
| @Examples({ | ||
| "on spawn:", | ||
| "\tif event-entity is persistent:", | ||
| "\t\tmake event-entity not persistent" | ||
| }) | ||
| @Since("INSERT VERSION") | ||
| public class CondIsPersistent extends PropertyCondition<Object> { | ||
|
|
||
| static { | ||
| register(CondIsPersistent.class, "persistent", "entities/blocks"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean check(Object object) { | ||
| if (object instanceof Entity entity) { | ||
| return entity.isPersistent(); | ||
| } else if (object instanceof Block block && block.getBlockData() instanceof Leaves leaves) { | ||
| return leaves.isPersistent(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPropertyName() { | ||
| return "persistent"; | ||
| } | ||
|
|
||
| } | ||
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,63 @@ | ||
| package ch.njol.skript.effects; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Examples; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import ch.njol.skript.lang.Effect; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.entity.LivingEntity; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Entity Despawn") | ||
| @Description({ | ||
| "Make a living entity despawn when the chunk they're located at is unloaded.", | ||
| "Setting a custom name on a living entity automatically makes it not despawnable.", | ||
| "More information on what and when entities despawn can be found at " | ||
| + "<a href=\"https://minecraft.wiki/w/Mob_spawning#Despawning\">reference</a>." | ||
| }) | ||
| @Examples({ | ||
| "make all entities not despawnable on chunk unload", | ||
| "spawn zombie at location(0, 0, 0):", | ||
| "\tforce event-entity to not despawn when far away", | ||
| }) | ||
| @Since("INSERT VERSION") | ||
| public class EffEntityUnload extends Effect { | ||
|
|
||
| static { | ||
| Skript.registerEffect(EffEntityUnload.class, | ||
| "make %livingentities% despawn[able] (on chunk unload|when far away)", | ||
| "force %livingentities% to despawn (on chunk unload|when far away)", | ||
| "prevent %livingentities% from despawning [on chunk unload|when far away]"); | ||
| } | ||
|
|
||
| private Expression<LivingEntity> entities; | ||
| private boolean despawn; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| //noinspection unchecked | ||
| entities = (Expression<LivingEntity>) exprs[0]; | ||
| despawn = matchedPattern != 2; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void execute(Event event) { | ||
| for (LivingEntity entity : entities.getArray(event)) { | ||
| entity.setRemoveWhenFarAway(despawn); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| if (despawn) | ||
| return "make " + entities.toString(event, debug) + " despawn on chunk unload"; | ||
| return "prevent " + entities.toString(event, debug) + " from despawning on chunk unload"; | ||
| } | ||
|
|
||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,82 @@ | ||
| package ch.njol.skript.effects; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.doc.Description; | ||
| import ch.njol.skript.doc.Examples; | ||
| import ch.njol.skript.doc.Name; | ||
| import ch.njol.skript.doc.Since; | ||
| import ch.njol.skript.lang.Effect; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.block.Block; | ||
| import org.bukkit.block.data.type.Leaves; | ||
| import org.bukkit.entity.Entity; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Persistent") | ||
| @Description({ | ||
| "Make entities, players, or leaves be persistent.", | ||
| "Persistence of entities is whether they are retained through server restarts.", | ||
| "Persistence of leaves is whether they should decay when not connected to a log block within 6 meters.", | ||
| "Persistence of players is if the player's playerdata should be saved when they leave the server. " | ||
| + "Players' persistence is reset back to 'true' when they join the server.", | ||
| "Passengers inherit the persistence of their vehicle, meaning a persistent zombie put on a " | ||
| + "non-persistent chicken will become non-persistent. This does not apply to players.", | ||
| "By default, all entities are persistent." | ||
| }) | ||
| @Examples({ | ||
| "prevent all entities from persisting", | ||
| "force {_leaves} to persist", | ||
| "", | ||
| "command /kickcheater <cheater: player>:", | ||
| "\tpermission: op", | ||
| "\ttrigger:", | ||
| "\t\tprevent {_cheater} from persisting", | ||
| "\t\tkick {_cheater}" | ||
| }) | ||
| @Since("INSERT VERSION") | ||
| public class EffPersistent extends Effect { | ||
|
|
||
| static { | ||
| Skript.registerEffect(EffPersistent.class, | ||
| "make %entities/blocks% [:not] persist[ent]", | ||
| "force %entities/blocks% to [:not] persist", | ||
| "prevent %entities/blocks% from persisting"); | ||
| } | ||
|
|
||
| private Expression<?> source; | ||
| private boolean persist; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| source = exprs[0]; | ||
| if (matchedPattern < 2) { | ||
| persist = !parseResult.hasTag("not"); | ||
| } else { | ||
| persist = false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void execute(Event event) { | ||
| for (Object object : source.getArray(event)) { | ||
| if (object instanceof Entity entity) { | ||
| entity.setPersistent(persist); | ||
| } else if (object instanceof Block block && block.getBlockData() instanceof Leaves leaves) { | ||
| leaves.setPersistent(persist); | ||
| block.setBlockData(leaves); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| if (persist) | ||
| return "make " + source.toString(event, debug) + " persistent"; | ||
| return "prevent " + source.toString(event, debug) + " from persisting"; | ||
| } | ||
|
|
||
| } |
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,17 @@ | ||
| test "entity persistence": | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| spawn a zombie, a skeleton and a villager at test-location | ||
| assert all entities are persistent with "All entities should be persistent by default" | ||
| prevent all entities from persisting | ||
| assert all entities are not persistent with "Failed to make all entities not persistent" | ||
| force all entities to persist | ||
| assert all entities are persistent with "Failed to make all entities persistent" | ||
| clear all entities | ||
|
|
||
| test "leaves persistence": | ||
| set {_old} to block data of test-block | ||
| set test-block to oak leaves | ||
| prevent test-block from persisting | ||
| assert test-block is not persistent with "Failed to make leaves not persistent" | ||
| force test-block to persist | ||
| assert test-block is persistent with "Failed to make leaves persist" | ||
| set block data of test-block to {_old} | ||
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.