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

832 lines
21 KiB
Java
Raw Normal View History

2016-04-13 05:52:53 +02:00
package eu.univento.core.api.player;
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.effects.Effects;
import eu.univento.core.api.languages.Messages;
import eu.univento.core.api.server.Game;
import eu.univento.core.api.server.ServerSettings;
import eu.univento.core.api.server.Servers;
import eu.univento.core.api.utils.NameFetcher;
import net.minecraft.server.v1_9_R1.EnumParticle;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
import org.bukkit.craftbukkit.v1_9_R1.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;
//private MongoDB mongoDB = Core.getMongoDB();
//private MongoCollection userCollection = mongoDB.getDatabase().getCollection("users");
/**
* 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() {
/**
Date date = new Date();
Document doc = new Document("uuid", getUniqueId());
userCollection.updateOne(doc, new Document("$set", new Document("lastOnline", date)));
HashMap<String, Object> location = new HashMap<>();
location.put("X", getLocation().getX());
location.put("Y", getLocation().getY());
location.put("Z", getLocation().getZ());
location.put("Yaw", getLocation().getYaw());
location.put("Pitch", getLocation().getPitch());
userCollection.updateOne(doc, new Document("$set", new Document("Pos", new Document("Loc", location))));
*/
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;
}
}
/**
* 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;
}
}
/**
* get normal player
*
* @return Player
*/
public Player getPLAYER() {
return PLAYER;
}
/**
public void insertToDatabase() {
DBObject obj = new BasicDBObject("uuid", getUniqueId());
Date date = new Date();
obj.put("rank", "Player");
obj.put("firstLogin", date);
obj.put("lastLogin", date);
obj.put("lastOnline", date);
obj.put("lastIP", spigot().getRawAddress().getHostName());
obj.put("tsid", 0);
obj.put("timesJoined", 1);
obj.put("timePlayed", 0);
obj.put("coins", 0);
obj.put("experience", 0);
obj.put("secrets", 0);
obj.put("foundSecrets", new ArrayList<String>());
obj.put("foundEggs", new ArrayList<String>());
HashMap<String, Object> settings = new HashMap<>();
settings.put("playerVisibility", "all");
settings.put("inventoryAnimation", true);
settings.put("teleportAnimation", true);
settings.put("partyRequests", true);
settings.put("friendRequests", true);
settings.put("friendJump", true);
settings.put("chatSounds", true);
settings.put("effects", true);
settings.put("storyMode", true);
settings.put("language", "EN");
obj.put("Settings", settings);
HashMap<String, Object> location = new HashMap<>();
location.put("X", getLocation().getX());
location.put("Y", getLocation().getY());
location.put("Z", getLocation().getZ());
location.put("Yaw", getLocation().getYaw());
location.put("Pitch", getLocation().getPitch());
obj.put("Pos", location);
userCollection.insertOne(obj);
}
public void updateDatabaseEntry() {
Date date = new Date();
Document doc = new Document("uuid", getUniqueId());
userCollection.updateOne(doc, new Document("$set", new Document("lastLogin", date)));
userCollection.updateOne(doc, new Document("$set", new Document("lastIP", spigot().getRawAddress().getHostName().toString())));
userCollection.updateOne(doc, new Document("$set", new Document("timesPlayed", getTimesJoined() + 1)));
}
public void setRank(Perms.Ranks rank) {
userCollection.updateOne(new Document("uuid", getUniqueId()), new Document("$set", new Document("rank", rank.toString())));
}
public void setTSID(int id) {
userCollection.updateOne(new Document("uuid", getUniqueId()), new Document("$set", new Document("tsid", id)));
}
public void setCoins(int coins) {
userCollection.updateOne(new Document("uuid", getUniqueId()), new Document("$set", new Document("coins", coins)));
}
public void setExperience(int experience) {
userCollection.updateOne(new Document("uuid", getUniqueId()), new Document("$set", new Document("experience", experience)));
}
public void setSecrets(int secrets) {
userCollection.updateOne(new Document("uuid", getUniqueId()), new Document("$set", new Document("secrets", secrets)));
}
private Object getObjectFromDatbase(String name) {
FindIterable<Document> cursor = userCollection.find(new Document("uuid", getUniqueId()));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) {
return null;
}
return doc.get(name);
}
private int getIntegerFromDatabase(String name) {
FindIterable<Document> cursor = userCollection.find(new Document("uuid", getUniqueId()));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) {
return 0;
}
return doc.getInteger(name);
}
private String getStringFromDatabase(String name) {
FindIterable<Document> cursor = userCollection.find(new Document("uuid", getUniqueId()));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) {
return null;
}
return doc.getString(name);
}
private Date getDateFromDatabase(String name) {
FindIterable<Document> cursor = userCollection.find(new Document("uuid", getUniqueId()));
cursor.cursorType(CursorType.NonTailable);
Document doc = cursor.first();
if (doc == null) {
return null;
}
return doc.getDate(name);
}
public Perms.Ranks getRank() {
return Perms.Ranks.valueOf(getStringFromDatabase("rank"));
}
public Date getFirstLogin() {
return getDateFromDatabase("firstLogin");
}
public Date getLastLogin() {
return getDateFromDatabase("lastLogin");
}
public Date getLastOnline() {
return getDateFromDatabase("lastOnline");
}
public String getLastIP() {
return getStringFromDatabase("lastIP");
}
public int getTSID() {
return getIntegerFromDatabase("tsid");
}
public int getTimesJoined() {
return getIntegerFromDatabase("timesJoined");
}
public int getCoins() {
return getIntegerFromDatabase("coins");
}
public int getExperience() {
return getIntegerFromDatabase("experience");
}
public int getSecrets() {
return getIntegerFromDatabase("secrets");
}
public ArrayList<String> getFoundSecrets() {
return (ArrayList<String>) getObjectFromDatbase("foundSecrets");
}
public ArrayList<String> getFoundEggs() {
return (ArrayList<String>) getObjectFromDatbase("foundEggs");
}
public HashMap<String, Object> getSettings() {
return (HashMap<String, Object>) getObjectFromDatbase("settings");
}
*/
/**
* 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();
}
}
public void nick(String nick) {
NickName.setName(this, nick);
NickName.changeSkin(this, nick);
}
public void unnick() {
NickName.changeSkin(this, getName());
NickName.setName(this, getName());
}
/**
* 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);
}
/**
* 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.ENTITY_PLAYER_LEVELUP, 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());
}
public int getFoundSecrets() {
return 0;
}
public String getLanguage() {
return "DE";
}
public void setLanguage(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);
}
public void playParticle(Location loc, EnumParticle ep, float f, int count) {
if(isSetting("effects"))
Effects.playEffectToPlayer(PLAYER, loc, ep, f, count);
}
public void setAttackSpeed(double speed) {
AttributeModifier modifier = new AttributeModifier("Core", speed, AttributeModifier.Operation.ADD_NUMBER);
getAttribute(Attribute.GENERIC_ATTACK_SPEED).addModifier(modifier);
}
public double getAttackSpeed() {
return getAttribute(Attribute.GENERIC_ATTACK_SPEED).getValue();
}
public void resetAttackSpeed() {
AttributeModifier modifier = new AttributeModifier("Core", getAttackSpeed(), AttributeModifier.Operation.ADD_NUMBER);
getAttribute(Attribute.GENERIC_ATTACK_SPEED).removeModifier(modifier);
}
public Messages getMessages() {
return new Messages(this);
}
public Statistics getStatistics(Game game) {
return new Statistics(this, game);
}
}