I'm developing a mod for 1.8 Minecraft Forge that implements a custom block, let's call it NewbieBlock. In the mod, though, I need to keep and maintain a Collection of the coordinates of all NewbieBlocks that are placed or destroyed. Simple description is, onBlockPlacedBy => store the coordinates of that NewbieBlock in the Collection; onBlockHarvested => remove the harvested NewbieBlock's coordinates from the Collection.
I've gotten my mod working - it does everything I need it to, with one pivotal exception: when I quit the singleplayer game (or, the multiplayer dedicated server restarts), upon re-entering the game that coordinates Collection is now empty, and thus useless - my mod then no longer does much of anything.
I'd expect that though - Java's garbage collection handles that. But I also figure that Minecraft (or Forge specifically) must have some system with which it can store such data, to bring it back the next time you load up the game. I discovered some description of NBT for Minecraft but am not clear on whether that data must be associated with a block or not, and its usage is not very clear to me either - despite much Google research I haven't come across a tutorial on this matter. (Perhaps I just don't know what I'm looking for, or where to find it.)
TL;DR:
Given a Java object variable, for example,
HashMap<String, BlockPos> newbieBlocks = new HashMap<String, BlockPos>();
How would I go about saving and loading the contents of this HashMap so that it persists through Minecraft gameplay sessions and server restarts? IE when the server starts, it reads the information from its save, and when the server stops, it writes it back to disk to store whatever changes occurred.
Thank you muchly in advance! I'm going to scan through this forum to see if there are any unresolved threads I might be able to assist with.
It sounds like there are two main issues you need to solve. The first is finding a way to write the map to the hard drive, so that it still exists after the game is restarted. There are a few ways to do that, write it to a custom file format, use Gson, or maybe even write it to the world file. It's up to you. There are thousands of posts on reading/writing information to the hard disk in Java. The second issue is making sure that the client side always has the most accurate list of entries. This can be done using the packet system. There are a few great tutorials that explain how to use them in Minecraft. Packets are arguably less important, and may not even be an issue for you, depending on what you want to accomplish. The main reason to use them would be to ensure that the map on the server, and all clients are the same.
I had this exact same problem. Make a class extending WorldSavedData. In the read and write from nbt methods you will probably want to write the size of the Collection first and then all of it... Now just make a static instance of it somewhere. Everytime you change the list you will probably want to call the method markDirty on it.
You should also have an event handler. So, TextureData is my class extending WorldSavedData
@SubscribeEvent
public void load(WorldEvent.Load event){
if(!event.world.isRemote){
TextureData data = ((TextureData) world.perWorldStorage.loadData(TextureData.class,"aniwhat"));
if(data == null){
textureData = new TextureData("aniwhat"); //textureData should be the static instance of TextureData.
I'm developing a mod for 1.8 Minecraft Forge that implements a custom block, let's call it NewbieBlock. In the mod, though, I need to keep and maintain a Collection of the coordinates of all NewbieBlocks that are placed or destroyed. Simple description is, onBlockPlacedBy => store the coordinates of that NewbieBlock in the Collection; onBlockHarvested => remove the harvested NewbieBlock's coordinates from the Collection.
I've gotten my mod working - it does everything I need it to, with one pivotal exception: when I quit the singleplayer game (or, the multiplayer dedicated server restarts), upon re-entering the game that coordinates Collection is now empty, and thus useless - my mod then no longer does much of anything.
I'd expect that though - Java's garbage collection handles that. But I also figure that Minecraft (or Forge specifically) must have some system with which it can store such data, to bring it back the next time you load up the game. I discovered some description of NBT for Minecraft but am not clear on whether that data must be associated with a block or not, and its usage is not very clear to me either - despite much Google research I haven't come across a tutorial on this matter. (Perhaps I just don't know what I'm looking for, or where to find it.)
TL;DR:
Given a Java object variable, for example,
How would I go about saving and loading the contents of this HashMap so that it persists through Minecraft gameplay sessions and server restarts? IE when the server starts, it reads the information from its save, and when the server stops, it writes it back to disk to store whatever changes occurred.
Thank you muchly in advance! I'm going to scan through this forum to see if there are any unresolved threads I might be able to assist with.
It sounds like there are two main issues you need to solve. The first is finding a way to write the map to the hard drive, so that it still exists after the game is restarted. There are a few ways to do that, write it to a custom file format, use Gson, or maybe even write it to the world file. It's up to you. There are thousands of posts on reading/writing information to the hard disk in Java. The second issue is making sure that the client side always has the most accurate list of entries. This can be done using the packet system. There are a few great tutorials that explain how to use them in Minecraft. Packets are arguably less important, and may not even be an issue for you, depending on what you want to accomplish. The main reason to use them would be to ensure that the map on the server, and all clients are the same.
Farewell everyone o/
I had this exact same problem. Make a class extending WorldSavedData. In the read and write from nbt methods you will probably want to write the size of the Collection first and then all of it... Now just make a static instance of it somewhere. Everytime you change the list you will probably want to call the method markDirty on it.
You should also have an event handler. So, TextureData is my class extending WorldSavedData
@SubscribeEvent
public void load(WorldEvent.Load event){
if(!event.world.isRemote){
TextureData data = ((TextureData) world.perWorldStorage.loadData(TextureData.class,"aniwhat"));
if(data == null){
textureData = new TextureData("aniwhat"); //textureData should be the static instance of TextureData.
world.perWorldStorage.setData("aniwhat",textureData);
}else{
textureData = data;
}
}
All of this will save to world/data/aniwhat.dat/aniwhat
Animate your minecraft builds with Animated Structures: Link Removed