SA-MP Query and RCON API for Java

Version: 1.1 (27/06/2012)

Download

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.

 

SampQuery methods:
SampQuery(serverport)
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.(serverportpassword)
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(commanddelay)

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!");
        }
    }
}

4 thoughts on “SA-MP Query and RCON API for Java

  1. Error:
    [root@server rwa]# ./SampQueryExample.java
    : No existe el fichero o el directorio
    : no se encontró la ordenlínea 2: *
    ./SampQueryExample.java: línea 3: error sintáctico cerca del elemento inesperado `(‘
    ‘/SampQueryExample.java: línea 3: ` * @author Edward McKnight (EM-Creations.co.uk)

    1. Hi,

      Check the SA-MP forums, I think someone has used it in an Android App (not sure which IDE they used though) if you can tell me the specific problem you have I might be able to help?

      Maybe over the summer I could make it compatible as a Gradle dependency too.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

This site uses Akismet to reduce spam. Learn how your comment data is processed.