JavaPluginClass似乎不适用于Listener类

时间:2015-08-22 16:44:39

标签: java bukkit

我正在尝试像往常一样处理事件,但是这个事件并不起作用,而且我绝对没有错误。任何想法的家伙?

 public class JavaPluginClass extends JavaPlugin{

 HashMap<String, Location> games = new HashMap<String, Location>();
 HashMap<String, Location> currentLocation = new HashMap<String, Location>();
 ArrayList<String> gamesList = new ArrayList<String>();
 HashMap<String, String> attackersPlayers = new HashMap<String, String>();
 HashMap<String, String> defensePlayers = new HashMap<String, String>();
 HashMap<String, Boolean> playerJoined = new HashMap<String, Boolean>();

 //Teams:
 ScoreboardManager manager = Bukkit.getScoreboardManager();
 Scoreboard board = manager.getNewScoreboard();
 Team attack = board.registerNewTeam("Attacker");
 Team defense = board.registerNewTeam("Defense");

 StringBuilder sb = new StringBuilder();

 public void onEnable(){
        new joinOrNot(this);
  getLogger().info("Final Frontier enabled!");
 }

 public void onDisable(){
  getLogger().info("Final Frontier disabled!");
 }


 public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){

  Player p = (Player) sender;

  attack.setAllowFriendlyFire(false);
  defense.setAllowFriendlyFire(false);

  if (cmd.getName().equalsIgnoreCase("ff")){


    if (args.length == 0){
     //Help page: come back to this
     p.sendMessage("This will display help page");
     return true;
    }


   if (args.length == 1){
    if (args[0].equalsIgnoreCase("create") || args[0].equalsIgnoreCase("join")){
     p.sendMessage(ChatColor.RED + "Invalid usage! Type /ff for a bit o' help!");
     return true;
    }
    if (args[0].equalsIgnoreCase("list")){

     p.sendMessage(ChatColor.YELLOW + "Available games: " + ChatColor.GREEN + sb.toString());
     return true;

    }

    if (args[0].equalsIgnoreCase("attack") && playerJoined.get(p.getName()) == true){
     p.sendMessage(ChatColor.RED + "You are now attacking! Better watch out, eh?");
     attack.addPlayer(p);
     return true;
    }
    else if(args[0].equalsIgnoreCase("defend") && playerJoined.get(p.getName()) == true){
     p.sendMessage(ChatColor.YELLOW + "You are now defending! Whip dem weapons out!");
     defense.addPlayer(p);
     return true;
    }
    else if (args[0].equalsIgnoreCase("attack") || args[0].equalsIgnoreCase("defend") && playerJoined.get(p.getName()) == false){
     p.sendMessage(ChatColor.RED + "You should probably join first! God darn kids these days!");
     return true;
    }

    else if (args[0].equalsIgnoreCase("leave")){
     playerJoined.remove(p.getName());
     playerJoined.put(p.getName(), false);
     return true;
    }

   }

    if (args.length == 2){

     if (args[0].equalsIgnoreCase("create")){
      if(args[1] != null){
       p.sendMessage(ChatColor.GREEN + "You have created game " + args[1]);
       games.put(args[1], p.getLocation());
       sb.append(args[1] + ", ");
       return true;
      }

    }

     if (args[0].equalsIgnoreCase("join")){
      if (args[1] != null && games.containsKey(args[1])){

       playerJoined.remove(p.getName());
       playerJoined.put(p.getName(), true);
       p.teleport(games.get(args[1]));
       p.sendMessage(ChatColor.GREEN + "You have joined the battle of "
       + ChatColor.YELLOW + "Final Frontier! Now choose a god darn team!");
       return true;
      }
      if (!games.containsKey(args[1])){
       p.sendMessage(ChatColor.RED + "Woooah watch it soldier! Don't get cocky! That arena don't darn exist!");
       return true;
      }
     }

   }


  }

  return false;
 }
}

这是Listener类:

package me.katsunicalis.finalfrontier;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class joinOrNot implements Listener{

 JavaPluginClass getter;

 public joinOrNot(JavaPluginClass plugin){

  plugin.getServer().getPluginManager().registerEvents(this, plugin);

 }

 @EventHandler
 public void playerJoins(PlayerJoinEvent e){
  Player p = e.getPlayer();

  getter.playerJoined.put(p.getName(), false);
 }

}

但是,当我尝试做我正在做的事情时,即使没有任何错误,它也会向我抛出错误。

1 个答案:

答案 0 :(得分:3)

您没有在“joirOrNot”类中初始化“getter”字段。

JavaPluginClass getter;

该字段具有空值(因为您未将其设置为任何值)。加入服务器时,您的错误是NullPointerException,因为您正在从一个空字段访问一个或多个字段:

getter.playerJoined.put(p.getName(), false);

要解决此问题,只需使用以下命令初始化Listener构造函数中的字段:

this.getter = plugin;

附注:

  • 您可以将“getter”字段声明为“final”,以便IDE通知您此类错误
  • 遵循Java约定,您的类名“joinOrNot”应以大写字母开头。