Skip to content

Commit 8d12304

Browse files
committed
refactor: create FriendsList and convert client to use it
1 parent 25cd580 commit 8d12304

File tree

8 files changed

+228
-117
lines changed

8 files changed

+228
-117
lines changed

src/main/java/org/runejs/client/Game.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.runejs.client.cache.CacheArchive;
55
import org.runejs.client.cache.CacheFileChannel;
66
import org.runejs.client.cache.cs.ClientScript;
7+
import org.runejs.client.chat.FriendList;
78
import org.runejs.client.chat.SocialList;
89
import org.runejs.client.frame.*;
910
import org.runejs.client.frame.console.Console;
@@ -75,6 +76,8 @@ public class Game {
7576
*/
7677
public static final CutsceneCamera cutsceneCamera = new CutsceneCamera();
7778

79+
public static FriendList friendList;
80+
7881
public static final SocialList ignoreList = new SocialList(100);
7982

8083
public static int anInt784 = 0;
@@ -676,7 +679,11 @@ public static void setConfigToDefaults() {
676679
}
677680
}
678681
MovedStatics.interactiveObjectTemporaryNodeCache = new LinkedList();
679-
Player.friendsCount = 0;
682+
683+
// friends list size depends on membership status
684+
int friendsListSize = MovedStatics.anInt1049 == 1 ? 200 : 100;
685+
Game.friendList = new FriendList(200, friendsListSize);
686+
680687
Player.friendListStatus = 0;
681688
GameInterface.resetInterface(ChatBox.dialogueId);
682689
ChatBox.dialogueId = -1;

