Correction de bug de la protection des animaux apprivoisés

This commit is contained in:
Marc Baloup 2015-02-12 14:38:11 -05:00
parent c88da20337
commit 86d80c893b

View File

@ -4,15 +4,14 @@ 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.Entity;
import org.bukkit.entity.LivingEntity; 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.entity.Vehicle;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityTameEvent; import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent; import org.bukkit.event.vehicle.VehicleEnterEvent;
public class TamedEntityProtectManager implements Listener { public class TamedEntityProtectManager implements Listener {
@ -51,13 +50,20 @@ public class TamedEntityProtectManager implements Listener {
public void onVehicleEnter(VehicleEnterEvent event) { public void onVehicleEnter(VehicleEnterEvent event) {
if (event.getVehicle().getWorld().getPVP()) return; if (event.getVehicle().getWorld().getPVP()) return;
Vehicle vehicle = event.getVehicle(); if (!isConcerned(event.getEntered(), event.getVehicle())) return;
if (!(vehicle instanceof Tameable)) return;
Tameable tameableEntity = (Tameable) vehicle; Tameable tameableEntity = (Tameable) event.getVehicle();
if (!tameableEntity.getOwner().equals(event.getEntered())) if (!tameableEntity.getOwner().equals(event.getEntered()))
{
event.setCancelled(true); event.setCancelled(true);
Player p = (Player) event.getEntered();
p.sendMessage(ChatColor.RED+"Cet animal est à "+tameableEntity.getOwner().getName()+" vous ne pouvez pas y toucher");
}
} }
@ -65,19 +71,45 @@ public class TamedEntityProtectManager implements Listener {
public void onEntityDamage(EntityDamageByEntityEvent event) { public void onEntityDamage(EntityDamageByEntityEvent event) {
if (event.getEntity().getWorld().getPVP()) return; if (event.getEntity().getWorld().getPVP()) return;
Entity damager = event.getDamager(); if (!isConcerned(event.getDamager(), event.getEntity())) return;
if (!(damager instanceof Player)) return;
Entity target = event.getEntity();
if (!(target instanceof Tameable)) return;
Player p = (Player) damager; Player p = (Player) event.getDamager();
Tameable animal = (Tameable) target; Tameable animal = (Tameable) event.getEntity();
if (!animal.getOwner().equals(p)) if (!animal.getOwner().equals(p)) {
event.setCancelled(true); event.setCancelled(true);
p.sendMessage(ChatColor.RED+"Cet animal est à "+animal.getOwner().getName()+" vous ne pouvez pas y toucher");
}
} }
@EventHandler
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (event.getPlayer().getWorld().getPVP()) return;
if (!isConcerned(event.getPlayer(), event.getRightClicked())) return;
Tameable animal = (Tameable) event.getRightClicked();
if (animal.getOwner().equals(event.getPlayer())) return;
}
private boolean isConcerned(Object player, Object tameable) {
if (!(player instanceof Player)) return false;
if (!(tameable instanceof Tameable)) return false;
if (((Tameable)tameable).getOwner() == null) return false;
return true;
}
} }