Skip to content

Commit

Permalink
Entity Potion Effect Event (#6532)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Sep 22, 2024
1 parent 83808a1 commit c4c5560
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1526,6 +1527,11 @@ public String toVariableNameString(EnchantmentOffer eo) {
.name("Transform Reason")
.description("Represents a transform reason of an <a href='events.html#entity transform'>entity transform event</a>.")
.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"));
}

}
29 changes: 29 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<PotionEffect, EntityPotionEffectEvent>() {
@Override
public PotionEffect get(EntityPotionEffectEvent event) {
return event.getOldEffect();
}
}, EventValues.TIME_PAST);
EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffect.class, new Getter<PotionEffect, EntityPotionEffectEvent>() {
@Override
public PotionEffect get(EntityPotionEffectEvent event) {
return event.getNewEffect();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(EntityPotionEffectEvent.class, PotionEffectType.class, new Getter<PotionEffectType, EntityPotionEffectEvent>() {
@Override
public PotionEffectType get(EntityPotionEffectEvent event) {
return event.getModifiedType();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(EntityPotionEffectEvent.class, EntityPotionEffectEvent.Cause.class, new Getter<EntityPotionEffectEvent.Cause, EntityPotionEffectEvent>() {
@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"))
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtEntityPotion.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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<PotionEffectType> potionEffects;
private Expression<EntityPotionEffectEvent.Cause> cause;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) {
potionEffects = (Expression<PotionEffectType>) args[0];
cause = (Expression<EntityPotionEffectEvent.Cause>) 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";
}
}
26 changes: 26 additions & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4c5560

Please sign in to comment.