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