Posted July 20, 201510 yr I have a class that contains BlockPos, IBlockState, and world Id. Is there a way to save this type of data in a NBTTagCompound? className is named myBlockPos
July 20, 201510 yr You can always split objects in to basic objects. All basic objects, you can save in nbt. For example BlockPos, can be split into int x, int y, int z . And IBlockState can be converted to int meta by using get metaFromState. Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
July 20, 201510 yr Just overwrite the method writeToNBT and readFromNBT to save/load your data. For example: @Override public void writeToNBT(NBTTagCompound nbtTagCompound) { super.writeToNBT(nbtTagCompound); nbtTagCompound.setInteger("WorldID", world.getId()) ........ }
July 20, 201510 yr Just overwrite the method writeToNBT and readFromNBT to save/load your data. For example: @Override public void writeToNBT(NBTTagCompound nbtTagCompound) { super.writeToNBT(nbtTagCompound); nbtTagCompound.setInteger("WorldID", world.getId()) ........ } I think he has custom class and no writeToNbt and readToNbt methods (and maybe no super methods). And he asks about how he can save "complex" variables like objects to nbt. Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
July 20, 201510 yr Author Would it be possible to save custom classes? It would be easier in the long run and I would also learn something.
July 20, 201510 yr Would it be possible to save custom classes? It would be easier in the long run and I would also learn something. Yes, but it dpends on how and where: Per dimension? Per world? Per mc? Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
July 20, 201510 yr I think you can save byte arrays in nbt. So you could serialize your class and save the byte array. (If byte array dont works I am pretty sure you can save int arrays, so convert byte to int isnt that hard)
July 20, 201510 yr Author I've got the read and write methods working. Where do I call them though? In a event handler when the world loads?
July 20, 201510 yr Author I have a list of blocks and block locations that are suppose to regenerate in specific areas. I have a command that I can use to add them to a list and once I save and quit, I would like to store this information so that it may be used the next time I load into the world.
July 21, 201510 yr Author Use WorldSavedData. Here is an Example. What is the purpose of having a nextQuestId and fracturerUsed?
July 21, 201510 yr Author Alright I think I'm getting somewhere. I've noticed that you didn't use the methods nbt.setTag or nbt.getTag inside readFromNBT and writeToNBT. I also see that you are using a method inside QuestologyWorldData get(World world), called world.setItemData(IDENTIFIER, data) Why not use nbt.setTag/getTag and what is "world.setItemData" Also, is markDirty() saving the information? Thank you! Sorry for the load of questions!
July 21, 201510 yr Author When I use my command to add new information, it runs through the write method and adds the data I want it to inside of the nbt and It also works correctly while reading the information. But, when I restart the game, it doesn't find anything there. My only guess would be that the world Data information I am using is different every time I load the game. Here is some code. Event Handler @SubscribeEvent public void onWorldLoad(WorldEvent.Load event){ PacketDispatcher.sendToAll(new SyncWorldData(event.world)); } PacketDispatcher / sendTo methods public class PacketDispatcher { // a simple counter will allow us to get rid of 'magic' numbers used during packet registration private static byte packetId = 0; private boolean used; /** * The SimpleNetworkWrapper instance is used both to register and send packets. * Since I will be adding wrapper methods, this field is private, but you should * make it public if you plan on using it directly. */ private static final SimpleNetworkWrapper dispatcher = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID); /** * Call this during pre-init or loading and register all of your packets (messages) here */ public static final void registerPackets() { registerMessage(SyncMiningStats.class); registerMessage(SyncExcavationStats.class); registerMessage(SyncWoodCuttingStats.class); registerMessage(SyncWorldData.class); } /** * Registers an {@link AbstractMessage} to the appropriate side(s) */ private static final <T extends AbstractMessage<T> & IMessageHandler<T, IMessage>> void registerMessage(Class<T> clazz) { // We can tell by the message class which side to register it on by using #isAssignableFrom (google it) // Also, one can see the convenience of using a static counter 'packetId' to keep // track of the current index, rather than hard-coding them all, plus it's one less // parameter to pass. if (AbstractMessage.AbstractClientMessage.class.isAssignableFrom(clazz)) { PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.CLIENT); } else if (AbstractMessage.AbstractServerMessage.class.isAssignableFrom(clazz)) { PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER); } else { // hopefully you didn't forget to extend the right class, or you will get registered on both sides PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId, Side.CLIENT); PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER); } } //========================================================// // The following methods are the 'wrapper' methods; again, // this just makes sending a message slightly more compact // and is purely a matter of stylistic preference //========================================================// /** * Send this message to the specified player's client-side counterpart. * See {@link SimpleNetworkWrapper#sendTo(IMessage, EntityPlayerMP)} */ public static final void sendTo(IMessage message, EntityPlayerMP player) { PacketDispatcher.dispatcher.sendTo(message, player); } /** * Send this message to everyone. * See {@link SimpleNetworkWrapper#sendToAll(IMessage)} */ public static void sendToAll(IMessage message) { PacketDispatcher.dispatcher.sendToAll(message); } /** * Send this message to everyone within a certain range of a point. * See {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)} */ public static final void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) { PacketDispatcher.dispatcher.sendToAllAround(message, point); } /** * Sends a message to everyone within a certain range of the coordinates in the same dimension. * Shortcut to {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)} */ public static final void sendToAllAround(IMessage message, int dimension, double x, double y, double z, double range) { PacketDispatcher.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z, range)); } /** * Sends a message to everyone within a certain range of the player provided. * Shortcut to {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)} */ public static final void sendToAllAround(IMessage message, EntityPlayer player, double range) { PacketDispatcher.sendToAllAround(message, player.worldObj.provider.getDimensionId(), player.posX, player.posY, player.posZ, range); } /** * Send this message to everyone within the supplied dimension. * See {@link SimpleNetworkWrapper#sendToDimension(IMessage, int)} */ public static final void sendToDimension(IMessage message, int dimensionId) { PacketDispatcher.dispatcher.sendToDimension(message, dimensionId); } /** * Send this message to the server. * See {@link SimpleNetworkWrapper#sendToServer(IMessage)} */ public static final void sendToServer(IMessage message) { PacketDispatcher.dispatcher.sendToServer(message); } } SnycWorldData Class public class SyncWorldData extends AbstractClientMessage<SyncWorldData>{ private NBTTagCompound world_data; public SyncWorldData(){} public SyncWorldData(World world){ world_data = new NBTTagCompound(); WorldData.get(world).writeToNBT(world_data); //get method public static WorldData get(World world){ WorldData data = (WorldData)world.loadItemData(WorldData.class, Identifier); if (data == null){ data = new WorldData(); world.setItemData(Identifier, data); } return data; } //WriteToNBT method public void writeToNBT(NBTTagCompound nbt) { NBTTagList tagList = new NBTTagList(); NBTTagCompound tag = new NBTTagCompound(); for(Tuple test : currentRespawnLocs.keySet()){ BlockPos pos = test.getPos(); IBlockState state = test.getState(); int blockId = Requirements.getBlockId(state); int blockMeta = Requirements.getBlockMeta(state); int worldId = test.getWorldId(); //STORES INFORMATION IN ARRAY OF INTS int[] tempPos = new int[]{pos.getX(),pos.getY(),pos.getZ(), blockId, blockMeta, worldId}; posArray.add(tempPos); } for(int i = 0; i < posArray.size(); i++){ int[] currentMyBlockPos = posArray.get(i); if(currentMyBlockPos != null && nbt.getIntArray("myBlockPos" + i) != null){ tag.setIntArray("myBlockPos" + i, currentMyBlockPos); //APPEND IF TagList HAS NO LENGTH YET if(tagList.hasNoTags()) tagList.appendTag(tag); else tagList.set(0, tag); } } //STORE TagList INSIDE NBT nbt.setTag(Identifier, tagList); } } protected void read(PacketBuffer buffer) throws IOException{ world_data = buffer.readNBTTagCompoundFromBuffer(); } protected void write(PacketBuffer buffer) throws IOException{ buffer.writeNBTTagCompoundToBuffer(world_data); } public void process(EntityPlayer player, Side side){ WorldData.get(player.worldObj).readFromNBT(world_data); //READ /** This undoes what the write method does. It takes all the integers and converts them to blockPos, an IBlockState, and worldId. It then adds them to the hashMap currentRespawnLocs(a public class variable. */ public void readFromNBT(NBTTagCompound nbt) { NBTTagList tagList = nbt.getTagList(Identifier, Constants.NBT.TAG_COMPOUND); for(int i = 0; tagList.getCompoundTagAt(0).hasKey("myBlockPos" + i); i++){ int[] currentMyBlockPos = tagList.getCompoundTagAt(0).getIntArray("myBlockPos" + i); BlockPos pos = new BlockPos(currentMyBlockPos[0], currentMyBlockPos[1], currentMyBlockPos[2]); int blockId = currentMyBlockPos[3]; int blockMeta = currentMyBlockPos[4]; int worldId = currentMyBlockPos[5]; Block tempBlock = Block.getBlockById(blockId); IBlockState state = tempBlock.getStateFromMeta(blockMeta); currentRespawnLocs.put(new Tuple(this.pos, this.state, this.worldId), true); } } } } I know that this is a lot of information. I don't expect you, nor anyone, to read all of it. I am just curious if you can spot any errors. The packetDispatcher should be just fine, as I have used it to store information with players.
July 21, 201510 yr Author Your code is hard to follow, please post the classes on pastebin, with syntax highlighting. And as separate things, not intertwined and with spoilers like you did. Will do:p Also... why are you using NBT to send your data? You tell me. haha! I haven't used Pastebin before. Is this how I share it? http://pastebin.com/u/Atijaf
July 21, 201510 yr Author Event handler for WorldData http://pastebin.com/3GQee1TY PacketDispatcher. http://pastebin.com/mYgCMeVH WorldData Class http://pastebin.com/RU0fXDcK Tuple Class http://pastebin.com/Xw2H5iNU
July 21, 201510 yr Author There was one more class that I forgot to include in pastebin. Also, every time i start minecraft and debug. It goes through the method, WorldData get(World world), and data is null so it creates a new one. Is that suppose to happen? I also never call the method, setDirty(); I went ahead and changed the static variable to a private class variable. No change... I'll leave it a private class variable though Here's my SyncWorldData class http://pastebin.com/dgNBV6G2
July 21, 201510 yr Author alright. I'll give that a go. Would I call that method in the writeToNBT method?
July 21, 201510 yr Author I've messed with the code a little bit and created a method called saveData. I call this method after I add data to nbt. public void saveData(World world){ WorldData data = (WorldData) world.loadItemData(WorldData.class, Identifier); world.setItemData(Identifier, data); data.markDirty(); world.getPerWorldStorage().saveAllData(); } I run that bit of code, save and quit, reload, and data is null again. The only thing that I can think of that would be wrong is the retrieval of the information.. Or maybe even saving it. Is there anything wrong with this bit of code. I am coding in 1.8, if that makes a difference.
July 21, 201510 yr Author I apologize! Haha. How long should I allow it to sit until it saves? I used markDirty and waited about 5 minutes to see if it had changed, but it was still markedDirty when I checked it.
July 22, 201510 yr Author It will save when you either exist the world or open the ingame menu. In multiplayer it will save at the intervals set in the config. Thanks for explaining that. That's what I was worried about... I am thinking that I probably missed something obvious near the beginning. After I use markDirty(), it sets dirty to true. I then save it. Re enter the command to add more data and before it even gets to the mothod, markDirty(), dirty is still true. (I'm gussing dirty is suppose to be switched back to false when it saves) I will look at my code for a while and possibly make a new thread that deals more with WorldSavedData.
July 22, 201510 yr Author All I can figure out is that the method "readFromNBT(NBTTagCompound nbt) is not getting called automatically, but when I manually call it, nbt has nothing saved in it. Am I suppose to register WorldData? or just use the method "public static WorldData get(World world). Also, when the command to set a few variables is used and I use the method "nbt.setTag(Identifier, tagList)" nbt gets data, and then closes. If I call the method again, nbt has no data again, as if I never used the command once. It's weird...
July 22, 201510 yr You probably want to make all variables statis, make setters for them and in the setters call markDirty(). Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support. 1.12 -> 1.13 primer by williewillus. 1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support. http://www.howoldisminecraft1710.today/
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.