Solved Saving Metadata

Discussion in 'Plugin Development' started by Blir, Jul 30, 2013.

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

    Blir

    Anyone got any idea on how to save meta data to file? I can't seem to get anything to work, and I can't find anything helpful by searching.

    This is what I've got so far:

    Code:java
    1. private void saveMeta(ItemStack item, Snapshot snap, int idx) {
    2. ObjectOutputStream oos = null;
    3. try {
    4. new File(getDataFolder() + "/meta").mkdirs();
    5. File file = new File(getDataFolder() + "/meta/" + snap.getUser() + "-" + snap.getName() + "-" + idx + ".sav");
    6. file.createNewFile();
    7. oos = new ObjectOutputStream(new FileOutputStream(file));
    8. oos.writeObject(item.getItemMeta().serialize());
    9. } catch (IOException ex) {
    10. getLogger().log(Level.WARNING, "Error saving meta data for {0}''s snapshot {1} at {2}", new Object[]{snap.getUser(), snap.getName(), idx});
    11. getLogger().log(Level.WARNING, null, ex);
    12. } finally {
    13. if (oos != null) {
    14. try {
    15. oos.close();
    16. } catch (IOException ex) {
    17. getLogger().log(Level.WARNING, "Error saving meta data for {0}''s snapshot {1} at {2}", new Object[]{snap.getUser(), snap.getName(), idx});
    18. getLogger().log(Level.WARNING, null, ex);
    19. }
    20. }
    21. }
    22. }


    Which seems to work until it reaches an org.bukkit.FireworkEffect, at which point it throws a NotSerializableException, so I'm expecting to have to scrap this method completely.
     
  2. Offline

    TomFromCollege

    Why is org.bukkit.FireworkEffect in the Metadata?
    I'm guessing the ItemStack is a firework (Guessing)
    if this is the case, handle it seperately?
    if(item.getType() == Material.FIREWORK)
     
  3. Offline

    Blir

    But how would I save it other than serialization?
     
  4. Offline

    Comphenix

    Just use YamlConfiguration:
    Code:java
    1. File file = new File("C:\\Temp\\Test.yaml");
    2.  
    3. ItemStack test = new ItemStack(Material.GOLD_HOE);
    4. ItemMeta meta = test.getItemMeta();
    5.  
    6. meta.setDisplayName("Immoral Hoe");
    7. test.setItemMeta(meta);
    8.  
    9. // Saving item meta (and item stacks in general)
    10. YamlConfiguration saving = new YamlConfiguration();
    11. saving.set("meta", meta);
    12. saving.save(file);
    13.  
    14. // Loading it again
    15. YamlConfiguration loading = new YamlConfiguration();
    16. loading.load(file);
    17.  
    18. ItemMeta loaded = (ItemMeta) loading.get("meta");
    19.  
    20. System.out.println("Saved: " + meta);
    21. System.out.println("Loaded: " + loaded);
     
  5. Offline

    Blir

    Oh okay. I'm not very familiar with yaml stuff, but I'll give it a shot.

    Edit: It took me ages, but I got it! Thanks Comphenix :D
     
    Comphenix likes this.
Thread Status:
Not open for further replies.

Share This Page