Package com.wakachamo.jrcon

Source Code of com.wakachamo.jrcon.SourceHelper

package com.wakachamo.jrcon;

import com.github.koraktor.steamcondenser.exceptions.SteamCondenserException;
import com.github.koraktor.steamcondenser.steam.SteamPlayer;
import com.github.koraktor.steamcondenser.steam.servers.SourceServer;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
*
* @author Joaquim
*/
public class SourceHelper {
   
    private static SourceHelper helper = null;
   
    private SourceServer server;
    private boolean authenticated;
   
    public static SourceHelper getHelper() {
        return helper;
    }
   
    public static boolean authenticateNewHelper(String address, String password) {
        helper = new SourceHelper(address, password);
        return helper.isAuthenticated();
    }
    private String[] mapList;
   
    public SourceHelper(String address, String password) {
        try {
            this.server = new SourceServer(address);
            this.authenticated = this.server.rconAuth(password);
        } catch (TimeoutException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SteamCondenserException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    public boolean isAuthenticated() {
        if (!this.authenticated) {
            JRLog("SourceHelper: Not authenticated");
        }
        return this.authenticated;
    }
   
    public String executeCommand(String command) {
        System.out.println("Executing \"" + command + "\"");
        String output = null;
        try {
            output = this.server.rconExec(command);
        } catch (TimeoutException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SteamCondenserException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Got output: \"" + output + "\"");
        return output;
    }
   
    public String getCvar(String cvar) {
        String result = this.executeCommand(cvar);
        String value = result.substring((cvar.length() + 6)).split("\"")[0];
        return value;
    }
   
    public HashMap<String, Object> getServerInfo() {
        if (this.isAuthenticated()) {
            try {
                return this.server.getServerInfo();
            } catch (SteamCondenserException ex) {
                Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimeoutException ex) {
                Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
   
    public String[] getServerTags() {
        System.out.println(this.getServerInfo().get("serverTags"));
        return null;
    }
   
    public String[] getMapNames() {
        if (this.mapList == null) {
            String result = this.executeCommand("maps *");
            String[] rawMapList = result.split("\n");
            this.mapList = new String[rawMapList.length - 1];
            for (int i = 1; i < rawMapList.length; i++) {
                this.mapList[i-1] = rawMapList[i].substring(16).split(".bsp")[0];
            }
        }
        return this.mapList;
    }
   
    public String getCurrentMap() {
        return (String)this.getServerInfo().get("mapName");
    }
   
    public void setMap(String mapName) {
        this.executeCommand("changelevel " + mapName);
    }
   
    public HashMap<String, SteamPlayer> getPlayers() {
        try {
            this.server.updatePlayers();
            return this.server.getPlayers();
        } catch (SteamCondenserException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimeoutException ex) {
            Logger.getLogger(SourceHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
   
    public Object[] getPlayersArray() {
        return this.getPlayers().values().toArray();
    }
   
    public String getHostname() {
        return (String)this.getServerInfo().get("serverName");
    }
   
    public String setHostname(String hostname) {
        return this.executeCommand("hostname " + hostname);
    }
   
    public int getMaxPlayers() {
        return ((Integer)this.getServerInfo().get("maxPlayers")).intValue();
    }
   
    public int getNumberOfBots() {
        return ((Integer)this.getServerInfo().get("numberOfBots")).intValue();
    }
   
    public int getNumberOfPlayers() {
        return ((Integer)this.getServerInfo().get("numberOfPlayers")).intValue();
    }
   
    public String getDescription() {
        return (String)this.getServerInfo().get("gameDescription");
    }
   
    public static void JRLog(Object object) {
        System.out.println("jRCON: " + object);
    }

    public ListModel getPlayerListModel() {
        Object[] playersArray = this.getPlayersArray();
        DefaultListModel playerListModel = new DefaultListModel();
        for (Object playerObj : playersArray) {
            SteamPlayer currentPlayer = (SteamPlayer)playerObj;
            playerListModel.addElement(currentPlayer);
        }
        return playerListModel;
    }

    public void kickPlayer(SteamPlayer player) {
        String playerName = player.getName();
        this.executeCommand("kick " + playerName);
    }

    public void banPlayer(SteamPlayer player) {
        String playerName = player.getName();
        this.executeCommand("ban " + playerName);
    }
   
}
TOP

Related Classes of com.wakachamo.jrcon.SourceHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.