Javanyx Tutorial

Onyx with its NyxServer is a plugin container written in java. Nyx plugins are simple javascript text files and can be added and removed on the fly. Some plugins: TMX, Dedimania, NextMapInfo, CopperDonation and many more.

Moderators: PaGaisu, Vegeta, TM-Patrol

Post Reply
PaGaisu
Pit Crew
Pit Crew
Posts: 126
Joined: 16 Jan 2006 10:48
Contact:

Javanyx Tutorial

Post by PaGaisu » 25 Jun 2008 22:29

Let s start by setting up our development environment:

Development environment
- Choose a text editor to create some javascript files. I advice Notepad++, this editor contains a javascript language color highlighter.
- Open the javanyx documentation: here. You will find details of the nadeo API and some others.
- Open also the W3C javascript documentation: here for the basic objects: String, Math, Date, Array.

Helloworld script

Now, time to take a look to the traditional Helloworld script:

Code: Select all

// 1st step: import of the javanyx package so that we can use the NadeoApi.
importPackage(Packages.com.dnd.trackmania.scriptnyx.api);

//2nd step: creation of an instance of the Nadeo API
var nadeoApi 	= new NadeoApi(this);

//We register a callback on the PLAYER_CONNECT, so each time a player connects, the function "playerConnect" will be called
nadeoApi.registerCallback(NadeoApi.CALLBACK_PLAYERCONNECT,"playerConnect");


//This function is called each time a player joins (we registered it as a callback)
function playerConnect(login,nickname,isSpec){
      //we send to the player a message "Helloworld"
      nadeoApi.chatSend("Helloworld","",login);
}
To learn more about this script, take a look at the online javanyx documentation on the Nadeo Api to the methods:
- registerCallback you ll see the others callbacks
- chatSend you can send message on the behalf of the Admin for example.

To load this script:
1) Ensure Onyx is running.
2) Create a directory helloworld in the modules directory.
3) Copy this code to a file helloworld.nyx and save it in the modules/helloworld/ directory

Onyx will load it automatically. Now, connects your server, you shoul receive the message "Helloworld" on your game chat.



An advanced script

Let s create what is call the "bind" script.
The goal of this script is for a player to type /gg for example in the game chat and to display "Good game".

1) Let s create a file bind.ini and add some bind definitions:

Code: Select all

gg=$f00Good Game!
bb=$0f0bye everybody
2) Let s create the javascript file:

Code: Select all

// 1st step: import of the javanyx package so that we can use the NadeoApi and the UtilsApi.
importPackage(Packages.com.dnd.trackmania.scriptnyx.api);

//2nd step: creation of an instance of the Nadeo API and the Utils Api
var nadeoApi 	= new NadeoApi(this);
var utilsApi    = new UtilsApi(this);

//3rd step: we find the path to the bind.ini
var bindFile = utilsApi.getScriptPath()+"/bind.ini";

//4th step: we open the bind file, our binds are now in an array
var binds=utilsApi.openPropertyFile(bindFile); 

//5th step: we register a callback on the SERVER_COMMAND, so each time will type a command, we will intercept it.
//A command is a text beginning by a '/'
nadeoApi.registerCallback(NadeoApi.CALLBACK_SERVERCOMMAND,"serverCommand");


//Now the function serverCommand called each time a player type a server command
function serverCommand(command,login,args){
	//we check that the command is in our binds array
	if(binds[command]!=undefined){
		//we got a match in our array
		
		//we get the nickname associated to the login
		var nickname = nadeoApi.getNickname(login);
		
		//we send the message with the bind value on the behalf of the player to everybody
		nadeoApi.chatSend(binds[command],nickname,"");		
	}
}
To learn more about this script, take a look at the online javanyx documentation on the Nadeo and Utils Api to the methods:
- NadeoApi:registerCallback you ll see the others callbacks.
- NadeoApi:chatSend you can send message on the behalf of other players for example.
- NadeoApi:getNickname to get the nickname of a player.
- UtilsApi:openPropertyFile to open an ini synthax file.
- UtilsApi:getScriptPath to get the location of the current script.


We need to create a descriptor file for our module (called module.xml):

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="description">Bind manager script</entry>
<entry key="version">1.0.0</entry>
<entry key="author">you</entry>
<entry key="provider-url">http://www.onyx-software.com/onyx/modules</entry>
<entry key="entry-point">bind.nyx</entry>
<entry key="type">NYX</entry>
</properties>
Note: You can add your own parameters if you want. It must respect the following synthax: <entry key="KEY">VALUE</entry>
Then, you can pick up these arguments with the UtilsApi:getArguments function.


To load our bind script:
1) Ensure Onyx is running.
2) Create a directory bind in the modules directory.
3) Copy the bind.ini to the modules/bind/ directory.
4) Copy the javascript code to a file bind.nyx and save it in the modules/bind/ directory.
5) Copy the module descriptor module.xml to the modules/bind/ directory.

Now, connect your server and type /gg, it should display Good Game in red.

Post Reply