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? Thanks.
Amgis 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! -Dahwn
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: Code:java public static String LocationToString(Location loc) {return loc.getWorld().getName()+","+loc.getBlockX()+","+loc.getBlockY()+","+loc.getBlockZ()+","+loc.getPitch()+","+loc.getYaw();} String To Location: Code:java public static Location StringToLocation(String s) {String stoLoc[] = s.split("\\,");Location loc = new Location(Main.instance.getServer().getWorld(stoLoc[0]),0,0,0);loc.setX(Double.parseDouble(stoLoc[1]));loc.setY(Double.parseDouble(stoLoc[2]));loc.setZ(Double.parseDouble(stoLoc[3]));loc.setPitch(Float.parseFloat(stoLoc[4]));loc.setYaw(Float.parseFloat(stoLoc[5]));return loc;} Now, The Material. is easy too! We can use: Code:java Block block = event.getBlock();block.getType().toString(); Example of adding it to the config: Code:java @EventHandlerpublic void onPlace(BlockPlaceEvent event){org.bukkit.block.Block block = event.getBlock();Location loc = block.getLocation();String mat = block.getType().toString();FileConfiguration config = Main.instance.getConfig();config.set("block.loc", Utils.LocationToString(loc));config.set("block.type", mat);Main.instance.saveConfig(); } 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! Thanks, -CaLxCyMru
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. CaLxCyMru 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.
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.