package com.sklm.lhb.listener; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import com.sklm.lhb.combat.Combat;
public class DamageListener implements Listener {
public DamageListener(Combat plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
} @EventHandler
public void damage(EntityDamageByEntityEvent event) {
if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) {
boolean fb_pvp = DamageUtil.getIsPVP("FB");
Player player = (Player) event.getDamager();
if((player.getWorld().getName()).equals("FB")){
if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) {
Player player_self = (Player) event.getDamager();
boolean b = DamageUtil.isCommonGroup((Player)event.getEntity(), player_self);
String pvp_world = DamageUtil.getWorld_name();
if(b) {
event.setCancelled(true);
return ;
}else {
event.setCancelled(false);
}
}
}
if((player.getWorld().getName()).equals("ZC")) {
if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) {
if(event.getDamager() instanceof Arrow) {
event.setCancelled(true);
}
event.setCancelled(true);
return ;
}else {
event.setCancelled(false);
}
}
}else {
//检测受到的伤害是否为投射物(弓箭,枪之类的伤害)
if(event.getDamager() instanceof Projectile) {
if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) {
boolean fb_pvp = DamageUtil.getIsPVP("FB");
Player player = (Player) ((Projectile) event.getDamager()).getShooter();
if((player.getWorld().getName()).equals("FB")){
if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) {
Player player_self = (Player) ((Projectile) event.getDamager()).getShooter();
boolean b = DamageUtil.isCommonGroup((Player)event.getEntity(), player_self);
String pvp_world = DamageUtil.getWorld_name();
if(b) {
event.setCancelled(true);
return ;
}else {
event.setCancelled(false);
}
}
}
if((player.getWorld().getName()).equals("ZC")) {
if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) {
event.setCancelled(true);
return ;
}else {
event.setCancelled(false);
}
}
}
}
} } }
注意在检测玩家受到的伤害时候,近身类武器可以直接通过event.getDamager() instanceof Player 来判断是否受到伤害,但是在检测是否受到投掷类伤害的时候要通过event.getDamager instanceof Projectile来判断是否受到来自投掷类武器的伤害