[Tool/Web/Other] [Java] Query and RCON API (25/06/2012)
#1

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

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

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

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.
Reply
#5

Wow, nice.
Reply
#6

Version 1.1 released. Lots of new methods added.
Reply
#7

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.
Reply
#8

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.
Reply
#9

the mirrors don't work
Reply
#10

Perfect very nice , good job mate
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)