Skip to content

Commit c4c5560

Browse files
AsleeeppsovdeethUnderscoreTud
authored
Entity Potion Effect Event (SkriptLang#6532)
* Entity Potion Effect Event and event-values * Updated syntax for EvtEntityPotion, and readded the imports to EventValues. * Forgot to change e to event. * Added suggested changes :) * silly me * even sillier me (how did I manage to do that) * even sillier me (how did I manage to do that) * I don't get why the build is failing * BRO * Added type stuff, and made check a lot better * Changes * change * done * resolve conflict * Update src/main/java/ch/njol/skript/classes/data/BukkitClasses.java Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com> --------- Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>
1 parent 83808a1 commit c4c5560

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/main/java/ch/njol/skript/classes/data/BukkitClasses.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.bukkit.entity.Projectile;
6262
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
6363
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
64+
import org.bukkit.event.entity.EntityPotionEffectEvent;
6465
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
6566
import org.bukkit.event.entity.EntityTransformEvent.TransformReason;
6667
import org.bukkit.event.inventory.ClickType;
@@ -1526,6 +1527,11 @@ public String toVariableNameString(EnchantmentOffer eo) {
15261527
.name("Transform Reason")
15271528
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
15281529
.since("2.8.0"));
1530+
Classes.registerClass(new EnumClassInfo<>(EntityPotionEffectEvent.Cause.class, "entitypotioncause", "entity potion causes")
1531+
.user("(entity )?potion ?effect ?cause")
1532+
.name("Entity Potion Cause")
1533+
.description("Represents the cause of the action of a potion effect on an entity, e.g. arrow, command")
1534+
.since("INSERT VERSION"));
15291535
}
15301536

15311537
}

