641 lines
14 KiB
Java
641 lines
14 KiB
Java
package eu.univento.core.api.player;
|
|
|
|
import com.github.theholywaffle.teamspeak3.api.wrapper.ClientInfo;
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.Actionbar;
|
|
import eu.univento.core.api.Utils;
|
|
import eu.univento.core.api.database.MySQL;
|
|
import eu.univento.core.api.server.ServerSettings;
|
|
import eu.univento.core.api.server.Servers;
|
|
import eu.univento.core.api.utils.NameFetcher;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Color;
|
|
import org.bukkit.Sound;
|
|
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 java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Timestamp;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* player id from database
|
|
*/
|
|
private int id = 0;
|
|
|
|
/**
|
|
* if player has open inventory
|
|
*/
|
|
private boolean openInventory;
|
|
|
|
/**
|
|
* inits player
|
|
* @param player Player
|
|
*/
|
|
private CustomPlayer(Player player) {
|
|
super((CraftServer) Bukkit.getServer(), ((CraftPlayer) player).getHandle());
|
|
PLAYERS.put(player.getName().toLowerCase(), this);
|
|
PLAYER = player;
|
|
id = getID();
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gets custom player from player
|
|
* @param player Player
|
|
* @return CustomPlayer
|
|
*/
|
|
public static CustomPlayer getPlayer(Player player) {
|
|
return getPlayer(player.getName());
|
|
}
|
|
|
|
/**
|
|
* gets custom player from database id
|
|
* @param id database id of player
|
|
* @return CustomPlayer
|
|
*/
|
|
public static CustomPlayer getPlayer(int id) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT ID, UUID FROM users WHERE ID='" + id + "');");
|
|
ResultSet rs = st.executeQuery();
|
|
String uuidString = rs.getString("UUID");
|
|
sql.closeConnection();
|
|
UUID uuid = UUID.fromString(uuidString);
|
|
NameFetcher names = new NameFetcher(Arrays.asList(uuid));
|
|
Map<UUID, String> map = null;
|
|
try {
|
|
map = names.call();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
String name = map.toString();
|
|
String Name = name.substring(name.indexOf('=') + 1, name.indexOf('}'));
|
|
|
|
return CustomPlayer.getPlayer(Name);
|
|
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get normal player
|
|
* @return Player
|
|
*/
|
|
public Player getPLAYER() {
|
|
return PLAYER;
|
|
}
|
|
|
|
/**
|
|
* connects player to server in bungeecord
|
|
* @param server ServerPinger 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(Perms.Ranks rank) {
|
|
try {
|
|
return Perms.isAllowed(this, rank);
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* rank of player
|
|
* @return Ranks
|
|
*/
|
|
public Perms.Ranks getRank() {
|
|
try {
|
|
return Perms.getRank(this);
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return Perms.Ranks.Player;
|
|
}
|
|
}
|
|
|
|
public Perms.Ranks getFreshRank() {
|
|
try{
|
|
return Perms.getRankFresh(this);
|
|
}catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return Perms.Ranks.Player;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sets rank of this custom player
|
|
* @param r Ranks
|
|
*/
|
|
public void setRank(Perms.Ranks r) {
|
|
try {
|
|
Perms.setRank(this, r);
|
|
} catch (ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* prefix of player
|
|
* @return String
|
|
*/
|
|
public String getPrefix() {
|
|
if(customPrefix != null) return customPrefix;
|
|
if(isNicked()) return Perms.getPrefix(Perms.Ranks.Premium);
|
|
return Perms.getPrefix(getRank());
|
|
}
|
|
|
|
/**
|
|
* suffix of player
|
|
* @return String
|
|
*/
|
|
public String getSuffix() {
|
|
if(customSuffix != null) return customSuffix;
|
|
if(isNicked()) return Perms.getSuffix(Perms.Ranks.Premium);
|
|
return Perms.getSuffix(getRank());
|
|
}
|
|
|
|
/**
|
|
* color of player (Ranks)
|
|
* @return String
|
|
*/
|
|
public String getColor() {
|
|
if(customColor != null) return customColor;
|
|
if(isNicked()) return Perms.getColor(Perms.Ranks.Premium);
|
|
return Perms.getColor(getRank());
|
|
}
|
|
|
|
public Color getArmorColor() {
|
|
return Perms.getArmorColor(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() {
|
|
Perms.Teams teams = new Perms.Teams(this);
|
|
if(isNicked()) {
|
|
return teams.Premium;
|
|
}else {
|
|
return Perms.Teams.getTeam(this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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(this, reason, who, time);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
/**
|
|
* if player is banned
|
|
* @return true/false
|
|
*/
|
|
public boolean isBanned() {
|
|
try {
|
|
return BanSystem.isBanned(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get ban reason
|
|
* @return String
|
|
*/
|
|
public String getBanReason() {
|
|
try {
|
|
return BanSystem.getBanReason(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* get uuid of player who banned
|
|
* @return String
|
|
*/
|
|
public String getWhoBanned() {
|
|
try {
|
|
return BanSystem.getWhoBanned(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Timestamp getBanTime() {
|
|
try {
|
|
return BanSystem.getBanTime(this);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* unbans the player
|
|
*/
|
|
public void removeAllBans() {
|
|
try {
|
|
BanSystem.removeAllBans(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get nickname of player
|
|
* @return String
|
|
*/
|
|
public String getNick() {
|
|
return NickName.getNick(this);
|
|
}
|
|
|
|
/**
|
|
* if player is nicked
|
|
* @return true/false
|
|
*/
|
|
public boolean isNicked() {
|
|
if(ServerSettings.isGame()) {
|
|
try {
|
|
return NickName.isNicked(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* checks if player is nicked
|
|
* @return true/false
|
|
*/
|
|
public boolean isNickedReal() {
|
|
try {
|
|
return NickName.isNicked(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* sets nickname
|
|
* @param is boolean
|
|
*/
|
|
public void setNicked(boolean is) {
|
|
try {
|
|
NickName.setNick(this, is);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* gets Client Info from TeamSpeak
|
|
* @return ClientInfo
|
|
*/
|
|
public ClientInfo getTSClientInfo() {
|
|
TeamSpeak ts = Core.getTs();
|
|
try {
|
|
return ts.getClientInfo(ts.getTsId(this));
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* verifys player account on teamspeak
|
|
*/
|
|
public void verifyTs() {
|
|
try {
|
|
Core.getTs().verify(this);
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void checkVerifiedTs() {
|
|
try {
|
|
Core.getTs().checkVerifiyed(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public boolean hasVerifiedTs() {
|
|
try {
|
|
return Core.getTs().hasVerified(this);
|
|
} catch (SQLException | ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* checks if player has played on the network before
|
|
* @return true/false
|
|
*/
|
|
@Override
|
|
public boolean hasPlayedBefore() {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
String uuid = this.getUniqueId().toString();
|
|
ResultSet rs = sql.getConnection().createStatement().executeQuery("SELECT * FROM users WHERE UUID= '" + uuid + "';");
|
|
if (rs.next()) {
|
|
sql.closeConnection();
|
|
return true;
|
|
}else {
|
|
sql.closeConnection();
|
|
return false;
|
|
}
|
|
|
|
} 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);
|
|
}
|
|
|
|
/**
|
|
* gets database id of player
|
|
* @return Integer
|
|
*/
|
|
public int getID() {
|
|
if(id == 0) {
|
|
MySQL sql = Core.returnSQL();
|
|
try {
|
|
sql.openConnection();
|
|
String uuid = this.getUniqueId().toString();
|
|
PreparedStatement st = sql.getConnection().prepareStatement("SELECT * FROM users WHERE UUID ='" + uuid + "';");
|
|
ResultSet rs = st.executeQuery();
|
|
if(rs.next()) {
|
|
int id = rs.getInt("ID");
|
|
this.id = id;
|
|
sql.closeConnection();
|
|
return id;
|
|
}else {
|
|
sql.closeConnection();
|
|
|
|
return 0;
|
|
}
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
e.printStackTrace();
|
|
return 0;
|
|
}
|
|
}else {
|
|
return id;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* checks if players has a open inventory
|
|
* @return true/false
|
|
*/
|
|
public boolean hasOpenInventory() {
|
|
return openInventory;
|
|
}
|
|
|
|
/**
|
|
* sets opened inventory
|
|
* @param openInventory true/false
|
|
*/
|
|
public void setOpenInventory(boolean openInventory) {
|
|
this.openInventory = openInventory;
|
|
}
|
|
|
|
/**
|
|
* gets coins from player
|
|
* @return coins as integer
|
|
*/
|
|
public int getCoins() {
|
|
return Coins.getCoins(this);
|
|
}
|
|
|
|
/**
|
|
* sets coins if player
|
|
* @param coins coins as integer
|
|
*/
|
|
public void setCoins(int coins) {
|
|
Coins.setCoins(this, coins);
|
|
}
|
|
|
|
/**
|
|
* adds coins to player coins
|
|
* @param coins coins as integer
|
|
*/
|
|
public void addCoins(int coins) {
|
|
int temp = Coins.getCoins(this);
|
|
Coins.setCoins(this, temp + coins);
|
|
}
|
|
|
|
/**
|
|
* substracts coins from player coins
|
|
* @param coins coins as integer
|
|
*/
|
|
public void substractCoins(int coins) {
|
|
int temp = Coins.getCoins(this);
|
|
Coins.setCoins(this, temp - coins);
|
|
}
|
|
|
|
/**
|
|
* sends message with actionbar to player
|
|
* @param text String
|
|
*/
|
|
public void sendActionBar(String text) {
|
|
Actionbar.send(PLAYER, text);
|
|
}
|
|
|
|
/**
|
|
* gets experience from player
|
|
* @return experience experience as integer
|
|
*/
|
|
public int getExperience() {
|
|
return Experience.getExperience(this);
|
|
}
|
|
|
|
/**
|
|
* sets experience of player
|
|
* @param experience experience to set
|
|
*/
|
|
public void setExperience(int experience) {
|
|
Experience.setExperience(this, experience);
|
|
}
|
|
|
|
/**
|
|
* adds experience to player
|
|
* @param experience experience to add
|
|
*/
|
|
public void addExperience(int experience) {
|
|
int temp = Experience.getExperience(this);
|
|
Experience.setExperience(this, temp + experience);
|
|
setExp(0F);
|
|
giveExp(getExperience());
|
|
playSound(getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);
|
|
}
|
|
|
|
/**
|
|
* removes experience from player
|
|
* @param experience experience to remove
|
|
*/
|
|
public void substractExperience(int experience) {
|
|
int temp = Experience.getExperience(this);
|
|
Experience.setExperience(this, temp - experience);
|
|
setExp(0F);
|
|
giveExp(getExperience());
|
|
}
|
|
|
|
public void refreshExperience() {
|
|
setLevel(0);
|
|
setExp(0F);
|
|
giveExp(getExperience());
|
|
}
|
|
//TODO: add other functions for secrets
|
|
|
|
public int getFoundSecrets() {
|
|
return 0;
|
|
}
|
|
|
|
public String getLang() {
|
|
return PlayerSettings.get("lang", this);
|
|
}
|
|
|
|
public void setLang(String lang) {
|
|
PlayerSettings.set(lang, "lang", this);
|
|
};
|
|
|
|
public boolean isSetting(String setting) {
|
|
return PlayerSettings.isSet(setting, this);
|
|
}
|
|
|
|
public String getSetting(String setting) { return PlayerSettings.get(setting, this); }
|
|
|
|
public void changeSetting(String setting) {
|
|
PlayerSettings.change(setting, this);
|
|
}
|
|
} |