Solved Big custom config problem. Need help please!

Discussion in 'Plugin Development' started by bowlerguy66, Dec 13, 2015.

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

    bowlerguy66

    Hi! I have been working on this problem for I don't know how long, so I'm throwing everything I got at this post.

    The problem:
    I moved a couple of relevant methods into a class so it makes my code look better; but now that I have moved them, they don't supply what they are supposed to (Returning the players data from the config file) anymore. So when the removeScoreboard() method is called, noting happens.

    What is supposed to happen:
    There is a scoreboard I use to display the users stats, and the stats are stored in a custom config file in the main class.

    Some more explaining:
    So what I did as a temporary fix was change the constructor that accessed the main class (Therefore accessing the custom config file) to static, it displayed the scoreboard, but not the user's actual stats. Just the default ones.

    Getting to the code:
    This is where it all starts, the scoreboard class where it tries to generate the scoreboard.
    Code:
        public void removeBoard(Player player) {    // Yes, I do realize it adds the board         
    
            ScoreboardManager sm = Bukkit.getServer().getScoreboardManager();
        
            if(player.getScoreboard() == null) {
                return;
            }
    
            if(Bukkit.getServer().getScoreboardManager() == null) {
                return;
            }
    
            Scoreboard sb = sm.getNewScoreboard();
            Objective objective = sb.registerNewObjective("test", "dummy");
    
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.AQUA + player.getName() + "'s stats");
        
            if(!player.getWorld().getName().equalsIgnoreCase("guncraft")) {
                player.setScoreboard(sm.getNewScoreboard());
                return;
            }
        
            if(!pdata.hasAllData(player)) {         // Checks if the user has data in the custom config file.
                pdata.makeAllData(player);          // If not, make all of the data for the player then display the scoreboard
            }
                
            int playerkills = pdata.getPlayerKills(player); // Gets the kills
            int playerdeaths = pdata.getDeaths(player);     // Gets the deaths
            int azomkills = pdata.getZombieKills(player);   // Gets the zombie kills
            double pbalance = pdata.getBalance(player);     // Gets the balance of the player
    
            // Displays everything accordingly. No problems here.
        
            Score bar = objective.getScore(ChatColor.DARK_GRAY + "######## Your stats ########");
            bar.setScore(10);
    
            Score balance = objective.getScore(ChatColor.GRAY + "Balance: " + ChatColor.AQUA + pbalance);
            balance.setScore(9);
    
            Score kills = objective.getScore(ChatColor.GRAY + "Player kills: " + ChatColor.AQUA + playerkills);
            kills.setScore(8);
    
            Score zomkills = objective.getScore(ChatColor.GRAY + "Zombie kills: " + ChatColor.AQUA + azomkills);
            zomkills.setScore(7);
    
            Score deaths = objective.getScore(ChatColor.GRAY + "Deaths: " + ChatColor.AQUA + playerdeaths);
            deaths.setScore(6);
    
            player.setScoreboard(sb);
        
        }
    
    The hasAllData() method:
    Code:
        public boolean hasAllData(Player p) {
                                        
            if(!hasName(p)) { // Checks if the player has the name, this is where the error starts
                return false; // It is not allowed to continue because the hasName() is broken.
            } else if(!hasPlayerKills(p)) {
                return false;
            } else if(!hasZombieKills(p)) {
                return false;
            } else if(!hasDeaths(p)) {
                return false;
            } else if(!hasBalance(p)) {
                return false;
            } else if(!hasChatEnabled(p)) {
                return false;
            } else {
                return true;
            }
                    
        }
    
    The hasName() method:
    Code:
        public boolean hasName(Player p) {
        
            String uuid = p.getUniqueId().toString();
        
            if(pFile == null) {  // pFile is the custom config
                                 // Any time that I use the pFile, it throws and NPE on that line.
                return false;    //Checking to prove that it was the config that was the problem
            }
        
            if(!pFile.contains("Players." + uuid)) { // If the config has the player AT ALL
                return false;
            }
        
            if(pFile.contains("Players." + uuid + ".Name")) { // If the player has the name.
                return true;
            } else {
                return false;
            }
        
        }
    
    The pFile:
    Code:
        File playerFile;
        FileConfiguration pFile;
    
        public FileConfiguration getPlayer() {  // I changed it so it just returns the pFile here. all the pFiles in the code were replaced with getPlayer()
            return pFile;
        }
    
        public void savePlayer() {              // There is a save method in the main class. I just used that.
            main.savePlayer();
        }
    
        public void loadFiles() {               // My take on loading files.
                
            playerFile = new File(main.getDataFolder(), "players.yml"); // The file location.
        
            if(playerFile.exists()) {                               
                try {
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data loaded properly");
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be loaded!");
                    e.printStackTrace();
                }
            } else {
                try {
                    playerFile.createNewFile();
                    pFile = YamlConfiguration.loadConfiguration(playerFile);
                    System.out.println(new Msg().symbol + ChatColor.GREEN + "Player data created and loaded");        
                } catch(Exception e) {
                    System.out.println(new Msg().symbol + ChatColor.RED + "Player data could not be created!");        
                    e.printStackTrace();            
                }
            }    
        }
    
    Additional info:
    When I use the load files, it says Player data loaded properly. Like normal

    Edits:

    #1: I can guarantee that it is the getPlayer() method that is causing the problem.
    #2: Updated the problem
     
    Last edited: Dec 13, 2015
    TheNewTao likes this.
  2. Offline

    Tecno_Wizard

    @bowlerguy66, love the amount of info you put into this (Inline comments, why don't you people use more of them!) but you forgot to really explain what the issue was. Doesn't work really doesn't suffice as an explanation of the problem. Even saying nothing happens is better that saying nothing.
     
  3. Offline

    bowlerguy66

    @Tecno_Wizard Yeah I guess I didn't put much. Updating the main thread
     
  4. Offline

    Tecno_Wizard

    @bowlerguy66, sorry for the delay, alerts are flaky recently.

    You do realize the method named removeBoard adds the board, right? (Sometimes it's the dumbest stuff, I know)
     
  5. Offline

    bowlerguy66

    @Tecno_Wizard Yeah, just too lazy to change the name of the method
     
  6. Offline

    Tecno_Wizard

  7. Offline

    bowlerguy66

    @Tecno_Wizard The scoreboard was supposed to come up, no matter what
     
  8. Offline

    bowlerguy66

  9. Offline

    bowlerguy66

    bump! I really need help with this. I have been working on it for the past 2 days

    Well, I solved it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 17, 2015
Thread Status:
Not open for further replies.

Share This Page