src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import org.bukkit.event.entity.EntityTeleportEvent;
110110
import org.bukkit.event.entity.EntityTransformEvent;
111111
import org.bukkit.event.entity.EntityTransformEvent.TransformReason;
112+
import org.bukkit.event.entity.EntityPotionEffectEvent;
112113
import org.bukkit.event.entity.FireworkExplodeEvent;
113114
import org.bukkit.event.entity.HorseJumpEvent;
114115
import org.bukkit.event.entity.ItemDespawnEvent;
@@ -178,6 +179,7 @@
178179
import org.bukkit.inventory.ItemStack;
179180
import org.bukkit.inventory.PlayerInventory;
180181
import org.bukkit.inventory.Recipe;
182+
import org.bukkit.potion.PotionEffect;
181183
import org.bukkit.potion.PotionData;
182184
import org.bukkit.potion.PotionEffectType;
183185
import org.bukkit.potion.PotionType;
@@ -548,6 +550,33 @@ public DamageCause get(final EntityDeathEvent e) {
548550
return ldc == null ? null : ldc.getCause();
549551
}
550552
}, 0);
553+
554+
// Entity Potion Effect
555+
EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffect.class, new Getter<PotionEffect, EntityPotionEffectEvent>() {
556+
@Override
557+
public PotionEffect get(EntityPotionEffectEvent event) {
558+
return event.getOldEffect();
559+
}
560+
}, EventValues.TIME_PAST);
561+
EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffect.class, new Getter<PotionEffect, EntityPotionEffectEvent>() {
562+
@Override
563+
public PotionEffect get(EntityPotionEffectEvent event) {
564+
return event.getNewEffect();
565+
}
566+
}, EventValues.TIME_NOW);
567+
EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffectType.class, new Getter<PotionEffectType, EntityPotionEffectEvent>() {
568+
@Override
569+
public PotionEffectType get(EntityPotionEffectEvent event) {
570+
return event.getModifiedType();
571+
}
572+
}, EventValues.TIME_NOW);
573+
EventValues.registerEventValue(EntityPotionEffectEvent.class, EntityPotionEffectEvent.Cause.class, new Getter<EntityPotionEffectEvent.Cause, EntityPotionEffectEvent>() {
574+
@Override
575+
public EntityPotionEffectEvent.Cause get(EntityPotionEffectEvent event) {
576+
return event.getCause();
577+
}
578+
}, EventValues.TIME_NOW);
579+
551580
// ProjectileHitEvent
552581
// ProjectileHitEvent#getHitBlock was added in 1.11
553582
if (Skript.methodExists(ProjectileHitEvent.class, "getHitBlock"))
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* This file is part of Skript.
3+
*
4+
* Skript is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Skript is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* Copyright Peter Güttinger, SkriptLang team and contributors
18+
*/
19+
package ch.njol.skript.events;
20+
21+
import ch.njol.skript.Skript;
22+
import ch.njol.skript.lang.Expression;
23+
import ch.njol.skript.lang.Literal;
24+
import ch.njol.skript.lang.SkriptEvent;
25+
import ch.njol.skript.lang.SkriptParser.ParseResult;
26+
import org.bukkit.event.Event;
27+
import org.bukkit.event.entity.EntityPotionEffectEvent;
28+
import org.bukkit.potion.PotionEffectType;
29+
30+
import javax.annotation.Nullable;
31+
32+
public class EvtEntityPotion extends SkriptEvent {
33+
34+
static {
35+
Skript.registerEvent("Entity Potion Effect", EvtEntityPotion.class, EntityPotionEffectEvent.class,
36+
"entity potion effect [modif[y|ication]] [[of] %-potioneffecttypes%] [due to %-entitypotioncause%]")
37+
.description("Called when an entity's potion effect is modified.", "This modification can include adding, removing or changing their potion effect.")
38+
.examples(
39+
"on entity potion effect modification:",
40+
"\t\tbroadcast \"A potion effect was added to %event-entity%!\" ",
41+
"",
42+
"on entity potion effect modification of night vision:")
43+
.since("INSERT VERSION");
44+
}
45+
46+
@SuppressWarnings("unchecked")
47+
private Expression<PotionEffectType> potionEffects;
48+
private Expression<EntityPotionEffectEvent.Cause> cause;
49+
50+
@Override
51+
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) {
52+
potionEffects = (Expression<PotionEffectType>) args[0];
53+
cause = (Expression<EntityPotionEffectEvent.Cause>) args[1];
54+
return true;
55+
}
56+
57+
@Override
58+
public boolean check(Event event) {
59+
EntityPotionEffectEvent potionEvent = (EntityPotionEffectEvent) event;
60+
boolean effectMatches = potionEffects == null ||
61+
(potionEvent.getOldEffect() != null && potionEffects.check(event, effectType -> effectType.equals(potionEvent.getOldEffect().getType()))) ||
62+
(potionEvent.getNewEffect() != null && potionEffects.check(event, effectType -> effectType.equals(potionEvent.getNewEffect().getType())));
63+
64+
boolean causeMatches = cause == null || cause.check(event, cause -> cause.equals(potionEvent.getCause()));
65+
66+
return effectMatches && causeMatches;
67+
}
68+
69+
70+
@Override
71+
public String toString(@Nullable Event event, boolean debug) {
72+
return "on entity potion effect modification";
73+
}
74+
}

src/main/resources/lang/default.lang

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,32 @@ environments:
22442244
the_end: end, the end
22452245
custom: custom
22462246

2247+
2248+
entity potion causes:
2249+
area_effect_cloud: enter area effect cloud
2250+
arrow: arrow infliction
2251+
attack: attack
2252+
beacon: beacon effect
2253+
command: command
2254+
conduit: conduit effect
2255+
conversion: conversion, converted
2256+
death: death
2257+
dolphin: dolphin boost
2258+
expiration: expiration, expired
2259+
food: food
2260+
illusion: illusion
2261+
milk: drinking milk
2262+
plugin: plugin
2263+
potion_drink: potion drunk, drinking potion
2264+
potion_splash: potion splash, splash potion
2265+
spider_spawn: spider spawn, spawned spider
2266+
totem: removal by resurrection
2267+
turtle_helmet: turtle helmet effect
2268+
unknown: unknown
2269+
villager_trade: villager trade
2270+
patrol_captain: pillager captain, patrol captain
2271+
wither_rose: wither rose infliction
2272+
22472273
# -- Moon Phases --
22482274
moon phases:
22492275
first_quarter: first quarter

0 commit comments

Comments
 (0)