Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello Forge Community!

I was wondering how I can create an NBT file and write to it. I need the methods and what I have to set as the file name. I'm mostly confused on the "writing aspect of it." All help is appreciated.

Thanks for reading.

I poked around with this once a while back. There should be an NBTTools or NBTHelper (NBTCompressionTools?) class somewhere that deals with doing the actual File IO. You'd pretty much just need to invoke the methods in it and pass in the NBT and the file path.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

10 minutes ago, Draco18s said:

NBTTools or NBTHelper (NBTCompressionTools?)

CompressedStreamTools, yes.

 

41 minutes ago, Geometrically said:

I need the methods and what I have to set as the file name. I'm mostly confused on the "writing aspect of it."

A file can be anything you want, you can set anything as the "file name". All you need to do is write the NBT using methods from CompressedStreamTools into your FileOutputStream.

  • Author
1 minute ago, V0idWa1k3r said:

CompressedStreamTools, yes.

 

A file can be anything you want, you can set anything as the "file name". All you need to do is write the NBT using methods from CompressedStreamTools into your FileOutputStream.

I looked into it. I forgot to mention that I need to write it in a decompressed NBT format. The only method that I found that uses OutputStream/FileOutputStream is 

writeCompressed(NBTTagCompound compound, OutputStream outputStream);

But that only seems to write compressed. Can I use that for decompressed as well? I remember in 1.9 there was a method called "a" that did this. They seem to have removed this in 1.10.2.  Let me know if I can

1 minute ago, Geometrically said:

I looked into it. I forgot to mention that I need to write it in a decompressed NBT format. The only method that I found that uses OutputStream/FileOutputStream is 


writeCompressed(NBTTagCompound compound, OutputStream outputStream);

But that only seems to write compressed

 

There are safeWrite and write methods that write the NBT without compression.

  • Author

Okay, I tried using those methods and got this. The problem is that it doesn't even generate the file. Here is the code:

package com.geometrically.TestEnviorment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;

public class ServerSender {

	public static String fileLocation = ValveModelLoad.valveChecker.substring(ValveModelLoad.valveChecker.length() - 1);
	private final File file;
	private NBTTagCompound nbt;
	
	public ServerSender(File file){
		
		this.file = file;
			
	}
	  public ServerSender(String folder, String name) {
	        this(new File(folder, name + ".dat"));
	    }

	    public ServerSender(String path) {
	        this(new File(path));
	    }
	    public void write() {
	        try {

	            if(!file.exists()) {
	                file.createNewFile();
	            }

	            CompressedStreamTools.safeWrite(nbt, file);

	        } catch(IOException e) {
	            e.printStackTrace();
	        }

	    }
	    public static void main() {
	        
	        ServerSender file = new ServerSender(fileLocation, "storage.dat");
	        file.clear();
	       
	        file.write();
	    }
	    
	  
	    public void clear() {
	        nbt = new NBTTagCompound();
	    }
    
}

The fileLocation variable is the filelocation of the mod.

Your file end-name will be storage.dat.dat as you add the extension in the constructor.

Use the debugger to find out what's wrong. Put a breakpoint in your write method. I would say that if the file is not even being created then the issue most likely is within the fileLocation field so inspect it.

1 hour ago, V0idWa1k3r said:

CompressedStreamTools, yes.

Thanks. Last time I messed with it was 1.7 and I am not sure where that code is any more. And its been made entirely obsolete since (the purpose it served got written morebetter by someone else).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author
7 minutes ago, V0idWa1k3r said:

Your file end-name will be storage.dat.dat as you add the extension in the constructor.

Use the debugger to find out what's wrong. Put a breakpoint in your write method. I would say that if the file is not even being created then the issue most likely is within the fileLocation field so inspect it.

Okay, the code works now, how do I write data to the file. I need like a category and information

18 minutes ago, Geometrically said:

how do I write data to the file

You already are. The NBTTagCompound you are writing to the file is the 'data' you are writing. As to the specifications

19 minutes ago, Geometrically said:

I need like a category and information

Fill in the NBTTagCompound as you wish then. Do you know how NBT tags work? If you don't I suggest looking into vanilla classes that serialize/deserialize NBT, tileentities for example. 

It looks to me that you are writing some generic data into NBT. Is there a reason you are using NBT specifically? 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.