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

ExprLastDamage - fix everything #6797

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
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
46 changes: 18 additions & 28 deletions src/main/java/ch/njol/skript/expressions/ExprLastDamage.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,34 @@
*/
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.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.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Last Damage")
@Description("The last damage that was done to an entity. Note that changing it doesn't deal more/less damage.")
@Examples({"set last damage of event-entity to 2"})
@Since("2.5.1")
public class ExprLastDamage extends SimplePropertyExpression<LivingEntity, Number> {

static {
register(ExprLastDamage.class, Number.class, "last damage", "livingentities");
}

@Nullable
private ExprDamage damageExpr;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
damageExpr = new ExprDamage();
return true;
}


@Nullable
@Override
@SuppressWarnings("null")
public Number convert(LivingEntity livingEntity) {
return damageExpr.get(livingEntity.getLastDamageCause())[0];
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
return livingEntity.getLastDamage() / 2;
}

@Nullable
@Override
public Class<?>[] acceptChange(ChangeMode mode) {
Expand All @@ -72,35 +59,38 @@ public Class<?>[] acceptChange(ChangeMode mode) {
return null;
}
}


@SuppressWarnings("ConstantValue")
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
if (delta != null) {
if (delta != null && delta[0] instanceof Number) {
double damage = ((Number) delta[0]).doubleValue() * 2;

switch (mode) {
case SET:
for (LivingEntity entity : getExpr().getArray(e))
entity.setLastDamage((Long) delta[0]);
entity.setLastDamage(damage);
break;
case REMOVE:
delta[0] = (Long) delta[0] * -1;
damage = damage * -1;
case ADD:
for (LivingEntity entity : getExpr().getArray(e))
entity.setLastDamage((Long) delta[0] + entity.getLastDamage());
entity.setLastDamage(damage + entity.getLastDamage());
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
assert false;
}
}
}

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

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

}