Problem/Bug .yml Integer set and get Error

Discussion in 'Plugin Help/Development/Requests' started by ScorixEar, Sep 21, 2015.

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

    ScorixEar

    Hello guys and girls.
    in My Plugin, i want to set to coordiantions of a minecraft-player, save this in a yml-file and test it in a entitydamagebyentityevent

    in my main class i use this:
    Code:
    public File config ;
        public FileConfiguration configc;
        public static DeathListener getInstance()
        {
            return plugin;
        }
        public void onEnable()
        {
            plugin = this;
            config=YamlHandler.createFile("config.yml");
            configc=YamlHandler.createYamlFile(file);
            Bukkit.getPluginManager().registerEvents(new PlayerAttackListener(), this);
            getCommand("koord1").setExecutor(new Koord1Command());
          }
        public File getConfigFile()
        {
            return config;
        }
        public FileConfiguration getRawConfigFile()
        {
            return configc;
        }
    the command sets this:
    Code:
    @Override
        public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
            Player p = (Player)cs;
                DeathListener.getInstance().getRawConfigFile().set("Koords1.X", p.getLocation().getBlockX());
                DeathListener.getInstance().getRawConfigFile().set("Koords1.Y", p.getLocation().getBlockY());
                DeathListener.getInstance().getRawConfigFile().set("Koords1.Z", p.getLocation().getBlockZ());
                YamlHandler.saveYamlFile(DeathListener.getInstance().getRawConfigFile(), DeathListener.getInstance().getConfigFile());
                p.sendMessage("§8[§9DeathListener§8] §aKoords1 gesetzt!");
            return true;
        }
    and the AttackListener gets this:
    Code:
    if(e.getEntity() instanceof Player&&((e.getDamager() instanceof Player )|| (e.getDamager() instanceof Arrow&&((Arrow) e.getDamager()).getShooter() instanceof Player) ||(e.getDamager() instanceof FishHook&&((FishHook) e.getDamager()).getShooter() instanceof Player)))
            {
                e.getEntity().sendMessage("ja\n");
                int x1=Integer.valueOf(DeathListener.getInstance().getRawConfigFile().getString("Koords1.X"));
                int y1=Integer.valueOf(DeathListener.getInstance().getRawConfigFile().getString("Koords1.Y"));
                int z1=Integer.valueOf(DeathListener.getInstance().getRawConfigFile().getString("Koords1.Z"));
                int x3=e.getEntity().getLocation().getBlockX();
                int y3=e.getEntity().getLocation().getBlockY();
                int z3=e.getEntity().getLocation().getBlockZ();
    e.getEntity().sendMessage(x1+" "+x2+" "+x3+"\n");
    But there is an Error: it says that Koords1.X Koords1.Y and Koords1.Z were null
    In the config.yml are the koords but I did not find the bug

    To understand the main class, here is the YamlHandler. This class works:
    Code:
    public class YamlHandler
    {
        public static File createFile(String filename)
        {
            File dir = new File(DeathListener.getInstance().getDataFolder().getAbsolutePath() + File.separator + "files");
            dir.mkdirs();
          
            File f= new File(DeathListener.getInstance().getDataFolder().getAbsolutePath() + File.separator + "files" + File.separator + filename);
            if(!f.exists())
            {
              
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return f;
        }
        public static FileConfiguration createYamlFile(File f)
        {
            FileConfiguration fc= YamlConfiguration.loadConfiguration(f);
          
            return fc;
        }
      
        public static void saveYamlFile(FileConfiguration c,File f)
        {
            try {
                c.save(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }  
    }
     
    Last edited: Sep 21, 2015
  2. Offline

    pie_flavor

    @ScorixEar First of all, a lot of that file.exists() code is unnecessary. loadConfiguration returns a blank configuration if it couldn't find a file, and you can just save the edited configuration. Second, it's considered good practice to do new File(pathToFolder, fileName) instead of new File(pathToFile). Third, you should check whether they actually are a player before casting to Player. Fourth, \n often shows a weird character. You don't actually need to end anything ever with a \n in minecraft.
    Answer to your question: Koords1.X is not a String, it is an int. So if you do getString() on it, it returns a null because there is no string there.
     
  3. Offline

    ScorixEar

    Thank your for your answer, but the most things you said, are also not nessecary. As I said, the YamlHandler works, there is no Error, because I used him in many projects. Than the check of the player is: if(e.getEntity() instanceof Player) in the PlayerAttackListener. This is the first line, so I tested it,
    To foruth, I ended it, to get a enter inthe Text, because this is not the full code there come other message, I just copied and know that ;)
    And to the answer, this is not the answer, Im Sorry :D
    I also tested it with .getInt(...) and it returns 0, although there stands 4 oder -42 or other.
    I set the Code with the Command to a Int and if I want to get the Int, there stands 0, but why :confused:
    The getString tells us also, that there actually stand null instead of 0. But I set it 42 for example. Why thinks the getmethod, this is null :confused:
    @pie_flavor
    lg Paul
     
  4. Offline

    ScorixEar

  5. Offline

    pie_flavor

    @ScorixEar I know they are not necessary, but they are a combination of general coding principles and unnecessary code. It won't kill you, but it is bad practices. And perhaps the part with the bug isn't listed here - I noticed that you have included only parts of some classes, would you mind including the entire class?
     
Thread Status:
Not open for further replies.

Share This Page