Jump to content

[TUT] Java SDK


laserlaser

Do you like this tutorial?  

34 members have voted

  1. 1. Do you like this tutorial?

    • Yes
      30
    • No
      4


Recommended Posts

Urun-java-png.png

[Tutorial] JAVA SDK

Hi everybody!

Today, i will make a tutorial for this community... It's my gift :mrgreen:

This tutorial is to teach to you how use the JAVA SDK and what we can do with it.

Summary

  • What is Java
  • What is the Java SDK
  • What we can do with it
  • How to install
  • How to use
  • How to use with callJava
  • Download a demo

What is the Java

Java is a programming language originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities.

I think that, those informations is enough...

What is the Java SDK

SDK mean Software Development Kit, it's a library allow to developpers to make his applications.

Multiple platforms propose his SDK. Just search on Google :mrgreen:

Examples:

  • Facebook SDK
  • Have so much... Search on Google (Google have too his SDK) :mrgreen:

Download

What we can do with it

The JAVA SDK from MTA offer much possibility.

You can do everything with it if you have a great imagination :)

Examples ?

  • Remote management (all controls, kick, ban, mute, chat, pm)
  • Background timers (For no lag)
  • Facebook abilities with Facebook SDK
  • Twitter abilities with Twitter SDK
  • More, more, more, more

How to install

Let's start ! :twisted:

First, you must:

  • A MTA-Server
  • A knowledge of java
  • The resource JSDK ( They are all in JavaSDK.zip )
  • The library JAVA SDK

Now we can start :D

Unzip the file.And enter into folder.You will see 3 folders.

Copy all files in "modules/" folder into "your/server/mods/deathmatch/modules". If this folder doesn't exist, create and copy...

Copy all files in "resources/" folder into "your/server/mods/deathmatch/resources".And give admin rights to "jsdk" resource.

Result: In "modules/" folder there are "ml_sockets.dll or ml_sockets.so". In "resources/" folder there are "jsdk" resource

Now download and run the ide. I will use eclipse, there are too many alternatives.Netbeans, eclipse, JCreator ....

Create a new java project.

Select the project name on Package explorer.Click right, and select the properties.

Go to "Java Build Path", Select "Libraries" tab.

Click "Add external jars", Select "SDK/JavaSDK.jar" in your unzipped folder.

Now we're ready to develop something..

Create a new java class.If

  
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
  
} 
  

this line doesn't exists, add this lines.

Write

  
import com.mtasa.*; 
import com.mtasa.elements.*; 
import com.mtasa.functions.*; 
  

top of the class.

The library is successfully imported to our class, now our library are installed :)

How to use

Go into main function..And create a new object type is MTA

MTA object's constructor

  
public MTA(String host="127.0.0.1", int port=22005, String username="",String password=""){ 
    .... 
} 
  

Explain:

  • The server ip you want to connect(Default 127.0.0.1)
  • The server port you want to connect(Default 22005)
  • The username you want to connect with(Default empty)
  • The password you want to connect with(Default empty)

IMPORTANT: The account must have a acl rights.

  
<right name="general.ModifyOtherObjects" access="true"></right> 
<right name="general.http" access="true"></right> 
  

In admin rights they are both is true...

I created the object

  
try { 
    MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
} catch (MTAException e) { 
    e.printStackTrace(); 
} 
  

The object's constructor can throw the MTAException, so we must put it into try-catch block.

Create a LuaArgs object...

LuaArgs object's constructor

  
public LuaArgs(MTA server){ 
    .... 
} 
  

Explain:

  • The server object you want to make call

I created..And add the one parameter.

Now my try block look like;

  
MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
     
LuaArgs largs = new LuaArgs(mta); 
largs.add("Hello Mr.Green"); 
  
  

Now I can call the functions...

LuaArgs MTA.callFunction(String funcName, LuaArgs args)

Explain:

  • The default MTA function you want to call (ex: outputChatBox, outputDebugString)
  • The arguments to send function
  • Returns: LuaArgs object, what function returned to we.

LuaArgs MTA.call(String resourcename, String funcName, LuaArgs args)

