Solved Issues with location saving and loading from config

Discussion in 'Plugin Development' started by deery50, Jul 15, 2013.

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

    deery50

    I have a script that I use to control my config file and in part of that script I have these 2 things:
    Code:
    public static void setlocation(String path,Location item) {
       config.set("vardata." + path + ".world", item.getWorld().getName());
       config.set("vardata." + path + ".x", item.getX());
       config.set("vardata." + path + ".y", item.getY());
       config.set("vardata." + path + ".z", item.getZ());
       save();
    }
    
    and
    Code:
        public static Location getlocation(String path){
       World world = Bukkit.getWorld(config.getString("vardata." + path + ".world"));
       int x = config.getInt("vardata." + path + ".x");
       int y = config.getInt("vardata." + path + ".y");
       int z = config.getInt("vardata." + path + ".z");
       Location loc = new Location(world, x, y, z);
    return loc;
        }
    
    but I seem to be having problems with getlocation, can anyone see the problem?

    anyone got any clue what the problem could be? No errors were shown in eclipse, but once the server ran, It had an error at this point.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    Ewe Loon

    try using getServer().getWorld(config.getString("vardata." + path + ".world"))
    also check if it returns a null
    also change x,y,z to double and use getDouble not getInt , have a look in the config and see if its writing floating point numbers
     
  3. Offline

    deery50

    To my knowledge Bukkit and getServer() do exactly the same thing, and the getDouble had the same error outcome.
     
  4. Offline

    seemethere

    Could we see the error that you're getting? deery50
     
  5. Offline

    deery50

    seemethere
    Code:
    java.lang.ExceptionInInitializerError
    at com.runetooncraft.plugins.Playermobs.Playermobs.onEnable(Playermobs.java:19)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
    at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
    at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
    at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
    at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
    at com.runetooncraft.plugins.Playermobs.Config.getlocation(Config.java:83)
    at com.runetooncraft.plugins.Playermobs.Playermobslistener.<clinit>(Playermobslistener.java:10)
    ... 12 more
    
    there is not really much to it, but here it is.
     
  6. Offline

    seemethere

    deery50 Which line is this:

    Code:
    com.runetooncraft.plugins.Playermobs.Config.getlocation(Config.java:83)
     
  7. Offline

    deery50

    seemethere
    That is part of the code I showed in the first post:
    Code:
        public static Location getlocation(String path){
       World world = Bukkit.getServer().getWorld(config.getString("vardata." + path + ".world"));
       double x = config.getDouble("vardata." + path + ".x");
       double y = config.getDouble("vardata." + path + ".y");
       double z = config.getDouble("vardata." + path + ".z");
       Location loc = new Location(world, x, y, z);
    return loc;
        }
    
     
  8. Offline

    seemethere

    deery50 I understand that's the snippet, but it says the NPE is occuring on line 83, so without knowing which line is line 83, we can't really understand exactly where the error is.
     
    deery50 likes this.
  9. Offline

    deery50

    seemethere Line 83 is the first line of the statement, so this:
    Code:
        public static Location getlocation(String path){
    
     
  10. Offline

    soulofw0lf

    that means either config is null or it the location your sending it is. how are you accessing it(aka give us the snipet of code that is sending the location to it)? and i'm assuming config is a method you wrote to access getConfig? show us that method as well.
     
  11. Offline

    ColaCraft

    Maybe you can change X, Y, & Z to integers, as they are integers.

    config.getInt("whatever") is a thing, I am sure- however I don't have my java editor open.


    Also, be sure you are getting the location of the player- the whole "Location item" seems to lead back to nothing, you're trying to grab the location of... nothing.

    Also, try "item.getWorld().getX();"

    That may work better.
     
  12. Offline

    deery50

    soulofw0lf
    I thought It may be easier just to give you the full Config Handler script I have so far: http://pastebin.com/Wx9XYS3f
    and this is the code snippet I am using to access the statement getlocation:
    Code:
    public static Location spawn = Config.getlocation("spawn.location");
    ColaCraft
    I had them as integers but a user told me to change them to doubles...I'll change them back to integers. and the Location item thing would relate to anything, not just a player location, but any type of bukkit location. so if I was to relay it to a players location I would use Player.getLocation()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  13. Offline

    ColaCraft



    For item.getX();, etc do this:

    item.getWorld(config.getString("vardata."+ path +".world").getX();

    That might be easier to handle the locations.

    I just woke up, so I can't really see the issue- I am going to check in my plugins to see how I have handled locations in the past, successfully.
     
    deery50 likes this.
  14. Offline

    deery50

    ColaCraft
    I'm sorry, where should i Insert that code?
     
  15. Offline

    ColaCraft

    Yeah- this is how I've done it in the past:


    int x = this.getConfig().getInt("ID." + key + ".x");
    int y = this.getConfig().getInt("ID." + key + ".y");
    int z = this.getConfig().getInt("ID." + key + ".z");
    Block block = this.getServer().getWorld(getConfig().getString("world")).getBlockAt(x, y, z);


    Insert it here:

    1. public static void setlocation(String path,Location item) {
    2. config.set("vardata." + path + ".world", item.getWorld().getName());
    3. config.set("vardata." + path + ".x", item.getX());
    4. config.set("vardata." + path + ".y", item.getY());
    5. config.set("vardata." + path + ".z", item.getZ());
    6. save();
    7. }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  16. Offline

    deery50

    I'm sorry, I am good with Java but pretty new to the bukkit API. I just don't understand how that would be implemented into that code. Because You are getting the World, and the world name is getting the string from the config file? But in that portion, aren't you setting the world?

    That seems almost identical to my code, so I wonder what I have done wrong.

    Is there any way you could paste your entire element because I fail to see what I am doing wrong. Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  17. Offline

    blablubbabc

    As you are getting a NullPointerException, you should check for everything that could be null.

    Here are some possible scenarios:

    * Did you initialize your config class? All your methods are static, but in order that config (the private YamlConfiguration object with that name in your Config class) is not null, you must call the constructor first. I would suggest you remove all that "static" keywords from your class, as they are not needed. And then it's easier for you to see if that is the problem..

    * Did you call your "load()" method before using that getLocation() method? -> config (the private YamlConfiguration object with that name in your Config class) not null?

    * Is the config file exiting and are the proper values in there? Makes sure of that! Because config.getString(node) returns null if the node does not exist.

    * After you made sure that config (the private YamlConfiguration object in your Config class) is not null , and that the config file really exists and there are proper data in it: Bukkit.getServer().getWorld(world_name) returns null if the world is not yet loaded -> so check there for null and if it detects null, then you have two possibilities:

    * either you softdepend your world management plugin (which will very likely load all worlds it knows about before your plugin enables)
    * or you load the world yourself (if you expect a world with the given world_name to exist for sure) -> Bukkit.getServer().createWorld(new WorldCreator(world_name));
     
  18. Offline

    deery50

    blablubbabc
    Well...I spent around 2 hours trying to figure out how to connect every script to the Config script without having them set to static and....for some reason booleans were being returned as null and I spent a very long amount of time trying to figure out why that was happening and it doesn't matter how much I search it doesn't make sense...so I decided just to keep them as static. Then I added something to the setlocation statement to also retrieve the world name, the setlocation statement seems to work anyway...it's the getlocation statement that I really just don't understand.
     
  19. Offline

    blablubbabc

    "for some reason"

    Do some debugging: add some message output that helps you to understand, step by step, what's happening. That helps you to understand what in concrete does not work.

    I can't help you more than givign you hints, because your given code does not show the part were you are actually accessing the class. So it depends on the cirsumstances under which this class fails I would guess..
     
  20. Offline

    deery50

    blablubbabc
    This is the class in which I am accessing that snippet: http://pastebin.com/2SY6fd5k
    This is my main class: http://pastebin.com/enNuGmYU
    I am very sorry if I seem annoying, i'm just tired of these errors that I don't understand (which I know is just the nature of learning something new). It is awesome you are giving up time to help me out.

    Edit:
    Also when I add messages to the getlocation snippet they don't even get called.

    Edit again:
    Ok well I have figured some things out, I know for sure the problem is with:
    Code:
    config.getString("vardata." + path + ".world");
    int x = config.getInt("vardata." + path + ".x");
    int y = config.getInt("vardata." + path + ".y");
    int z = config.getInt("vardata." + path + ".z");
    
    Every one of them is returning null
     
  21. Offline

    blablubbabc

    In your main class in onEnable() :
    getServer().getPluginManager().registerEvents(new Playermobslistener(), this);
    loadconfig();
    You are loading the config AFTER you have cretaed the Playermobslistener.. but in that Playermoblistener you have:
    public Location spawn = Config.getlocation("spawn.location");
    which access the Config class BEFORE it was initialized. So like I said above, the config variable in the Config class is still null..

    You can avoid this type of problems by not accessing your Config class in a static manner, but by passing around an instance of Config, which needs to have be created first by calling the constructor..
     
    deery50 likes this.
  22. Offline

    deery50

    It is finally working! Thanks so much! But one thing, when I make it not static and create the constructor:
    Code:
    private Config Config;
    
    I get a null exception when I call this location:
    Code:
    private Location spawn = Config.getlocation("spawn.location");
    
    This does not seem to happen when the statements in the config file are static.
    Here is the full error:
    Code:
    java.lang.NullPointerException
    at com.runetooncraft.plugins.Playermobs.Playermobslistener.<init>(Playermobslistener.java:13)
    at com.runetooncraft.plugins.Playermobs.Playermobs.onEnable(Playermobs.java:21)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
    at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
    at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
    at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
    at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    
     
  23. Offline

    blablubbabc

    private Config Config;

    replace that with:
    private Config config; (<-lower-case, because Config is also the name of the class, and the compiler will probably mix it up with the class, but you want to access the instance (a object) of this class (that are two different things..)

    Also make sure you assign config the instance you create in your main class onEnable(), for example by passing it to your Listener class via the constructor.

    Then you will have to replaced the static access (
    Config.getlocation("spawn.location");
    )

    with a object-oriented access:

    config.getLocation(...);
     
  24. Offline

    deery50

    I did all you said and the same error is still occuring.
     
  25. Offline

    blablubbabc

    Could you give me your current code?
     
  26. Offline

    deery50

    this is the listener script now:

    Code:
    package com.runetooncraft.plugins.Playermobs;
     
    import java.io.File;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class Playermobslistener implements Listener {
    private Config config;
    private Location spawn = config.getlocation("spawn.location");
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player p = event.getPlayer();
    String playername = p.getName();
    if (p.hasPlayedBefore()) {
    Messenger.playermessage("Welcome back to Playermobs!", p);
    }else{
    Messenger.playermessage("It is your first time!", p);
    }
    if (config.isadmin(playername)){
    Messenger.playermessage("You have been listed as an Admin", p);
    config.setbool("playerdata." + playername + ".admintoggle", true);
    Messenger.playermessage("Automatic admin mode has been enabled. This means that you will not interact with the game. In order to turn admin mode off, just type /pm admin", p);
    }
    p.teleport(spawn);
    }
    }
    
    and this is the config script:
    Code:
    package com.runetooncraft.plugins.Playermobs;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
     
    public class Config {
     
    private YamlConfiguration config;
    private File configFile;
     
    public Config(File configFile) {
            this.config = new YamlConfiguration();
            this.configFile = configFile;
    }
     
    public boolean load() {
            try {
                if (!configFile.exists()) {
                    configFile.createNewFile();
                    getdefaults();
                }
                config.load(configFile);
                return true;
            }
            catch (Exception e) {
                Messenger.severe("Config Failed to load, returned error:\n" + e.getMessage());
                return false;
            }
    }
    public boolean save() {
    try {
    config.save(configFile);
    Messenger.debug("Saved Config");
    } catch (Exception e) {
                Messenger.severe("Config Failed to save, returned error:\n" + e.getMessage());
    }
    return true;
    }
    public void getdefaults(){
    String[] admins = {"deery50"};
    config.addDefault("Playermobs.admins", Arrays.asList(admins));
    config.addDefault("vardata.spawn.location.world", Bukkit.getWorlds().get(0).getName());
    config.set("vardata.spawn.location.x", 0);
    config.addDefault("vardata.spawn.location.x", 0);
    config.addDefault("vardata.spawn.location.y", 0);
    config.addDefault("vardata.spawn.location.z", 0);
    config.options().copyDefaults(true);
    save();
    }
    public YamlConfiguration getConfig() {
    return config;
    }
        public boolean isadmin(String playername) {
       Messenger.debug(playername);
       if(config.getList("Playermobs.admins").contains(playername)){
       return true;
       }else{
    return false;
       }
        }
        public void setstring(String path,String item){
       config.set("vardata." + path, item);
       save();
        }
        public boolean getbool(String path){
       return config.getBoolean("vardata." + path);
        }
        public String getstring(String path){
       return config.getString("vardata." + path);
        }
        public int getint(String path){
       return config.getInt("vardata." + path);
        }
        public Location getlocation(String path){
    //    Messenger.debug(config.getString("vardata." + path + ".world"));
       String worldstring = config.getString("vardata." + path + ".world");
    //    String worldstring = "world";
    //    Bukkit.createWorld(new WorldCreator(worldstring));
       World world = Bukkit.getWorld(worldstring);
       int x = config.getInt("vardata." + path + ".x");
       int y = config.getInt("vardata." + path + ".y");
       int z = config.getInt("vardata." + path + ".z");
    //    int x = config.getInt("vardata.spawn.location.x");
    //    int y = config.getInt("vardata.spawn.location.y");
    //    int z = config.getInt("vardata.spawn.location.z");
       Location loc = new Location(world, x, y, z);
    return loc;
        }
    public void setbool(String path,boolean item) {
       config.set("vardata." + path, item);
       save();
    }
    public void setint(String path,int item) {
       config.set("vardata." + path, item);
       save();
    }
    public void setlocation(String path,Location item,World world) {
       config.set("vardata." + path + ".world", world.getName());
       config.set("vardata." + path + ".x", item.getX());
       config.set("vardata." + path + ".y", item.getY());
       config.set("vardata." + path + ".z", item.getZ());
       save();
    }
    public void setlist(String path,ArrayList<String> list) {
     List<String> configList = config.getStringList(path);
       configList.addAll(list);
       config.set("vardata." + path, configList);
       save();
    }
    }
    
     
  27. Offline

    blablubbabc

    private Config config;

    You are missing a constructor for your Listener class and your are missing to pass the config object from your main class to your listener class and your are missing to assign the config variable in your listener this passed Config instance ..

    In your main class:

    Config config = null;

    in your onEnable():

    loadconfig();
    getServer().getPluginManager().registerEvents(new Playermobslistener(config), this);

    in your loadConfig() method:
    replace:
    Config config = new Config(file);
    with
    config = new Config(file);

    in your listener class:

    Config config;
    Location spawn;

    public Playermobslistener(Config config) {
    this.config = config;
    this.spawn = config.getlocation("spawn.location");
    }
     
    deery50 likes this.
  28. Offline

    deery50

    I thought private Config config; was the constructor. and what do you mean pass the config object? I am very bad with Java terminology.
     
  29. Offline

    blablubbabc

    I see.. Check my edits above: it's kinda a step-by-step what-to-do what I meant..

    You really should start with some simple basic java tutorials .. :/
     
    deery50 likes this.
  30. Offline

    deery50

    Thanks for all the help! I have been learning java for more than 5 years with basic tutorials, advance tutorials, etc. Some things I just forget but after you went over the code I remembered and understand all of it. Also when you are first learning an API, things can be quiet different. I am sorry if I asked stupid questions. I didn't copy any of my code from anything other than what was found in the javadocs for bukkit and what you just told me so I must know something :p. Anyway thanks for all the help!
     
    blablubbabc likes this.
Thread Status:
Not open for further replies.

Share This Page