|
| 1 | +package com.github.skriptdev.skript.plugin.elements.sections; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.hytale.utils.EntityUtils; |
| 4 | +import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration; |
| 5 | +import com.hypixel.hytale.server.core.entity.Entity; |
| 6 | +import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap; |
| 7 | +import com.hypixel.hytale.server.core.modules.entitystats.asset.EntityStatType; |
| 8 | +import com.hypixel.hytale.server.core.modules.entitystats.modifier.Modifier.ModifierTarget; |
| 9 | +import com.hypixel.hytale.server.core.modules.entitystats.modifier.StaticModifier; |
| 10 | +import com.hypixel.hytale.server.core.modules.entitystats.modifier.StaticModifier.CalculationType; |
| 11 | +import io.github.syst3ms.skriptparser.file.FileSection; |
| 12 | +import io.github.syst3ms.skriptparser.lang.CodeSection; |
| 13 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 14 | +import io.github.syst3ms.skriptparser.lang.Statement; |
| 15 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 16 | +import io.github.syst3ms.skriptparser.lang.entries.SectionConfiguration; |
| 17 | +import io.github.syst3ms.skriptparser.log.SkriptLogger; |
| 18 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 19 | +import io.github.syst3ms.skriptparser.parsing.ParserState; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +public class SecApplyStatModifier extends CodeSection { |
| 25 | + |
| 26 | + public static void register(SkriptRegistration reg) { |
| 27 | + reg.newSection(SecApplyStatModifier.class, |
| 28 | + "apply entity stat modifier to %entities%") |
| 29 | + .name("Entity Stat Modifier - Apply") |
| 30 | + .description("Applies a static modifier to the specified entities.", |
| 31 | + "**Entries**:", |
| 32 | + " - `stat` = The EntityStatType to modify (required).", |
| 33 | + " - `key` = The string key used to distinguish your modifier from others (required).", |
| 34 | + " - `value` = The numeric value to modify (required).", |
| 35 | + " - `target` = Whether to target the min/max of the stat. " + |
| 36 | + "[optional: use either `min` or `max`, default = max].", |
| 37 | + " - `type` = Whether to use additive or multiplicative calculation. " + |
| 38 | + "[optional: use either `additive` or `multiplicative`, default = additive].") |
| 39 | + .examples("apply entity stat modifier to player:", |
| 40 | + "\tstat: health", |
| 41 | + "\tkey: \"MyKey\"", |
| 42 | + "\tvalue: 5", |
| 43 | + "\ttarget: max", |
| 44 | + "\ttype: multiplicative") |
| 45 | + .since("INSERT VERSION") |
| 46 | + .register(); |
| 47 | + } |
| 48 | + |
| 49 | + private Expression<Entity> entities; |
| 50 | + |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + @Override |
| 53 | + public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull ParseContext parseContext) { |
| 54 | + this.entities = (Expression<Entity>) expressions[0]; |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + SectionConfiguration config = new SectionConfiguration.Builder() |
| 59 | + .addExpression("stat", EntityStatType.class, false) |
| 60 | + .addExpression("key", String.class, false) |
| 61 | + .addExpression("value", Number.class, false) |
| 62 | + .addOptionalKey("target") |
| 63 | + .addOptionalKey("type") |
| 64 | + .build(); |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean loadSection(@NotNull FileSection section, @NotNull ParserState parserState, @NotNull SkriptLogger logger) { |
| 68 | + return this.config.loadConfiguration(null, section, parserState, logger); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Optional<? extends Statement> walk(@NotNull TriggerContext ctx) { |
| 73 | + Optional<? extends Statement> nextStatement = getNext(); |
| 74 | + Expression<EntityStatType> statExpr = this.config.getExpression("stat", EntityStatType.class).orElse(null); |
| 75 | + if (statExpr == null) return nextStatement; |
| 76 | + |
| 77 | + Optional<? extends EntityStatType> statSingle = statExpr.getSingle(ctx); |
| 78 | + if (statSingle.isEmpty()) return nextStatement; |
| 79 | + |
| 80 | + EntityStatType stat = statSingle.get(); |
| 81 | + int statIndex = EntityStatType.getAssetMap().getIndex(stat.getId()); |
| 82 | + |
| 83 | + Expression<String> keyExpr = this.config.getExpression("key", String.class).orElse(null); |
| 84 | + if (keyExpr == null) return nextStatement; |
| 85 | + |
| 86 | + Optional<? extends String> keyOptional = keyExpr.getSingle(ctx); |
| 87 | + if (keyOptional.isEmpty()) return nextStatement; |
| 88 | + |
| 89 | + String key = keyOptional.get(); |
| 90 | + |
| 91 | + ModifierTarget target = ModifierTarget.MAX; |
| 92 | + Object targetKey = this.config.getValue("target"); |
| 93 | + if (targetKey != null && targetKey.equals("min")) { |
| 94 | + target = ModifierTarget.MIN; |
| 95 | + } |
| 96 | + |
| 97 | + CalculationType type = CalculationType.ADDITIVE; |
| 98 | + Object calcType = this.config.getValue("type"); |
| 99 | + if (calcType != null && calcType.equals("multiplicative")) { |
| 100 | + type = CalculationType.MULTIPLICATIVE; |
| 101 | + } |
| 102 | + |
| 103 | + float amount = 0; |
| 104 | + Optional<Expression<Number>> valExpr = this.config.getExpression("value", Number.class); |
| 105 | + if (valExpr.isPresent()) { |
| 106 | + Optional<? extends Number> single = valExpr.get().getSingle(ctx); |
| 107 | + if (single.isPresent()) { |
| 108 | + amount = single.get().floatValue(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + StaticModifier modifier = new StaticModifier(target, type, amount); |
| 113 | + |
| 114 | + for (Entity entity : this.entities.getArray(ctx)) { |
| 115 | + EntityStatMap component = EntityUtils.getComponent(entity, EntityStatMap.getComponentType()); |
| 116 | + if (component == null) continue; |
| 117 | + |
| 118 | + component.putModifier(statIndex, key, modifier); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + return nextStatement; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 127 | + return "apply entity stat modifier to " + this.entities.toString(ctx, debug); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments