Core/src/de/joethei/core/api/CustomPlayer.java

295 lines
5.6 KiB
Java

package de.joethei.core.api;
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 de.joethei.core.api.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(this);
}
}
/**
* suffix of player
* @return String
*/
public String getSuffix() {
if(customSuffix != null) {
return customSuffix;
}
return Perms.getSuffix(this);
}
/**
* color of player (Ranks)
* @return String
*/
public String getColor() {
if(customColor != null) {
return customColor;
}
return Perms.getColor(this);
}
/**
* 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;
}
/**
* 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();
}
}
}