Jump to content

Create Your Voiced Command In Java


Recommended Posts

Hello Cheaters, 

 

It's my First Topic in this great forums, that's why i decided to try to help the community as i got help here before , by creating some tutorials specially for Backend (Java / C# if needed / PHP), and forgive me if this breaking any rules and let me know as soon as you notice that to correct :) . So let's cut that talk and get started.

 

Table of Contents :

  • What is Voiced Commands about ?
  • For which Pack/Version is this tutorial ?
  • Create your first Command
  • Register your Command into GameServer

What is Voiced Commands About :

 

Well, Voiced Commands are that commands that Player can use into chat input and it's starting with a dot (.) , for example (.dressme, .join , .register ....... etc).  and we usually add such commands to private servers to make life easier for players like adding a command which will convert player adena to Goldbars, another one for teleporting to custom location ..... and so on.

 

For which Pack/Version is this Tutorial :

 

Screenshot, Codes and the one i work on is a my private modified pack that's based on L2JServer for Hi5, but to be honest it should be working fine with all Packs if you can find some files locations like IVoicedCommandHandler.java and GameServer.java , so if you have a search skills then you can apply it to any pack and any chronicle and we will discuss that, dun worry.

 

Com'on this is too much talk why we i don't just shut up  get started  :gusta: .

 

Create your first Voiced Command :

 

We can create our command code in two places (Server Pack or Data Pack), personally i prefer add it to Data Pack Scripts, but for the sake of simplicity and to make it easy for you to implement it in different packs and older version of L2 i'll do it this time in Server Pack.

 

Step 1 :

 

So go to your Server Pack Java Source and fine a Package called com.l2jserver.gameserver.handler or you can create your own package if you can work with Java well.

 

once you get to that location (or your own package), right click on that > New > Class

 

image.png

 

Step 2 :

 

- Choose a Name for your Voiced Command Handler File, i'll name it ServerInfoVoicedCommandHandler , since i will make this command show player the server info document , well it's not useful but this tutorial meant for educations purpose not a product, so you can use your imagination and make your amazing command :)

- Clear what's inside Superclass text

- Click on Add in front of Interfaces section and Search for IVoicedCommandHandler, and when u find it Click Ok

 

image.png

 

 

image.png

 

 

Step 3 :

 

Once our class Created we will notice that we have 2 important sections (methods), useVoicedCommand and getVoicedCommandList.

 

useVoicedCommand  : is the method of block of code that will execute when a player use our command, so this is where our code will be

getVoicedCommandList : is where our server will look to know which command(s) this file/class can handle .

 

Note : if you are not familiar with Java , here is a note .. we usually add the code of any method between the curly-braces (  { } ) and any thing between { } we call it code block, if any need a guide for Java let me know and see if i can help :)

 

So inside getVoicedCommandList code block we need to add this code :

return new String[] {"serverinfo"};

this line of code will tell Game Server that this class can handle voiced command .serverinfo

 

Step 4 :

 

Now we need to Implement the actual feature of this command, so we need to write the right code that do that job, in our case we need to show and html file to the player this file can contain server info, so we need first to create an html file into our data/html/custom folder and i'll name it serverinfo.html, and here is a simple code that we can test it with .

<html><title>L2JSamDev Info</title>
<body>
<center>
	<br><br>
	<center>
	<img src="L2UI_CH3.herotower_deco" width=256 height=32><br>
	<font name="hs9" color="00aff0">Hello This is the Server Info</font><br>
	<img src="L2UI_CH3.herotower_deco" width=256 height=32><br>
</center>
</body>
</html>

Step 5 :

 

We need to add the code to useVoicedCommand method that show this html document to user, and here is the simplest code for this task

//Get Html Content
String documentContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/custom/serverinfo.html");
//If not Found then Stop
if(documentContent == null) {return false;}
//If Document Found then Prepare a new Message to Send it to Player
NpcHtmlMessage message = new NpcHtmlMessage();
message.setHtml(documentContent);
//Send Document to Player
activeChar.sendPacket(message);
//Well it's Success
return true;

Note : if you get errors like HtmCache or NpcHtmlMessage cannot resolve, just hover over it and click Import HtmCache/NpcHtmlMessa 

 

image.png

 

 

So our final Code will look like : 

package com.l2jserver.gameserver.handler;

import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;

public class ServerInfoVoicedCommand implements IVoicedCommandHandler {

	@Override
	public boolean useVoicedCommand(String command, L2PcInstance activeChar,
			String params) {
		//Get Html Content
		String documentContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/custom/serverinfo.html");
		//If not Found then Stop
		if(documentContent == null) {return false;}
		//If Document Found then Prepare a new Message to Send it to Player
		NpcHtmlMessage message = new NpcHtmlMessage();
		message.setHtml(documentContent);
		//Send Document to Player
		activeChar.sendPacket(message);
		//Well it's Success
		return true;
	}

