Solved Get ItemStack[] from config

Discussion in 'Plugin Development' started by 1SmallVille1, Mar 16, 2013.

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

    1SmallVille1

    So I was just wondering how you would get an ItemStack[] from the config. I can get a regular ItemStack just by :
    Code:
    ItemStack helm = new ItemStack(Material.getMaterial(this.getConfig().getString("helmet")));
    For my code I have:
    Code:
    ItemStack[] is = new ItemStack[](Material.getMaterial(this.getConfig().getString("invContents")));
    But it's not working

    [Bump]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    ZeusAllMighty11

  3. Offline

    1SmallVille1

  4. Offline

    ZeusAllMighty11

    1SmallVille1

    It follows the same basic concept.

    You need to read up on regex string splitting and parsing values.
     
  5. Offline

    Cirno

    I don't think you need regex string splitting, why not just use a YAML list?
     
  6. Offline

    Morthis

    You need to read it as a list. Use config.getStringList and then loop through the list, creating each ItemStack.
     
  7. If you used config.set() with an array ItemStack[] then you can use config.get() (not get string, not get string list) and store the contents in a List<ItemStack> because YAML doesn't return arrays.

    Or if you want to be safe store it as Object obj and check if it's instanceof List then check if each element is instanceof ItemStack I guess.

    Then you can do whatever with that list.
     
    Zen3515 likes this.
  8. Offline

    1SmallVille1

    Morthis
    How would I get ItemStack from StringList? This is the only problem I'm having
     
  9. Offline

    Unknowncmbk

    Use a helper method that reads the name or item id and converts it to an item stack.
     
  10. That's not required.
    Read what I just said if you don't want to waste time converting stuff manually.
     
  11. Offline

    1SmallVille1

    I'm sorry if I'm coming off as completely incompetent, but could you please show me some examples? I've never used config.set() before and need help... For example, where do you use it? I would assume that the easiest would be a new command that used the args, but I also have a feeling there's a better way.... Any help is appreciated!
     
  12. The config thing has nothing to do with commands, you just use the set() method when you want to set a node to the config, setting an ItemStack object or array or list will save it properly because it implements ConfigurationSerializable (more info about that: http://wiki.bukkit.org/Configuration_API_Reference#Serializing_and_Deserializing_Objects), you'll have to save the config afterwards to apply the changes to the config file.

    But here, example saving equipped armor, with default config (getConfig()), if you want custom config: http://wiki.bukkit.org/Configuration_API_Reference#Mirroring_the_JavaPlugin_implementation
    Code:
     // for saving...
    ItemStack[] armorArray = player.getInventory().getArmorContents();
    getConfig().set("player." + player.getName() + ".armor", armorArray);
    saveConfig();
    
    // and for loading...
    // This is the fast way if you're 100% sure that there's a list of itemstacks at that node...
    @SuppressWarnings("unchecked")
    List<ItemStack> armorList = (List<ItemStack>)getConfig().get("player." + player.getName() + ".armor");
    ItemStack[] armor = armorList.toArray(new ItemStack[0]); // the 'new ItemStack[0]' is just a type definition for toArray() to avoid casting.
    player.getInventory().setArmorContents(armor);
     
    Zen3515 and CeramicTitan like this.
  13. Offline

    1SmallVille1

    All right, this is fantastic, thank you so much
     
Thread Status:
Not open for further replies.

Share This Page