Bukkit插件编程之检测玩家受到的伤害是来自投射类武器还是近身武器
Posted lihuibin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bukkit插件编程之检测玩家受到的伤害是来自投射类武器还是近身武器相关的知识,希望对你有一定的参考价值。
1 package com.sklm.lhb.listener; 2 3 import org.bukkit.entity.Arrow; 4 import org.bukkit.entity.Player; 5 import org.bukkit.entity.Projectile; 6 import org.bukkit.event.EventHandler; 7 import org.bukkit.event.Listener; 8 import org.bukkit.event.entity.EntityDamageByEntityEvent; 9 10 11 import com.sklm.lhb.combat.Combat; 12 public class DamageListener implements Listener { 13 public DamageListener(Combat plugin) { 14 plugin.getServer().getPluginManager().registerEvents(this, plugin); 15 } 16 17 @EventHandler 18 public void damage(EntityDamageByEntityEvent event) { 19 if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) { 20 boolean fb_pvp = DamageUtil.getIsPVP("FB"); 21 Player player = (Player) event.getDamager(); 22 if((player.getWorld().getName()).equals("FB")){ 23 if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) { 24 Player player_self = (Player) event.getDamager(); 25 boolean b = DamageUtil.isCommonGroup((Player)event.getEntity(), player_self); 26 String pvp_world = DamageUtil.getWorld_name(); 27 if(b) { 28 event.setCancelled(true); 29 return ; 30 }else { 31 event.setCancelled(false); 32 } 33 } 34 } 35 if((player.getWorld().getName()).equals("ZC")) { 36 if((event.getDamager() instanceof Player)&&(event.getEntity() instanceof Player)) { 37 if(event.getDamager() instanceof Arrow) { 38 event.setCancelled(true); 39 } 40 event.setCancelled(true); 41 return ; 42 }else { 43 event.setCancelled(false); 44 } 45 } 46 }else { 47 //检测受到的伤害是否为投射物(弓箭,枪之类的伤害) 48 if(event.getDamager() instanceof Projectile) { 49 if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) { 50 boolean fb_pvp = DamageUtil.getIsPVP("FB"); 51 Player player = (Player) ((Projectile) event.getDamager()).getShooter(); 52 if((player.getWorld().getName()).equals("FB")){ 53 if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) { 54 Player player_self = (Player) ((Projectile) event.getDamager()).getShooter(); 55 boolean b = DamageUtil.isCommonGroup((Player)event.getEntity(), player_self); 56 String pvp_world = DamageUtil.getWorld_name(); 57 if(b) { 58 event.setCancelled(true); 59 return ; 60 }else { 61 event.setCancelled(false); 62 } 63 } 64 } 65 if((player.getWorld().getName()).equals("ZC")) { 66 if((event.getDamager() instanceof Projectile)&&(event.getEntity() instanceof Player)) { 67 event.setCancelled(true); 68 return ; 69 }else { 70 event.setCancelled(false); 71 } 72 } 73 } 74 } 75 } 76 77 } 78 79 }
注意在检测玩家受到的伤害时候,近身类武器可以直接通过event.getDamager() instanceof Player 来判断是否受到伤害,但是在检测是否受到投掷类伤害的时候要通过event.getDamager instanceof Projectile来判断是否受到来自投掷类武器的伤害
以上是关于Bukkit插件编程之检测玩家受到的伤害是来自投射类武器还是近身武器的主要内容,如果未能解决你的问题,请参考以下文章