Skip to content

Commit

Permalink
Add EntityPotionEffectEvent (#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 authored Jul 9, 2024
1 parent a82f451 commit 90ba7a5
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 31 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ tasks {
// Backwards compatible jar directory
destinationDirectory.set(file("$projectDir/target"))
archiveClassifier.set("")

exclude("javax/annotation/**")
}

runShadow {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
import cn.nukkit.entity.item.*;
import cn.nukkit.entity.projectile.EntityArrow;
import cn.nukkit.entity.projectile.EntityThrownTrident;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.event.entity.*;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.event.entity.EntityDamageEvent.DamageModifier;
import cn.nukkit.event.entity.ProjectileLaunchEvent;
import cn.nukkit.event.inventory.InventoryCloseEvent;
import cn.nukkit.event.inventory.InventoryPickupArrowEvent;
import cn.nukkit.event.inventory.InventoryPickupItemEvent;
Expand Down Expand Up @@ -4340,7 +4337,7 @@ protected void respawn() {
this.deadTicks = 0;
this.noDamageTicks = 60;

this.removeAllEffects();
this.removeAllEffects(EntityPotionEffectEvent.Cause.DEATH);
this.setHealth(this.getMaxHealth());
this.getFoodData().setLevel(20, 20);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/blockentity/BlockEntityBeacon.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockID;
import cn.nukkit.event.entity.EntityPotionEffectEvent;
import cn.nukkit.inventory.BeaconInventory;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.level.format.FullChunk;
Expand Down Expand Up @@ -121,7 +122,7 @@ public boolean onUpdate() {
e.setVisible(false);

//Add the effect
p.addEffect(e);
p.addEffect(e, EntityPotionEffectEvent.Cause.BEACON);
}

//If we have a secondary power as regen, apply it
Expand All @@ -139,7 +140,7 @@ public boolean onUpdate() {
e.setVisible(false);

//Add effect
p.addEffect(e);
p.addEffect(e, EntityPotionEffectEvent.Cause.BEACON);
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/cn/nukkit/command/defaults/EffectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
import cn.nukkit.command.data.CommandParameter;
import cn.nukkit.event.entity.EntityPotionEffectEvent;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.potion.Effect;
import cn.nukkit.potion.InstantEffect;
Expand Down Expand Up @@ -62,9 +63,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
return true;
}
if (args[1].equalsIgnoreCase("clear")) {
for (Effect effect : player.getEffects().values()) {
player.removeEffect(effect.getId());
}
player.removeAllEffects(EntityPotionEffectEvent.Cause.COMMAND);
sender.sendMessage(new TranslationContainer("commands.effect.success.removed.all", player.getDisplayName()));
return true;
}
Expand Down Expand Up @@ -117,11 +116,11 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
}
return true;
}
player.removeEffect(effect.getId());
player.removeEffect(effect.getId(), EntityPotionEffectEvent.Cause.COMMAND);
sender.sendMessage(new TranslationContainer("commands.effect.success.removed", effect.getName(), player.getDisplayName()));
} else {
effect.setDuration(duration).setAmplifier(amplification);
player.addEffect(effect);
player.addEffect(effect, EntityPotionEffectEvent.Cause.COMMAND);
Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.effect.success", effect.getName(), String.valueOf(effect.getAmplifier()), player.getDisplayName(), String.valueOf(effect.getDuration() / 20)));
}
return true;
Expand Down
58 changes: 49 additions & 9 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ protected void initEntity() {

effect.setAmplifier(e.getByte("Amplifier")).setDuration(e.getInt("Duration")).setVisible(e.getBoolean("ShowParticles"));

this.addEffect(effect);
this.addEffect(effect, null); // No event
}
}

Expand Down Expand Up @@ -743,15 +743,35 @@ public Map<Integer, Effect> getEffects() {
}

public void removeAllEffects() {
this.removeAllEffects(EntityPotionEffectEvent.Cause.UNKNOWN);
}

