Skip to content

Commit

Permalink
Adds some more taming syntax (SkriptLang#6850)
Browse files Browse the repository at this point in the history
* Adds tame syntax

* Adds test

* Adds Skript doc annotations

* NotSoDelayed Suggestions

Co-authored-by: NotSoDelayed <72163224+NotSoDelayed@users.noreply.github.com>

* Suggestions
- Removed 'de' as an option to untame a tameable entity
- Adds untaming to test
- Deletes value within variables instead of killing

* Fixes test

* Changed tests slightly

* Auggestion

Co-authored-by: NotSoDelayed <72163224+NotSoDelayed@users.noreply.github.com>

* Suggestions

* Changes to JetBrains nullable annotation

---------

Co-authored-by: NotSoDelayed <72163224+NotSoDelayed@users.noreply.github.com>
Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent 17c77f4 commit bc0fe53
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
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);
}

}
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"

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}

0 comments on commit bc0fe53

Please sign in to comment.