[Solved]Saving/Loading float with a config File

Discussion in 'Plugin Development' started by krazytraynz, Jul 31, 2012.

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

    krazytraynz

    I want to use a config file for the float in the createExplosion method, but I don't quite know how to use a config file for floats. Does anyone have any tips that could point me in the right direction? Thanks!
     
  2. Offline

    Slayer9x9

    Try saving the float as a string, then parse the string using Float.parseFloat("0.5"); or something like that when you need to.
     
  3. Offline

    r0306

    krazytraynz
    You can cast the float to a double and save/retrieve it as a double, casting it back to a float when you need. Since doubles are more accurate than floats, no data will be lost so don't worry.
     
  4. Offline

    krazytraynz

    So I would do something like
    Code:
    File senderFile = null;
    FileConfiguration config = YamlConfiguration.loadConfiguration(senderFile);
          Float Explosion = (float) config.getDouble("Explosion", 0);
    target.getWorld().createExplosion(target.getLocation(), Explosion);
     
  5. Offline

    r0306

  6. Offline

    krazytraynz

    r0306
    I realized I had the senderFile set as null, what file would I have to use? It gives me an error if I try to use the config file.
     
  7. getConfig() if you want to use config.yml.
    Or you can use:
    Code:
    FileConfiguration config = YamlConfiguration.loadConfiguration(getDataFolder() + File.separator + "yourcustomfile.yml");
     
  8. Offline

    krazytraynz

    getConfig() gives me an error, and if I fix that it gives me a second one. if I use the second method, I get an error with getDataFolder()
     
  9. Offline

    r0306

    krazytraynz
    Are you doing this inside the main class? If not, you will have to create a constructor that stores an instance of the main class.
     
  10. getConfig() and getDataFolder() can be called from the JavaPlugin extended class (meaning your main class), so you need your plugin pointer because you're obviously calling it from another class :}
     
  11. Offline

    krazytraynz

    r0306
    I'm doing it in a command executor class, and I have this constructor:
    Code:
    private KrazyBukkit krazy;
    public DetonateExecutor(KrazyBukkit krazy) {
    this.krazy = krazy;
    }
    I tried krazy.getConfig(), but that's what was giving me an error.
     
  12. Offline

    r0306

    krazytraynz
    You have to pass an instance on to this class from your main:
    Code:
    public void onEnable()
    {
     
      getCommand("command").setExecutor(new DetonateExecutor(this));
    }
     
  13. Offline

    krazytraynz

    r0306
    I already did this:
    Code:
    private DetonateExecutor detonate;
    public void onEnable(){
    detonate = new DetonateExecutor(this);
    getCommand("detonate").setExecutor(detonate);
    }
    Wouldn't it work the same way?
     
  14. If you would tell us what that error is, maybe we can find out why that doesn't work :)
     
  15. Offline

    krazytraynz

    Digi
    Here's the error I get with the getConfig() - Type mismatch: cannot convert from FileConfiguration to File

    When I fix that, I get this error with the loadConfiguration - The method loadConfiguration(File) in the type YamlConfiguration is not applicable for the arguments (FileConfiguration)

    Code:
    package com.github.KrazyTraynz;
     
    import java.io.File;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
     
    public class DetonateExecutor implements CommandExecutor
    {
    private KrazyBukkit krazy;
    public DetonateExecutor(KrazyBukkit krazy) {
    this.krazy = krazy;
    }
     
     
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("detonate")){
      if (args.length < 1){
          sender.sendMessage(ChatColor.RED + "Not enough arguments!");
              return false;
      }
      if (args.length > 1){
         sender.sendMessage(ChatColor.RED + "Too many arguments!");
                 return false;
      }
      FileConfiguration senderFile = krazy.getConfig();
      Player s = (Player)sender;
          Player other = (Bukkit.getServer().getPlayer(args[0]));
          FileConfiguration config = YamlConfiguration.loadConfiguration(senderFile);
          Float Explosion = (float) config.getDouble("Explosion", 0);
          if (other == null) {
              sender.sendMessage(ChatColor.RED + args[0] + " is not online, but I'm sure you'll get them next time!");
              return false;
           }
     
  16. That's an error due to wrong use as you're using it wrong.

    getConfig() returns the FileConfiguration object directly, you don't need to load it... it already does that.
     
  17. Offline

    krazytraynz

    Digi
    Oh... I feel stupid now :eek: Anyways, if I haven't bothered you too much already, I have one more question. If I wanted to use a config for a vector with 3 integers, would I have to save it as one path, or 3 different ones?
     
  18. Whatever you want, both are fine.

    For one path you'll need to separate them by a comma or something and then use string.split(",") and Integer.valueOf() on each array element of that split to get the values back.
     
  19. Offline

    krazytraynz

    Digi
    Where would I put string.split(",")? After I get the config?
     
  20. "string" is a String object and split(",") splits the string into an array by separating it by that comma... I don't know how else I can explain this, you need some basic programming awareness, arrays are similar in all programming languages.... but you should read about some basic Java and learn to decide for yourself how a simple method like that can be used :}
     
  21. Offline

    krazytraynz

    Maybe you're right xD I'll try to learn Java a little bit more this time.
     
  22. Offline

    Croug

    you can save the vector object to a yaml path and load it with getVector, also, you can use getLong instead of casting a double so you don't have to limit yourself to two decimal points.
     
Thread Status:
Not open for further replies.

Share This Page