[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


Messages In This Thread
[Java] Query and RCON API (25/06/2012) - by StatusRed - 25.06.2012, 15:25
Re: [Java] Query and RCON API (25/06/2012) - by TTJJ - 25.06.2012, 15:49
Re: [Java] Query and RCON API (25/06/2012) - by peterory - 26.06.2012, 04:09
Re: [Java] Query and RCON API (25/06/2012) - by StatusRed - 26.06.2012, 06:52
Re: [Java] Query and RCON API (25/06/2012) - by Edu33 - 26.06.2012, 12:15
Re: [Java] Query and RCON API (25/06/2012) - by StatusRed - 27.06.2012, 12:47
Re: [Java] Query and RCON API (25/06/2012) - by R@f - 29.06.2012, 20:25
Re: [Java] Query and RCON API (25/06/2012) - by StatusRed - 30.06.2012, 08:53
Re: [Java] Query and RCON API (25/06/2012) - by mikkidan - 04.01.2013, 19:20
Re: [Java] Query and RCON API (25/06/2012) - by RedCrossER - 04.01.2013, 20:51

Forum Jump:


Users browsing this thread: 1 Guest(s)