2015-12-06 20:23:13 +01:00
|
|
|
package eu.univento.core.api.player;
|
2015-12-06 11:04:47 +01:00
|
|
|
|
|
|
|
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;
|
2015-12-06 20:23:13 +01:00
|
|
|
import eu.univento.core.api.Config;
|
2016-02-27 09:08:17 +01:00
|
|
|
import eu.univento.core.api.database.MySQL;
|
2015-12-06 20:23:13 +01:00
|
|
|
import eu.univento.core.api.player.Perms.Ranks;
|
2016-02-27 09:08:17 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.logging.Level;
|
2015-12-06 11:04:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* basic functions for teamspeak communication
|
|
|
|
* @author joethei
|
|
|
|
* @version 1.0
|
|
|
|
*/
|
|
|
|
public class TeamSpeak {
|
2016-02-27 09:08:17 +01:00
|
|
|
|
|
|
|
private TS3Query query;
|
2015-12-06 11:04:47 +01:00
|
|
|
private TS3Api api;
|
2016-02-27 09:08:17 +01:00
|
|
|
|
|
|
|
int verified = 11;
|
|
|
|
|
|
|
|
/**
|
2015-12-06 11:04:47 +01:00
|
|
|
* inits class
|
|
|
|
*/
|
|
|
|
public TeamSpeak() {
|
|
|
|
final TS3Config config = new TS3Config();
|
|
|
|
config.setHost(Config.readString("TS.IP"));
|
|
|
|
config.setQueryPort(Config.readInt("TS.QueryPort"));
|
2016-02-27 09:08:17 +01:00
|
|
|
config.setDebugLevel(Level.OFF);
|
2015-12-06 11:04:47 +01:00
|
|
|
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);
|
2016-02-27 09:08:17 +01:00
|
|
|
api.setNickname("Rechteverteiler von " + Bukkit.getServerName());
|
2015-12-06 11:04:47 +01:00
|
|
|
this.api = api;
|
2016-02-27 09:08:17 +01:00
|
|
|
this.query = query;
|
2015-12-06 11:04:47 +01:00
|
|
|
}
|
2016-02-27 09:08:17 +01:00
|
|
|
|
|
|
|
public void disconnect() {
|
|
|
|
query.exit();
|
|
|
|
}
|
2015-12-06 11:04:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets teamspeak api
|
|
|
|
* @return TS3Api
|
|
|
|
*/
|
2016-02-27 09:08:17 +01:00
|
|
|
TS3Api getAPI() {
|
2015-12-06 11:04:47 +01:00
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets client info for id
|
|
|
|
* @param id database id
|
|
|
|
* @return ClientInfo
|
|
|
|
*/
|
2016-02-27 09:08:17 +01:00
|
|
|
ClientInfo getClientInfo(int id) {
|
2015-12-06 11:04:47 +01:00
|
|
|
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();
|
2016-02-27 09:08:17 +01:00
|
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT TS_ID FROM users WHERE ID='" + p.getID() + "'");
|
|
|
|
ResultSet rs = st.executeQuery();
|
2015-12-06 11:04:47 +01:00
|
|
|
rs.next();
|
2015-12-06 20:23:13 +01:00
|
|
|
if (rs.getInt("TS_ID") != 0) {
|
|
|
|
int id = rs.getInt("TS_ID");
|
2015-12-06 11:04:47 +01:00
|
|
|
sql.closeConnection();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
sql.closeConnection();
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-27 09:08:17 +01:00
|
|
|
|
|
|
|
public String getUUID(int id) throws SQLException, ClassNotFoundException {
|
|
|
|
MySQL sql = Core.returnSQL();
|
|
|
|
sql.openConnection();
|
|
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT UUID FROM users WHERE TS_ID='" + id + "';");
|
|
|
|
ResultSet rs = st.executeQuery();
|
|
|
|
if(rs.next()) {
|
|
|
|
String uuid = rs.getString("UUID");
|
|
|
|
sql.closeConnection();
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
sql.closeConnection();
|
|
|
|
return null;
|
|
|
|
}
|
2015-12-06 11:04:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-02-27 09:08:17 +01:00
|
|
|
void setTsId(CustomPlayer p, int id) throws ClassNotFoundException, SQLException {
|
2015-12-06 11:04:47 +01:00
|
|
|
MySQL sql = Core.returnSQL();
|
|
|
|
sql.openConnection();
|
2016-02-27 09:08:17 +01:00
|
|
|
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE users SET TS_ID='" + id + "' WHERE UUID='" + p.getUniqueId().toString() + "';");
|
|
|
|
st.execute();
|
2015-12-06 11:04:47 +01:00
|
|
|
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
|
|
|
|
*/
|
2016-02-27 09:08:17 +01:00
|
|
|
private void setRank(CustomPlayer p, Client client) throws ClassNotFoundException, SQLException {
|
|
|
|
//for(ServerGroup groups : api.getServerGroupsByClient(client)) {
|
|
|
|
// api.removeClientFromServerGroup(groups, client);
|
|
|
|
//}
|
|
|
|
api.addClientToServerGroup(getIdForRank(p.getRank()), client.getDatabaseId());
|
|
|
|
api.addClientToServerGroup(verified, client.getDatabaseId());
|
|
|
|
api.pokeClient(client.getId(), "Du wurdest von unserem freundlichem Bot verifiziert");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void verify(CustomPlayer p) throws SQLException, ClassNotFoundException {
|
|
|
|
TS3Api api = getAPI();
|
|
|
|
if(hasVerified(p)) {
|
|
|
|
p.sendMessage("§cDu hast bereits einen verknüpften Account");
|
|
|
|
}else {
|
|
|
|
Client client = api.getClientByNameExact(p.getName(), false);
|
|
|
|
if(client != null) {
|
|
|
|
setRank(p, client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p.sendMessage("§cDu bist gerade nicht im TS online oder dein Nickname entspricht nicht dem IGN");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Client getClient(CustomPlayer p) throws SQLException, ClassNotFoundException {
|
|
|
|
if(hasVerified(p)) {
|
|
|
|
int id = getTsId(p);
|
|
|
|
for (Client client : api.getClients()) {
|
|
|
|
if (client.getDatabaseId() == id) {
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CustomPlayer getPlayer(Client client) throws SQLException, ClassNotFoundException {
|
|
|
|
Player player = Bukkit.getPlayer(UUID.fromString(getUUID(client.getDatabaseId())));
|
|
|
|
return CustomPlayer.getPlayer(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkVerifiyed(CustomPlayer p) throws SQLException, ClassNotFoundException {
|
|
|
|
if(hasVerified(p)) {
|
|
|
|
Client client = getClient(p);
|
|
|
|
if(client != null) {
|
|
|
|
//for(ServerGroup group : api.getServerGroupsByClient(client)) {
|
|
|
|
// if(group.getId() == getIdForRank(p.getRank())) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// setRank(p, client);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasVerified(CustomPlayer p) throws SQLException, ClassNotFoundException {
|
|
|
|
return getTsId(p) != 0;
|
|
|
|
}
|
2015-12-06 11:04:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets group id for rank
|
|
|
|
* @param r Ranks
|
|
|
|
* @return Integer
|
|
|
|
*/
|
|
|
|
public int getIdForRank(Ranks r) {
|
|
|
|
switch(r) {
|
2016-02-27 09:08:17 +01:00
|
|
|
case Admin: return 10;
|
|
|
|
case Developer: return 13;
|
|
|
|
case SrModerator: return 60;
|
|
|
|
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 Player: return 8;
|
|
|
|
default: return 0;
|
2015-12-06 11:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|