From d69c79b0b879071554e753c712b18aaf7db58f0c Mon Sep 17 00:00:00 2001 From: Lee Date: Tue, 21 Nov 2023 16:34:26 -0800 Subject: [PATCH] disable pvp on pets --- .../lee/code/pets/listeners/PetListener.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/lee/code/pets/listeners/PetListener.java b/src/main/java/lee/code/pets/listeners/PetListener.java index 1dfa5cb..748ccd7 100644 --- a/src/main/java/lee/code/pets/listeners/PetListener.java +++ b/src/main/java/lee/code/pets/listeners/PetListener.java @@ -13,10 +13,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityPortalEvent; -import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.entity.*; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -159,4 +156,28 @@ public void onEntityPortal(EntityPortalEvent e) { if (e.isCancelled()) return; if (pets.getPetManager().isPet(e.getEntity())) e.setCancelled(true); } + + @EventHandler + public void onDamagePlayerOnPet(EntityDamageByEntityEvent e) { + if (e.getDamager() instanceof Player attacker && e.getEntity() instanceof Player victim) { + if (attacker.getVehicle() != null) { + if (pets.getPetManager().isPet(attacker.getVehicle())) e.setCancelled(true); + } + if (victim.getVehicle() != null) { + if (pets.getPetManager().isPet(victim.getVehicle())) e.setCancelled(true); + } + } + } + + @EventHandler + public void onShootPlayerOnPet(ProjectileHitEvent e) { + if (e.getEntity().getShooter() instanceof Player attacker && e.getHitEntity() instanceof Player victim) { + if (attacker.getVehicle() != null) { + if (pets.getPetManager().isPet(attacker.getVehicle())) e.setCancelled(true); + } + if (victim.getVehicle() != null) { + if (pets.getPetManager().isPet(victim.getVehicle())) e.setCancelled(true); + } + } + } }