Core/src/eu/univento/core/api/player/TeamSpeak.java

148 lines
4.3 KiB
Java

package eu.univento.core.api.player;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import com.github.theholywaffle.teamspeak3.TS3Api;
import com.github.theholywaffle.teamspeak3.TS3Config;
import com.github.theholywaffle.teamspeak3.TS3Query;
import com.github.theholywaffle.teamspeak3.api.wrapper.Client;
import com.github.theholywaffle.teamspeak3.api.wrapper.ClientInfo;
import eu.univento.core.Core;
import eu.univento.core.api.Config;
import eu.univento.core.api.MySQL;
import eu.univento.core.api.player.Perms.Ranks;
/**
* basic functions for teamspeak communication
* @author joethei
* @version 1.0
*/
public class TeamSpeak {
private TS3Api api;
private static int verified = 11;
public static int getVerifiedId() {
return verified;
}
/**
* inits class
*/
public TeamSpeak() {
final TS3Config config = new TS3Config();
config.setHost(Config.readString("TS.IP"));
config.setQueryPort(Config.readInt("TS.QueryPort"));
config.setDebugLevel(Level.ALL);
config.setLoginCredentials(Config.readString("TS.QueryUser"), Config.readString("TS.Query.Pass"));
final TS3Query query = new TS3Query(config);
query.connect();
final TS3Api api = query.getApi();
api.selectVirtualServerById(1);
api.setNickname("Rechteverteiler");
this.api = api;
}
/**
* gets teamspeak api
* @return TS3Api
*/
public TS3Api getAPI() {
return api;
}
/**
* gets client info for id
* @param id database id
* @return ClientInfo
*/
public ClientInfo getClientInfo(int id) {
return api.getClientInfo(id);
}
/**
* gets database id from teamspeak for player
* @param p CustomPlayer
* @return Integer
* @throws SQLException SQL server not available or throwing error
* @throws ClassNotFoundException class couldn't be found
*/
public int getTsId(CustomPlayer p) throws ClassNotFoundException, SQLException {
MySQL sql = Core.returnSQL();
sql.openConnection();
ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM PlayerData WHERE player_uuid='" + p.getUniqueId() + "'");
rs.next();
if (rs.getInt("TS_ID") != 0) {
int id = rs.getInt("TS_ID");
sql.closeConnection();
return id;
}
sql.closeConnection();
return 0;
}
/**
* writes id of player to database
* @param p CustomPlayer
* @param id database id
* @throws SQLException SQL server not available or throwing error
* @throws ClassNotFoundException class couldn't be found
*/
private void setTsId(CustomPlayer p, int id) throws ClassNotFoundException, SQLException {
MySQL sql = Core.returnSQL();
sql.openConnection();
sql.getConnection().createStatement().execute("UPDATE PlayerData SET TS_ID='" + id + "' WHERE player_uuid='" + p.getUniqueId() + "';");
sql.closeConnection();
}
/**
* sets rank on teamspeak
* @param p CustomPlayer
* @throws SQLException SQL server not available or throwing error
* @throws ClassNotFoundException class couldn't be found
*/
public void setRank(CustomPlayer p) throws ClassNotFoundException, SQLException {
TS3Api api = getAPI();
for(int i = 0; i >= api.getClients().size(); i++) {
String player_ip = p.getAddress().getHostName();
Client client = api.getClients().get(i);
ClientInfo info = api.getClientInfo(client.getId());
String ts_ip = info.getIp();
if(player_ip == ts_ip) {
int db_id = getTsId(p);
if(db_id == 0) {
api.removeClientFromServerGroup(getIdForRank(p.getRank()), db_id);
api.removeClientFromServerGroup(getVerifiedId(), db_id);
}
api.addClientToServerGroup(getIdForRank(p.getRank()), client.getDatabaseId());
api.addClientToServerGroup(getVerifiedId(), client.getDatabaseId());
setTsId(p, client.getDatabaseId());
}
}
}
/**
* gets group id for rank
* @param r Ranks
* @return Integer
*/
public int getIdForRank(Ranks r) {
switch(r) {
case Admin: return 10;
case Developer: return 13;
case Moderator: return 12;
case HeadBuilder: return 19;
case Builder: return 20;
case Supporter: return 14;
case Youtuber: return 21;
case Premium: return 22;
case Spieler: return 42;
default: return 0;
}
}
}