diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/cheat_protect/TamedEntityProtectManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/cheat_protect/TamedEntityProtectManager.java index 55fc42f..5a1c307 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/cheat_protect/TamedEntityProtectManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/cheat_protect/TamedEntityProtectManager.java @@ -4,7 +4,6 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; import org.bukkit.ChatColor; import org.bukkit.entity.AnimalTamer; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Tameable; import org.bukkit.event.EventHandler; @@ -31,18 +30,15 @@ public class TamedEntityProtectManager implements Listener { public void onEntityTame(EntityTameEvent event) { if (event.getEntity().getWorld().getPVP()) return; - LivingEntity entity = event.getEntity(); - AnimalTamer playerOwner = event.getOwner(); - if (entity instanceof Tameable) { - Tameable tameableEntity = (Tameable) entity; - tameableEntity.setOwner(playerOwner); - - if (playerOwner instanceof Player) { - Player p = (Player) playerOwner; - p.sendMessage(ChatColor.GREEN+"Vous venez de protéger cet animal"); - } - - } + if (!(event.getEntity() instanceof Tameable)) return; + if (!(event.getOwner() instanceof Player)) return; + + Tameable tameableEntity = (Tameable) event.getEntity(); + + tameableEntity.setOwner(event.getOwner()); + + Player p = (Player) event.getOwner(); + p.sendMessage(ChatColor.GREEN+"Vous venez de protéger cet animal"); } @@ -54,15 +50,12 @@ public class TamedEntityProtectManager implements Listener { Tameable tameableEntity = (Tameable) event.getVehicle(); - if (!tameableEntity.getOwner().equals(event.getEntered())) - { - event.setCancelled(true); - - Player p = (Player) event.getEntered(); - - p.sendMessage(ChatColor.RED+"Cet animal est à "+tameableEntity.getOwner().getName()+" vous ne pouvez pas y toucher"); - - } + if (tameableEntity.getOwner().equals(event.getEntered())) return; + + event.setCancelled(true); + Player p = (Player) event.getEntered(); + p.sendMessage(ChatColor.RED+"Cet animal est à "+getOwnerDisplayName(tameableEntity)+", vous ne pouvez pas y toucher"); + } @@ -76,11 +69,10 @@ public class TamedEntityProtectManager implements Listener { Player p = (Player) event.getDamager(); Tameable animal = (Tameable) event.getEntity(); - if (!animal.getOwner().equals(p)) { - event.setCancelled(true); - p.sendMessage(ChatColor.RED+"Cet animal est à "+animal.getOwner().getName()+" vous ne pouvez pas y toucher"); - } + if (animal.getOwner().equals(p)) return; + event.setCancelled(true); + p.sendMessage(ChatColor.RED+"Cet animal est à "+getOwnerDisplayName(animal)+", vous ne pouvez pas y toucher"); } @@ -96,6 +88,9 @@ public class TamedEntityProtectManager implements Listener { if (animal.getOwner().equals(event.getPlayer())) return; + event.setCancelled(true); + event.getPlayer().sendMessage(ChatColor.RED+"Cet animal est à "+getOwnerDisplayName(animal)+", vous ne pouvez pas y toucher"); + } @@ -112,4 +107,20 @@ public class TamedEntityProtectManager implements Listener { + + + public String getOwnerDisplayName(Tameable animal) { + if (animal == null || animal.getOwner() == null) + return null; + + AnimalTamer owner = animal.getOwner(); + if (owner instanceof Player) // si le joueur est en ligne + return ((Player) owner).getDisplayName(); + + return owner.getName(); + } + + + + }