Skip to content

Commit

Permalink
Create ExprFallenDistance.java (#3158)
Browse files Browse the repository at this point in the history
  • Loading branch information
fednelpat authored Jul 20, 2020
1 parent c5cf4e5 commit 7b68e48
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprFallDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

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.util.coll.CollectionUtils;

@Name("Fall Distance")
@Description({"The distance an entity has fallen for."})
@Examples({"set all entities' fall distance to 10",
"on damage:",
"\tsend \"%victim's fall distance%\" to victim"})
@Since("INSERT VERSION")
public class ExprFallDistance extends SimplePropertyExpression<Entity, Number> {

static {
register(ExprFallDistance.class, Number.class, "[the] fall[en] (distance|height)", "entities");
}

@Nullable
@Override
public Number convert(Entity entity) {
return entity.getFallDistance();
}

@Nullable
@Override
public Class<?>[] acceptChange(ChangeMode mode) {
return (mode == ChangeMode.RESET || mode == ChangeMode.REMOVE_ALL || mode == ChangeMode.DELETE) ? null : CollectionUtils.array(Number.class);
}

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
if (delta != null) {
Entity[] entities = getExpr().getArray(e);
if (entities.length < 1)
return;
Float number = ((Number) delta[0]).floatValue();
for (Entity entity : entities) {

Float fallDistance = entity.getFallDistance();

switch (mode) {
case ADD:
entity.setFallDistance(fallDistance + number);
break;
case SET:
entity.setFallDistance(number);
break;
case REMOVE:
entity.setFallDistance(fallDistance - number);
break;
default:
assert false;
}
}
}
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

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

}

0 comments on commit 7b68e48

Please sign in to comment.