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

376 lines
7.5 KiB
Java

package eu.univento.core.api.player;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
import com.github.theholywaffle.teamspeak3.api.wrapper.ClientInfo;
import eu.univento.core.Core;
import eu.univento.core.api.MySQL;
import eu.univento.core.api.Servers;
import eu.univento.core.api.Utils;
import eu.univento.core.api.player.Perms.Ranks;
/**
* custom player implementation
* @author joethei
* @version 1.0
*/
public class CustomPlayer extends CraftPlayer {
/**
* all normal players
*/
private static final HashMap<String, CustomPlayer> PLAYERS = new HashMap<>();
/**
* normal player
*/
private final Player PLAYER;
/**
* custom prefix for chat
*/
private String customPrefix;
/**
* custom suffix for chat
*/
private String customSuffix;
/**
* custom color for chat
*/
private String customColor;
/**
* inits player
* @param player Player
*/
private CustomPlayer(Player player) {
super((CraftServer) Bukkit.getServer(), ((CraftPlayer) player).getHandle());
PLAYERS.put(player.getName().toLowerCase(), this);
PLAYER = player;
}
/**
* called on player leaving
*/
public void onLeave() {
if (PLAYERS.containsKey(getName().toLowerCase())) {
PLAYERS.remove(getName().toLowerCase());
}
}
/**
* init player
* @param player Player
* @return CustomPlayer
*/
public static CustomPlayer getPlayer(String player) {
if (PLAYERS.containsKey(player.toLowerCase())) {
return PLAYERS.get(player.toLowerCase());
} else {
Player p = Bukkit.getPlayer(player);
return p == null ? null : new CustomPlayer(p);
}
}
/**
* get normal player
* @return Player
*/
public Player getPLAYER() {
return PLAYER;
}
/**
* connects player to server in bungeecord
* @param server Server to connect to
*/
public void connectToServer(String server) {
Servers.connectServer(PLAYER, server);
}
/**
* if player is allowed to do stuff
* @param rank Ranks
* @return true/false
*/
public boolean isAllowed(Ranks rank) {
try {
return Perms.isAllowed(this, rank);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
/**
* gets fresh rank of player
* @return Ranks
*/
public Ranks getRankFresh() {
try {
return Perms.getRankFresh(this);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* rank of player
* @return Ranks
*/
public Ranks getRank() {
return Perms.getRank(this);
}
/**
* prefix of player
* @return String
*/
public String getPrefix() {
if(customPrefix != null) {
return customPrefix;
}else {
return Perms.getPrefix(getRank());
}
}
/**
* suffix of player
* @return String
*/
public String getSuffix() {
if(customSuffix != null) {
return customSuffix;
}
return Perms.getSuffix(getRank());
}
/**
* color of player (Ranks)
* @return String
*/
public String getColor() {
if(customColor != null) {
return customColor;
}
return Perms.getColor(getRank());
}
/**
* sets a custom prefix
* @param prefix String
*/
public void setCustomPrefix(String prefix) {
customPrefix = prefix;
}
/**
* sets a custom suffix
* @param suffix String
*/
public void setCustomSuffix(String suffix) {
customSuffix = suffix;
}
/**
* sets a custom color
* @param color String
*/
public void setCustomColor(String color) {
customColor = color;
}
/**
* gets scoreboard team for player
* @return Team
*/
public Team getTeam() {
return Perms.getTeam(getRank());
}
/**
* if player is muted
* @return true/false
*/
public boolean isMuted() {
try {
return MuteSystem.isMuted(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* bans player
* @param reason why ban him ?
* @param who who banned him ?
* @param time ban duration
*/
public void ban(String reason, CustomPlayer who, Integer time) {
try {
BanSystem.setBanned(PLAYER, reason, who.getPLAYER(), time);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* if player is banned
* @return true/false
*/
public boolean isBanned() {
try {
return BanSystem.isBanned(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* get ban reason
* @return String
*/
public String getBanReason() {
try {
return BanSystem.getBanReason(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* get uuid of player who banned
* @return String
*/
public String getWhoBanned() {
try {
return BanSystem.getWhoBanned(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* unbans the player
*/
public void unban() {
try {
BanSystem.unban(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* get nickname of player
* @return String
*/
public String getNick() {
return NickName.getNick(PLAYER);
}
/**
* if player is nicked
* @return true/false
*/
public boolean isNicked() {
try {
return NickName.isNicked(PLAYER);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* sets nickname
* @param is boolean
*/
public void setNick(boolean is) {
try {
NickName.setNick(PLAYER, is);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* gets Client Info from TeamSpeak
* @return ClientInfo
*/
public ClientInfo getTSClientInfo() {
TeamSpeak ts = new TeamSpeak();
try {
return ts.getClientInfo(ts.getTsId(this));
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
/**
* verifys player account on teamspeak
*/
public void verifyTs() {
TeamSpeak ts = new TeamSpeak();
try {
ts.setRank(this);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* checks if player has played on the network before
* @return true/false
*/
@Override
public boolean hasPlayedBefore() {
MySQL sql = Core.returnSQL();
boolean bool;
try {
sql.openConnection();
String uuid = this.getUniqueId().toString();
ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM PlayerData WHERE player_uuid='" + uuid + "';");
if (rs.next())
bool = true;
bool = false;
sql.closeConnection();
return bool;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
/**
* checks if inventory is empty
* @return true/false
*/
public boolean hasEmptyInventory() {
return Utils.hasEmptyInventory(PLAYER);
}
/**
* clears all potion effects from player
*/
public void clearPotionEffects() {
Utils.clearPotionEffects(PLAYER);
}
}