src/main/java/org/runejs/client/MovedStatics.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,8 +2972,11 @@ else if(i < 101)
29722972
i--;
29732973
else
29742974
i -= 101;
2975-
addActionRow(English.remove, 0, 0, 0, ActionRowType.REMOVE_FRIEND.getId(), Native.white + Player.friendUsernames[i]);
2976-
addActionRow(English.message, 0, 0, 0, ActionRowType.MESSAGE_FRIEND.getId(), Native.white + Player.friendUsernames[i]);
2975+
2976+
String username = Game.friendList.getPlayerUsername(i);
2977+
2978+
addActionRow(English.remove, 0, 0, 0, ActionRowType.REMOVE_FRIEND.getId(), Native.white + username);
2979+
addActionRow(English.message, 0, 0, 0, ActionRowType.MESSAGE_FRIEND.getId(), Native.white + username);
29772980
return true;
29782981
}
29792982
if(i >= 401 && i <= 500) {

src/main/java/org/runejs/client/cache/media/gameInterface/GameInterface.java

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public static void updateGameInterface(GameInterface gameInterface) {
335335
gameInterface.actionType = 0;
336336
gameInterface.disabledText = English.pleaseWait;
337337
} else {
338-
int fCount = Player.friendsCount;
338+
int fCount = Game.friendList.getCount();
339339
if(type > 700)
340340
type -= 601;
341341
else
@@ -346,7 +346,7 @@ public static void updateGameInterface(GameInterface gameInterface) {
346346
gameInterface.disabledText = "";
347347
gameInterface.actionType = 0;
348348
} else {
349-
gameInterface.disabledText = Player.friendUsernames[type];
349+
gameInterface.disabledText = Game.friendList.getPlayerUsername(type);
350350
gameInterface.actionType = 1;
351351
}
352352
}
@@ -355,30 +355,31 @@ public static void updateGameInterface(GameInterface gameInterface) {
355355
type -= 101;
356356
else
357357
type -= 701;
358-
int count = Player.friendsCount;
358+
int count = Game.friendList.getCount();
359359
if(Player.friendListStatus != 2)
360360
count = 0;
361361
if(type >= count) {
362362
gameInterface.disabledText = "";
363363
gameInterface.actionType = 0;
364364
} else {
365-
if(Player.friendWorlds[type] == 0) {
365+
int world = Game.friendList.getPlayerWorld(type);
366+
if(world == 0) {
366367
gameInterface.disabledText = Native.red + English.offline;
367-
} else if(Player.friendWorlds[type] < 5000) {
368-
if(Player.friendWorlds[type] == Player.worldId) {
369-
gameInterface.disabledText = Native.green + English.world + Player.friendWorlds[type];
368+
} else if(world < 5000) {
369+
if(world == Player.worldId) {
370+
gameInterface.disabledText = Native.green + English.world + world;
370371
} else {
371-
gameInterface.disabledText = Native.yellow + English.world + Player.friendWorlds[type];
372+
gameInterface.disabledText = Native.yellow + English.world + world;
372373
}
373-
} else if(Player.worldId == Player.friendWorlds[type]) {
374-
gameInterface.disabledText = Native.green + English.classic + (-5000 + Player.friendWorlds[type]);
374+
} else if(Player.worldId == world) {
375+
gameInterface.disabledText = Native.green + English.classic + (-5000 + world);
375376
} else {
376-
gameInterface.disabledText = Native.yellow + English.classic + (Player.friendWorlds[type] + -5000);
377+
gameInterface.disabledText = Native.yellow + English.classic + (world + -5000);
377378
}
378379
gameInterface.actionType = 1;
379380
}
380381
} else if(type == 203) {
381-
int count = Player.friendsCount;
382+
int count = Game.friendList.getCount();
382383
if(Player.friendListStatus != 2)
383384
count = 0;
384385
gameInterface.scrollHeight = 20 + 15 * count;
@@ -949,21 +950,17 @@ public static void processMenuActions(int arg1) {
949950
int i_18_ = class1.indexOf(Native.white);
950951
if(i_18_ != -1) {
951952
long l = TextUtils.nameToLong(class1.substring(i_18_ + 5).trim());
952-
int i_19_ = -1;
953-
for(int i_20_ = 0; i_20_ < Player.friendsCount; i_20_++) {
954-
if(Player.friends[i_20_] == l) {
955-
i_19_ = i_20_;
956-
break;
957-
}
958-
}
959-
if(i_19_ != -1 && Player.friendWorlds[i_19_] > 0) {
953+
954+
int i_19_ = Game.friendList.getPlayerIndex(l);
955+
956+
if(i_19_ != -1 && Game.friendList.getPlayerWorld(i_19_) > 0) {
960957
anInt876 = 3;
961958
ChatBox.redrawChatbox = true;
962959
ChatBox.inputType = 0;
963960
ChatBox.chatMessage = "";
964961
ChatBox.messagePromptRaised = true;
965-
sendingMessageTo = Player.friends[i_19_];
966-
Native.enterPlayerNameHeader = English.prefixEnterMessageToSendTo + Player.friendUsernames[i_19_];
962+
sendingMessageTo = l;
963+
Native.enterPlayerNameHeader = English.prefixEnterMessageToSendTo + Game.friendList.getPlayerUsername(i_19_);
967964
}
968965
}
969966
}
@@ -1855,21 +1852,12 @@ public static void runClientScriptsForInterface(int minY, int arg1, int scrollWi
18551852

18561853
public static void removeFriend(long nameAsLong) {
18571854
if (nameAsLong != 0) {
1858-
int i = 0;
1859-
for (/**/; Player.friendsCount > i; i++) {
1860-
if (Player.friends[i] == nameAsLong) {
1861-
Player.friendsCount--;
1862-
redrawTabArea = true;
1863-
for (int i_13_ = i; i_13_ < Player.friendsCount; i_13_++) {
1864-
Player.friendUsernames[i_13_] = Player.friendUsernames[1 + i_13_];
1865-
Player.friendWorlds[i_13_] = Player.friendWorlds[i_13_ + 1];
1866-
Player.friends[i_13_] = Player.friends[1 + i_13_];
1867-
}
18681855

1869-
OutgoingPackets.sendMessage(
1870-
new ModifySocialListOutboundMessage(nameAsLong, ModifySocialListOutboundMessage.SocialList.FRIEND, ModifySocialListOutboundMessage.SocialListAction.REMOVE));
1871-
break;
1872-
}
1856+
if (Game.friendList.removePlayer(nameAsLong) != -1) {
1857+
redrawTabArea = true;
1858+
1859+
OutgoingPackets.sendMessage(
1860+
new ModifySocialListOutboundMessage(nameAsLong, ModifySocialListOutboundMessage.SocialList.FRIEND, ModifySocialListOutboundMessage.SocialListAction.REMOVE));
18731861
}
18741862
}
18751863
}
@@ -1917,7 +1905,7 @@ public static void manageTextInputs() {
19171905
long l = MovedStatics.nameToLong(ChatBox.chatMessage);
19181906
addFriend(l);
19191907
}
1920-
if(anInt876 == 2 && Player.friendsCount > 0) {
1908+
if(anInt876 == 2 && !Game.friendList.isEmpty()) {
19211909
long l = MovedStatics.nameToLong(ChatBox.chatMessage);
19221910
removeFriend(l);
19231911
}
@@ -2096,25 +2084,21 @@ public static void manageTextInputs() {
20962084

20972085
private static void addFriend(long name) {
20982086
if(name != 0L) {
2099-
if(Player.friendsCount >= 100 && MovedStatics.anInt1049 != 1 || Player.friendsCount >= 200) {
2087+
if(Game.friendList.isFull()) {
21002088
ChatBox.addChatMessage("", English.friendsListIsFull, 0);
21012089
} else {
21022090
String username = TextUtils.formatName(TextUtils.longToName(name));
2103-
for(int i = 0; Player.friendsCount > i; i++) {
2104-
if(Player.friends[i] == name) {
2105-
ChatBox.addChatMessage("", username + English.isAlreadyOnYourFriendList, 0);
2106-
return;
2107-
}
2091+
if(Game.friendList.containsPlayer(name)) {
2092+
ChatBox.addChatMessage("", username + English.isAlreadyOnYourFriendList, 0);
2093+
return;
21082094
}
21092095
if (Game.ignoreList.containsPlayer(name)) {
21102096
ChatBox.addChatMessage("", English.pleaseRemove + username + English.suffixFromYourIgnoreListFirst, 0);
21112097
return;
21122098
}
21132099
if(!username.equals(Player.localPlayer.playerName)) {
2114-
Player.friendUsernames[Player.friendsCount] = username;
2115-
Player.friends[Player.friendsCount] = name;
2116-
Player.friendWorlds[Player.friendsCount] = 0;
2117-
Player.friendsCount++;
2100+
Game.friendList.addPlayer(name);
2101+
21182102
redrawTabArea = true;
21192103

21202104
OutgoingPackets.sendMessage(
@@ -2143,11 +2127,9 @@ private static void addIgnore(long arg1) {
21432127
ChatBox.addChatMessage("", class1 + English.suffixIsAlreadyOnYourIgnoreList, 0);
21442128
return;
21452129
}
2146-
for(int i = 0; Player.friendsCount > i; i++) {
2147-
if(Player.friends[i] == arg1) {
2148-
ChatBox.addChatMessage("", English.pleaseRemove + class1 + English.fromYourFriendListFirst, 0);
2149-
return;
2150-
}
2130+
if(Game.friendList.containsPlayer(arg1)) {
2131+
ChatBox.addChatMessage("", English.pleaseRemove + class1 + English.fromYourFriendListFirst, 0);
2132+
return;
21512133
}
21522134
if(!class1.equals(Player.localPlayer.playerName)) {
21532135
Game.ignoreList.addPlayer(arg1);
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package org.runejs.client.chat;
2+
3+
import org.runejs.client.cache.media.gameInterface.GameInterface;
4+
import org.runejs.client.media.renderable.actor.Player;
5+
import org.runejs.client.util.TextUtils;
6+
7+
/**
8+
* A {@link SocialList} that contains a list of friends.
9+
*/
10+
public class FriendList extends SocialList {
11+
/**
12+
* The usernames of the players in this list.
13+
*/
14+
private String[] playerUsernames;
15+
16+
/**
17+
* The worlds of the players in this list.
18+
*/
19+
private int[] playerWorlds;
20+
21+
/**
22+
* The maximum amount of players that can be added to this list.
23+
*
24+
* This is different from {@link #getSize()} because players can be added from
25+
* the server, even if the list is "full" for the player to add more.
26+
*/
27+
private int limit;
28+
29+
/**
30+
* Creates a new friend list with the specified size.
31+
*
32+
* @param size The size of the list.
33+
*/
34+
public FriendList(int size, int limit) {
35+
36+
super(size);
37+
this.limit = limit;
38+
39+
this.playerUsernames = new String[size];
40+
this.playerWorlds = new int[size];
41+
}
42+
43+
@Override
44+
public int removePlayer(long p) {
45+
int index = super.removePlayer(p);
46+
47+
if (index == -1) {
48+
return -1;
49+
}
50+
51+
for (int i = index; i < this.count; i++) {
52+
this.playerUsernames[i] = this.playerUsernames[i + 1];
53+
this.playerWorlds[i] = this.playerWorlds[i + 1];
54+
}
55+
56+
return index;
57+
}
58+
59+
@Override
60+
public void addPlayer(long p) {
61+
super.addPlayer(p);
62+
63+
this.playerWorlds[this.count - 1] = 0;
64+
this.playerUsernames[this.count - 1] = TextUtils.formatName(TextUtils.longToName(p));
65+
}
66+
67+
@Override
68+
public boolean isFull() {
69+
return this.count >= this.limit;
70+
}
71+
72+
/**
73+
* Adds a player to this list.
74+
* @param p The player to add.
75+
* @param username The username of the player.
76+
* @param world The world of the player.
77+
*/
78+
public void addPlayer(long p, String username, int world) {
79+
int newIndex = this.count;
80+
this.players[newIndex] = p;
81+
this.playerUsernames[newIndex] = username;
82+
this.playerWorlds[newIndex] = world;
83+
this.count++;
84+
}
85+
86+
/**
87+
* Gets the username of the player at the specified index.
88+
* @param index The index of the player.
89+
* @return The username of the player.
90+
*/
91+
public String getPlayerUsername(int index) {
92+
return this.playerUsernames[index];
93+
}
94+
95+
/**
96+
* Gets the world of the player at the specified index.
97+
* @param index The index of the player.
98+
* @return The world of the player.
99+
*/
100+
public int getPlayerWorld(int index) {
101+
return this.playerWorlds[index];
102+
}
103+
104+
/**
105+
* Checks if this list contains the specified player.
106+
* @param username The username of the player.
107+
* @return {@code true} if this list contains the player, otherwise {@code false}.
108+
*/
109+
public boolean containsPlayerUsername(String username) {
110+
for (int i = 0; i < this.count; i++) {
111+
if (username.equalsIgnoreCase(this.playerUsernames[i]))
112+
return true;
113+
}
114+
115+
return false;
116+
}
117+
118+
/**
119+
* Set the world of the player at the specified index.
120+
* @param index The index of the player.
121+
* @param world The world of the player.
122+
*/
123+
public void setPlayerWorld(int index, int world) {
124+
this.playerWorlds[index] = world;
125+
}
126+
127+
/**
128+
* Sorts the list:
129+
* - putting players in the same world as the player at the top,
130+
* - then other online players,
131+
* - then offline players.
132+
*/
133+
public void sort() {
134+
boolean bool = false;
135+
while(!bool) {
136+
bool = true;
137+
for(int i = 0; this.count - 1 > i; i++) {
138+
if(Player.worldId != this.playerWorlds[i] && this.playerWorlds[1 + i] == Player.worldId || this.playerWorlds[i] == 0 && this.playerWorlds[i + 1] != 0) {
139+
bool = false;
140+
int world = this.playerWorlds[i];
141+
this.playerWorlds[i] = this.playerWorlds[i + 1];
142+
this.playerWorlds[1 + i] = world;
143+
String name = this.playerUsernames[i];
144+
this.playerUsernames[i] = this.playerUsernames[1 + i];
145+
this.playerUsernames[1 + i] = name;
146+
long hash = this.players[i];
147+
this.players[i] = this.players[i + 1];
148+
this.players[1 + i] = hash;
149+
GameInterface.redrawTabArea = true;
150+
}
151+
}
152+
}
153+
}
154+
}

src/main/java/org/runejs/client/chat/SocialList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public class SocialList {
77
/**
88
* The players in this list.
99
*/
10-
private long[] players;
10+
protected long[] players;
1111

1212
/**
1313
* The amount of players currently in this list.
1414
*/
15-
private int count = 0;
15+
protected int count = 0;
1616

1717
/**
1818
* Creates a new social list with the specified size.
@@ -89,7 +89,7 @@ public int removePlayer(long p) {
8989
* @param p The player to get the index of.
9090
* @return The index of the specified player, or {@code -1} if the player was not in this list.
9191
*/
92-
private int getPlayerIndex(long p) {
92+
public int getPlayerIndex(long p) {
9393
for (int i = 0; i < this.count; i++) {
9494
if (this.players[i] == p) {
9595
return i;

0 commit comments

Comments
 (0)