public void removeAllEffects(EntityPotionEffectEvent.Cause cause) {
for (Effect effect : this.effects.values()) {
this.removeEffect(effect.getId());
this.removeEffect(effect.getId(), cause);
}
}

public void removeEffect(int effectId) {
this.removeEffect(effectId, EntityPotionEffectEvent.Cause.UNKNOWN);
}

public void removeEffect(int effectId, EntityPotionEffectEvent.Cause cause) {
if (this.effects.containsKey(effectId)) {
Effect effect = this.effects.get(effectId);

if (cause != null) {
EntityPotionEffectEvent event =
new EntityPotionEffectEvent(this, effect, null, EntityPotionEffectEvent.Action.REMOVED, cause);

this.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
}

this.effects.remove(effectId);

effect.remove(this);

this.recalculateEffectColor();
Expand All @@ -767,8 +787,28 @@ public boolean hasEffect(int effectId) {
}

public void addEffect(Effect effect) {
this.addEffect(effect, EntityPotionEffectEvent.Cause.UNKNOWN);
}

public void addEffect(Effect effect, EntityPotionEffectEvent.Cause cause) {
if (effect == null) {
return; //here add null means add nothing
return;
}

if (cause != null) {
Effect oldEffect = this.effects.get(effect.getId());

EntityPotionEffectEvent event = new EntityPotionEffectEvent(
this,
oldEffect,
effect,
oldEffect == null ? EntityPotionEffectEvent.Action.ADDED : EntityPotionEffectEvent.Action.CHANGED,
cause);

this.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
}

effect.add(this);
Expand Down Expand Up @@ -1150,12 +1190,12 @@ public boolean attack(EntityDamageEvent source) {
this.getLevel().addLevelEvent(this, LevelEventPacket.EVENT_SOUND_TOTEM);

this.extinguish();
this.removeAllEffects();
this.removeAllEffects(EntityPotionEffectEvent.Cause.TOTEM);
this.setHealth(1);

this.addEffect(Effect.getEffect(Effect.REGENERATION).setDuration(800).setAmplifier(1));
this.addEffect(Effect.getEffect(Effect.FIRE_RESISTANCE).setDuration(800));
this.addEffect(Effect.getEffect(Effect.ABSORPTION).setDuration(100).setAmplifier(1));
this.addEffect(Effect.getEffect(Effect.REGENERATION).setDuration(800).setAmplifier(1), EntityPotionEffectEvent.Cause.TOTEM);
this.addEffect(Effect.getEffect(Effect.FIRE_RESISTANCE).setDuration(800), EntityPotionEffectEvent.Cause.TOTEM);
this.addEffect(Effect.getEffect(Effect.ABSORPTION).setDuration(100).setAmplifier(1), EntityPotionEffectEvent.Cause.TOTEM);

EntityEventPacket pk = new EntityEventPacket();
pk.eid = this.getId();
Expand Down Expand Up @@ -1353,7 +1393,7 @@ public boolean entityBaseTick(int tickDiff) {
this.justCreated = false;

if (!this.isAlive()) {
this.removeAllEffects();
this.removeAllEffects(EntityPotionEffectEvent.Cause.DEATH);
this.despawnFromAll();
if (!this.isPlayer) {
this.close();
Expand All @@ -1375,7 +1415,7 @@ public boolean entityBaseTick(int tickDiff) {
effect.setDuration(effect.getDuration() - tickDiff);

if (effect.getDuration() <= 0) {
this.removeEffect(effect.getId());
this.removeEffect(effect.getId(), EntityPotionEffectEvent.Cause.EXPIRATION);
}
}
}
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/cn/nukkit/event/entity/EntityPotionEffectEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package cn.nukkit.event.entity;

import cn.nukkit.entity.Entity;
import cn.nukkit.event.Cancellable;
import cn.nukkit.event.HandlerList;
import cn.nukkit.potion.Effect;
import lombok.Getter;

import javax.annotation.Nullable;

/**
* Called when a potion effect is modified on an entity.
* If the event is cancelled, no change will be made on the entity.
*/
public class EntityPotionEffectEvent extends EntityEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();

public static HandlerList getHandlers() {
return handlers;
}

/**
* Gets the old potion effect of the changed type, which will be removed. Null if Action == ADDED.
*/
@Getter
@Nullable
private final Effect oldEffect;
/**
* Gets new potion effect of the changed type to be applied. Null if Action == REMOVED.
*/
@Getter
@Nullable
private final Effect newEffect;
/**
* Gets the action which will be performed on the potion effect type.
*/
@Getter
private final Action action;
/**
* Gets the cause why the effect has changed.
*/
@Getter
private final Cause cause;

public EntityPotionEffectEvent(Entity entity, Effect oldEffect, Effect newEffect, Action action, Cause cause) {
this.entity = entity;
this.oldEffect = oldEffect;
this.newEffect = newEffect;
this.action = action;
this.cause = cause;
}

/**
* An enum to specify the action to be performed.
*/
public enum Action {

/**
* When the potion effect is added because the entity didn't have it's type.
*/
ADDED,
/**
* When the entity already had the potion effect type, but the effect is changed.
*/
CHANGED,
/**
* When the potion effect type is completely removed.
*/
REMOVED
}

/**
* An enum to specify the cause why an effect was changed.
*/
public enum Cause {

/**
* When the entity stands inside an area effect cloud.
*/
AREA_EFFECT_CLOUD,
/**
* When the entity is hit by an spectral or tipped arrow.
*/
ARROW,
/**
* When the entity is inflicted with a potion effect due to an entity
* attack (e.g. a cave spider or a shulker bullet).
*/
ATTACK,
/**
* When an entity gets the effect from an axolotl.
*/
AXOLOTL,
/**
* When beacon effects get applied due to the entity being nearby.
*/
BEACON,
/**
* When a potion effect is changed due to the /effect command.
*/
COMMAND,
/**
* When the entity gets the effect from a conduit.
*/
CONDUIT,
/**
* When a conversion from a villager zombie to a villager is started or
* finished.
*/
CONVERSION,
/**
* When all effects are removed due to death (Note: This is called on
* respawn, so it's player only!)
*/
DEATH,
/**
* When the entity gets the effect from a dolphin.
*/
DOLPHIN,
/**
* When the effect was removed due to expiration.
*/
EXPIRATION,
/**
* When an effect is inflicted due to food (e.g. when a player eats or a
* cookie is given to a parrot).
*/
FOOD,
/**
* When all effects are removed due to a bucket of milk.
*/
MILK,
/**
* When a player gets bad omen after killing a patrol captain.
*/
PATROL_CAPTAIN,
/**
* When a potion effect is modified through the plugin methods.
*/
PLUGIN,
/**
* When the entity drinks a potion.
*/
POTION_DRINK,
/**
* When the entity is inflicted with an effect due to a splash potion.
*/
POTION_SPLASH,
/**
* When a spider gets effects when spawning on hard difficulty.
*/
SPIDER_SPAWN,
/**
* When the entity gets effects from a totem item saving it's life.
*/
TOTEM,
/**
* When the entity gets water breathing by wearing a turtle helmet.
*/
TURTLE_HELMET,
/**
* When the Cause is missing.
*/
UNKNOWN,
/**
* When a villager gets regeneration after a trade.
*/
VILLAGER_TRADE,
/**
* When an entity gets the effect from a warden.
*/
WARDEN,
/**
* When an entity comes in contact with a wither rose.
*/
WITHER_ROSE,
/**
* When nearby elder guardian gives mining fatigue to player.
*/
ELDER_GUARDIAN
}
}
Loading

0 comments on commit 90ba7a5

Please sign in to comment.