Explain:

  • The resource name where are the function
  • The function name, it must be exported and http is true...Like
  • The arguments to send function
  • Returns: LuaArgs object, what function returned to we.

NOTICE: If you send the one argument, you can create it with MTA.luaArg(Object o) method.

So,

  
MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
     
LuaArgs largs = new LuaArgs(mta); 
largs.add("Hello Mr.Green"); 
  
  

is equals to

  
MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
     
LuaArgs largs = mta.luaArg("Hello Mr.Green"); 
  
  

But I don't send one argument, I must create A LuaArgs object...I added many arguments to Largs. And I will make call to outputDebugString

Now my try blog is:

  
MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
             
LuaArgs largs = new LuaArgs(mta); 
largs.add("Hello Mr.Green"); 
largs.add(0); // Custom message 
largs.add(255); // R 
largs.add(0); // G 
largs.add(0); // B 
mta.callFunction("outputDebugString", largs); 
  

When program runs, in server-console outputs "Hello Mr.Green"

We did it! But It's the hardest way :) You can do it easily.How?

  
boolean MTA.out.outputDebugString(message, dlevel=3, r=0, g=0, b=0); 
  

Explain:

  • The message you want to output
  • Debug level, 0: Custom, 1:Error, 2:Warning, 3:Information, There are a constants.Ex: Output.LEVEL_WARNING, Output.LEVEL_ERROR :)
  • The color codes (r,g,b)
  • Returns: boolean

Just one line !

Now my try block

  
MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
mta.out.outputDebugString("Hello Mr.Green",Output.LEVEL_CUSTOM, 255,0,0); 
  

Just 2 lines ! :twisted:

How to use with callJava

Now, we will use callJava function.First of all i will explain jsdk exported functions

  
function callJava(int remote_clientid[,arg1,arg2,...]) 
  

Explain:

  • remote_clientid It's the socket's id.It give information about SDK Connectors.
  • Arguments

  
function getSDKPort() 
  

Explain:

  • Returns: Opened SDK port.JavaSDK will connect our server with this port

  
function getConnections() 
  

Explain:

  • Returns: Table, All SDK's remote_clientid

  
function getSocket() 
  

Explain:

  • Returns: Socket ID (Server)

Events:

  
onJavaClientConnect (int socket_id, string remote_clientip, int remote_clientid); -- Call when SDK connected to server 
onJavaClientDisconnect (int socket_id, int remote_clientid); -- Call when SDK disconnected from server 
  

Now we can start Ahhh lua part.So most boring part

Create a new resource, if you don't know creating new resource, you can press 'X' button on your browser

There are 2 files: server.lua, meta.xml.You know how to design meta.xml, so i don't write it here.

Let's open the Notepad++ and begin writing..

  
function onplayerjoin() 
    -- TODO: Auto generated method sub 
end 
addEventHandler("onPlayerJoin",getRootElement(), onplayerjoin) 
function onsdkjoin(remote_clientid) 
  
end 
addEventHandler("onJavaClientConnect",getRootElement(), onsdkjoin) 
  

This is a very simple lua file.2 event only.Now we send somethings with callJava..

