Adding output to txt file
Hello I have this method of code
Code:
public static final void loadNPCSpawns(int regionId) {
File file = new File("data/npcs/packedSpawns/" + regionId + ".ns");
if (!file.exists())
return;
try {
RandomAccessFile in = new RandomAccessFile(file, "r");
FileChannel channel = in.getChannel();
ByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0,
channel.size());
while (buffer.hasRemaining()) {
int npcId = buffer.getShort() & 0xffff;
int plane = buffer.get() & 0xff;
int x = buffer.getShort() & 0xffff;
int y = buffer.getShort() & 0xffff;
boolean hashExtraInformation = buffer.get() == 1;
int mapAreaNameHash = -1;
boolean canBeAttackFromOutOfArea = true;
if (hashExtraInformation) {
mapAreaNameHash = buffer.getInt();
canBeAttackFromOutOfArea = buffer.get() == 1;
}
World.spawnNPC(npcId, new WorldTile(x, y, plane),
mapAreaNameHash, canBeAttackFromOutOfArea);
}
channel.close();
in.close();
} catch (Throwable e) {
Logger.handle(e);
}
}
This loads files in .ns format and then reads the npcid and coords etc. What I want to do is when it reads the .ns file I want it to write the files contents to another text file. I have been trying to do this with little success this is the code that I am trying to add. Not sure if this is the correct/best way to do this.
Code:
BufferedWriter out = new BufferedWriter(fstream);
out.write(npcId, new WorldTile(x, y, plane),
mapAreaNameHash, canBeAttackFromOutOfArea);
Thanks for any help.
Re: Adding output to txt file
Wow, this looks a lot like a Runescape bot script or some kind of bot method to me.
So what you are really trying to do is serialize (i.e. flatten) the WorldTile instance. Since you probably want it readable, you would be well served but implementing a toString() method in WorldTile that creates a string containing all the instance data.
Re: Adding output to txt file
Quote:
Originally Posted by
jlczuk
Wow, this looks a lot like a Runescape bot script or some kind of bot method to me.
So what you are really trying to do is serialize (i.e. flatten) the WorldTile instance. Since you probably want it readable, you would be well served but implementing a toString() method in WorldTile that creates a string containing all the instance data.
Hello thanks for the reply! It is for Runescape but not a bot. Its for my rsps. The source that I downloaded has all the spawns packed into a file and I'm trying to unpack them. Could you maybe give me a small example of implementing tostring() method that I could work off of?
Re: Adding output to txt file
I can do that. This example shows the general idea, but I'm sure it isn't exactly what you want. The returned string will be a non-delimited sequence of characters, so you won't really be able to use it. You need to devise a protocol for how you want the flattened object represented it so you can create a new instance from that representation when you read it back in.
Code:
class Example {
int i=1;
int j=2;
char c='a';
String name="fubar";
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(i);
sb.append(j);
sb.append(c);
sb.append(name);
return sb.toString(); //ironically, using toString here too
}
}