[Ruse] Improved Java Website / URL Launching Tutorial

Welcome!

Today I’ll be showing a SUPER simple way to replace the old, outdated and frankly terrible way that most Ruse servers handle launching URLs.

First off, go ahead and create a new packet in your PacketSender (May be called PacketHandler).

public PacketSender openURL(String url) {
		
		return this;
	}

I called mine openURL. (Big brain, I know!)

Next, you’ll want to head over to your Client Configuration file. Scroll to the bottom, and you’ll see a load of seemingly confusing numbers.

You can usually find them easily:

public static final int[] packetSizes = {...

Find one that is currently 0

I have chosen packet 220.

Next, do a search in Client.java for:

case (*the packet number you selected*):

image

If you don’t find anything, open up your PacketHandler / PacketSender again.

Search for the packet number you’ve selected.

image

Again, hopefully nothing is found… Great!

If you did find that the packet you selected is already in use, select a new number and try again.

Once you find the unused number, change the 0 to -1. This allows the packet to accept non specific data.

Bare in mind that the first number of each row ends in a 0.

E.g…

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //110
^ Would be 110
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //120
                        ^ Would be 127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //130
                              ^ Would be 139

Now, we need to finish the packet code in PacketSender.

public PacketSender openURL(String url) {
		PacketBuilder out = new PacketBuilder(220, PacketType.BYTE); // The Packet And Output Type
		out.putString(url); //Generating a string to send to the client
		player.getSession().queueMessage(out); //Send the packet to the client
		return this;
	}

That’s the packet done! You’ll be able to use this anywhere.

E.g:

String url = "https://test.com"
player.getPacketSender().openURL(url);

Now, head back to your Client.java file and do another search.

This time, we’re searching for:

switch (opcode)

This is where your client handles the information that it receives from packets.

We’ll need to make a new case for the packet number that you selected (As shown in the image above).

Now, here is the code annotated:

case 220://OPENING URLS
//Assigning the received string to a new string called url
String url = inStream.readString(); 
//This code uses the native java.awt.Desktop library.
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
	System.out.println("URL: " + url);
	Desktop.getDesktop().browse(new URI(url));
}
//Setting to match the change we made to the packet in configuration.java
opCode = -1;
return true; //Returns true once handled

That’s it! You should be good to go.

I would suggest wrapping the above code in a try / catch block in case you pass a non-valid URL.

Here is an example command that utilises this new ability…

if (command[0].equals("google")) {
			String query = (wholeCommand.substring(command[0].length() + 1)).replace(" ", "%20");
			player.getPacketSender().openURL("https://www.google.com/search?q=" + query);
		}

You should update any existing command such as ::donate or ::store so that they open using the native way!

Flower Flex GIF by GIPHY Studios Originals

Done :heart:

As always, join our Discord for more support!