Skip to content
Merged
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
37 changes: 37 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondEntityUnload.java
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 src/main/java/ch/njol/skript/conditions/CondIsPersistent.java
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";
}

}
63 changes: 63 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffEntityUnload.java
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";
}

}
82 changes: 82 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffPersistent.java
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";
}

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffPersistent.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "entity persistence":
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}