Solved Help With Configs

Discussion in 'Plugin Development' started by robertcarr22, Aug 2, 2013.

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

    robertcarr22

    Im new to plugin coding and trying to make a plugin that shows players points.
    I am trying to save the points to the configs and import them from the configs but when running the server I get a java.lang.NullPointerException on for this line:
    Code:
    plugin.getConfig().set("Scores." + p, value);
    Im pretty sure its because there is no Scores.<player-name> path to set a value to but i'm not sure how to set up the path :/

    Any help would be great! :)
     
  2. Could you show a bit more code so I can see what you're trying to do?
     
  3. Offline

    xTrollxDudex

    robertcarr22
    plugin or the config is null, can I see the code? The config null can be fixed by this.saveDefaultConfig() in the onEnable()
     
  4. Offline

    robertcarr22

    ScoreboardData.java (class with error line)
    Code:
    package io.github.robertcarr22.incepticraft;
     
    public class ScoreboardData {
     
        private static Incepticraft plugin;
     
        public static void savePoints(String p, Integer value){
         
            plugin.getConfig().set("Scores." + p, value);
            plugin.saveConfig();
        }
     
        public static int getPoints(String p){
            int points;
         
            points = plugin.getConfig().getInt("Scores." + p);
            return points;
         
         
        }
     
     
    }
    ScoreboardCreate.java (methods to create scoreboard etc)
    Code:
    package io.github.robertcarr22.incepticraft;
     
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
     
    public class ScoreboardCreator {
     
        private static Incepticraft plugin;
     
        public static HashMap<String, Scoreboard> playerScoreboards = new HashMap<String, Scoreboard>();
        public static HashMap<String, Score> pointscore = new HashMap<String, Score>();
     
        public static void create(Player p, String BoardName, String BoardDisplayName){
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
     
            Objective objective = board.registerNewObjective(BoardName, "dummy");
     
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(BoardDisplayName);
         
            Score point = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Points" + ChatColor.GRAY + ":"));
         
            ScoreboardData.savePoints(p.getName(), 0);
         
            int points = plugin.getConfig().getInt("Scores." + p.getName());
            point.setScore(points);
     
            pointscore.put(p.getName(), point);
     
     
            playerScoreboards.put(p.getName(), board);
        }
     
        public static void update(Player p){
     
            Score point = pointscore.get(p.getName());
     
            point.setScore(ScoreboardData.getPoints(p.getName()));
     
        }
     
        public static void set(Player p){
     
            p.setScoreboard(playerScoreboards.get(p.getName()));
     
        }
     
     
    }
    
    onPlayerJoinEvent:
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent ev){
            Player p = ev.getPlayer();
            String pname = p.getName().toLowerCase();
     
            if (!ScoreboardCreator.playerScoreboards.containsKey(pname))
                ScoreboardCreator.create(p, pname, ChatColor.LIGHT_PURPLE + "Incepticraft Score");
     
            ScoreboardCreator.update(p);
            ScoreboardCreator.set(p);
     
        }
    In onEnable() the code to update the scoreboard:
    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Player p:Bukkit.getOnlinePlayers()){
     
                        if (!ScoreboardCreator.playerScoreboards.containsKey(p.getName()))
                            ScoreboardCreator.create(p, p.getName(), ChatColor.BLUE + "Incepticraft Score");
     
                        ScoreboardCreator.update(p);
                        ScoreboardCreator.set(p);
                    }
                }
          },0L,100L);
    EDIT:
    Full onEnable() (already have the saveDefaultConfig):
    Code:
    public void onEnable(){
           
            PluginManager pm = getServer().getPluginManager();
            //getLogger().info("");
           
            this.saveDefaultConfig();
           
            //Scoreboard
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Player p:Bukkit.getOnlinePlayers()){
     
                        if (!ScoreboardCreator.playerScoreboards.containsKey(p.getName()))
                            ScoreboardCreator.create(p, p.getName(), ChatColor.BLUE + "Incepticraft Score");
     
                        ScoreboardCreator.update(p);
                        ScoreboardCreator.set(p);
                    }
                }
          },0L,100L);
           
            //Register Commands
            getCommand("isay").setExecutor(new isayCommandExecutor(this));   
            getCommand("fish").setExecutor(new fishCommandExecutor(this));
            getCommand("iscore").setExecutor(new iscoreCommandExecutor(this));
           
            //Register Events
            pm.registerEvents(this.eventListener, this);
           
           
                   
            getLogger().info("onEnable Invoked!");
           
           
        }
     
  5. For one, you don't need "plugin.getConfig() , plugin.SaveConfig() , etc.". You just need to type getConfig(), saveConfig() , and so on.
     
  6. Offline

    xTrollxDudex

    robertcarr22
    Plugin is null, use a constructor to pass an instance of the main class to the scoreboard class.
    PHP:
    //in the onEnable
    new ScoreboardDatathis);
    //in scoreboarddata
    Inceptioncraft plugin;
    public 
    ScoreboardData(Inceptioncraft plugin){
    this.plugin plugin;
    }
    The Gaming Grunts
    That would work if only ScoreboardData was in the main class...
     
  7. ah ok :p
     
  8. Offline

    xTrollxDudex

    Whoops, in line 2 it should be
    PHP:
    new ScoreboardData(this);
     
  9. Offline

    robertcarr22

    Great Works perfectly now! :D Thank you! :D

    And thanks @The Gaming Grunts for taking the time to help also! :)
     
  10. Offline

    xTrollxDudex

  11. Offline

    robertcarr22

    Wait sorry found another problem... when the server restarts the scores reset :/ do I add something to the onDisable()?
     
  12. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page