Yatopia/patches/api/0008-PlayerAttackEntityEvent.patch
2020-09-22 13:15:06 +03:00

135 lines
3.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
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..beabe65801d88e4dfc6d21ccc1faf2839a60e41a
--- /dev/null
+++ b/src/main/java/net/yatopia/api/event/PlayerAttackEntityEvent.java
@@ -0,0 +1,121 @@
+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}.
+ * <p>
+ * 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(HumanEntity attacker, Entity attacked, 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()}
+ *
+ * @return attack item
+ */
+ @NotNull
+ public ItemStack getAttackItem() {
+ return attackItem;
+ }
+
+ /**
+ * Returns whether or not the hit is a critical hit.
+ * <p>
+ * 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.
+ * <p>
+ * <b>WARNING: Critical hit is not included.</b>
+ *
+ * @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; }
+ public static HandlerList getHandlerList() { return handlers; }
+ //
+}