SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Tools and Files

Reply
 
Thread Tools Display Modes
Old 25/06/2012, 03:25 PM   #1
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default [Java] Query and RCON API (25/06/2012)

SA-MP Query and RCON API for Java
Edward McKnight (EM-Creations.co.uk) - StatusRed
Version: 1.1.1 (30/06/2012)
Java Version: 6 (should work with earlier versions also however)

After creating the Python SA-MP API, I decided to create a Java version.

As well as the examples here; included in the download are SampRconExample.java and SampQueryExample.java files. These are full of examples of how to call methods as well as documentation.

It's also worth noting (unlike the Python API) that addresses like: server.101stdivision.net won't work at the moment, however I'm working on making it work. You can however just type: nslookup <server hostname> in command prompt to get the IP that way, then use that.

Feel free to post if you have any questions or problems.


SampQuery methods:
SampQuery(server, port)
SampQuery.connect()
SampQuery.close()
SampQuery.getInfo()
SampQuery.getBasicPlayers()
SampQuery.getDetailedPlayers()
SampQuery.getRules()
SampQuery.getPing() - Added in version 1.1

Example | Output server info and basic players:
Code:
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same package as the SampQuery class.
 */

public class SampQueryExample {
    public static void main(String args[]) {
        System.out.println("Starting program..");
        SampQuery query = new SampQuery("127.0.0.1", 7777);
        
        if (query.connect()) { // If a successful connection has been made
            
            String[] serverInfo = query.getInfo(); // Get server info
            System.out.println(serverInfo[0]+" - "+serverInfo[1]+"/"+serverInfo[2]+" - "+serverInfo[3]+" | "+serverInfo[4]+" | "+serverInfo[5]);
            
            String[][] basicPlayers = query.getBasicPlayers(); // Get basic players, connection will time out if the player counter is above 100 and will return an empty array if no players are online
            System.out.println("Basic players:");
            for (int i = 0; basicPlayers.length > i; i++) {
                System.out.println((i + 1)+") "+basicPlayers[i][0]+" - "+basicPlayers[i][1]);
            }
            query.close(); // Close the connection
        } else {
            System.out.println("Server did not respond!");
        }
    }
}
SampRcon methods:
SampRcon.(server, port, password)
SampRcon.connect()
SampRcon.close()
SampRcon.getCommandList()
SampRcon.getServerVariables()
SampRcon.setWeather(weatherID=1)
SampRcon.setGravity(gravity=0.008)
SampRcon.ban(playerID)
SampRcon.kick(playerID)
SampRcon.banAddress(address)
SampRcon.unbanAddress(address)
SampRcon.reloadLog()
SampRcon.reloadBans()
SampRcon.say(message)
SampRcon.changeGameMode(gameMode)
SampRcon.setGameModeText(gameModeText) - Added in version 1.1
SampRcon.nextGameMode()
SampRcon.gmx() {same as SampRcon.nextGameMode()}
SampRcon.execConfig(config)
SampRcon.loadFilterscript(fs)
SampRcon.loadFS(fs) {same as SampRcon.loadFilterscript(fs)}
SampRcon.unloadFilterscript(fs)
SampRcon.unloadFS(fs) {same as SampRcon.unloadFilterscript(fs)}
SampRcon.reloadFilterscript(fs)
SampRcon.reloadFS(fs) {same as SampRcon.reloadFilterscript(fs)}
SampRcon.exit()
SampRcon.setHostName(hostName) - Added in version 1.1
SampRcon.setMapName(mapName) - Added in version 1.1
SampRcon.setTime(time) - Added in version 1.1
SampRcon.setURL(url) - Added in version 1.1
SampRcon.setPassword(password) - Added in version 1.1
SampRcon.removePassword() - Added in version 1.1
SampRcon.setRconPassword(password) - Added in version 1.1
SampRcon.disableRcon() - Added in version 1.1
SampRcon.enableQuery() - Added in version 1.1
SampRcon.disableQuery() - Added in version 1.1
SampRcon.enableAnnounce() - Added in version 1.1
SampRcon.disableAnnounce() - Added in version 1.1
SampRcon.setMaxNPCs(maxNPCs) - Added in version 1.1
SampRcon.call(command, delay)

Example | Output command list:
Code:
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same package as the SampRcon class.
 */

public class SampRconExample {
    public static void main(String args[]) {
        System.out.println("Starting program..");
        SampRcon query = new SampRcon("127.0.0.1", 7777, "changeme1");
        
        if (query.connect()) { // If a successful connection has been made
            // Output command list
            String[] commands = query.getCommandList();
            
            for (int i = 0; commands.length > i; i++) {
                System.out.println(commands[i]);
            }         
            query.close(); // Close the connection
        } else {
            System.out.println("Server did not respond!");
        }
    }
}
Example | Ban player:
Code:
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same package as the SampRcon class.
 */