	@Override
	public String[] getVoicedCommandList() {
		return new String[] {"serverinfo"};
	}

}

Step 6 :

 

- UPDATED as suggested By @meIron ( Thanks for Suggestion :) )

 

Just one more task to be good to go and test our new command, we need to register this command to the main VoicedCommandHandler, simple go to VoicedCommandHandler.java file which usually located at com.l2jserver.gameserver.handler or com.PACKNAME.gameserver.handler and in constructor which is the method called VoicedCommandHandler() add this like of code AFTER :

_datatable = new HashMap<>();

Add this

registerHandler(new ServerInfoVoicedCommand());

in the end it will look like 

 

constructor.png

 

Tip : you can register it in another way by adding it to MasterHandle.java in Datapack but we seek simplicity in this tutorial

 

Step 7 : 

 

Let's Build that Project and Log in Game to test it

 

image.jpg

 

Voila, Our Useless command Working  :-beep- yeah:

 

If you have any question or need another java tutorial or even have an idea to implement and wonder how it could be just lemme know, maybe i can help :)

Edited by SamDev-Coder
  • Upvote 2
Link to comment
Share on other sites

Good work my friend. Just one thing.

VoicedCommandHandler.getInstance()

isn't register all the commands inside?

Link to comment
Share on other sites

Good work my friend. Just one thing.

VoicedCommandHandler.getInstance()

isn't register all the commands inside?

Thank you Brother for you comment :)

well VoicedCommandHandler.getInstance() will call the Constructor Method and in Constructor method, this is the code

protected VoicedCommandHandler()
{
    _datatable = new HashMap<>();
}

this will just initiate the HashMap, but it wont initiate it more than once since this class implement Singleton Pattern, which means Constructor being called one time only

Link to comment
Share on other sites

Well you can edit your constructor and call one method that registering all your commands like 

registerHandler(new Online());
registerHandler(new Event());
registerHandler(new Some());
registerHandler(new Voiced());
registerHandler(new Commands());
registerHandler(new Here());

to avoid in gameserver all these new lines that contains .getInstance() etc

it's clearly about readability :P

Link to comment
Share on other sites

@melron

 

You're totally right brother i agree with you, your code is pretty to be honest :)

But you can say it's just a habit as Programming Instructor, used to reduce topics and try to focus on basics to not let readers get confused about strange terms like : Constructors, OOP, Design Patterns, Encapsulation, Polymorphism .... etc .

For my self i would like to add it in a custom package to make things organized and i have a Handlers class that register all voiced, bypasses, admin ... etc.

 

I like readability as you do :)

Link to comment
Share on other sites

Thanks for this share.. We need active people to help the others.

Good job and take care what merlon says.. Will be more clearly.

 

Keep sharing..

Link to comment
Share on other sites

Thanks for this share.. We need active people to help the others.

Good job and take care what merlon says.. Will be more clearly.

 

Keep sharing..

Thank you brother, and i hope i can help others :)

and of course meIron is talking right, so i'll update the main post

Link to comment
Share on other sites

  • 7 months later...

can you make for me one 4 commands like .buffer open the buffer shop ,,,,    .donate open the donation shop ,,,,,,, .Gk open the gk menu and .gmshop to open the normal shop ?? pls ? because im really poor in java  and i dont understand many things from this topic 

Link to comment
Share on other sites

Answer is no. Even if he make you the command, THEN, you need to create bypasses to handle everything.

 

If you don't understand, read till you do. If you can't, simply skip the idea, don't add new commands. Once you learn, you add them.

Link to comment
Share on other sites

On 15.04.2018 at 1:03 AM, SweeTs said:

Answer is no. Even if he make you the command, THEN, you need to create bypasses to handle everything.

 

If you don't understand, read till you do. If you can't, simply skip the idea, don't add new commands. Once you learn, you add them.

Thank you bro . but you can be more explicit , just if u want !

Link to comment
Share on other sites

hi everyone , maybe any can explain me how i can make a command like .buffer ? when i type .buffer in chat i want to open html page from comunity board with buffs ? please ? i think its not hard to make a simple code to open a html page of buffer but im really poor in java and i need help please , thanks everyone

Link to comment
Share on other sites

1 hour ago, Prostyle1990 said:

hi everyone , maybe any can explain me how i can make a command like .buffer ? when i type .buffer in chat i want to open html page from comunity board with buffs ? please ? i think its not hard to make a simple code to open a html page of buffer but im really poor in java and i need help please , thanks everyone

poor is your brain, use the rest of it to do some search in the forum and you will learn by viewing codes and what they do.

Link to comment
Share on other sites

55 minutes ago, Nightw0lf said:

poor is your brain, use the rest of it to do some search in the forum and you will learn by viewing codes and what they do.

thanks for your comment , but i already searched on forum but still i dont know how to make a voice command that why i ask here maybe you can show me an example of a voice command to open an html like gatekeeper html or buffer ....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...