|
| 1 | +package fr.jamailun.ultimatespellsystem.extension.functions; |
| 2 | + |
| 3 | +import fr.jamailun.ultimatespellsystem.api.UltimateSpellSystem; |
| 4 | +import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression; |
| 5 | +import fr.jamailun.ultimatespellsystem.api.runner.SpellRuntime; |
| 6 | +import fr.jamailun.ultimatespellsystem.dsl.nodes.expressions.functions.FunctionArgument; |
| 7 | +import fr.jamailun.ultimatespellsystem.dsl.nodes.expressions.functions.FunctionType; |
| 8 | +import fr.jamailun.ultimatespellsystem.dsl.nodes.type.Duration; |
| 9 | +import fr.jamailun.ultimatespellsystem.dsl.nodes.type.TypePrimitive; |
| 10 | +import org.bukkit.NamespacedKey; |
| 11 | +import org.bukkit.attribute.Attribute; |
| 12 | +import org.bukkit.attribute.AttributeModifier; |
| 13 | +import org.bukkit.entity.LivingEntity; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +/** |
| 20 | + * Add absorption hearts. |
| 21 | + */ |
| 22 | +public class AddAbsorptionFunction extends AbstractFunction { |
| 23 | + |
| 24 | + public AddAbsorptionFunction() { |
| 25 | + super( |
| 26 | + "add_absorption", |
| 27 | + // Returns new health |
| 28 | + TypePrimitive.BOOLEAN.asType(), |
| 29 | + // Args : the entity to heal, and the amount |
| 30 | + List.of( |
| 31 | + new FunctionArgument( |
| 32 | + FunctionType.accept(TypePrimitive.ENTITY), |
| 33 | + "entity", false |
| 34 | + ), |
| 35 | + new FunctionArgument( |
| 36 | + FunctionType.accept(TypePrimitive.NUMBER), |
| 37 | + "amount", false |
| 38 | + ), |
| 39 | + new FunctionArgument( |
| 40 | + FunctionType.accept(TypePrimitive.DURATION), |
| 41 | + "duration", false |
| 42 | + ) |
| 43 | + ) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Boolean compute(@NotNull List<RuntimeExpression> arguments, @NotNull SpellRuntime runtime) { |
| 49 | + LivingEntity target = toLivingEntity("add_absorption:entity", arguments.get(0), runtime); |
| 50 | + double amount = toDouble("add_absorption:amount", arguments.get(1), runtime); |
| 51 | + Duration duration = runtime.safeEvaluate(arguments.get(2), Duration.class); |
| 52 | + if(target == null || duration == null) |
| 53 | + return false; |
| 54 | + var absorptionAttribute = target.getAttribute(Attribute.GENERIC_MAX_ABSORPTION); |
| 55 | + if(absorptionAttribute == null) |
| 56 | + return false; |
| 57 | + |
| 58 | + var absKey = new NamespacedKey("ultimate-spell-system", "add_absorption-"+ UUID.randomUUID()); |
| 59 | + var modifier = new AttributeModifier(absKey, amount, AttributeModifier.Operation.ADD_NUMBER); |
| 60 | + |
| 61 | + UltimateSpellSystem.getScheduler().run(() -> { |
| 62 | + // Add absorption hearts |
| 63 | + absorptionAttribute.addTransientModifier(modifier); |
| 64 | + target.setAbsorptionAmount(target.getAbsorptionAmount() + amount); |
| 65 | + // Remove after duration |
| 66 | + UltimateSpellSystem.getScheduler().runTaskLater(() -> { |
| 67 | + target.setAbsorptionAmount(Math.max(0, target.getAbsorptionAmount() - amount)); |
| 68 | + absorptionAttribute.removeModifier(modifier); |
| 69 | + }, duration.toTicks()); |
| 70 | + }); |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | +} |
0 commit comments