Serializing blocks

Discussion in 'Plugin Development' started by Amgis, Apr 17, 2014.

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


    I'd like to write my own serialize() function for Bukkit's Block to save and load blocks. I've seen other people attempt this, but I can't find much information on this subject.

    Obviously, the following data would need to be saved for each block:
    • its location
    • its material
    But should I also be saving it's data from Block.getData() ? What about metadata? Block faces? Light levels?
  2. Offline


    I used the BlockState of all the blocks which got destroyed in an explosion to regenerate them 3 seconds after it back. I think what you want will work with an BlockState ArrayList!

  3. Offline


    Hi Amgis :)

    You would need to store it to some sort of config file. First off, I'd start with the Location. Here is a example Sterilizer for locations:

    Location To String:
    1. public static String LocationToString(Location loc) {
    2. return loc.getWorld().getName()+","+loc.getBlockX()+","+loc.getBlockY()+","+loc.getBlockZ()+","+loc.getPitch()+","+loc.getYaw();
    3. }

    String To Location:
    1. public static Location StringToLocation(String s) {
    2. String stoLoc[] = s.split("\\,");
    3. Location loc = new Location(Main.instance.getServer().getWorld(stoLoc[0]),0,0,0);
    4. loc.setX(Double.parseDouble(stoLoc[1]));
    5. loc.setY(Double.parseDouble(stoLoc[2]));
    6. loc.setZ(Double.parseDouble(stoLoc[3]));
    7. loc.setPitch(Float.parseFloat(stoLoc[4]));
    8. loc.setYaw(Float.parseFloat(stoLoc[5]));
    9. return loc;
    10. }

    Now, The Material. is easy too! We can use:
    1. Block block = event.getBlock();
    2. block.getType().toString();

    Example of adding it to the config:

    1. @EventHandler
    2. public void onPlace(BlockPlaceEvent event){
    3. org.bukkit.block.Block block = event.getBlock();
    4. Location loc = block.getLocation();
    5. String mat = block.getType().toString();
    6. FileConfiguration config = Main.instance.getConfig();
    7. config.set("block.loc", Utils.LocationToString(loc));
    8. config.set("block.type", mat);
    9. Main.instance.saveConfig();
    11. }

    Now, the .getData() I will have to look into that and I will get back to you ASAP.
    Hope what I have so far helps you!:)

  4. Offline


    Dahwn Serialization is the process of translating data structures into a format that can be written to a file. I'm not looking for a way to store blocks in the program, but rather in files.

    Thanks for the information. I've looked into this a bit more and I may use java's built-in serialization methods (using ObjectOutputStream), because saving upwards of a hundred thousand blocks in a config file would take up too much memory.
  5. Offline


    Amgis All you'd need to store is the location, type, and any block state data. The lighting level is unnecessary, because that's calculated on the fly depending on the block's surroundings.
Thread Status:
Not open for further replies.

Share This Page