IMPORTANT: If you use callJava, you should open TCP Port (If you don't changed, default is 2205)

Let's write into onsdkjoin

  
function onsdkjoin(remote_clientid) 
    -- callJava( remote_clientid , args ) -- I teach that function's usingÉ 
    callJava(remote_clientid,"pop","Welcome to our server"); -- Send "pop" and "Welcome to our server" messages.Why I send "pop"? Cuz, InputEvents receives all strings.So We should know the event or message to do it.. 
end 
  

Write into onplayerjoin

  
function onplayerjoin() 
    for _,remote_clientid in ipairs(exports.jsdk:getConnections()) do -- Put into for loop the connection table 
        callJava(remote_clientid,"join",getPlayerName(source));  
    end 
end 
  

It's the simple code, when players connect it calls all sdk with 2 argument, first "join", second player name..

Let's complete

My class's last state is:

  
import com.mtasa.*; 
import com.mtasa.elements.*; 
import com.mtasa.functions.*; 
public class SDKTut { 
  
    /** 
     * @param args 
     */ 
    public static void main(String[] args) { 
        try { 
            MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
             
            mta.out.outputDebugString("Hello Mr.Green",Output.LEVEL_CUSTOM, 255,0,0); 
        } catch (MTAException e) { 
            e.printStackTrace(); 
        } 
    } 
  
} 
  

Now i will add input events with mta.addInputEvent function..

  
mta.addInputEvent(new InputEvent() { 
                 
    @Override 
    public void onAction(LuaArgs arguments, String input) throws MTAException { 
                 
    } 
}); 
  

Explain:

  • arguments: argument list in callJava
  • input: arguments json string.

The first argument, I was send events.So I will check it with parseString

Write into onAction:

  
if (arguments.parseString(0).equals("pop")){ 
     
}else if(arguments.parseString(0).equals("join")){ 
     
} 
  

Now I'm knowing the input's event...If it's pop, I will show it with JOptionPane...If "join" event I will print the player name...

  
if (arguments.parseString(0).equals("pop")){ 
    JOptionPane.showMessageDialog(null, arguments.parseString(1)); 
}else if(arguments.parseString(0).equals("join")){ 
    System.out.println("Player joined: "+arguments.parseString(1)); 
} 
  

We did it! But not finished.Last step!

Connecting the socket..

  
try { 
    mta.sockOpen(); 
} catch (UnknownHostException | IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
  

This function can throw UnkownHostExpcetion and IOException.Now I'm changing the my codes..

My codes(last state):

  
try { 
    MTA mta = new MTA("127.0.0.1", 22005, "admin", "12345"); 
     
    mta.out.outputDebugString("Hello Mr.Green",Output.LEVEL_CUSTOM, 255,0,0); 
    mta.addInputEvent(new InputEvent() { 
         
        @Override 
        public void onAction(LuaArgs arguments, String input) throws MTAException { 
            if (arguments.parseString(0).equals("pop")){ 
                JOptionPane.showMessageDialog(null, arguments.parseString(1)); 
            }else if(arguments.parseString(0).equals("join")){ 
                System.out.println("Player joined: "+arguments.parseString(1)); 
            } 
        } 
    }); 
    mta.sockOpen(); 
} catch (MTAException e) { 
    e.printStackTrace(); 
} catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
  

TO BE CONTINUED...

Edited by Guest
Link to comment
  • 3 months later...

hey can you help me??

i have downloaded the package 0.2.

I have startet the module (in console is written that it is started)

but i get this error:

[2012-11-19 14:17:39] MODULE: Loaded "Sockets Module" (1.10) by "Gamesnert, MCvarial & x86" 
[2012-11-19 14:17:39] MODULE: Loaded "MySQL 5.0 database module" (0.50) by "Alberto Alonso <[email protected]>" 
[2012-11-19 14:17:39] Starting resources... 
[2012-11-19 14:17:39] ERROR: jsdk\server.lua:75: attempt to call global 'socket_create' (a nil value) 

Link to comment
java.lang.UnsupportedClassVersionError: com/mtasa/MTAException : Unsupported major.minor version 51.0 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClassCond(Unknown Source) 
    at java.lang.ClassLoader.defineClass(Unknown Source) 
    at java.security.SecureClassLoader.defineClass(Unknown Source) 
    at java.net.URLClassLoader.defineClass(Unknown Source) 
    at java.net.URLClassLoader.access$000(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
Exception in thread "main"  

Link to comment
  • 1 month later...

now i have it.. the module is active and sdk resource is started

i have a problem with the javacode

my code at the moment is:

 try { 
            MTA mta = new MTA("IP",PORT, "USER", "PW"); 
        } catch (MTAException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

i know that the IP, Port, user and pw is korrekt, acl rights are given too but i get always this:

com.mtasa.MTAException: Username or password is invalid 
    at com.mtasa.MTA.call(MTA.java:193) 
    at com.mtasa.MTA.callFunction(MTA.java:208) 
    at com.mtasa.MTA.(MTA.java:131) 
    at start.main(start.java:14) 
  

can you help me?

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...