NullPointerException在EntitytDamageByEntity事件Bukkit上

时间:2014-05-19 17:40:15

标签: java nullpointerexception bukkit

如果他们右键点击一个幽冥星,我试图让玩家不能被损坏。然后,只要int damagetime,它们就无法受到损坏。这是我的代码:

public class MineSurvival extends JavaPlugin implements Listener{

private HashMap<String, Long> lastUsage = new HashMap<String, Long>();
private HashMap<String, Integer> daysMap = new HashMap<>();
private HashMap<String, Long> damage = new HashMap<>();
private HashMap<String, Boolean> killable = new HashMap<>();
private final int cdtime = 10;
private final int damagetime = 10;
@Override
public void onEnable(){
getServer().getPluginManager().registerEvents(this, this);
getConfig();
for(String str:getConfig().getKeys(true)) {
int p = getConfig().getInt(str);
daysMap.put(str, p);
}
saveConfig();
}

@Override
public void onDisable(){
for(Entry<String, Integer> pointstostore: daysMap.entrySet()) {
getConfig().set(pointstostore.getKey(), pointstostore.getValue());
}
saveConfig();
}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    final Player player = (Player) sender;
    Integer days = this.daysMap.get(player.getName());
    PermissionUser user = PermissionsEx.getUser(player);
if(commandLabel.equalsIgnoreCase("days")){
    if(player.hasPermission("minesurvival.days") || player.hasPermission("minesurvival.hcdays")){
    long lastUsed = 0;
    if(lastUsage.containsKey(player.getName())){
        lastUsed = lastUsage.get(player.getName());
    }

    long cdmillis = System.currentTimeMillis() + (cdtime * 1000);;

    if(System.currentTimeMillis()-lastUsed>=cdmillis){
        if(daysMap.containsKey(player.getName())){
            days++;
            this.daysMap.put(player.getName(), days);
            player.sendMessage(ChatColor.RED + "You have stayed alive for " + ChatColor.BLUE + days + ChatColor.RED + " days!");
            getServer().dispatchCommand(getServer().getConsoleSender(), "pex user " + player.getName() + " prefix &c[&b" + days + "&c]");
        lastUsage.put(player.getName(), System.currentTimeMillis());

        }
        else if(daysMap.containsKey(player.getName())){
            if(days==10 || days==99){
            days++;
            this.daysMap.put(player.getName(), days);
            Bukkit.broadcastMessage(ChatColor.GREEN +(player.getDisplayName() + ChatColor.RED + "Has survived" + ChatColor.BLUE + days + ChatColor.RED + "!"));
            lastUsage.put(player.getName(), System.currentTimeMillis());
        }
        }
        else if(!daysMap.containsKey(player.getName())){
            daysMap.put(player.getName(), 0);
        }
    }
    else{
        int timeLeft = (int) (cdtime-((System.currentTimeMillis()-lastUsed)/1000));
        player.sendMessage(ChatColor.RED + "It hasn't been a day yet " + ChatColor.GREEN + timeLeft + ChatColor.RED + " seconds left");
        return true;
    }


}
    else if(!player.hasPermission("minesurvival.days")){
        player.sendMessage(ChatColor.RED + "No permission");
    }
}
if(commandLabel.equalsIgnoreCase("hardcore")){
    if(!player.hasPermission("minesurvival.normal")){
        user.addPermission("minesurvival.hardcore");
    player.sendMessage(ChatColor.RED + "You are now playing hardcore");
}
    else if(player.hasPermission("minesurvival.normal")){
        player.sendMessage(ChatColor.RED + "You cannot switch from normal to hardcore!");
    }
}
if(commandLabel.equalsIgnoreCase("normal")){
    if(!player.hasPermission("minesurvival.hardcore")){
        user.addPermission("minesurvival.normal");
    player.sendMessage(ChatColor.RED + "You are now playing normal");
    }
    else if(player.hasPermission("minesurvival.hardcore")){
        player.sendMessage(ChatColor.RED + "You cannot switch from hardcore to normal!");
    }
}
if(commandLabel.equalsIgnoreCase("timeleft")){
    if(damage.containsKey(player.getName())){
            damage.put(player.getName(), System.currentTimeMillis());

            }
        }
return false;

}
@EventHandler
public void onPlayerDeathEvent(PlayerDeathEvent event){
    Player player = (Player) event.getEntity();
    PermissionUser user = PermissionsEx.getUser(player);
    if(player instanceof Player){
        Integer daysdeath = this.daysMap.get(player.getName());
        if(player.hasPermission("minesurvival.normal")){
            Integer death = (int) Math.round((daysdeath-((daysdeath*20)/100)));
            this.daysMap.put(player.getName(), death);
            user.removePermission("minesurvival.normal");
            player.sendMessage(ChatColor.RED + "Because you were playing normal, your days have been reset to " + ChatColor.GREEN + death);
        }
        else if(player.hasPermission("minesurvival.hardcore")){
            Integer hcdeath = (int) Math.round((daysdeath-((daysdeath*80)/100)));
            this.daysMap.put(player.getName(), hcdeath);
            user.removePermission("minesurvival.hardcore");
            player.sendMessage(ChatColor.RED + "Because you were playing hardcore, your days have been reset to " + ChatColor.GREEN + hcdeath);
        }
        else{
            return;
        }
    }
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    ItemStack is = event.getItem();
    Player player = event.getPlayer();
    long dmgmillis = System.currentTimeMillis() + (damagetime * 1000);
    long DamageUsed = 0;
    if (is.getType() == Material.NETHER_STAR) {
        player.sendMessage("4");
        damage.put(player.getName(), (long) 1);
        player.getInventory().remove(Material.NETHER_STAR);
        player.sendMessage(ChatColor.GREEN + "You have activated your 10 second powerup!");
        DamageUsed = damage.get(player.getName());
        if(System.currentTimeMillis()-DamageUsed>=dmgmillis){
            player.sendMessage("3");
            killable.put(player.getName(), true);
        }
}
    else{
        return;
    }
}
@EventHandler
public void onPlayerDamage(EntityDamageByEntityEvent event){
    Entity damagedE = event.getEntity();
    Entity damagerE = event.getDamager();
    if(!(damagedE instanceof Player)) return;
    if(!(damagerE instanceof Player)) return;

    Player damaged = (Player) event.getEntity();
    if(damaged instanceof Player){
        damaged.sendMessage("2");
    if(killable.get(damaged.getName())){
        damaged.sendMessage("1");
        event.setCancelled(true);
    }
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    killable.put(player.getName(), false);
}
}

