Skip to content
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

Entity/Block Persistence + Entity Despawnable #7564

Open
wants to merge 7 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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";
}

}
49 changes: 49 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,49 @@
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.",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"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.",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"Any entity riding an entity that is not persistent, will make the rider not persistent.",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"By default, all entities are persistent."
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@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";
}

}
65 changes: 65 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,65 @@
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.skript.lang.SyntaxStringBuilder;
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."
})
@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% [:not] despawn[able] (on chunk unload|when far away)",
"force %livingentities% to [:not] despawn (on chunk unload|when far away)");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

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 = !parseResult.hasTag("not");
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) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append("make", entities);
if (!despawn)
builder.append("not");
builder.append("despawn on chunk unload");
return builder.toString();
}

TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

}
80 changes: 80 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,80 @@
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.skript.lang.SyntaxStringBuilder;
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.",
"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.",
"Any entity riding an entity that is not persistent, will make the rider not persistent.",
"By default, all entities are persistent."
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples({
"make all entities not persistent",
"force {_leaves} to persist",
"",
"command /kickcheater <cheater: player>:",
"\tpermission: op",
"\ttrigger:",
"\t\tmake {_cheater} not persistent",
"\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");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<?> source;
private boolean persist;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
source = exprs[0];
persist = !parseResult.hasTag("not");
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) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append("make", source);
if (!persist)
builder.append("not");
builder.append("persistent");
return builder.toString();
}

}
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":
TheAbsolutionism 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"
make all entities not persistent
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
make test-block not persistent
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}