Jump to content

Recommended Posts

Posted

Im about to start working on a project that involves taking an in game structure and writing it to a file (including tile nbt data). Then reading that file and turning it back into an in game structure in much the same way the world edit plugin works. Given enough tile i can figure it all out myself but i am very new to file io with java so i am just wondering if anyone can point me to some good tutorials dealing with stuff like this.

I am the author of Draconic Evolution

Posted

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.

Posted

Thanks that will be a massive help!

 

Im also interested in information about actually writing that data to a file however im sure thats just simple java stuff that i can figure out myself.

 

That won't cut it for 1.7 and onwards, as IDs are dynamically allocated per world.

I can probably just modify it to use the name instead of the id. Would be a larger file but im not too concerned about that.

 

Edit: or i could add a key that assigns the id of every type of block in the structure to the name of the block.

 

I am the author of Draconic Evolution

Posted

Yeah, FileIO is just regular java.  Real easy stuff.  Especially with the existence of the CompressedStreamTools (IIRC the class name).

 

I'm completely failing to locate the code snipped that helped me get started, unfortunately.

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.

Posted

Yeah, FileIO is just regular java.  Real easy stuff.  Especially with the existence of the CompressedStreamTools (IIRC the class name).

 

I'm completely failing to locate the code snipped that helped me get started, unfortunately.

Thanks. I have just started researching file io. There are still some unknowns but from what iv got so far it looks simple enough.

 

My biggest concern when learning to do something like this is that i will learn how to do it one way. And there will be another easier and more efficient way that i missed.

 

Edit: You proved my point exactly! I was trying to figure out how to turn a structure into some sort of data format that i could read and write with FileInputStream and FileOutputStream. But with CompressedStreamTools it looks like i can just use an NBTCompound!

I am the author of Draconic Evolution

Posted

Ooh, io is something I really like in java. Anyway, you could use BrufferedWriters and BufferedReaders, as far as I know that is a vary efficient way. But to make it easier I would recommend using Json. Within minecraft, there is an api called Gson that can convert java objects to a Json file.

 

Here is some info/tutorials for the things mentioned above:

- Gson tutorial: http://www.mkyong.com/java/gson-streaming-to-read-and-write-json/

- BufferedWriters: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

- BufferedReaders: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

 

Hope this helps, if there is any confusion about the tutorials listed just ask. :)

Posted

Thankyou for all your help! If it wasn't for all your help i would have spent hours messing with input and out put streams and who knows what else... But as it turns out this is going to be the easiest part of the hole project!

 

@Awesome_Spider I will keep those links and look at them later because im sure that will come in handy at some point. But i am in love with the idea of writing the structure to nbt because nbt is soo easy to use. So i think i will be going with CompressedStreamTools.

 

Unless i take a look at what you posted and like it even more... Actually now that i think about it being able to write an object to a file would probably be easier... Will have to take a good look at both before i decide for sure.

I am the author of Draconic Evolution

Posted

Edit: You proved my point exactly! I was trying to figure out how to turn a structure into some sort of data format that i could read and write with FileInputStream and FileOutputStream. But with CompressedStreamTools it looks like i can just use an NBTCompound!

 

A schematic file is just an .nbt with a different extension ;) but with a known set of root-level keys.

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.

Posted

Edit: You proved my point exactly! I was trying to figure out how to turn a structure into some sort of data format that i could read and write with FileInputStream and FileOutputStream. But with CompressedStreamTools it looks like i can just use an NBTCompound!

 

A schematic file is just an .nbt with a different extension ;) but with a known set of root-level keys.

 

What i meant by that is i would have spent hours trying to make something that dose exactly what CompressedStreamTools dose.

 

So after looking at both methods i think i will be going with CompressedStreamTools because it looks easier to deal with then Gson.

I am the author of Draconic Evolution

Posted

One more question. I noticed in the Schematic file format the blocks are saved in an int array. But it occurred to me that the max value of a byte is 127 or 256 if you take advantage of the full range. However with mods block id's go much higher then that. Im just wondering if anyone knows how things like world edit get around that?

 

The only thing i can think of is to use an int array instead but im sure there is a better way because an int for each block would be very inefficient. Or is an int only as big as its value when saved to a file? Tested and now know that an int is infact bigger on disk then a byte even if it has the same value. Not that i think about it that was probably a stupid question.

I am the author of Draconic Evolution

Posted

Well i dont care at all about schematics but i do care a little about the size of the file. 2 byte arrays would be half the size of an int array. But then again the schematics wont be that big anyway so in the long run it may not matter too much.

 

And on second thought i dont need to learn byte manipulation to use two bytes i can probably just use a math function. Now i wish i paid more attention in maths class lol.

 

Edit: I guess i did pay attention in math class!!

int value;

byte b1 = (byte)(value % 127);
byte b2 = (byte)(value / 127);

int result = (b2 * 127) + b1;

I am the author of Draconic Evolution

  • 3 years later...
Posted
On 2/20/2015 at 12:23 PM, Awesome_Spider said:

Ooh, io is something I really like in java. Anyway, you could use BrufferedWriters and BufferedReaders, as far as I know that is a vary efficient way. But to make it easier I would recommend using Json. Within minecraft, there is an api called Gson that can convert java objects to a Json file.

 

Here is some info/tutorials for the things mentioned above:

- Gson tutorial: http://www.mkyong.com/java/gson-streaming-to-read-and-write-json/

- BufferedWriters: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

- BufferedReaders: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

 

Hope this helps, if there is any confusion about the tutorials listed just ask. :)

Thanks @Aewsome_Spider for the tutorials to BufferedReaders and Writers. I found a better tutorial for reading files in Java - goes over a lot more ways to read files and even tests which way is fastest. 

 

Java file reading tutorial: https://funnelgarden.com/java_read_file/

 

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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