SA-MP Forums Archive
[Tool/Web/Other] [Java] Query and RCON API (25/06/2012) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Tools and Files (https://sampforum.blast.hk/forumdisplay.php?fid=82)
+---- Thread: [Tool/Web/Other] [Java] Query and RCON API (25/06/2012) (/showthread.php?tid=354062)



[Java] Query and RCON API (25/06/2012) - StatusRed - 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.

Repost on my website with syntax highlighting for the examples

Download

Download Mirror #1
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:
Код:
/**
 *
 * @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:
Код:
/**
 *
 * @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:
Код:
/**
 *
 * @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!");
        }
    }
}



Re: [Java] Query and RCON API (25/06/2012) - TTJJ - 25.06.2012

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


Re: [Java] Query and RCON API (25/06/2012) - peterory - 26.06.2012

very nice
can the client see the rcon message?
like the player chat


Re: [Java] Query and RCON API (25/06/2012) - StatusRed - 26.06.2012

Quote:
Originally Posted by peterory
Посмотреть сообщение
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.


Re: [Java] Query and RCON API (25/06/2012) - Edu33 - 26.06.2012

Wow, nice.


Re: [Java] Query and RCON API (25/06/2012) - StatusRed - 27.06.2012

Version 1.1 released. Lots of new methods added.


Re: [Java] Query and RCON API (25/06/2012) - R@f - 29.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.


Re: [Java] Query and RCON API (25/06/2012) - StatusRed - 30.06.2012

Quote:
Originally Posted by R@f
Посмотреть сообщение
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.


Re: [Java] Query and RCON API (25/06/2012) - mikkidan - 04.01.2013

the mirrors don't work


Re: [Java] Query and RCON API (25/06/2012) - RedCrossER - 04.01.2013

Perfect very nice , good job mate