Searching for a up-to-date item serializer

Discussion in 'Plugin Development' started by HungerCraftNL, May 12, 2016.

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

    HungerCraftNL

    Hi guys,

    It has been a long time since I've posted something here, good to be back. I have a question today, do one of you guys have a premade class for serializing that's up-to-date for Bukkit 1.8.*, I've been trying different classes that can be found online but they are not complete (missing lores, enchantements etc.). The class will be used for MySQL data storing.

    Thank you.

    Shadow
     
  2. Offline

    Lordloss

    Just use the bukkit configuration API, it works great.
     
  3. Offline

    HungerCraftNL

    Can you provide me with a valid example to serialize the ItemStack into a string with the Bukkit API?
     
  4. Offline

    mcdorli

    Just use ConfigurationSection#set with the itemstack as argument.
     
  5. Offline

    HungerCraftNL

    I am using the .serialize function now, and setting it toString(), but how can I restore the map from that string?
    Code:
    String item = p.getItemInHand().serialize().toString();
    
     
  6. Offline

    mcdorli

    Deserialize it, check what toString returns.
     
  7. Offline

    HungerCraftNL

    I am not stupid lol, ofcourse I need to deserialize it, but my question is how because there is no valid function to do this as far as I know.

    Output:
    Code:
    {type=IRON_PICKAXE, meta=UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§bWrench, lore=[§6Wrench Item: Normal, §bTimes left: §71, §bWrench type: §7Spawner]}}
    
     
  8. Offline

    I Al Istannen

    @HungerCraftNL
    You need to create a YamlConfiguration (or use the Plugin#getConfig() one). The you can use YamlConfiguration#set(String path, Object value). In this case value is the itemstack you want to save. Save the config using YamlConfiguration#save(File file) (or Plugin#saveConfig() if you use the Plugin#getConfig() one).
    Upon deserialization you use YamlConfiguration#loadFromFile(File saveFile) (or just call Plugin#getConfig() again if you use it).
    Then you can access the ItemStack again with YamlConfiguration#get(String path). Just cast the resulting Object to an itemstack.
     
  9. Offline

    timtower Administrator Administrator Moderator

    @I Al Istannen MySQL though, so thought is good, implementation less.
    @HungerCraftNL I believe that there is a way to turn the serialized itemstack into a string and in reverse, uses a yamlconfiguration for buffering / serializing. Not 100% sure though, believe that I used it somewhere, can take a look for you if I still have it.
     
  10. Offline

    HungerCraftNL

    Thank you for your answer, but I am not storing any data to YML files so your post is completly useless.

    I've rade some things about ObjectInputStream/ObjectOutputStream, but it has to be readable through a web interface too so that'll be hard. I'll start on an own function to serialize it.
     
  11. Offline

    timtower Administrator Administrator Moderator

    @HungerCraftNL Readable through a web interface? So you want to be able to show it on a website in a way like:
    "Player just acquired this item with this and this enchantment and lores" ?
    If so: your own function would be best, I suggest splitting everything and use a full blown database for it instead of a single string.
     
  12. Offline

    I Al Istannen

    @timtower @HungerCraftNL
    RAH. Sorry, totally skipped the sentence. Just heard somebody mention Configuration API and Configuration section. Sorry about that.
    I can't help you much with it, but good luck ;)
     
    timtower likes this.
  13. Offline

    HungerCraftNL

    I have 5 years of experience with the Bukkit API I know how to store objects in YAML files by know haha :p

    I'm thinking about storing it in a JSON format, worked a lot with JSON before in JavaScript and PHP so I think it'll be fine in Java too. I'll post the answer when I find one, thank you for your thinking guys.
     
    I Al Istannen and timtower like this.
  14. Offline

    mcdorli

    The String contains everything you need.
     
  15. Offline

    HungerCraftNL

    Can you explain that to me, since I need to deserialize the String too...
     
  16. Offline

    mcdorli

    Code:
    {type=IRON_PICKAXE, meta=UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§bWrench, lore=[§6Wrench Item: Normal, §bTimes left: §71, §bWrench type: §7Spawner]}}
    You can use Material.fromString(), to get what the type means,
    display-name=§bWrench contains the display name,
    lore=[§6Wrench Item: Normal, §bTimes left: §71, §bWrench type: §7Spawner] is the lore
     
  17. Offline

    HungerCraftNL

    Yeah, if the method Material.fromString(String str) existed...
     
  18. @HungerCraftNL
    Pretty sure it is something like Material.get(String) or something, just take a look at the methods in Material, I have used it quite a bit...

    EDIT: here is the method
    Material.getMaterial(String mat)
     
  19. Offline

    HungerCraftNL

    That converts from a String (example: APPLE) the a Material object, are you sure it works with serialized string as well?
     
  20. Offline

    timtower Administrator Administrator Moderator

    @HungerCraftNL A Material does not contain information like metadata so it won't work for your full string.
     
  21. Offline

    mcdorli

    I said, you can get every information you need out of it.
     
Thread Status:
Not open for further replies.

Share This Page