Solved Nickname Command null promblem.

Discussion in 'Plugin Development' started by MCJoshua345, Mar 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    MCJoshua345

    Ok, so I have a nickname command, and this is the EventHandler that I'm having trouble with:

    Code:
    @EventHandler
       public void onPlayerChat(AsyncPlayerChatEvent e){
          if(Main.instance.getConfig().getString(e.getPlayer().getName()) != null) {
               e.getPlayer().setDisplayName(Main.instance.getConfig().getString(e.getPlayer().getName() + "-Nick") + ChatColor.RESET);}
           }
         
    }
    Unfortunatley, there's no else. Yet. Because if you don't have a nickname, your name becomes null. I also have a prefix name system. It goes like this:
    Code:
      @EventHandler
      public void playerChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String pname = p.getName();
      if (p.hasPermission("lobby.member")) {
            String member = "" + "[" + ChatColor.GRAY + "Mortal" + ChatColor.RESET +  "] " + pname + "";
            p.setDisplayName(member);
      }else if (p.hasPermission("lobby.demigod")){
          String demigod = "" + ChatColor.BLACK + "[" + ChatColor.YELLOW + "Demigod" + ChatColor.BLACK + "] " + ChatColor.RESET + pname + "";
          p.setDisplayName(demigod);
      }else if (p.hasPermission("lobby.spartan")){
          String spartan = "" + ChatColor.BLACK + "[" + ChatColor.RED + ChatColor.BOLD + "SPARTAN" + ChatColor.RESET + ChatColor.BLACK + "]" + ChatColor.RESET + pname + "";
          p.setDisplayName(spartan);
      }else if (p.hasPermission("lobby.mod")) {
            String mod = "" + ChatColor.BLACK + "[" + ChatColor.WHITE + ChatColor.BOLD + "Siren" + ChatColor.BLACK + "] " + ChatColor.RESET + ChatColor.GOLD + pname + ChatColor.RESET + "";
            p.setDisplayName(mod);
      } else if (p.hasPermission("lobby.builder")) {
        String builder = "" + ChatColor.BLACK + "[" + ChatColor.GREEN + ChatColor.BOLD + "Cyclop" + ChatColor.BLACK + "] " + ChatColor.GOLD + pname + ChatColor.RESET + "";
        p.setDisplayName(builder);
      } else if (p.hasPermission("lobby.admin")) {
        String admin = "" + ChatColor.BLACK + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Minor God" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.GOLD +  pname + ChatColor.RESET + "";
        p.setDisplayName(admin);
      } else if (p.hasPermission("lobby.wes")){
        String wes = "" + ChatColor.BLACK + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Minor God" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.BLACK + "[" + ChatColor.GREEN + "Cyclop" + ChatColor.BLACK + "] " + ChatColor.GOLD + pname + ChatColor.RESET + "";
        p.setDisplayName(wes);
      } else if (p.hasPermission("lobby.coowner")) {
            String coowner = "" + ChatColor.BLACK + "[" + ChatColor.DARK_BLUE + ChatColor.BOLD + "POSEIDON" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + ChatColor.BOLD + "Elder Cyclop" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.GOLD + ChatColor.BOLD + pname + ChatColor.RESET + "";
            p.setDisplayName(coowner);
      } else if (p.hasPermission("lobby.owner")) {
        String owner = "" + ChatColor.BLACK + "[" + ChatColor.GOLD + ChatColor.BOLD + "KRONOS" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.BLACK + "[" + ChatColor.GREEN + ChatColor.BOLD + "Cyclop" + ChatColor.BLACK +  "] " + ChatColor.GOLD + ChatColor.BOLD + pname + ChatColor.RESET + "";
        p.setDisplayName(owner);
      }
      }
    So what I need, is a way to fix the EventHandler that's in the Nick class, so that when you don't have a nickname, it goes to your default name based on your permissions. Thanks!
     
  2. Offline

    pie_flavor

    @MCJoshua345 First of all, your event handler won't work. Put them in reverse order, as the higher up classes may have the lower permissions as well. Second, it's not that you don't have an else statement that makes it null. Instead, check if (Main.instance.getConfig().containsKey(e.getPlayer().getName()).

    Third, just as a side note. Cyclop is not a word. Cyclops is singular and Cyclopes is plural.
     
    Regablith likes this.
  3. Offline

    MCJoshua345

    @pie_flavor
    Thanks for the Greek mythology pointer.... It turns out I just needed to add like, 6 characters to match up with the new {playername}-Nick: format. Thanks!
     
  4. Offline

    mine-care

    Try to avoid getting from the config all time... It is REALY inefficient... Instead load everything you need from config onEnable
     
  5. Offline

    pie_flavor

    @mine-care I don't see how it's inefficient. getConfig returns the same FileConfiguration object, and it will only actually load it from the file the first time you do that, as well as when you do reloadConfig().
     
  6. Offline

    mine-care

    @pie_flavor i see your point but it takes more time to do that than simply storing it to a variable/list/map and retrieving it from there, i didnt have the time to explain further more what i ment (scholl breaks are so small -_-)... Thanks for your quote =D it was pretty useful indeed.
     
Thread Status:
Not open for further replies.

Share This Page