Correction de bug dans TamedEntityProtectManager

This commit is contained in:
Marc Baloup 2015-02-12 21:07:41 -05:00
parent 86d80c893b
commit d6ee50816c

View File

@ -4,7 +4,6 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable; import org.bukkit.entity.Tameable;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -31,18 +30,15 @@ public class TamedEntityProtectManager implements Listener {
public void onEntityTame(EntityTameEvent event) { public void onEntityTame(EntityTameEvent event) {
if (event.getEntity().getWorld().getPVP()) return; if (event.getEntity().getWorld().getPVP()) return;
LivingEntity entity = event.getEntity(); if (!(event.getEntity() instanceof Tameable)) return;
AnimalTamer playerOwner = event.getOwner(); if (!(event.getOwner() instanceof Player)) return;
if (entity instanceof Tameable) {
Tameable tameableEntity = (Tameable) entity; Tameable tameableEntity = (Tameable) event.getEntity();
tameableEntity.setOwner(playerOwner);
tameableEntity.setOwner(event.getOwner());
if (playerOwner instanceof Player) {
Player p = (Player) playerOwner; Player p = (Player) event.getOwner();
p.sendMessage(ChatColor.GREEN+"Vous venez de protéger cet animal"); 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(); Tameable tameableEntity = (Tameable) event.getVehicle();
if (!tameableEntity.getOwner().equals(event.getEntered())) if (tameableEntity.getOwner().equals(event.getEntered())) return;
{
event.setCancelled(true); event.setCancelled(true);
Player p = (Player) event.getEntered();
Player p = (Player) event.getEntered(); p.sendMessage(ChatColor.RED+"Cet animal est à "+getOwnerDisplayName(tameableEntity)+", vous ne pouvez pas y toucher");
p.sendMessage(ChatColor.RED+"Cet animal est à "+tameableEntity.getOwner().getName()+" vous ne pouvez pas y toucher");
}
} }
@ -76,11 +69,10 @@ public class TamedEntityProtectManager implements Listener {
Player p = (Player) event.getDamager(); Player p = (Player) event.getDamager();
Tameable animal = (Tameable) event.getEntity(); Tameable animal = (Tameable) event.getEntity();
if (!animal.getOwner().equals(p)) { if (animal.getOwner().equals(p)) return;
event.setCancelled(true);
p.sendMessage(ChatColor.RED+"Cet animal est à "+animal.getOwner().getName()+" vous ne pouvez pas y toucher");
}
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; 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();
}
} }