From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ivan Pekov Date: Tue, 22 Sep 2020 12:52:57 +0300 Subject: [PATCH] PlayerAttackEntityEvent Added per request diff --git a/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java b/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..7a4bd2d6518409972651373c3e6ea0fd17f8fcf7 --- /dev/null +++ b/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java @@ -0,0 +1,123 @@ +package net.yatopia.api.event; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +/** + * Called when the {@link Player} attacks a {@link Entity}. + *

+ * This event is fired earlier than {@link org.bukkit.event.entity.EntityDamageByEntityEvent} . + */ +public class PlayerAttackEntityEvent extends Event implements Cancellable { + + private boolean cancelled = false; + private final HumanEntity attacker; + private final Entity attacked; + private final ItemStack attackItem; + + private boolean criticalHit; + private float damage; + + public PlayerAttackEntityEvent(@NotNull HumanEntity attacker, @NotNull Entity attacked, @NotNull ItemStack attackItem, boolean criticalHit, float damage) { + this.attacker = attacker; + this.attacked = attacked; + this.attackItem = attackItem; + this.criticalHit = criticalHit; + this.damage = damage; + } + + /** + * Returns the attacker. + * + * @return attacker + */ + @NotNull + public HumanEntity getAttacker() { + return attacker; + } + + /** + * Returns the entity attacked. + * + * @return attacked entity + */ + @NotNull + public Entity getAttackedEntity() { + return attacked; + } + + /** + * Returns the attack item used to damage the {@link #getAttackedEntity()} + *

+ * If there wasn't any item, it would return ItemStack(Material.AIR) + * + * @return attack item + */ + @NotNull + public ItemStack getAttackItem() { + return attackItem; + } + + /** + * Returns whether or not the hit is a critical hit. + *

+ * Minecraft multiplies the damage by 1.5 if the hit is critical. + * + * @return value + */ + public boolean isCritical() { + return criticalHit; + } + + /** + * Sets if the hit is critical. The value may be ignored if the server has disabled criticals. + * + * @param critical whether or not you want the hit to be critical. + */ + public void setCritical(boolean critical) { + this.criticalHit = critical; + } + + /** + * Returns the damage which is going to be dealt. + *

+ * WARNING: Critical hit is not included. + * + * @return damage + */ + public float getDamage() { + return damage; + } + + /** + * Sets the damage the {@link #getAttackedEntity()} will receive. If the hit is a {@link #isCritical()}, then the set damage + * will be multiplied by 1.5 + * + * @param damage damage + */ + public void setDamage(float damage) { + this.damage = damage; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + // + private static final HandlerList handlers = new HandlerList(); + @NotNull @Override public HandlerList getHandlers() { return handlers; } + @NotNull public static HandlerList getHandlerList() { return handlers; } + // +}