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

Adds some more taming syntax #6850

Merged
merged 11 commits into from
Aug 15, 2024
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
34 changes: 34 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsTamed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.Entity;
import org.bukkit.entity.Tameable;

@Name("Is Tamed")
@Description("Check if a tameable entity is tamed (horse, parrot, cat, etc.).")
@Examples({
"send true if {_horse} is tamed",
"tame {_horse} if {_horse} is untamed"
})
@Since("INSERT VERSION")
public class CondIsTamed extends PropertyCondition<Entity> {

static {
register(CondIsTamed.class, "(tamed|domesticated)", "entities");
}

@Override
public boolean check(Entity entity) {
return (entity instanceof Tameable) && ((Tameable) entity).isTamed();
}

@Override
protected String getPropertyName() {
return "tamed";
}

}
54 changes: 54 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffTame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.Entity;
import org.bukkit.entity.Tameable;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Tame / Untame")
@Description("Tame a tameable entity (horse, parrot, cat, etc.).")
@Examples({
"tame {_horse}",
"untame {_horse}"
})
@Since("INSERT VERSION")
public class EffTame extends Effect {

static {
Skript.registerEffect(EffTame.class, "[:un](tame|domesticate) %entities%");
}

private boolean tame;
private Expression<Entity> entities;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
tame = !parseResult.hasTag("un");
entities = (Expression<Entity>) exprs[0];
return true;
}

@Override
protected void execute(Event event) {
for (Entity entity : entities.getArray(event)) {
if (entity instanceof Tameable)
((Tameable) entity).setTamed(tame);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (tame ? "tame " : "untame ") + entities.toString(event, debug);
}
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

}
21 changes: 21 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsTamed.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test "is tamed":
spawn a horse at (spawn of world "world"):
set {_e} to entity
spawn an adult zombie at (spawn of world "world"):
set {_z} to entity

assert {_e} is not tamed with "a normally spawned horse should not be tamed"

tame {_e}
assert {_e} is tamed with "taming a horse should do exactly that"
cheeezburga marked this conversation as resolved.
Show resolved Hide resolved

untame {_e}
assert {_e} is not tamed with "untaming a horse should do exactly that"

tame {_}
tame {_z}
assert {_} is not tamed with "taming null should do nothing"
assert {_z} is not tamed with "taming an invalid entity should do nothing"

delete entity within {_e}
delete entity within {_z}