Solved Rank is Null?

Discussion in 'Plugin Development' started by MagmaticBacca, Nov 21, 2015.

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

    MagmaticBacca

    I have been using this to get strings from the Config
    Code:
    String Rank = getConfig().getString("Rank." + player.getName());
    and that gets the string perfectly if the player actually has a value stored in the config.
    but if they dont it returns null. i checked if it was null but then it dosent work help?
     
  2. Offline

    CraftCreeper6

    @MagmaticBacca
    Follow Java Naming Conventions, this includes variable names.

    The reason it returns null is because there is no value.

    You should check if the config contains a value before calling .getString()
     
  3. Offline

    MagmaticBacca

    Hmm dosent work maybe i need to show you my code. im probobly doing something horrible idiotic.

    Code:
    @EventHandler
        public void onTalk2(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            if (!player.isOp()) {
                String Rank = getConfig().getString("Rank." + player.getName());
                if (!getConfig().contains(player.getName())) {
                    event.setFormat(ChatColor.YELLOW + player.getName() + ": " +
                            ChatColor.WHITE + event.getMessage());
                }
                else{
                    event.setFormat(ChatColor.GOLD + "" + ChatColor.BOLD +Rank +" "+ ChatColor.YELLOW + player.getName() + ": " +
                            ChatColor.WHITE + event.getMessage());
                }
            } else
                event.setFormat(ChatColor.DARK_RED + "" + ChatColor.BOLD + "OWNER " + ChatColor.GOLD + player.getName() + ": " +
                        ChatColor.RED + event.getMessage());
        }
    }

    ?
     
  4. Offline

    CraftCreeper6

    @MagmaticBacca
    if config contains "Rank.playername" then
    do something
    otherwise
    don't do something
     
  5. Offline

    RoflFrankoc

    Are you sure it is not null? After you set it in the config did you save the config?
    Maybe do this:
    Code:
    if (getConfig.getString("Ranks."+player.getName())==null){
    player.sendMessage("The play does not have a rank");
    }else{
    ....
    }
    
    PS:
    You might wanna change the Format to this:
    Code:
    event.setFormat(ChatColor.DARK_RED + "" + ChatColor.BOLD + "OWNER " + ChatColor.GOLD + "%s"+ ": "+ChatColor.RED + "%s");
    
    This gets ridof a few bugs like when you type "%" or "%s" in chat.
     
  6. Offline

    MagmaticBacca

Thread Status:
Not open for further replies.

Share This Page