Saving special blocks to a file

Discussion in 'Plugin Development' started by kralv, May 9, 2012.

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

    kralv

    I'm currently trying to save blocks to a file in order to be reloaded again in the future. Currently I am saving them using a simple Serializable class that saves the type id and the blocks metadata as a byte. Like this...
    Code:
    public class VBlock implements Serializable{
     
            int id = 0;
            byte data;
            private static final long serialVersionUID = 1L;
            public VBlock(int i, byte d){
                id=i;
                data=d;
            }
           
        }
    This works fine for normal blocks and things like half-blocks or colored wool. However, it does not save the text on a sign, or the items in a chest. Is there a way to correctly save more complicated blocks like these?
     
  2. let me extend your code(quick&dirty, may contain bugs, ...)
    Code:java
    1. public class VBlock implements Serializable
    2. {
    3. private final int id = 0;
    4. private final byte data;
    5. private final ItemStack[] inv; //ItemStack is serializebable, so we can store it here.
    6. private final String[] lines;
    7.  
    8. private static final long serialVersionUID = 1L;
    9.  
    10. public VBlock(Block block)
    11. {
    12. id=i;
    13. data=d;
    14. Material mat = block.getType();
    15. if(mat == Material.CHEST)
    16. {
    17. Inventory realInv = ((Chest)block.getState()).getBlockInventory();
    18. inv = new ItemStack(realInv.getSize());
    19. for(int i = 0; i < arrayInv.length; i++)
    20. inv[i] = realInv.getItem(i);
    21. lines = null;
    22. }
    23. else if(mat == Material.WALL_SIGN || mat == Material.SIGN_POST)
    24. {
    25. inv = null;
    26. lines = ((Sign)block.getState()).getLines();
    27. }
    28. else
    29. {
    30. inv = null;
    31. lines = null;
    32. }
    33.  
    34. public Block setMe(Location loc)
    35. {
    36. loc.getBlock().setTypeIdAndData(id, data, true);
    37. if(inv != null)
    38. {
    39. Chest chest = (Chest)block.getState();
    40. Inventory realInv = chest.getBlockInventory();
    41. for(int i = 0; i < inv.length; i++)
    42. inv.setItem(i, inv[i]);
    43. chest.update();
    44. }
    45. else if(lines != null)
    46. {
    47. Sign sign = (Sign)block.getState();
    48. for(int i = 0; i < 4; i++)
    49. sign.setLine(i, lines[i]);
    50. sign.update();
    51. }
    52. }
    53. }[/i][/i][/i]
     
  3. I am struggling saving a HashMap<Block, Integer> to a file,
    I know that the block needs to be serialized. Can someone please make an example on how to do this?
    More info: There are multiple blocks stored per Integer as well...

    Thanks in advance! :)
     
Thread Status:
Not open for further replies.

Share This Page