ItemStacks in Configuration Files

Discussion in 'Plugin Development' started by Build_and_Break, May 25, 2014.

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

    Build_and_Break

    Hello,

    I haven't been able to find any documentation on how to use ItemStacks in configuration files. I'm making a drop party plugin that uses Math.random() and returns any number between 1 and 11. I need to make a configuration file that allows server administrators to change the items for each number. This has to be done with ItemStacks, and it can't drop more than 1 of the item so ItemStacks make sense. I've got the entire if/else statement for the Math.random() already set up. Each one looks a bit like this:
    Code:java
    1.  
    2. run.getWorld().dropItemNaturally(run.getLocation(),new ItemStack(getConfig().getItemStack("Config.Items.1")));
    3. run.sendMessage(ChatColor.GREEN + "Config.Message.1");


    But just putting "Material.<WUTEVR>" as a default in the config generator doesn't work. I don't need an extensive API for this since it's designed to be lightweight.

    Thanks!
     
  2. Offline

    thecrystalflame

    ItemStacks allready extend ConfigurationSerializable (or something like that) so its just like saving a string or another data type to a config.

    instead of saving Material.whatever to your config save an item stack itself from your plugin.

    Code:java
    1.  
    2. getConfig().put(path, ItemStack);
    3.  
     
  3. Offline

    Build_and_Break

    This is what I currently have, which is basically what you have except I have
    Code:java
    1. FileConfiguration config = getConfig();
    to declare config instead of getConfig().

    Code:java
    1. private void initConfig() {
    2. FileConfiguration config = getConfig();
    3. config.addDefault("Config.Items.1", Material.DIAMOND);
    4. config.addDefault("Config.Items.2", Material.DIAMOND);
    5. config.addDefault("Config.Items.3", Material.BEACON);
    6. config.addDefault("Config.Items.4", Material.EMERALD);
    7. config.addDefault("Config.Items.5", Material.EXP_BOTTLE);
    8. config.addDefault("Config.Items.6", Material.NETHER_STAR);
    9. config.addDefault("Config.Items.7", Material.PORTAL);
    10. config.addDefault("Config.Items.8", Material.IRON_PICKAXE);
    11. config.addDefault("Config.Items.9", Material.CAKE);
    12. config.addDefault("Config.Items.10", Material.COOKIE);
    13. config.addDefault("Config.Items.11", Material.IRON_BLOCK);
    14.  
    15. config.addDefault("Config.Message.1", "You got a diamond!");
    16. config.addDefault("Config.Message.2", "You got a diamond!");
    17. config.addDefault("Config.Message.3", "You got a beacon!");
    18. config.addDefault("Config.Message.4", "You got an emerald!");
    19. config.addDefault("Config.Message.5", "You got an EXP bottle!");
    20. config.addDefault("Config.Message.6", "You got a nether star!");
    21. config.addDefault("Config.Message.7", "You got a portal!");
    22. config.addDefault("Config.Message.8", "You got an iron pick!");
    23. config.addDefault("Config.Message.9", "You got a cake! (Not a lie)");
    24. config.addDefault("Config.Message.10", "You got a cookie!");
    25. config.addDefault("Config.Message.11", "You got an iron block!");
    26.  
    27. config.options().copyDefaults(true);
    28. saveConfig();
    29. }


    Errors:

    Code:java
    1. 7:51:44 PM [INFO] Build_and_Break issued server command: /drop
    2. 7:51:44 PM [INFO] New drop!
    3. 7:51:44 PM [SEVERE] null
    4. 7:51:44 PM org.bukkit.command.CommandException: Unhandled exception executing command 'drop' in plugin WillowDrop v1.02
    5. 7:51:44 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-1481]
    6. 7:51:44 PM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Spigot-1481]
    7. 7:51:44 PM at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:726) ~[craftbukkit.jar:git-Spigot-1481]
    8. 7:51:44 PM at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:999) [craftbukkit.jar:git-Spigot-1481]
    9. 7:51:45 PM at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:830) [craftbukkit.jar:git-Spigot-1481]
    10. 7:51:45 PM at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Spigot-1481]
    11. 7:51:45 PM at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:65) [craftbukkit.jar:git-Spigot-1481]
    12. 7:51:45 PM at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:180) [craftbukkit.jar:git-Spigot-1481]
    13. 7:51:45 PM at net.minecraft.server.v1_7_R3.ServerConnection.c(ServerConnection.java:81) [craftbukkit.jar:git-Spigot-1481]
    14. 7:51:45 PM at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:720) [craftbukkit.jar:git-Spigot-1481]
    15. 7:51:45 PM at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283) [craftbukkit.jar:git-Spigot-1481]
    16. 7:51:45 PM at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:583) [craftbukkit.jar:git-Spigot-1481]
    17. 7:51:45 PM at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:489) [craftbukkit.jar:git-Spigot-1481]
    18. 7:51:45 PM at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Spigot-1481]
    19. 7:51:45 PM Caused by: java.lang.IllegalArgumentException: Cannot copy null stack
    20. 7:51:45 PM at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[craftbukkit.jar:git-Spigot-1481]
    21. 7:51:45 PM at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:127) ~[craftbukkit.jar:git-Spigot-1481]
    22. 7:51:45 PM at me.Build_.Drop.Drop.onCommand(Drop.java:77) ~[?:?]
    23. 7:51:45 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-1481]
    24. 7:51:45 PM ... 13 more


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

    thecrystalflame

    thats because your parsing materials straight into the config then trying to retrieve them as ItemStacks. you need to do it like this:

    Code:java
    1.  
    2. config.addDefault("Config.Items.1", new ItemStack(Material.DIAMOND);
    3.  
     
  5. Offline

    xize

    you can do it in 3 ways.. honest I would recommend to use the third version since its for me the best way to keep metadata but it will result in less user friendly.

    1. Manual Serializing like material:subdata:amount and re parse it manually.

    2. use something like a object outputstream most of this is used in mysql but since MemorySection implements Serializeable you may can use this (with a BUT I haven't checked if it 1. Really extends Serializeable and 2. if it even works with yaml:p)

    3. do something like this:

    loading:
    Code:
       List<ItemStack> items = Arrays.asList(((List<ItemStack>)con.get("items")).toArray(new ItemStack[0]));
    
    saving:
    Code:
    con.set("items", p.getInventory().getContents());
    
     
  6. Offline

    thecrystalflame


    "ItemStacks Allready implements ConfigurationSerializable" no need for outputstreams when he could be using ItemStacks instead of materials. saves allot of work considering the bukkit team has done most of it for him.
     
    xize likes this.
  7. Offline

    Build_and_Break

Thread Status:
Not open for further replies.

Share This Page