Java SDK: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This SDK allows you to call exported MTA functions from JAVA over HTTP.
This SDK allows you to call exported MTA functions from Java over HTTP.


==Getting Started==
==Getting Started==
Line 12: Line 12:
==Syntax==
==Syntax==
This class has one public static function, ''callFunction''. The syntax is as follows:
This class has one public static function, ''callFunction''. The syntax is as follows:
<syntaxhighlight lang="lua">[java5]
<syntaxhighlight lang="java5">
Object[] callFunction ( String serverHTTPAddress, String resourceName, String functionName, Object[] args )
Object[] callFunction ( String serverHTTPAddress, String resourceName, String functionName, Object[] args )
</syntaxhighlight>
</syntaxhighlight>
Line 22: Line 22:
==Examples==
==Examples==
This tests the resource functions exported from the 'echobot' resource:
This tests the resource functions exported from the 'echobot' resource:
<syntaxhighlight lang="lua">[java5]
<syntaxhighlight lang="java5">
Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getThisResource", null );
Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getThisResource", null );
Resource resource = (Resource)ret[0];
Resource resource = (Resource)ret[0];
Line 32: Line 32:


This example tests the element functions exported from the 'echobot' resource:
This example tests the element functions exported from the 'echobot' resource:
<syntaxhighlight lang="lua">[java5]
<syntaxhighlight lang="java5">
// call getRootElement
// call getRootElement
Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getRootElement", null );
Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getRootElement", null );
Line 45: Line 45:
</syntaxhighlight>
</syntaxhighlight>


For a more complex example that handles multiple returns, download the zip file and see MTAJavaTest.java.
==More complex example==
[[Image:Mta java sdk example.png]]
 
The zip file contains an example program (MTAJavaTest.java) that implements a console that allows you to see events on a server and chat to the players there. This allows you to see how you can handle multiple returns.


==Caveats==
==Caveats==
Line 51: Line 54:


==Download==
==Download==
[http://misc.opencoding.net/mta/mtajavasdk_0.1.zip Download Version 0.1]
* [http://misc.opencoding.net/mta/mtajavasdk_0.1.zip Download Version 0.1]
 
[[Category:Scripting Concepts]]
[[Category:SDK]]
[[Category:Tutorials]]

Latest revision as of 10:21, 15 July 2024

This SDK allows you to call exported MTA functions from Java over HTTP.

Getting Started

To use it, you need to make sure you need to have the following packages:

  • com.multitheftauto
  • org.json.simple
  • org.json.simple.parser

These are all included in the zip file below.

To get started, import com.multitheftauto.MTARPC.

Syntax

This class has one public static function, callFunction. The syntax is as follows:

Object[] callFunction ( String serverHTTPAddress, String resourceName, String functionName, Object[] args )
  • serverHTTPAddress: The server's HTTP address, in the form hostname:port (without "http://" prefixed)
  • resourceName: The name of the resource that has exported the function you want to call
  • functionName: The name of the function you want to call
  • args: An array of arguments you wish to pass. Most basic types are accepted - String, Integer, Double, Boolean, null etc, as well as the special classes com.multitheftauto.Element and com.multitheftauto.Resource.

Examples

This tests the resource functions exported from the 'echobot' resource:

Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getThisResource", null );
Resource resource = (Resource)ret[0];
Object[] arguments = {resource};
ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getResourceName", arguments );
String resourceName = (String)ret[0];
System.out.println("Resource name: " + resourceName );

This example tests the element functions exported from the 'echobot' resource:

// call getRootElement
Object[] ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getRootElement", null );
Element rootElement = (Element)ret[0];

// call getElementType
Object[] arguments = {rootElement};
ret = MTARPC.callFunction ( SERVER_HTTP_ADDRESS, "echobot", "getElementType", arguments );
String rootElementType = (String)ret[0];

System.out.println("Root element type: " + rootElementType );

More complex example

Mta java sdk example.png

The zip file contains an example program (MTAJavaTest.java) that implements a console that allows you to see events on a server and chat to the players there. This allows you to see how you can handle multiple returns.

Caveats

  • You cannot currently compare two Resource or Element objects that you expect to be identical - you need to do a "deep compare", comparing either the "id" fields or the "name" fields.

Download