Thread: Small stuff for RuneSource.. Snipplets..

Results 1 to 3 of 3
  1. #1 Small stuff for RuneSource.. Snipplets.. 
    Registered Member
    Join Date
    Nov 2010
    Posts
    262
    Thanks given
    9
    Thanks received
    17
    Rep Power
    15
    Remember this is snipplets! No crazy features here
    Though what is here, is some minor methods that is useful when working with runesource.
    I'll expand as I go on...

    Notes:
    * Some of these are from classes I made, which isn't included in the original runesource.
    * Buffer sizes may need to be tuned, and I used some kind of reference for most of these (other servers, and my own notes).
    * There is always a chance something is wrong, or ineffeciently (i tried to make it as efficient as possible though). Please notice me so I can correct it, if you find anything.

    Updating the friendserver:
    Code:
        /**
         * Friend server friends list loading status. Loading = 0 Connecting = 1 OK = 2
         *
         * @param status
         *          the status: OK = 2 Connecting = 1 Loading = 0
         */
        public void sendFriendServer(int status) {
            StreamBuffer.OutBuffer out = StreamBuffer.newOutBuffer(221);
            out.writeHeader(player.getEncryptor(), 221);
            out.writeByte(status);
            player.getPlayer().send(out.getBuffer());
        }
    Long -> username:
    Code:
            /**
    	 * Converts a long to a name.
    	 * @param l The long.
    	 * @return The name.
    	 */
    	public static String longToName(long l) {
    		int i = 0;
    		char ac[] = new char[12];
    		while (l != 0L) {
    			long l1 = l;
    			l /= 37L;
    			ac[11 - i++] = validChars[(int) (l1 - l * 37L)];
    		}
    		return new String(ac, 12 - i, i);
    	}
    Validchars array:
    Code:
            /**
    	 * An array of valid characters in a long username.
    	 */
    	public static final char validChars[] = { '_', 'a', 'b', 'c', 'd',
    		'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
    		'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
    		'4', '5', '6', '7', '8', '9', '!', '@', '#', '$', '%', '^', '&',
    		'*', '(', ')', '-', '+', '=', ':', ';', '.', '>', '<', ',', '"',
    		'[', ']', '|', '?', '/', '`' };
    Adding a friend (var friends is a list of longs):
    Code:
        /**
         * Adds a player to friends list
         *
         * @param player
         *            The player who owns the friend list.
         * @param name
         *            The player we're adding to friendlist.
         */
        public void addFriend(long name) {
        	if (friends.size() >= 200) {
                player.sendMessage("Your friends list is full.");
                return;
        	}
            if (Misc.longToName(name).equals(player.getUsername())) {
                player.sendMessage("You cannot add yourself, to your own friendlist.");
                return;
            }
        	if (friends.contains(name)) {
                String nameAsString = Misc.longToName(name);
                player.sendMessage(nameAsString + " is already on your friends list.");
                return;
        	}
        	friends.add(name);
        	player.getActionSender().sendFriend(name, getWorld(name));
        }
    Sending a friend:
    Code:
        /**
         * Sends a friend to the friend list.
         *
         * @param name
         *            The name as a long.
         * @param world
         *            The world id.
         */
        public void sendFriend(long name, int world) {
            StreamBuffer.OutBuffer out = StreamBuffer.newOutBuffer(50);
        	if (world != 0) {
        		world += 9;
        	}
            out.writeHeader(player.getEncryptor(), 50);
            out.writeLong(name);
            out.writeByte(world);
            player.getPlayer().send(out.getBuffer());
        }
    Saving friends (getFriends() returns a list of longs, that is the friends):
    Code:
                    // Player friends.
    		writer.write("[friends]:");
    		writer.newLine();
                    writer.write("" + player.getPrivateMessage().getFriends().size()); // The amount of friends.
                    writer.newLine();
                    for (final Long friend : player.getPrivateMessage().friends) {
                        if (friend != null) {
                            String friendAsString = Misc.longToName(friend);
                            writer.write(friendAsString);
                            writer.newLine();
                        }
                    }
    Loading friends:
    Code:
                    reader.readLine(); // Friends line.
                    int friendAmount = Integer.parseInt(reader.readLine());
                    for (int i = 0; i < friendAmount; i++) {
    			String friend = reader.readLine();
                            long friendAsLong = Misc.nameToLong(friend);
                            player.getPrivateMessage().friends.add(friendAsLong);
    		}
    Writing an array of bytes, to the buffer (didnt find this functionality in the current streambuffer - i might have overlooked it):
    Code:
                    /**
                     * Puts a sequence of bytes in the buffer.
                     *
                     * @param data
                     * @param offset
                     * @param length
                     */
                    public void writeBytes(byte[] data, int offset, int length) {
                            for (int i = offset; i < offset + length; i++) {
                                writeByte(data[i]);
                            }
                    }
    Reading an array of bytes, to the buffer (didnt find this functionality in the current streambuffer - i might have overlooked it):
    Code:
                    /**
                     * Reads an array of bytes to the buffer
                     * @param data
                     *          the bytes you want to read
                     * @param amount
                     *          the amount of bytes
                     * @param type
                     *          the type
                     * @return a buffer filled with the data
                     */
                    public byte[] readBytes(byte[] data, int amount, ValueType type) {
    			for (int i = 0; i < amount; i++) {
    				data[i] = (byte) readByte(type);
    			}
    			return data;
    		}
    Finding the world of a player (used in a packet for private-messaging):
    Code:
        /**
         * Get the world number of friend
         *
         * @param friend
         *            The friend
         * @return World number
         */
        private int getWorld(long friend) {
        	for (final Player p : PlayerHandler.getPlayers()) {
        		if (p != null) {
                        if (Misc.nameToLong(p.getUsername()) == friend) {
                            return 1;
                        }
        		}
    	}
    	return 0;
        }
    Runescape toolkit - ultra-lightweight! Only 20 kb!
    Reply With Quote  
     

  2. #2  
    Registered Member Rub My Ox's Avatar
    Join Date
    Jan 2010
    Posts
    133
    Thanks given
    25
    Thanks received
    2
    Rep Power
    4
    Looks good, thanks.

    Reply With Quote  
     

  3. #3  
    Community Veteran


    Join Date
    Jun 2007
    Posts
    1,684
    Thanks given
    300
    Thanks received
    309
    Rep Power
    481
    Pretty useful, send them over to blake to see if he needs any
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. RuneSource 317
    By blakeman8192 in forum Projects
    Replies: 318
    Last Post: 03-18-2012, 07:54 PM
  2. [613+] RuneSource [613+]
    By vtothef in forum Projects
    Replies: 4
    Last Post: 10-29-2010, 06:16 PM
  3. Small stuff.
    By Kastro in forum Show-off
    Replies: 26
    Last Post: 04-17-2010, 04:34 PM
  4. Small stuff.
    By Kastro in forum Show-off
    Replies: 11
    Last Post: 10-29-2009, 03:55 AM
  5. Some small stuff
    By Dennis in forum Show-off
    Replies: 10
    Last Post: 01-05-2009, 01:54 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •