Solved String to ItemStack

Discussion in 'Plugin Development' started by TheEnderman, Dec 29, 2016.

Thread Status:
Not open for further replies.
  1. So I am working on a plugin which uses item stacks saved to files. I have been able to convert an item stack into a string and save it to file but have been unable to reverse the process.
    The item stack in string format looks like this:
    ItemStack{DIAMOND_SWORD x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=Test Sword, enchants={DAMAGE_ALL=3}}}

    Is there anyway to turn this into a item stack?

    What I have so far:
    Code:java
    1.  
    2. String name = args[1];
    3. File myFile = new File(name + ".dat");
    4. try {
    5. FileInputStream inStream = new FileInputStream(myFile);
    6. int len = (int) myFile.length();
    7. byte[] data = new byte[len];
    8. inStream.read(data, 0, len);
    9. inStream.close();
    10. String stringFromFile = data.toString();
    11. //what now?
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16.  
     
  2. Offline

    DoggyCode™

    Bukkit contains actual methods for storing/retrieving items stack objects from a Yaml configuration


    Sent from my iPhone using Tapatalk
     
  3. DoggyCode™
    Are you suggesting I directly save the item stack to the config?
    Code:java
    1.  
    2. ItemStack item = player.getInventory().getItemInMainHand();
    3. String name = item.getItemMeta().getDisplayName();
    4. this.getConfig().set(name, item);
    5.  
     
  4. @TheEnderman Just use FileConfiguration#set(String path, Object yourItemStack) then use FileConfiguration#getItemStack(String path)
     
  5. The original point of the plugin was to have items saved to files, I could specify a .yml file as a config and save to that though.
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. @timtower
    Config works fine for what I am currently doing, but may mess with this more in the future.
     
  8. Offline

    timtower Administrator Administrator Moderator

    I believe that somebody wrote code to turn the config to a single string, but I am not 100% sure.
    But then you also get into the databases etc.
     
Thread Status:
Not open for further replies.

Share This Page