From dfaacd6e46c732f66681fddcbf2d3acc261a192c Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:56:21 -0600 Subject: [PATCH] Update healing event and related expressions (#6339) * Update healing event and related expressions * Update src/main/java/ch/njol/skript/expressions/ExprHealAmount.java Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> * Update src/main/java/ch/njol/skript/events/EvtHealing.java Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> * Apply changes * Sync aliases with master * Add event to setTime * Update src/main/java/ch/njol/skript/events/EvtHealing.java --------- Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Co-authored-by: Moderocky Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> --- .../skript/classes/data/BukkitClasses.java | 11 +- .../classes/data/BukkitEventValues.java | 11 ++ .../ch/njol/skript/events/EvtHealing.java | 100 ++++++++++++++++++ .../ch/njol/skript/events/SimpleEvents.java | 5 - .../skript/expressions/ExprHealAmount.java | 75 +++++++------ .../skript/expressions/ExprHealReason.java | 71 ++++--------- 6 files changed, 180 insertions(+), 93 deletions(-) create mode 100644 src/main/java/ch/njol/skript/events/EvtHealing.java diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index a54e5a54cc4..7ff72f27e69 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1448,12 +1448,13 @@ public String toVariableNameString(FireworkEffect effect) { .since("2.4") .requiredPlugins("Minecraft 1.14 or newer")); } + Classes.registerClass(new EnumClassInfo<>(RegainReason.class, "healreason", "heal reasons") - .user("(regen|heal) (reason|cause)") - .name("Heal Reason") - .description("The heal reason in a heal event.") - .examples("") - .since("2.5")); + .user("(regen|heal) (reason|cause)") + .name("Heal Reason") + .description("The health regain reason in a heal event.") + .since("2.5")); + if (Skript.classExists("org.bukkit.entity.Cat$Type")) { ClassInfo catTypeClassInfo; if (BukkitUtils.registryExists("CAT_VARIANT")) { diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 76e098cb602..6a49566c827 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -101,6 +101,8 @@ import org.bukkit.event.entity.EntityDropItemEvent; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.EntityPickupItemEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; import org.bukkit.event.entity.EntityResurrectEvent; import org.bukkit.event.entity.EntityTameEvent; import org.bukkit.event.entity.EntityTransformEvent; @@ -1908,5 +1910,14 @@ public ItemStack get(InventoryMoveItemEvent event) { return event.getItem(); } }, EventValues.TIME_NOW); + + // EntityRegainHealthEvent + EventValues.registerEventValue(EntityRegainHealthEvent.class, RegainReason.class, new Getter() { + @Override + @Nullable + public RegainReason get(EntityRegainHealthEvent event) { + return event.getRegainReason(); + } + }, EventValues.TIME_NOW); } } diff --git a/src/main/java/ch/njol/skript/events/EvtHealing.java b/src/main/java/ch/njol/skript/events/EvtHealing.java new file mode 100644 index 00000000000..21dd2c2e642 --- /dev/null +++ b/src/main/java/ch/njol/skript/events/EvtHealing.java @@ -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 . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.events; + +import org.bukkit.entity.Entity; +import org.bukkit.event.Event; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.Skript; +import ch.njol.skript.entity.EntityData; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser.ParseResult; + +public class EvtHealing extends SkriptEvent { + + static { + Skript.registerEvent("Heal", EvtHealing.class, EntityRegainHealthEvent.class, "heal[ing] [of %-entitydatas%] [(from|due to|by) %-healreasons%]", "%entitydatas% heal[ing] [(from|due to|by) %-healreasons%]") + .description("Called when an entity is healed, e.g. by eating (players), being fed (pets), or by the effect of a potion of healing (overworld mobs) or harm (nether mobs).") + .examples( + "on heal:", + "on player healing from a regeneration potion:", + "on healing of a zombie, cow or a wither:", + "\theal reason is healing potion", + "\tcancel event" + ) + .since("1.0, INSERT VERSION (by reason)"); + } + + @Nullable + private Literal> entityDatas; + + @Nullable + private Literal healReasons; + + @Override + @SuppressWarnings("unchecked") + public boolean init(Literal[] args, int matchedPattern, ParseResult parser) { + entityDatas = (Literal>) args[0]; + healReasons = (Literal) args[1]; + return true; + } + + @Override + public boolean check(Event event) { + if (!(event instanceof EntityRegainHealthEvent)) + return false; + EntityRegainHealthEvent healthEvent = (EntityRegainHealthEvent) event; + if (entityDatas != null) { + Entity compare = healthEvent.getEntity(); + boolean result = false; + for (EntityData entityData : entityDatas.getAll()) { + if (entityData.isInstance(compare)) { + result = true; + break; + } + } + if (!result) + return false; + } + if (healReasons != null) { + RegainReason compare = healthEvent.getRegainReason(); + boolean result = false; + for (RegainReason healReason : healReasons.getAll()) { + if (healReason == compare) { + result = true; + break; + } + } + if (!result) + return false; + } + return true; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "heal" + (entityDatas != null ? " of " + entityDatas.toString(event, debug) : "") + + (healReasons != null ? " by " + healReasons.toString(event, debug) : ""); + } + +} diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index c470d68a07d..3ac8cce5e1a 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -57,7 +57,6 @@ import org.bukkit.event.entity.EntityMountEvent; import org.bukkit.event.entity.EntityPortalEnterEvent; import org.bukkit.event.entity.EntityPortalEvent; -import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityResurrectEvent; import org.bukkit.event.entity.EntityTameEvent; import org.bukkit.event.entity.EntityToggleGlideEvent; @@ -209,10 +208,6 @@ public class SimpleEvents { .description("Called when an entity enters a nether portal or an end portal. Please note that this event will be fired many times for a nether portal.") .examples("on portal enter:") .since("1.0"); - Skript.registerEvent("Heal", SimpleEvent.class, EntityRegainHealthEvent.class, "heal[ing]") - .description("Called when an entity is healed, e.g. by eating (players), being fed (pets), or by the effect of a potion of healing (overworld mobs) or harm (nether mobs).") - .examples("on heal:") - .since("1.0"); Skript.registerEvent("Tame", SimpleEvent.class, EntityTameEvent.class, "[entity] tam(e|ing)") .description("Called when a player tames a wolf or ocelot. Can be cancelled to prevent the entity from being tamed.") .examples("on tame:") diff --git a/src/main/java/ch/njol/skript/expressions/ExprHealAmount.java b/src/main/java/ch/njol/skript/expressions/ExprHealAmount.java index 9e74f6aa68e..ae905e96fd7 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprHealAmount.java +++ b/src/main/java/ch/njol/skript/expressions/ExprHealAmount.java @@ -25,6 +25,7 @@ import ch.njol.skript.Skript; import ch.njol.skript.classes.Changer; +import ch.njol.skript.classes.Changer.ChangeMode; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Events; import ch.njol.skript.doc.Examples; @@ -32,49 +33,49 @@ import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; import ch.njol.util.Kleenean; import ch.njol.util.coll.CollectionUtils; @Name("Heal Amount") -@Description("The amount of health healed in a healing event.") -@Examples({"increase heal amount by 2", - "remove 0.5 from heal amount"}) -@Since("2.5.1") +@Description("The amount of health healed in a heal event.") +@Examples({ + "on player healing:", + "\tincrease the heal amount by 2", + "\tremove 0.5 from the healing amount" +}) @Events("heal") -public class ExprHealAmount extends SimpleExpression { - +@Since("2.5.1") +public class ExprHealAmount extends SimpleExpression { + static { - Skript.registerExpression(ExprHealAmount.class, Number.class, ExpressionType.SIMPLE, "[the] heal amount"); + Skript.registerExpression(ExprHealAmount.class, Double.class, ExpressionType.SIMPLE, "[the] heal[ing] amount"); } - - @SuppressWarnings("null") + private Kleenean delay; - + @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { if (!getParser().isCurrentEvent(EntityRegainHealthEvent.class)) { - Skript.error("The expression 'heal amount' may only be used in a healing event", ErrorQuality.SEMANTIC_ERROR); + Skript.error("The expression 'heal amount' may only be used in a healing event"); return false; } delay = isDelayed; return true; } - + @Nullable @Override - protected Number[] get(Event e) { - if (!(e instanceof EntityRegainHealthEvent)) + protected Double[] get(Event event) { + if (!(event instanceof EntityRegainHealthEvent)) return null; - - return new Number[]{((EntityRegainHealthEvent) e).getAmount()}; + return new Double[]{((EntityRegainHealthEvent) event).getAmount()}; } - + @Nullable @Override - public Class[] acceptChange(Changer.ChangeMode mode) { + public Class[] acceptChange(ChangeMode mode) { if (delay != Kleenean.FALSE) { Skript.error("The heal amount cannot be changed after the event has already passed"); return null; @@ -83,42 +84,48 @@ public Class[] acceptChange(Changer.ChangeMode mode) { return null; return CollectionUtils.array(Number.class); } - + @Override - public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) { - if (!(e instanceof EntityRegainHealthEvent)) + public void change(Event event, @Nullable Object[] delta, ChangeMode mode) { + if (!(event instanceof EntityRegainHealthEvent)) return; + EntityRegainHealthEvent healthEvent = (EntityRegainHealthEvent) event; double value = delta == null ? 0 : ((Number) delta[0]).doubleValue(); switch (mode) { case SET: case DELETE: - ((EntityRegainHealthEvent) e).setAmount(value); + healthEvent.setAmount(value); break; case ADD: - ((EntityRegainHealthEvent) e).setAmount(((EntityRegainHealthEvent) e).getAmount() + value); + healthEvent.setAmount(healthEvent.getAmount() + value); break; case REMOVE: - ((EntityRegainHealthEvent) e).setAmount(((EntityRegainHealthEvent) e).getAmount() - value); + healthEvent.setAmount(healthEvent.getAmount() - value); break; default: break; } } - + + @Override + public boolean setTime(int time) { + return super.setTime(time, EntityRegainHealthEvent.class); + } + @Override public boolean isSingle() { return true; } - + @Override - public Class getReturnType() { - return Number.class; + public Class getReturnType() { + return Double.class; } - + @Override - public String toString(@Nullable Event e, boolean debug) { + public String toString(@Nullable Event event, boolean debug) { return "heal amount"; } - + } diff --git a/src/main/java/ch/njol/skript/expressions/ExprHealReason.java b/src/main/java/ch/njol/skript/expressions/ExprHealReason.java index 12dc0270976..330ec4ed4d7 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprHealReason.java +++ b/src/main/java/ch/njol/skript/expressions/ExprHealReason.java @@ -18,67 +18,40 @@ */ package ch.njol.skript.expressions; -import org.bukkit.event.Event; -import org.bukkit.event.entity.EntityRegainHealthEvent; -import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.Skript; import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Events; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; +import ch.njol.skript.expressions.base.EventValueExpression; +import ch.njol.skript.registrations.EventValues; +import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; @Name("Heal Reason") -@Description("The heal reason of a heal event. Please click on the link for more information.") -@Examples({"on heal:", - "\tif heal reason = satiated:", - "\t\tsend \"You ate enough food and gained health back!\" to player"}) +@Description("The heal reason of a heal event.") +@Examples({ + "on heal:", + "\theal reason is satiated", + "\tsend \"You ate enough food and gained full health back!\"" +}) +@Events("heal") @Since("2.5") -public class ExprHealReason extends SimpleExpression { - +public class ExprHealReason extends EventValueExpression { + static { - Skript.registerExpression(ExprHealReason.class, RegainReason.class, ExpressionType.SIMPLE, "(regen|health regain|heal) (reason|cause)"); + register(ExprHealReason.class, RegainReason.class, "(regen|health regain|heal[ing]) (reason|cause)"); } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (!getParser().isCurrentEvent(EntityRegainHealthEvent.class)) { - Skript.error("Heal reason can only be used in an on heal event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Nullable - @Override - protected RegainReason[] get(Event e) { - if (!(e instanceof EntityRegainHealthEvent)) - return null; - return new RegainReason[]{((EntityRegainHealthEvent) e).getRegainReason()}; + public ExprHealReason() { + super(RegainReason.class); } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return RegainReason.class; - } - + @Override - public String toString(@Nullable Event e, boolean debug) { - return "heal reason"; + public boolean setTime(int time) { + if (time == EventValues.TIME_FUTURE) + return false; + return super.setTime(time); } - + }