Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Creating your own commands!

Joined
Jul 29, 2012
Messages
527
Reaction score
71
This is my first tutorial, and i have tried to explain certain parts of the code so that the less able coders can understand easier. Thankyou.


This is a quick guide on how to create your own commands, This is not so easy to find out yourself if you are still learning the basics of networking in java, so here goes!

What you will need:
Pokenet Server + client
Eclipse to be able to code and compile your server and client
(If you haven't done this yet, then follow this tutorial: http://forum.ragezone.com/f706/guide-compiling-pokenet-761057/ if it is still unclear about how to do it, send me a pm and i'll make a new tutorial with pictures).

For this tutorial i will making a basic command that will change your adminRights (can be what you want, so you can make numerous amounts of different ranks)
I will use adminRights = 100 as though it is Owner and the highest possible.

Ok, first off we go to the client to write out the part of the code that will check what you have typed and if it matches up to an if statement it will send along that code.
1. Go to client files>src>backend>ModeratorManager.java
--This file is where all of the packets are stored, whether it be for trading or battling, or just simple chat commands.


2. We are going to want a If statement to check for two variables. one would be if the command is longer than the command name (to make sure we have typed a name to get the new adminRights and the second one will be to make sure we have typed the first part of the command correctly.
So if we start just under the first commands finishing }. we can make our IF statement:
Code:
else if (x.length() >= 10 && x.substring(0, 10).equalsIgnoreCase("makeOwner ")) {
x.length() == 10 is checking if we have typed any player name
the equalsIgnoreCase is checkign if we have typed 'makeOwner ' perfectly.

Now we code the body of the command, as this is on the client it will be sending a specific letter series off to the server, so that it knows what kind of command it is.
Code:
m_ioSession.writeTcpMessage("Zmo" + x.substring(10));
}
We send a Tcp message across (if we are using tcp, change tcp to udp if you're using udp.) to the server with the 3 letters infront of the name we are sending. 'Ztc' Z will be the letter for custom commands. m will stand for make, and o will stand for owner.

Full Code for client:
Code:
else if (x.length() >= 10 && x.substring(0, 10).equalsIgnoreCase("makeOwner ")) {
m_ioSession.writeTcpMessage("Zmo" + x.substring(10));
}
3. Now save that and compile the client! remember to use the manifest and follow the tutorial linked at the start of this if you have forgotten how to compile.


4. Head over to your server files now and find server files>src>org.pokenet.server.network>TcpProtocolHandler.java

This file handles the incoming messages, and sorts them out by the letters that are infront of the actual message. We are going to be using switch cases in this part, to specifically look and sort for 1 letter at a time.

Search for this:
Code:
public void sessionClosed(IoSession session) throws Exception {
Now above that, you will see a few 'break;' you will want to start writing this code right after that last break;

First we are going to make a case statement looking for the letter 'Z' because the switch hsa already been made for looking at the very first letter.
Code:
case 'Z'://custom commands
//The rest of our code will go here
break;
Now the server knows that it has to keep an eye out for the letter Z at the beginning.

Now, we are going to have to look for the second letter of 'm'. For this we use a switch statement.
Code:
case 'Z':
switch(message.charAt(1)) {
case 'm':
//rest of code will then go here
break;
}
break;
If you look at the switch statement it is look at the character position "1". The position's always start from 0, like an array.

Ok, so we now have a second letter so looking for the third is going ot be very simpel and be doing the exact same as what we did for the second.
Code:
case 'Z':
switch(message.charAt(1)) {
case 'm':
switch(message.charAt(2)) {
case 'o':
//next lot of code will go here
break;
}
break;
}
break;


5.Now that we have the code set up to be looking directly at 'Zmo' we need to write the code to change the adminRights.

If you have seen, our players name starts at the character position for 3 so that is how we make the system look at the correct player when changing values. We are going to need to create pointer, which can be labelled by 'o' to which will point to the target you chose.
To do that we use this code:
Code:
PlayerChar o = m_players.get(message.substring(3));

Now that we have our pointer we need to set the adminRights like this:
Code:
o.setAdminLevel(100);
100 is the level you wish to set admin Rights to.

But, what happens if you input a name that isn't currently logged in? An error will occur! so to fix that we use a piece of code that will cycle through the list of online players attempting ot match the chosen player's name to any that are online.
Code:
if(m_players.containsKey(message.substring(3))) {

So our whole code for the server has now become:

Code:
case 'Z':
switch(message.charAt(1)) {
case 'm':
switch(message.charAt(2)) {
case 'o':
if(m_players.containsKey(message.substring(3))) {
PlayerChar o = m_players.get(message.substring(3));
o.setAdminLevel(1);
}
break;
}
break;
}
break;

With all of that done, compile your server files and get logged in.

This will work on yourself.

Fixes:

My adminRights doesn't stay after i change it and relog!
Answer in spoiler
For this fix, you are going to need to make the server save the admin rights to the database itself. go to src>org.pokenet.server.network>LogoutManager.java

Then in there search for
Code:
m_database.query("UPDATE pn_members SET lastLoginServer='null' WHERE id='" + player.getId() + "'");
Directly underneath that, place this:
Code:
m_database.query("UPDATE pn_members SET adminLevel='" + player.getAdminLevel() + "' WHERE id='" + player.getId() + "'");
This code saves adminRights on logout.


EXTRAS:
Want to also send them a message saying they have been promoted?
add this code to send a message that is orange (just like /announce colour).
Code:
o.getTcpSession().write("qYou have been made an Owner (AdminLevel:" + o.getAdminLevel() + ")");
									p.getTcpSession().write("qYou made " + message.substring(3) + " an Owner.");
It will send a message to you and your target in an orange colour saying they have been promoted to owner!

This will need to be placed underneath
Code:
o.setAdminLevel(1);

Thanks for reading, Post if you have any queries or just want to show off your own commands
 
Joined
Jul 29, 2012
Messages
527
Reaction score
71
Thanks for the reply, it didn't take too long to figure out nor post. I've also got /alltome working now.
Got any ideas on what commands to make? I'm basically setting up a heir achy for players rights then i'll be doing some changes to pokemon. At the moment it's Player=1 Mod=20 Admin=50 and Owner=100.

I can post some pictures of the working commands if you like?
 
Back
Top