Java SDK: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 45: Line 45:
</syntaxhighlight>
</syntaxhighlight>


For a more complex example that handles multiple returns, download the zipfile and see MTAJavaTest.java.
For a more complex example that handles multiple returns, download the zip file and see MTAJavaTest.java.


==Download==
==Download==
[http://misc.opencoding.net/mta/mtajavasdk_0.1.zip Download Version 0.1]

Revision as of 03:20, 27 June 2007

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:

[java5]
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:

[java5]
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:

[java5]
// 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 );

For a more complex example that handles multiple returns, download the zip file and see MTAJavaTest.java.

Download

Download Version 0.1