How to get an Int from a config

Discussion in 'Plugin Development' started by Glass_Eater84, Aug 22, 2014.

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

    Glass_Eater84

    Hey guys,
    I am trying to get an int from a config. This is what I am trying to work with.

    The int definition
    Code:java
    1. int level = getConfig().getInt("Knockback");


    The int invocation
    Code:java
    1. featherswag.addUnsafeEnchantment(Enchantment.KNOCKBACK, level);


    Stacktrace
    Code:
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:149) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:204) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:154) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:144) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at me.glasseater.fern.Main.<init>(Main.java:25) [Mobs.jar:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [?:1.6.0_65]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [?:1.6.0_65]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [?:1.6.0_65]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [?:1.6.0_65]
        at java.lang.Class.newInstance0(Class.java:357) [?:1.6.0_65]
        at java.lang.Class.newInstance(Class.java:310) [?:1.6.0_65]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:313) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:236) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:350) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:771) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: org.yaml.snakeyaml.parser.ParserException: null; expected '<document start>', but found Scalar
        at org.yaml.snakeyaml.parser.ParserImpl$ParseDocumentStart.produce(ParserImpl.java:231) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:146) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:109) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:121) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:399) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 27 more
    
    I think this may be a problem with the way I define my ints.

    I am not using any string builders, as I am not sure how we could use it for an int, if it is possible, please link me to any type of resource that could help me out.

    -Glass
     
  2. Offline

    Zettelkasten

    Can you post your config file?
     
  3. Offline

    Glass_Eater84

    Zettelkasten Sure.
    Code:
    Name:God Fern
    #Name of your item
     
    Lore:test lore
    #Lore of the item
     
    Knockback: 25
    #Knockback level
     
    Item:17
    #Item ID
     
  4. Offline

    Hoolean

    Hey there, could you please post the entire class? I think I may know what the issue is :3
     
  5. Offline

    Yekllurt

    Did you make
    <yml>.setInt("Path", 5);
    or
    <yml>.set("Path", 5);
    or
    <yml>.setString("Path", 5);
    i woud recommend <yml>.setInt("Path", 5);
     
  6. Offline

    MrAwellstein

    you need a space.
    so...
    Name:God Fern
    Lore:test lore
    Item:17
    becomes
    Name: "God Fern"
    Lore: "TextLore"
    Item: 17





    Also...
    at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]

    This here,shows that you received the error while drawing a String, not an int, I believe.
     
  7. Offline

    Glass_Eater84

    MrAwellstein Thanks! That fixed some of the problems. But not the name and lore part
    Glass
     
  8. Offline

    MrAwellstein


    What other problems? Btw, if you want you can add me on Skype, and I can help you when ever I'm on.
    SkypeID: Mr.Awellstein
     
  9. Offline

    Glass_Eater84

    MrAwellstein Ok cool! And the problem is getting the name and the lore.
     
  10. Offline

    MrAwellstein


    Can you provide a stack trace?
     
  11. Offline

    Glass_Eater84

    MrAwellstein
    Code:
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:149) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:204) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:154) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:144) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at me.glasseater.fern.Main.onEnable(Main.java:36) [Mobs.jar:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:389) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:439) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:375) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:773) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: org.yaml.snakeyaml.parser.ParserException: null; expected '<document start>', but found Scalar
        at org.yaml.snakeyaml.parser.ParserImpl$ParseDocumentStart.produce(ParserImpl.java:231) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:146) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:109) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:121) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:399) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
     
  12. Offline

    fireblast709

    Wrong. YamlConfiguration.loadConfiguration(File) will first read the file, dump that all into a String, and internally call that method ;3.
    Glass_Eater84 you are still experiencing the same error. Post the full YAML file you are using.
     
  13. Offline

    Glass_Eater84

    fireblast709
    Code:
    Name: God Fern
    #Name of your item
     
    Lore: test lore
    #Lore of the item
     
    Knockback: 25
    #Knockback level
     
    Item: 17
    #Item ID
     
  14. Offline

    fireblast709

    Glass_Eater84 Make sure the jar you use is updated (latest version) and restart the server.
     
  15. Offline

    Glass_Eater84

    fireblast709 Yes I know but I am not getting the name from the config nor the lore. I am getting the ID and the knockback level
     
  16. Offline

    fireblast709

    Glass_Eater84 what code do you use to get the name and lore? Also if you seriously get a stacktrace, I'm wondering why you get anything at all.
     
  17. Offline

    Glass_Eater84

    fireblast709

    This is how I create the Strings
    Code:java
    1. String name;
    2. String lore1;


    This is how I define them
    Code:java
    1. this.name = getConfig().getString("Name");
    2. this.lore1 = getConfig().getString("Lore");


    and this is how I invoke them.
    Code:java
    1. temStack featherswag = new ItemStack(Material.getMaterial(getConfig().getInt("Item")), 1);
    2. ItemMeta fs = featherswag.getItemMeta();
    3. featherswag.addUnsafeEnchantment(Enchantment.KNOCKBACK, level);
    4. ArrayList<String> lore = new ArrayList<String>();
    5. lore.add(lore1);
    6. fs.setDisplayName(name);


    What I really need to get fixed for the time being is the strings not being casted onto the ItemMeta.
     
  18. Offline

    ChipDev

    fs.setItemMeta(fs.getItemMeta()) ?
     
  19. Offline

    PandazNWafflez

  20. Offline

    Glass_Eater84

    PandazNWafflez Didn't work, took away the enchant from the config :/
     
  21. Offline

    fireblast709

    Glass_Eater84 get the ItemMeta after you set the enchantment.
     
  22. Offline

    Glass_Eater84

    fireblast709 Worked thanks! OFF TOPIC: How would I do two commands in one class? I tried this but it failed to work.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. if (cmd.getName().equalsIgnoreCase("feather")) {
    4. if (sender.hasPermission("feather.use")) {
    5. sender.sendMessage(featherswag);
    6. Player player = (Player)sender;
    7. ItemStack featherswag = new ItemStack(Material.*****, 1);
    8. featherswag.addUnsafeEnchantment(Enchantment.******, level);
    9. player.getInventory().addItem(new ItemStack[] { featherswag });
    10. player.updateInventory();
    11. return false;
    12. }
    13. return false;
    14. }
    15.  
    16. @SuppressWarnings("deprecation")
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    18. {
    19. if (cmd.getName().equalsIgnoreCase("fire")) {
    20. if (sender.hasPermission("*****")) {
    21. sender.sendMessage(featherswag);
    22. Player player = (Player)sender;
    23. ItemStack featherswag1 = new ItemStack(Material.******, 1);
    24. featherswag1.addUnsafeEnchantment(Enchantment.******, 2);
    25. player.getInventory().addItem(new ItemStack[] { featherswag1 });
    26. return false;
    27. }
     
  23. Offline

    PandazNWafflez

    Glass_Eater84 you just use the same method but after your if statement checking if the command name is feather, you put else if command name is fire.
     
  24. Offline

    fireblast709

    Glass_Eater84 use one onCommand method. Then, check if(feather)...else if(fire)
     
  25. Offline

    Glass_Eater84

    fireblast709 Can you show me an example, please. I have also tried this:
    Code:java
    1. public boolean onCommand2(CommandSender sender, Command cmd, String label, String[] args)
    2. {

    But that didn't work.
     
  26. Offline

    McMhz

    Example;
    Code:java
    1. public blablabla onCommand(parameters here){
    2. if(label.equalsIgnoreCase("command1"){
    3.  
    4. }else
    5. if(label.equalsIgnoreCase("command2"){
    6. sender.sendMessage("command lala");
    7. }
    8.  
    9. }

    Made without editor.
     
  27. Offline

    fireblast709

    Glass_Eater84 I gave you a pretty descriptive description in my last post. Use one onCommand method, and check the cmd.getName() in there.

    McMhz thanks for the spoonfeed :/. No, that was not a compliment before you got that idea.
     
    McMhz likes this.
  28. Offline

    Glass_Eater84

    fireblast709 McMhz Thanks! I did it, and got it working what I did was;
    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("fire")) {
    2. if (sender.hasPermission("fire.use")) {
    3. sender.sendMessage(fire);
    4. Player player = (Player)sender;
    5. ItemStack featherswag1 = new ItemStack(Material.***, 1);
    6. featherswag1.addUnsafeEnchantment(Enchantment.*****, 2);
    7. featherswag1.addUnsafeEnchantment(Enchantment.*****, level);
    8. player.getInventory().addItem(new ItemStack[] { featherswag1 });
    9. player.updateInventory();
    10. return false;
     
  29. Offline

    McMhz

    By the way, In future plugins, I recommend using lower-case paths in the config
    For example instead of using
    Knockback: 25

    VVV In the config, you should use this instead because configs are case-sensitive.
    knockback: 25

    But yeah, you don't have to do that, but It's recommended.
    //Edit: If yo do that you have to update the getConfig().getString() thingies too.
    But since your "paths" are just one word in this plugin, you don't have to do it.
     
Thread Status:
Not open for further replies.

Share This Page