@@ -50,135 +50,130 @@ public void onOpen(ServerHandshake handshakeData) {
5050
5151 @ Override
5252 public void onMessage (String message ) {
53- LOGGER .fine ("Got Packet: " +message );
54- JsonElement element = JsonParser .parseString (message );
53+ try {
54+ LOGGER .fine ("Got Packet: " + message );
55+ JsonElement element = JsonParser .parseString (message );
5556
56- JsonArray cmdList = element .getAsJsonArray ();
57+ JsonArray cmdList = element .getAsJsonArray ();
5758
58- for (int i = 0 ; cmdList .size () > i ; ++i ) {
59- JsonElement packet = cmdList .get (i );
60- //parse the packet first to see what command has been sent.
61- APPacket cmd = gson .fromJson (packet , APPacket .class );
59+ for (int i = 0 ; cmdList .size () > i ; ++i ) {
60+ JsonElement packet = cmdList .get (i );
61+ //parse the packet first to see what command has been sent.
62+ APPacket cmd = gson .fromJson (packet , APPacket .class );
6263
6364
64- //check if room info packet
65- if (cmd .cmd == APPacketType .RoomInfo ) {
66- RoomInfoPacket roomInfo = gson .fromJson (packet , RoomInfoPacket .class );
65+ //check if room info packet
66+ if (cmd .cmd == APPacketType .RoomInfo ) {
67+ RoomInfoPacket roomInfo = gson .fromJson (packet , RoomInfoPacket .class );
6768
68- //save room info
69- apClient .setRoomInfo (roomInfo );
69+ //save room info
70+ apClient .setRoomInfo (roomInfo );
7071
71- checkDataPackage (roomInfo .datapackageVersions );
72+ checkDataPackage (roomInfo .datapackageVersions );
7273
73- seedName = roomInfo .seedName ;
74+ seedName = roomInfo .seedName ;
7475
75- ConnectPacket connectPacket = new ConnectPacket ();
76- connectPacket .version = APClient .protocolVersion ;
77- connectPacket .name = apClient .getMyName ();
78- connectPacket .password = (apClient .getPassword () == null ) ? "" : apClient .getPassword ();
79- connectPacket .uuid = apClient .getUUID ();
80- connectPacket .game = apClient .getGame ();
81- connectPacket .tags = apClient .getTags ();
76+ ConnectPacket connectPacket = new ConnectPacket ();
77+ connectPacket .version = APClient .protocolVersion ;
78+ connectPacket .name = apClient .getMyName ();
79+ connectPacket .password = (apClient .getPassword () == null ) ? "" : apClient .getPassword ();
80+ connectPacket .uuid = apClient .getUUID ();
81+ connectPacket .game = apClient .getGame ();
82+ connectPacket .tags = apClient .getTags ();
8283
83- //send reply
84- sendPacket (connectPacket );
85- apClient .setRoomInfo (roomInfo );
86- }
87- else if (cmd .cmd == APPacketType .Connected ) {
88- ConnectedPacket connectedPacket = gson .fromJson (packet , ConnectedPacket .class );
89-
90- apClient .setTeam (connectedPacket .team );
91- apClient .setSlot (connectedPacket .slot );
92-
93- apClient .getRoomInfo ().networkPlayers .addAll (connectedPacket .players );
94- apClient .getRoomInfo ().networkPlayers .add (new NetworkPlayer (connectedPacket .team ,0 ,"Archipelago" ));
95- apClient .setAlias (apClient .getRoomInfo ().getPlayer (connectedPacket .team ,connectedPacket .slot ).alias );
96-
97- JsonElement data = packet .getAsJsonObject ().get ("slot_data" );
98-
99- ConnectionAttemptEvent attemptConnectionEvent = new ConnectionAttemptEvent (connectedPacket .team ,connectedPacket .slot ,seedName ,data );
100- //dont need to load the save here,
101- //apClient.loadSave(seedName, connectedPacket.slot);
102- apClient .onAttemptConnection (attemptConnectionEvent );
103-
104- if (!attemptConnectionEvent .isCanceled ()) {
105- authenticated = true ;
106- //only send locations if the connection is not canceled.
107- apClient .getLocationManager ().addCheckedLocations (connectedPacket .checkedLocations );
108- apClient .getLocationManager ().sendIfChecked (connectedPacket .missingLocations );
109-
110- ConnectionResultEvent connectionResultEvent = new ConnectionResultEvent (ConnectionResult .Success ,connectedPacket .team ,connectedPacket .slot ,seedName ,data );
111- apClient .onConnectResult (connectionResultEvent );
112- } else {
113- this .close ();
114- //close out of this loop because we are no longer interested in further commands from the server.
115- break ;
116- }
84+ //send reply
85+ sendPacket (connectPacket );
86+ apClient .setRoomInfo (roomInfo );
87+ } else if (cmd .cmd == APPacketType .Connected ) {
88+ ConnectedPacket connectedPacket = gson .fromJson (packet , ConnectedPacket .class );
11789
118- }
119- else if (cmd .cmd == APPacketType .ConnectionRefused ) {
120- ConnectionRefusedPacket error = gson .fromJson (cmdList .get (i ), ConnectionRefusedPacket .class );
121- apClient .onConnectResult (new ConnectionResultEvent (error .errors [0 ]));
122- }
123- else if (cmd .cmd == APPacketType .Print ){
124- PrintPacket print = gson .fromJson (packet , PrintPacket .class );
90+ apClient .setTeam (connectedPacket .team );
91+ apClient .setSlot (connectedPacket .slot );
12592
126- apClient .onPrint (print .getText ());
127- }
128- else if (cmd .cmd == APPacketType .DataPackage ){
129- JsonElement data = packet .getAsJsonObject ().get ("data" );
130- DataPackage dataPackage = gson .fromJson (data , DataPackage .class );
131- dataPackage .uuid = apClient .getUUID ();
132- apClient .updateDataPackage (dataPackage );
133- if (dataPackage .getVersion () != 0 ) {
134- apClient .saveDataPackage ();
135- }
136- }
137- else if (cmd .cmd == APPacketType .PrintJSON ){
138- LOGGER .finest ("PrintJSON packet" );
139- APPrint print = gson .fromJson (packet , APPrint .class );
93+ apClient .getRoomInfo ().networkPlayers .addAll (connectedPacket .players );
94+ apClient .getRoomInfo ().networkPlayers .add (new NetworkPlayer (connectedPacket .team , 0 , "Archipelago" ));
95+ apClient .setAlias (apClient .getRoomInfo ().getPlayer (connectedPacket .team , connectedPacket .slot ).alias );
96+
97+ JsonElement data = packet .getAsJsonObject ().get ("slot_data" );
98+
99+ ConnectionAttemptEvent attemptConnectionEvent = new ConnectionAttemptEvent (connectedPacket .team , connectedPacket .slot , seedName , data );
100+ //dont need to load the save here,
101+ //apClient.loadSave(seedName, connectedPacket.slot);
102+ apClient .onAttemptConnection (attemptConnectionEvent );
140103
141- //filter though all player IDs and replace id with alias.
142- for ( int p = 0 ; print . parts . length > p ; ++ p ) {
143- if ( print . parts [ p ]. type == APPrintType . playerID ) {
144- int playerID = Integer . parseInt (( print . parts [ p ]. text ) );
145- NetworkPlayer player = apClient .getRoomInfo ().getPlayer ( apClient . getTeam (), playerID );
104+ if (! attemptConnectionEvent . isCanceled ()) {
105+ authenticated = true ;
106+ //only send locations if the connection is not canceled.
107+ apClient . getLocationManager (). addCheckedLocations ( connectedPacket . checkedLocations );
108+ apClient .getLocationManager ().sendIfChecked ( connectedPacket . missingLocations );
146109
147- print .parts [p ].text = player .alias ;
110+ ConnectionResultEvent connectionResultEvent = new ConnectionResultEvent (ConnectionResult .Success , connectedPacket .team , connectedPacket .slot , seedName , data );
111+ apClient .onConnectResult (connectionResultEvent );
112+ } else {
113+ this .close ();
114+ //close out of this loop because we are no longer interested in further commands from the server.
115+ break ;
148116 }
149- else if (print .parts [p ].type == APPrintType .itemID ) {
150- int itemID = Integer .parseInt ((print .parts [p ].text ));
151- print .parts [p ].text = apClient .getDataPackage ().getItem (itemID );
117+
118+ } else if (cmd .cmd == APPacketType .ConnectionRefused ) {
119+ ConnectionRefusedPacket error = gson .fromJson (cmdList .get (i ), ConnectionRefusedPacket .class );
120+ apClient .onConnectResult (new ConnectionResultEvent (error .errors [0 ]));
121+ } else if (cmd .cmd == APPacketType .Print ) {
122+ PrintPacket print = gson .fromJson (packet , PrintPacket .class );
123+
124+ apClient .onPrint (print .getText ());
125+ } else if (cmd .cmd == APPacketType .DataPackage ) {
126+ JsonElement data = packet .getAsJsonObject ().get ("data" );
127+ DataPackage dataPackage = gson .fromJson (data , DataPackage .class );
128+ dataPackage .uuid = apClient .getUUID ();
129+ apClient .updateDataPackage (dataPackage );
130+ if (dataPackage .getVersion () != 0 ) {
131+ apClient .saveDataPackage ();
152132 }
153- else if (print .parts [p ].type == APPrintType .locationID ) {
154- int locationID = Integer .parseInt ((print .parts [p ].text ));
155- print .parts [p ].text = apClient .getDataPackage ().getLocation (locationID );
133+ } else if (cmd .cmd == APPacketType .PrintJSON ) {
134+ LOGGER .finest ("PrintJSON packet" );
135+ APPrint print = gson .fromJson (packet , APPrint .class );
136+
137+ //filter though all player IDs and replace id with alias.
138+ for (int p = 0 ; print .parts .length > p ; ++p ) {
139+ if (print .parts [p ].type == APPrintType .playerID ) {
140+ int playerID = Integer .parseInt ((print .parts [p ].text ));
141+ NetworkPlayer player = apClient .getRoomInfo ().getPlayer (apClient .getTeam (), playerID );
142+
143+ print .parts [p ].text = player .alias ;
144+ } else if (print .parts [p ].type == APPrintType .itemID ) {
145+ int itemID = Integer .parseInt ((print .parts [p ].text ));
146+ print .parts [p ].text = apClient .getDataPackage ().getItem (itemID );
147+ } else if (print .parts [p ].type == APPrintType .locationID ) {
148+ int locationID = Integer .parseInt ((print .parts [p ].text ));
149+ print .parts [p ].text = apClient .getDataPackage ().getLocation (locationID );
150+ }
156151 }
152+ apClient .onPrintJson (print , print .type , print .receiving , print .item );
153+ } else if (cmd .cmd == APPacketType .RoomUpdate ) {
154+ RoomUpdatePacket updatePacket = gson .fromJson (packet , RoomUpdatePacket .class );
155+ updateRoom (updatePacket );
156+ } else if (cmd .cmd == APPacketType .ReceivedItems ) {
157+ RecivedItems items = gson .fromJson (packet , RecivedItems .class );
158+ ItemManager itemManager = apClient .getItemManager ();
159+ itemManager .receiveItems (items .items , items .index );
160+ } else if (cmd .cmd == APPacketType .Bounced ) {
161+ apClient .onBounced (gson .fromJson (packet , BouncedPacket .class ));
162+ } else if (cmd .cmd == APPacketType .LocationInfo ) {
163+ LocationInfo locations = gson .fromJson (packet , LocationInfo .class );
164+ for (NetworkItem item : locations .locations ) {
165+ item .itemName = apClient .getDataPackage ().getItem (item .itemID );
166+ item .locationName = apClient .getDataPackage ().getLocation (item .locationID );
167+ item .playerName = apClient .getRoomInfo ().getPlayer (apClient .getTeam (), item .playerID ).alias ;
168+ }
169+ apClient .onLocationInfo (locations .locations );
157170 }
158- apClient .onPrintJson (print , print .type ,print .receiving ,print .item );
159- }
160- else if (cmd .cmd == APPacketType .RoomUpdate ){
161- RoomUpdatePacket updatePacket = gson .fromJson (packet ,RoomUpdatePacket .class );
162- updateRoom (updatePacket );
163- }
164- else if (cmd .cmd == APPacketType .ReceivedItems ){
165- RecivedItems items = gson .fromJson (packet , RecivedItems .class );
166- ItemManager itemManager = apClient .getItemManager ();
167- itemManager .receiveItems (items .items , items .index );
168- }
169- else if (cmd .cmd == APPacketType .Bounced ) {
170- apClient .onBounced (gson .fromJson (packet , BouncedPacket .class ));
171- }
172- else if (cmd .cmd == APPacketType .LocationInfo ) {
173- LocationInfo locations = gson .fromJson (packet , LocationInfo .class );
174- for (NetworkItem item : locations .locations ) {
175- item .itemName = apClient .getDataPackage ().getItem (item .itemID );
176- item .locationName = apClient .getDataPackage ().getLocation (item .locationID );
177- item .playerName = apClient .getRoomInfo ().getPlayer (apClient .getTeam (),item .playerID ).alias ;
178- }
179- apClient .onLocationInfo (locations .locations );
180171 }
181172 }
173+ catch (Exception e ) {
174+ LOGGER .warning ("Error proccessing incoming packet: " );
175+ e .printStackTrace ();
176+ }
182177 }
183178
184179 private void updateRoom (RoomUpdatePacket updateRoomPacket ) {
@@ -197,7 +192,7 @@ private void checkDataPackage(HashMap<String,Integer> versions) {
197192 HashSet <String > exclusions = new HashSet <>();
198193 for (Map .Entry <String , Integer > game : versions .entrySet ()) {
199194 //the game does NOT need updating add it to the exclusion list.
200- int myGameVersion = apClient .getDataPackage ().getVersions ().get (game .getKey ());
195+ int myGameVersion = apClient .getDataPackage ().getVersions ().getOrDefault (game .getKey (), 0 );
201196 int newGameVersion = game .getValue ();
202197 if ( newGameVersion <= myGameVersion && newGameVersion != 0 ) {
203198 exclusions .add (game .getKey ());
0 commit comments