这是我得到的错误:

这是错误: http://hastebin.com/ujawequtik.avrasm

2 个答案:

答案 0 :(得分:1)

问题出在代码的第172行:

at com.minetendo.minesurvival.MineSurvival.onPlayerDamage(MineSurvival.java:172)

在您的代码中似乎是这一行:

if(killable.get(damaged.getName())){

这是因为您的killable HashMap不包含受损播放器的名称。要解决此问题,您应该检查killable.contains(damaged.getName())

所以,你的活动应该是这样的:

@EventHandler
public void onPlayerDamage(EntityDamageByEntityEvent event){
  Entity damagedE = event.getEntity();
  Entity damagerE = event.getDamager();
  if(!(damagedE instanceof Player)) return;
  if(!(damagerE instanceof Player)) return;

  Player damaged = (Player) event.getEntity();
  if(damaged instanceof Player){
    damaged.sendMessage("2");
    if(killable.contains(damaged.getName())){//check if killable contains the damaged player's name
      if(killable.get(damaged.getName())){
        damaged.sendMessage("1");
        event.setCancelled(true);
      }
    }
  }
}

答案 1 :(得分:0)

在第181行,您应检查空指针:

if(killable.get(damaged.getName()) != null)

参见例如this question了解您的版本导致异常的详细信息。