From 937c388680e5b334f4637c64439b68f1d4803ab0 Mon Sep 17 00:00:00 2001 From: Wealthyturtle Date: Mon, 3 Feb 2020 20:56:01 +0800 Subject: [PATCH] Arrows stuck in entities expression (#1655) * Create ExprArrowsStuck.java * Improved Expression Pattern * Create ExprArrowsStuck.sk * Update ExprArrowsStuck.java * Update ExprArrowsStuck.sk --- .../skript/expressions/ExprArrowsStuck.java | 100 ++++++++++++++++++ .../syntaxes/expressions/ExprArrowsStuck.sk | 18 ++++ 2 files changed, 118 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprArrowsStuck.java create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprArrowsStuck.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprArrowsStuck.java b/src/main/java/ch/njol/skript/expressions/ExprArrowsStuck.java new file mode 100644 index 00000000000..85794e854c1 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprArrowsStuck.java @@ -0,0 +1,100 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.expressions; + +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +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.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.util.coll.CollectionUtils; + +@Name("Arrows Stuck") +@Description("The number of arrows stuck in a living entity.") +@Examples("set arrows stuck in player to 5") +@Since("INSERT VERSION") +public class ExprArrowsStuck extends SimplePropertyExpression { + + static { + if (Skript.methodExists(LivingEntity.class, "getArrowsStuck")) { + Skript.registerExpression(ExprArrowsStuck.class, Number.class, ExpressionType.PROPERTY, + "[number of] arrow[s] stuck in %livingentities%"); + } + } + + @Override + public Number convert(LivingEntity le) { + return le.getArrowsStuck(); + } + + @Override + @Nullable + public Class[] acceptChange(final ChangeMode mode) { + if (mode == ChangeMode.REMOVE_ALL) + return null; + return CollectionUtils.array(Number.class); + } + + @Override + public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) { + int d = delta == null ? 0 : ((Number) delta[0]).intValue(); + for (LivingEntity le : getExpr().getArray(e)) { + switch (mode) { + case ADD: + int r1 = le.getArrowsStuck() + d; + if (r1 < 0) r1 = 0; + le.setArrowsStuck(r1); + break; + case SET: + le.setArrowsStuck(d); + break; + case DELETE: + case RESET: + le.setArrowsStuck(0); + break; + case REMOVE: + int r2 = le.getArrowsStuck() - d; + if (r2 < 0) r2 = 0; + le.setArrowsStuck(r2); + break; + case REMOVE_ALL: + assert false; + } + } + } + + @Override + protected String getPropertyName() { + return "arrows stuck"; + } + + @Override + public Class getReturnType() { + return Number.class; + } + +} diff --git a/src/test/skript/tests/syntaxes/expressions/ExprArrowsStuck.sk b/src/test/skript/tests/syntaxes/expressions/ExprArrowsStuck.sk new file mode 100644 index 00000000000..ee22cfa5aae --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprArrowsStuck.sk @@ -0,0 +1,18 @@ +test "arrows stuck": + spawn zombie at location(0, 64, 0, world "world") + set {_m} to last spawned zombie + assert arrows stuck in {_m} is 0 with "entity spawned with arrows stuck" + set arrows stuck in {_m} to 31 + assert arrows stuck in {_m} is 31 with "arrows stuck set failed" + add 5 to arrows stuck in {_m} + assert arrows stuck in {_m} is 36 with "arrows stuck add ##1 failed" + remove 10 from arrows stuck in {_m} + assert arrows stuck in {_m} is 26 with "arrows stuck remove ##1 failed" + remove 999 from arrows stuck in {_m} + assert arrows stuck in {_m} is 0 with "arrows stuck remove ##2 failed" + remove -2 from arrows stuck in {_m} + assert arrows stuck in {_m} is 2 with "arrows stuck remove ##3 failed" + add -1 to arrows stuck in {_m} + assert arrows stuck in {_m} is 1 with "arrows stuck add ##2 failed" + delete arrows stuck in {_m} + assert arrows stuck in {_m} is 0 with "arrows stuck delete failed"