JavaSDK: Difference between revisions

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


*[http://forum.mtasa.com/viewtopic.php?f=148&t=46367 For more examples / tutorials]
==More complex example==
==More complex example==
[[Image:ExamplaSDK.png]]
[[Image:ExamplaSDK.png]]

Revision as of 15:06, 7 August 2012

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

Getting Started

To use it, you need to add library to your class-path. It's included in the zip file below.

To get started, copy modules/ml_sockets into your modules folder.And load that. After that, copy resources/jsdk into your resources folder.And start that.


Classes

com.mtasa.elements.Resource

public String getName();
public com.mtasa.MTA getServer();
public void setServer(com.mtasa.MTA newServer);
public String getResourceInfo(String attr);
public boolean setResourceInfo(String attr, String newVal);
public boolean stopResource();
public boolean startResource();
public com.mtasa.LuaArgs call(String functionName,LuaArgs parameters);
public String toString();

com.mtasa.elements.Element

public String getType();
public boolean is_a(Class<?> compareElement);
public boolean clearElementVisibleTo(Element visibleTo);
public Element cloneElement();
public boolean destroyElement();
public Point3D getElementPosition();
public boolean setElementPosition(double x,double y,double z);
public boolean setElementPosition(Point3D points);
public Point3D getElementRotation();
public boolean setElementRotation(double x,double y,double z);
public boolean setElementRotation(Point3D points);
public int getElementAlpha();
public boolean setElementAlpha(int alpha);
public float getElementHealth();
public boolean setElementHealth(float health);
public int getElementModel();
public boolean setElementModel(int model);
public int getElementInterior();
public boolean setElementInterior(int interior);
public int getElementDimension();
public boolean setElementDimension(int dimension);
public Point3D getElementVelocity();
public boolean setElementVelocity(double x,double y,double z);
public boolean setElementVelocity(Point3D points);
public boolean isElementVisibleTo(Element element);
public boolean setElementVisibleTo(Element element,boolean visible);
public boolean isElementFrozen();
public boolean setElementFrozen(boolean frozen);
public String getElementID();
public boolean setElementID(String new_id);
public String getElementData(String data_name);
public boolean setElementData(String data_name,String newVal);

com.mtasa.elements.Ped extends Element

All Element Functions;
// Cloth Types
public static final int CLOTH_SHIRT = 0;
public static final int CLOTH_HEAD = 1;
public static final int CLOTH_TROUSERS = 2;
public static final int CLOTH_SHOES = 3;
public static final int CLOTH_TATTOOS_LEFT_UPPER_ARM = 4;
public static final int CLOTH_TATTOOS_LEFT_LOWER_ARM = 5;
public static final int CLOTH_TATTOOS_RIGHT_UPPER_ARM = 6;
public static final int CLOTH_TATTOOS_RIGHT_LOWER_ARM = 7;
public static final int CLOTH_TATTOOS_BACK = 8;
public static final int CLOTH_TATTOOS_LEFT_CHEST = 9;
public static final int CLOTH_TATTOOS_RIGHT_CHEST = 10;
public static final int CLOTH_TATTOOS_STOMACH = 11;
public static final int CLOTH_TATTOOS_LOWER_BACK = 12;
public static final int CLOTH_NECKLACE = 13;
public static final int CLOTH_WATCH = 14;
public static final int CLOTH_GLASSES = 15;
public static final int CLOTH_HAT = 16;
public static final int CLOTH_EXTRA = 17;

// Fighting Styles
public static final int STYLE_STANDARD = 4;
public static final int STYLE_BOXING = 5;
public static final int STYLE_KUNG_FU = 6;
public static final int STYLE_KNEE_HEAD = 7;
public static final int STYLE_GRAB_KICK = 15;
public static final int STYLE_ELBOWS = 16;

// Ped stats
public static final int PROGRESS_MADE = 0;
public static final int TOTAL_PROGRESS = 1;
public static final int LONGEST_BASKETBALL = 2;

public static final int DIST_FOOT = 3;
public static final int DIST_CAR = 4;
public static final int DIST_BIKE = 5;
public static final int DIST_BOAT = 6;
public static final int DIST_GOLF_CART = 7;
public static final int DIST_HELICOPTOR = 8;
public static final int DIST_PLANE = 9;
public static final int LONGEST_WHEELIE_DIST = 10;
public static final int LONGEST_STOPPIE_DIST = 11;
public static final int LONGEST_2WHEEL_DIST = 12;

public static final int WEAPON_BUDGET = 13;
public static final int FASHION_BUDGET = 14;
public static final int PROPERTY_BUDGET = 15;
public static final int SPRAYING_BUDGET = 16;

public static final int LONGEST_WHEELIE_TIME = 17;
public static final int LONGEST_STOPPIE_TIME = 18;
public static final int LONGEST_2WHEEL_TIME = 19;
public static final int FOOD_BUDGET = 20;

public static final int FAT = 21;
public static final int STAMINA = 22;
public static final int BODY_MUSCLE = 23;
public static final int MAX_HEALTH = 24;
public static final int SEX_APPEAL = 25;

public static final int DIST_SWIMMING = 26;
public static final int DIST_CYCLE = 27;
public static final int DIST_TREADMILL = 28;
public static final int DIST_EXCERSISE_BIKE = 29;
public static final int TATTOO_BUDGET = 30;
public static final int HAIRDRESSING_BUDGET = 31;
public static final int PROSTITUTE_BUDGET = 33;

public static final int MONEY_SPENT_GAMBLING = 35;
public static final int MONEY_MADE_PIMPING = 36;
public static final int MONEY_WON_GAMBLING = 37;
public static final int BIGGEST_GAMBLING_WIN = 38;
public static final int BIGGEST_GAMBLING_LOSS = 39;
public static final int LARGEST_BURGLARY_SWAG = 40;
public static final int MONEY_MADE_BURGLARY = 41;
public static final int LONGEST_TREADMILL_TIME = 44;
public static final int LONGEST_EXCERSISE_BIKE_TIME = 45;
public static final int HEAVIEST_WEIGHT_BENCH_PRESS = 46;
public static final int HEAVIEST_WEIGHT_DUMBELLS = 47;
public static final int BEST_TIME_HOTRING = 48;
public static final int BEST_TIME_BMX = 49;
public static final int LONGEST_CHASE_TIME = 51;
public static final int LAST_CHASE_TIME = 52;
public static final int WAGE_BILL = 53;
public static final int STRIP_CLUB_BUDGET = 54;
public static final int CAR_MOD_BUDGET = 55;
public static final int TIME_SPENT_SHOPPING = 56;
public static final int TOTAL_SHOPPING_BUDGET = 62;
public static final int TIME_SPENT_UNDERWATER = 63;

public static final int RESPECT_TOTAL = 64;
public static final int RESPECT_GIRLFRIEND = 65;
public static final int RESPECT_CLOTHES = 66;
public static final int RESPECT_FITNESS = 67;
public static final int RESPECT = 68;

public static final int WEAPONTYPE_PISTOL_SKILL = 69;
public static final int WEAPONTYPE_PISTOL_SILENCED_SKILL = 70;
public static final int WEAPONTYPE_DESERT_EAGLE_SKILL = 71;
public static final int WEAPONTYPE_SHOTGUN_SKILL = 72;
public static final int WEAPONTYPE_SAWNOFF_SHOTGUN_SKILL = 73;
public static final int WEAPONTYPE_SPAS12_SHOTGUN_SKILL = 74;
public static final int WEAPONTYPE_MICRO_UZI_SKILL = 75;
public static final int WEAPONTYPE_MP5_SKILL = 76;
public static final int WEAPONTYPE_AK47_SKILL = 77;
public static final int WEAPONTYPE_M4_SKILL = 78;
public static final int WEAPONTYPE_SNIPERRIFLE_SKILL = 79;
public static final int SEX_APPEAL_CLOTHES = 80;
public static final int GAMBLING = 81;

public static final int PEOPLE_KILLED_BY_OTHERS = 120;
public static final int PEOPLE_KILLED_BY_PLAYER = 121;
public static final int CARS_DESTROYED = 122;
public static final int BOATS_DESTROYED = 123;
public static final int HELICOPTORS_DESTROYED = 124;
public static final int PROPERTY_DESTROYED = 125;
public static final int ROUNDS_FIRED = 126;
public static final int EXPLOSIVES_USED = 127;
public static final int BULLETS_HIT = 128;
public static final int TYRES_POPPED = 129;
public static final int HEADS_POPPED = 130;
public static final int WANTED_STARS_ATTAINED = 131;
public static final int WANTED_STARS_EVADED = 132;
public static final int TIMES_ARRESTED = 133;
public static final int DAYS_PASSED = 134;
public static final int TIMES_DIED = 135;
public static final int TIMES_SAVED = 136;
public static final int TIMES_CHEATED = 137;
public static final int SPRAYINGS = 138;
public static final int MAX_JUMP_DISTANCE = 139;
public static final int MAX_JUMP_HEIGHT = 140;
public static final int MAX_JUMP_FLIPS = 141;
public static final int MAX_JUMP_SPINS = 142;
public static final int BEST_STUNT = 143;
public static final int UNIQUE_JUMPS_FOUND = 144;
public static final int UNIQUE_JUMPS_DONE = 145;
public static final int MISSIONS_ATTEMPTED = 146;
public static final int MISSIONS_PASSED = 147;
public static final int TOTAL_MISSIONS = 148;
public static final int TAXI_MONEY_MADE = 149;
public static final int PASSENGERS_DELIVERED_IN_TAXI = 150;
public static final int LIVES_SAVED = 151;
public static final int CRIMINALS_CAUGHT = 152;
public static final int FIRES_EXTINGUISHED = 153;
public static final int PIZZAS_DELIVERED = 154;
public static final int ASSASSINATIONS = 155;
public static final int LATEST_DANCE_SCORE = 156;
public static final int VIGILANTE_LEVEL = 157;
public static final int AMBULANCE_LEVEL = 158;
public static final int FIREFIGHTER_LEVEL = 159;
public static final int DRIVING_SKILL = 160;
public static final int TRUCK_MISSIONS_PASSED = 161;
public static final int TRUCK_MONEY_MADE = 162;
public static final int RECRUITED_GANG_MEMBERS_KILLED = 163;
public static final int ARMOUR = 164;
public static final int ENERGY = 165;
public static final int PHOTOS_TAKEN = 166;
public static final int KILL_FRENZIES_ATTEMPTED = 167;
public static final int KILL_FRENZIES_PASSED = 168;
public static final int FLIGHT_TIME = 169;
public static final int TIMES_DROWNED = 170;
public static final int NUM_GIRLS_PIMPED = 171;
public static final int BEST_POSITION_HOTRING = 172;
public static final int FLIGHT_TIME_JETPACK = 173;
public static final int SHOOTING_RANGE_SCORE = 174;
public static final int VALET_CARS_PARKED = 175;
public static final int KILLS_SINCE_LAST_CHECKPOINT = 176;
public static final int TOTAL_LEGITIMATE_KILLS = 177;
public static final int BLOODRING_KILLS = 178;
public static final int BLOODRING_TIME = 179;
public static final int NO_MORE_HURRICANES = 180;
public static final int CITIES_PASSED = 181;
public static final int POLICE_BRIBES = 182;
public static final int CARS_STOLEN = 183;
public static final int CURRENT_GIRLFRIENDS = 184;
public static final int BAD_DATES = 185;
public static final int GIRLS_DATED = 186;
public static final int TIMES_SCORED_WITH_GIRL = 187;
public static final int DATES = 188;
public static final int GIRLS_DUMPED = 189;
public static final int TIMES_VISITED_PROSTITUTE = 190;
public static final int HOUSES_BURGLED = 191;
public static final int SAFES_CRACKED = 192;
public static final int STOLEN_ITEMS_SOLD = 194;
public static final int EIGHT_BALLS_IN_POOL = 195;
public static final int WINS_IN_POOL = 196;
public static final int LOSSES_IN_POOL = 197;
public static final int VISITS_TO_GYM = 198;
public static final int MEALS_EATEN = 200;
public static final int UNDERWATER_STAMINA = 225;
public static final int BIKE_SKILL = 229;
public static final int CYCLE_SKILL = 230;
// Functions:
public boolean addPedClothes(String texture,String model, int type);
public boolean doesPedHaveJetPack();
public String[] getPedClothes(int clothesType);
public int getPedFightingStyle();
public boolean setPedFightingStyle(int style);
public float getPedGravity();
public boolean setPedGravity(float style);
public int getPedSkin();
public boolean setPedSkin(int skin);
public float getPedRotation();
public boolean setPedRotation(float rot);
public boolean givePedJetpack();
public boolean removePedJetpack();
public Vehicle getPedOccupiedVehicle();
public float getPedStat(int stat);

com.mtasa.elements.Player extends Ped

All Element Functions;
All Ped Functions;
public String getPlayerName();
public String getPlayerName(boolean removecolorcodes);
public String getPlayerIP();
public String getPlayerSerial();
public int getPlayerMoney();
public int getPlayerPing();
public Team getPlayerTeam();
public int getPlayerWantedLevel();
public boolean givePlayerMoney(int money);
public boolean isPlayerMuted();
public boolean setPlayerMoney(int money);
public boolean setPlayerMuted(boolean muted);
public boolean setPlayerTeam(Team team);
public boolean spawnPlayer(double x,double y,double z);
public boolean spawnPlayer(Point3D p);
public boolean spawnPlayer(Point3D point,double rot,int skin,int interior,int dimension,Team team);

com.mtasa.elements.Blip extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.CollisionShape extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.GTAObject extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.Pickup extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.RadarArea extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.Team extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.elements.Vehicle extends Element

All Element Functions;
// Functions will be added soon..

com.mtasa.functions.ElementFuncs

public MTA getServer();
public void setServer(MTA server);
public static String type_to_string(Class<? extends Element> type);
public <E extends Element> E[] getElementsByType(Class<E> type);
public Object[] getElementsByType(String type);
public Element createElement(String type);
public Element getElementByID(String id);

com.mtasa.functions.Output

public static final int LEVEL_CUSTOM = 0;
public static final int LEVEL_ERROR = 1;
public static final int LEVEL_WARNING = 2;
public static final int LEVEL_INFO = 3;

// Functions:

public MTA getServer();
public void setServer(MTA server);
public boolean outputChatBox(Object message);
public boolean outputChatBox(Object message,Element toElement);
public boolean outputChatBox(Object message,Element toElement,int r,int g,int b);
public boolean outputChatBox(Object message,Element toElement,int r,int g,int b,boolean colorcoded);
public boolean outputConsole(Object message);
public boolean outputConsole(Object message,Element toElement);
public boolean outputDebugString(Object message);
public boolean outputDebugString(Object message,int dlevel);
public boolean outputDebugString(Object message,int dlevel,int r,int g,int b);
public boolean outputServerLog(Object message);

com.mtasa.functions.PlayerFuncs

public MTA getServer();
public void setServer(MTA server);
public Player getPlayerFromName(String name);
public Player getPlayerFromNamePart(String name);
public Player[] getAlivePlayers();
public Player[] getDeadPlayers();
public Player getRandomPlayer();
public int getPlayerCount()

com.mtasa.LuaArgs extends java.util.List

All list functions.So, you can use in generic for.
public MTA getServer();
public void setServer(MTA server);
public Element parseElement(int index);
public Player parsePlayer(int index);
public Pickup parsePickup(int index);
public Ped parsePed(int index);
public Blip parseBlip(int index);
public CollisionShape parseCollisionShape(int index);
public GTAObject parseGTAObject(int index);
public RadarArea parseRadarArea(int index);
public Team parseTeam(int index);
public Vehicle parseVehicle(int index) ;
public Resource parseResource(int index) ;
public String parseString(int index);
public Boolean parseBoolean(int index);
public Double parseDouble(int index);
public Float parseFloat(int index);
public Integer parseInt (int index);
public String toJson();
public void loadFromJSON(String json);
public Object[] jsonToObject(String json);
public static String toJson(Object o);
public static Object[] fromJson(String j);
public Object get(int index);

com.mtasa.MTA

public Output out;
public Element rootElement;
public PlayerFuncs players;
public ElementFuncs elements;
public static final String RESOURCE = "jsdk"; // JavaSDK Resource Name.

// Functions;
public void sockOpen(); // Port will used in callJava (Changed)
public Element parseElement(Object o);
public Resource parseResource(Object o);
public void sockClose();
public int getSocketPort();
public LuaArgs call(String resource,String function,LuaArgs args); // Function must be exported and given http="true"
public LuaArgs callFunction(String function,LuaArgs args); // This is for calling server-side functions.(etc:getElementByType)
public LuaArgs luaArg(Object i); // This is for only 1 parameter arguments.
// callJava Functions;
public void addInputEvent(InputEvent e); // Only usable with callJava and sockOpen
public void removeInputEvent(InputEvent e); // Only usable with callJava and sockOpen
public void clearInputEvent(); // Only usable with callJava and sockOpen
public ArrayList<InputEvent> getInputEvents(InputEvent e); // Only usable with callJava and sockOpen

// Getter-Setter;
public void setHost(String host);
public String getHost();
public void setPort(int port);
public int getPort();
public void setUsername(String username);
public String getUsername();
public void setPassword(String password);
public String getPassword();
public String getCharset();
public void setCharset(String charset);

com.mtasa.InputEvent( Interface )

public void onAction(LuaArgs args, String input) throws MTAException

com.mtasa.MTAException extends Exception

All exception functions;

Examples

MTA server = new MTA("localhost",22005,"admin","12345"); // Sweet, we are creating a new instance and connection.
/* Example 1: */
Player[] players = server.elements.getElementsByType(Player.class); // ElementFuncs deployed in server.elements :)
server.out.outputChatBox("There are "+players.length+" players",server.rootElement,180,25,25,false); // You don't need getRootElement(), it's deployed in server.rootElement variable.
LuaArgs ret = server.call("rcon","getThisResource",null); // We are calling getThisResource in rcon bot.It's exported :)
Resource thisRes = ret.parseResource(0); // Now, we parsed argument to Resource object.
Player playerRancho = server.players.getPlayerFromName("Rancho"); // We're getting player named Rancho, if he has a colorcode. We must add this 
if (playerRancho != null){ // If playerRancho exists
	server.out.outputChatBox("<PM> JavaSDK: #0055FFHello Sweety",playerRancho,255,255,255,true);
}else{ // else
	server.out.outputDebugString("There is no named player RANCHO!");
}
/* Example 2 : */
Ped[] peds = server.elements.getElementsByType(Ped.class); // We deployed Peds in the peds variable.
for (Ped ped: peds){ // Generic for, (foreach)
	if (ped.doesPedHaveJetPack()) // If ped has a jetpack
		ped.removePedJetpack(); // remove him jetpack
	else // else
		ped.givePedJetpack(); // give him jetpack 
}
/* Example 3: */
Element[] myElements = server.elements.getElementsByType("myElement"); // Now we get elements by the string type
// To do :)
/* Example 4: for callJava */
server.sockOpen(); // callJava open ports
server.addInputEvent(new InputEvent(){
	@Override
	public void onAction(LuaArgs args, String input) throws MTAException{
		String event = args.parseString(0); // Argumant 1 :) It's not default argument :)
		if (event.equals("onMyCall")){
			System.out.println("onMyCall: "+args.parseString(1));
		}
	}
});
// lua file:
for k,v in ipairs(exports.jsdk:getConnections()) do
    exports.jsdk:callJava(v,"onMyCall","Hello");
end
/* Example 5: is_a */
Player playerRancho = server.players.getPlayerFromName("Rancho"); // We're getting player named Rancho, if he has a colorcode. We must add this 

if (playerRancho != null){ // If playerRancho exists
	LuaArgs myCallbackargs = new LuaArgs(server); // create new instance
	myCallbackargs.add(playerRancho); // add a new argument
	myCallbackargs.add("How are u?"); // add a new argument
	LuaArgs ret = server.call("rcon","returnElement",myCallbackargs); // call the howAre function into rcon resource, and send the 2 parameter :) myCallbackargs
	for (Object o: ret){ // generic for returns
		Element e = (Element)o;
		server.out.outputServerLog("Returned value is a player?: "+o.is_a(Player.class));
	}
}else{ // else
	server.out.outputDebugString("There is no named player RANCHO!");
} 

More complex example

ExamplaSDK.png

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.
  • The zip contains src, and javadoc

Download

Contact

If you have any questions/suggestions you can contact author on MTA forum.