Add items to players from a config

Discussion in 'Plugin Development' started by BetaNyan, Jul 26, 2014.

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

    BetaNyan

    I have a configuration with a list of items.
    It looks like this:
    items:
    - DIAMOND_SWORD

    Here is my code:
    Code:java
    1. ItemStack[] items = (ItemStack[]) Main.instance.getConfig().getList("kits."+args[0]+".items").toArray();
    2. p.getInventory().addItem(items);


    But it doesn't give me any items?
    Why?
     
  2. Offline

    Dragonphase

    BetaNyan

    Because the items in your config do not represent a serialized ItemStack. An example of a serialized ItemStack:

    Code:
    items:
    - ==: org.bukkit.inventory.ItemStack
      type: DIAMOND_SWORD
     
  3. Offline

    BetaNyan

    What if I want people to be able to do only DIAMOND_SWORD? Could I do something like a Material[]?
     
  4. Offline

    fireblast709

    BetaNyan read the value as a String, then use Material.valueOf(thatString) to get the Material, then create a new ItemStack from that Material.
     
  5. Offline

    Niknea

    BetaNyan
    PHP:
    String name this.getConfig().getString(String Name); // Gets the ItemStack from the config.
    ItemStack item = new ItemStack(Material.getMaterial(item)); // Creates a new ItemStack, using the string "name" to create the ItemStack. NOTE: getMaterial(); method asks for a string.
    player.getInventory().addItem(item); // Adding the ItemStack to the player's inventory.
     
Thread Status:
Not open for further replies.

Share This Page