Results 1 to 4 of 4
  1. #1
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Adding output to txt file

    Hello I have this method of code
    Java 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.
    Java Code:
      			BufferedWriter out = new BufferedWriter(fstream);
      			out.write(npcId, new WorldTile(x, y, plane),
    					mapAreaNameHash, canBeAttackFromOutOfArea);
    Thanks for any help.

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

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

  3. #3
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Adding output to txt file

    Quote Originally Posted by jlczuk View Post
    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?

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

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

    Java 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
    	}
    }

Similar Threads

  1. Replies: 0
    Last Post: 02-22-2011, 10:33 PM
  2. Adding line numbers to code in output file
    By misterwebb in forum New To Java
    Replies: 2
    Last Post: 02-14-2011, 12:37 AM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •