Trying to get ArrayList from config.yml

Discussion in 'Plugin Development' started by arwrock, Jul 14, 2014.

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

    arwrock

    I'm trying to get an ArrayList from config.yml and then send a message to the player that types in a command that gives the first value of the ArrayList, and there is definitely a value, but it gives me this:
    Code:
    [14:51:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'drink' in plugin DrinkCraft v0.0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    Caused by: java.lang.NullPointerException
        at com.arwrock.main.Main.onCommand(Main.java:83) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
        ... 13 more
    
    It says an internal error occured.
    The config.yml:
    Code:
    Drinks:
      Beer:
        effect: '1'
        time: '120'
    
    I want it to send me a message saying Beer to test out the ArrayList.

    Here is my class:
    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2. public String itemName;
    3. public ItemStack potion = new ItemStack(Material.POTION,1);
    4. public ItemMeta meta = potion.getItemMeta();
    5. List<String> drinks;
    6.  
    7. public void onEnable(){
    8. getServer().getPluginManager().registerEvents(this, this);
    9. getLogger().info("Remember to check bukkit for the latest versions of DrinkCraft!");
    10. File file = new File(getDataFolder() + File.separator + "config.yml");
    11.  
    12.  
    13. if (!file.exists()){
    14.  
    15. getConfig().addDefault("Drinks.Beer.effect" , "1");
    16. getConfig().addDefault("Drinks.Beer.time" , "120");//adding default settings
    17. drinks = getConfig().getStringList("Drinks"); //Path Drinks>Beer>effect
    18.  
    19. //Save the default settings
    20. getConfig().options().copyDefaults(true);
    21. saveConfig();
    22. } else {
    23. CheckConfig();
    24. saveConfig();
    25. reloadConfig();
    26.  
    27. }
    28. }
    29.  
    30. public void onDisable(){
    31.  
    32. }
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. if(cmd.getName().equalsIgnoreCase("drink")){
    36. if(args.length == 2){
    37. if(args[0].equalsIgnoreCase("give")){
    38.  
    39. itemName = args[1];
    40. if(sender instanceof Player){
    41. if(sender.hasPermission("drinkcraft.give")){
    42. meta.setDisplayName(itemName);
    43. potion.setItemMeta(meta);
    44. PlayerInventory inventory = ((Player) sender).getInventory();
    45. inventory.addItem(potion);
    46. sender.sendMessage(drinks.get(0));
    47. return true;
    48.  
    49. }
    50. }
    51. else{
    52. sender.sendMessage("You must be a player to execute this command.");
    53. return false;
    54. }
    55. }
    56.  
    57. }
    58. else{
    59. sender.sendMessage("/drink list | Lists all drinks./drink give <number> | Gives you a certain drink.");
    60. sender.sendMessage("/drink give <id> | Gives you the drink specified.");
    61. sender.sendMessage("DrinkCraft made by Arwrock.");
    62.  
    63. return true;
    64. }
    65.  
    66. }
    67.  
    68. return false;
    69. }


    Thanks and sorry for the long class to look through. Also, there are no visible errors in Eclipse.
     
  2. Offline

    CoderMusgrove

    A list is like this:

    MyList:
    - listItemOne
    - listItemTwo
    - listItemThree

    so, what you're trying to get is not a list. If you'd like me to go in-depth on achieving what you need, I'll be glad to.
     
    arwrock likes this.
  3. Offline

    arwrock

    I have to go now, but how many spaces would there be to list stuff? just 1 between the - and the item? Thanks in advance.
     
  4. Offline

    TheMcScavenger

    [no spaces]listName:
    [two spaces]-[one space]item
    [two spaces]-[one space]item
    [two spaces]-[one space]item
     
  5. Offline

    arwrock

    EDIT: This doesn't work. It always deletes the two spaces on reload. I have no config changing methods in my plugin either.

    Actually on second thought, can you help me go about with a system where it says (for example):
    Code:
    Drinks:
      Beer:
          effects: 12, 1
          time: 120
      Vodka:
          effects: 12, 13, 10
          time: 150
    Basically it collects all the names (Beer, Vodka), and then collects the effects and times, and then stores them into an ArrayList?
    Thanks for your time!

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

    Gater12

    arwrock
    Loop through ConfigurationSection of Drinks.
     
  7. Offline

    arwrock

    Can you explain to me how that would work, maybe with code? I don't think that would work considering how nothing I've tried has worked. Thanks.
     
  8. Offline

    CoderMusgrove

    I'm giving you the code so please take in consideration how I'm doing this.

    Code:
    // Gets the configuration section path of "Drinks"
    ConfigurationSection sect = config.getConfigurationSection("Drinks");
    // Gets all of the keys from your current configuration section
    // NOTE: These are only your drink names!
    // the boolean represents if you want it to go in deeper,
    // meaning go in deeper and find the other keys, such as effects and time
    List<String> drinks = new ArrayList<>(sect.getKeys(false)); // I don't like sets
    // We're going to loop through the drinks, and find the values.
    for (int i = 0; i < drinks.size(); i++) {
        String drinkName = drinks.get(i);
        // Let's now access the configuration section of the drink itself.
        ConfigurationSection drinkSect = sect.getConfigurationSection(drinkName);
        // Do what else you need to now.
        String effects = drinkSect.getString("effects");
    }
    This code is from the top of my head, so I'm not 100% sure it works, but I'm 95% sure.
     
Thread Status:
Not open for further replies.

Share This Page