Skip to content

Commit

Permalink
Arrows stuck in entities expression (#1655)
Browse files Browse the repository at this point in the history
* Create ExprArrowsStuck.java

* Improved Expression Pattern

* Create ExprArrowsStuck.sk

* Update ExprArrowsStuck.java

* Update ExprArrowsStuck.sk
  • Loading branch information
Wealthyturtle authored Feb 3, 2020
1 parent cd2abe2 commit 937c388
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprArrowsStuck.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*
* 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<LivingEntity, Number> {

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<? extends Number> getReturnType() {
return Number.class;
}

}
18 changes: 18 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprArrowsStuck.sk
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 937c388

Please sign in to comment.