public class SampRconExample {
    public static void main(String args[]) {
        System.out.println("Starting program..");
        SampRcon query = new SampRcon("127.0.0.1", 7777, "changeme1");
        
        if (query.connect()) { // If a successful connection has been made
            query.ban(1); // Ban player ID 1
            query.reloadBans(); // Reload the server's bans file
            query.close(); // Close the connection
        } else {
            System.out.println("Server did not respond!");
        }
    }
}

Last edited by StatusRed; 30/06/2012 at 09:01 AM.
StatusRed is offline   Reply With Quote
Old 25/06/2012, 03:49 PM   #2
TTJJ
Huge Clucker
 
Join Date: May 2007
Posts: 262
Reputation: 24
Default Re: [Java] Query and RCON API (25/06/2012)

Looks amazing, I will give it a try later on.
__________________


My Releases:
[GAMEMODE] Just Roleplay
[FILTERSCRIPT] Flak Gun
TTJJ is offline   Reply With Quote
Old 26/06/2012, 04:09 AM   #3
peterory
Big Clucker
 
Join Date: Sep 2010
Location: China-Guangzhou
Posts: 135
Reputation: 0
Default Re: [Java] Query and RCON API (25/06/2012)

very nice
can the client see the rcon message?
like the player chat
peterory is offline   Reply With Quote
Old 26/06/2012, 06:52 AM   #4
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [Java] Query and RCON API (25/06/2012)

Quote:
Originally Posted by peterory View Post
very nice
can the client see the rcon message?
like the player chat
No. This is for connecting to remote servers, as in SA-MP servers that are running on other machines, the RCON packets simply don't send back chat logs.
StatusRed is offline   Reply With Quote
Old 26/06/2012, 12:15 PM   #5
Edu33
High-roller
 
Edu33's Avatar
 
Join Date: Apr 2012
Location: H3LL
Posts: 1,158
Reputation: 89
Default Re: [Java] Query and RCON API (25/06/2012)

Wow, nice.
__________________

Parei.
Edu33 is offline   Reply With Quote
Old 27/06/2012, 12:47 PM   #6
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [Java] Query and RCON API (25/06/2012)

Version 1.1 released. Lots of new methods added.
StatusRed is offline   Reply With Quote
Old 29/06/2012, 08:25 PM   #7
R@f
Huge Clucker
 
Join Date: Aug 2007
Location: Switzerland
Posts: 358
Reputation: 217
Default Re: [Java] Query and RCON API (25/06/2012)

Very nice class ! I used it as example for my own query class for my Android App.

And I found a small mistake at the line 323. The array size you defined for the reply is too small (1024), you should set it at 3072 because if you try to get the detailed player list of a server with a lot of players on it, it will cause an exception.
R@f is offline   Reply With Quote
Old 30/06/2012, 08:53 AM   #8
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [Java] Query and RCON API (25/06/2012)

Quote:
Originally Posted by R@f View Post
Very nice class ! I used it as example for my own query class for my Android App.

And I found a small mistake at the line 323. The array size you defined for the reply is too small (1024), you should set it at 3072 because if you try to get the detailed player list of a server with a lot of players on it, it will cause an exception.
You're right, thanks for making me aware. I'll change the download soon.

EDIT: Now uploaded the fixed version: 1.1.1.
StatusRed is offline   Reply With Quote
Old 04/01/2013, 08:20 PM   #9
mikkidan
Little Clucker
 
mikkidan's Avatar
 
Join Date: Dec 2012
Posts: 20
Reputation: 1
Default Re: [Java] Query and RCON API (25/06/2012)

the mirrors don't work
mikkidan is offline   Reply With Quote
Old 04/01/2013, 09:51 PM   #10
RedCrossER
Huge Clucker
 
Join Date: Nov 2012
Posts: 291
Reputation: 8
Default Re: [Java] Query and RCON API (25/06/2012)

Perfect very nice , good job mate
RedCrossER is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tool/Web/Other] [PHP] Server Query and RCON API - Be an admin at ease! (16/02/2010) Westie Tools and Files 158 29/08/2014 05:50 PM
[Tool/Web/Other] [Python] Query and RCON API (23/06/2012) StatusRed Tools and Files 13 25/06/2012 03:27 PM
[Android]SA-MP App - Query and send RCON commands to your servers R@f Everything and Nothing 25 04/06/2012 11:35 PM
SAMP RCON & Query API CaTaLinU Scripting Help 5 11/02/2012 12:25 PM
Westies API / SAMP Api (Query & RCON) Lee Strong Help Archive 2 14/10/2010 09:37 AM


All times are GMT. The time now is 12:06 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.