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 ba0a66a1f41..94b5fe789c4 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -61,6 +61,7 @@ import org.bukkit.entity.Projectile; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; import org.bukkit.event.entity.EntityTransformEvent.TransformReason; import org.bukkit.event.inventory.ClickType; @@ -1526,6 +1527,11 @@ public String toVariableNameString(EnchantmentOffer eo) { .name("Transform Reason") .description("Represents a transform reason of an entity transform event.") .since("2.8.0")); + Classes.registerClass(new EnumClassInfo<>(EntityPotionEffectEvent.Cause.class, "entitypotioncause", "entity potion causes") + .user("(entity )?potion ?effect ?cause") + .name("Entity Potion Cause") + .description("Represents the cause of the action of a potion effect on an entity, e.g. arrow, command") + .since("INSERT VERSION")); } } 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 82fca450068..c53810d5d69 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -109,6 +109,7 @@ import org.bukkit.event.entity.EntityTeleportEvent; import org.bukkit.event.entity.EntityTransformEvent; import org.bukkit.event.entity.EntityTransformEvent.TransformReason; +import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.entity.FireworkExplodeEvent; import org.bukkit.event.entity.HorseJumpEvent; import org.bukkit.event.entity.ItemDespawnEvent; @@ -178,6 +179,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.Recipe; +import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; @@ -548,6 +550,33 @@ public DamageCause get(final EntityDeathEvent e) { return ldc == null ? null : ldc.getCause(); } }, 0); + + // Entity Potion Effect + EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffect.class, new Getter() { + @Override + public PotionEffect get(EntityPotionEffectEvent event) { + return event.getOldEffect(); + } + }, EventValues.TIME_PAST); + EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffect.class, new Getter() { + @Override + public PotionEffect get(EntityPotionEffectEvent event) { + return event.getNewEffect(); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffectType.class, new Getter() { + @Override + public PotionEffectType get(EntityPotionEffectEvent event) { + return event.getModifiedType(); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(EntityPotionEffectEvent.class, EntityPotionEffectEvent.Cause.class, new Getter() { + @Override + public EntityPotionEffectEvent.Cause get(EntityPotionEffectEvent event) { + return event.getCause(); + } + }, EventValues.TIME_NOW); + // ProjectileHitEvent // ProjectileHitEvent#getHitBlock was added in 1.11 if (Skript.methodExists(ProjectileHitEvent.class, "getHitBlock")) diff --git a/src/main/java/ch/njol/skript/events/EvtEntityPotion.java b/src/main/java/ch/njol/skript/events/EvtEntityPotion.java new file mode 100644 index 00000000000..ab56287ef2b --- /dev/null +++ b/src/main/java/ch/njol/skript/events/EvtEntityPotion.java @@ -0,0 +1,74 @@ +/** + * 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 ch.njol.skript.Skript; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import org.bukkit.event.Event; +import org.bukkit.event.entity.EntityPotionEffectEvent; +import org.bukkit.potion.PotionEffectType; + +import javax.annotation.Nullable; + +public class EvtEntityPotion extends SkriptEvent { + + static { + Skript.registerEvent("Entity Potion Effect", EvtEntityPotion.class, EntityPotionEffectEvent.class, + "entity potion effect [modif[y|ication]] [[of] %-potioneffecttypes%] [due to %-entitypotioncause%]") + .description("Called when an entity's potion effect is modified.", "This modification can include adding, removing or changing their potion effect.") + .examples( + "on entity potion effect modification:", + "\t\tbroadcast \"A potion effect was added to %event-entity%!\" ", + "", + "on entity potion effect modification of night vision:") + .since("INSERT VERSION"); + } + + @SuppressWarnings("unchecked") + private Expression potionEffects; + private Expression cause; + + @Override + public boolean init(Literal[] args, int matchedPattern, ParseResult parseResult) { + potionEffects = (Expression) args[0]; + cause = (Expression) args[1]; + return true; + } + + @Override + public boolean check(Event event) { + EntityPotionEffectEvent potionEvent = (EntityPotionEffectEvent) event; + boolean effectMatches = potionEffects == null || + (potionEvent.getOldEffect() != null && potionEffects.check(event, effectType -> effectType.equals(potionEvent.getOldEffect().getType()))) || + (potionEvent.getNewEffect() != null && potionEffects.check(event, effectType -> effectType.equals(potionEvent.getNewEffect().getType()))); + + boolean causeMatches = cause == null || cause.check(event, cause -> cause.equals(potionEvent.getCause())); + + return effectMatches && causeMatches; + } + + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "on entity potion effect modification"; + } +} diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index fec454c2ac5..8b249d6c6b4 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -2244,6 +2244,32 @@ environments: the_end: end, the end custom: custom + +entity potion causes: + area_effect_cloud: enter area effect cloud + arrow: arrow infliction + attack: attack + beacon: beacon effect + command: command + conduit: conduit effect + conversion: conversion, converted + death: death + dolphin: dolphin boost + expiration: expiration, expired + food: food + illusion: illusion + milk: drinking milk + plugin: plugin + potion_drink: potion drunk, drinking potion + potion_splash: potion splash, splash potion + spider_spawn: spider spawn, spawned spider + totem: removal by resurrection + turtle_helmet: turtle helmet effect + unknown: unknown + villager_trade: villager trade + patrol_captain: pillager captain, patrol captain + wither_rose: wither rose infliction + # -- Moon Phases -- moon phases: first_quarter: first quarter