36 lines
1.7 KiB
Java
36 lines
1.7 KiB
Java
package me.sashegdev.blackknife;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
|
|
public class DamageUtil {
|
|
public static boolean canHurt(Player attacker, LivingEntity victim) {
|
|
if (victim == null || !victim.isValid() || victim.isDead()) return false;
|
|
if (attacker != null && attacker.getGameMode() == org.bukkit.GameMode.SPECTATOR) return false;
|
|
if (victim instanceof Player pl) {
|
|
if (pl.getGameMode() == org.bukkit.GameMode.CREATIVE || pl.getGameMode() == org.bukkit.GameMode.SPECTATOR) return false;
|
|
if (pl.isInvulnerable()) return false;
|
|
if (pl.getPersistentDataContainer().has(SwoonHandler.SWOONED, PersistentDataType.BYTE)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void apply(Player attacker, LivingEntity victim, double amount) {
|
|
if (!canHurt(attacker, victim)) return;
|
|
victim.getPersistentDataContainer().set(SwoonHandler.MAGIC_DAMAGE, PersistentDataType.BYTE, (byte) 1);
|
|
victim.damage(amount, attacker);
|
|
victim.getPersistentDataContainer().remove(SwoonHandler.MAGIC_DAMAGE);
|
|
victim.setNoDamageTicks(0);
|
|
victim.setMaximumNoDamageTicks(10);
|
|
BlackKnifePlugin.getInstance().getServer().getScheduler().runTaskLater(BlackKnifePlugin.getInstance(), () -> {
|
|
if (victim.isValid() && victim.getMaximumNoDamageTicks() == 0) victim.setMaximumNoDamageTicks(10);
|
|
}, 1L);
|
|
}
|
|
|
|
public static void applyRaw(Player attacker, LivingEntity victim, double amount) {
|
|
if (victim instanceof Player pl && pl.isBlocking()) amount /= 2.5;
|
|
apply(attacker, victim, amount);
|
|
}
|
|
}
|