Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.core2.user;
- import com.google.common.io.ByteArrayDataOutput;
- import com.google.common.io.ByteStreams;
- import java.sql.PreparedStatement;
- import java.util.Calendar;
- import java.util.UUID;
- import de.core2.communication.PluginMessageCallbackBungee;
- import de.core2.database.Database;
- import de.core2.manager.BukkitCaller;
- import de.core2.manager.ObjectUtils;
- import de.core2.manager.PacketManager;
- import net.md_5.bungee.api.ProxyServer;
- import net.md_5.bungee.api.connection.ProxiedPlayer;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- public class CraftUser {
- public static boolean BUKKIT_LOADED = false;
- private UUID uuid;
- private String name;
- private Integer craftcoins;
- public CraftUser(UUID uuid, String name, Integer coins, Rank rank, Long lastvote) {
- this.uuid = uuid;
- this.name = name;
- this.craftcoins = coins;
- this.rank = rank;
- this.lastvote = lastvote;
- }
- private Rank rank;
- private Long lastvote;
- public CraftUser create() {
- if (this.uuid == null || this.name == null) {
- return this;
- }
- this.craftcoins = Integer.valueOf(100);
- this.rank = Rank.SPIELER;
- this.lastvote = Long.valueOf(0L);
- Database.update("INSERT INTO CraftUsers (uuid, name, craftcoins, rank, lastvote) VALUES ('" + this.uuid + "','" + this.name + "','" + this.craftcoins + "','" + this.rank + "','" + this.lastvote + "') ON DUPLICATE KEY UPDATE uuid=" + this.uuid);
- return new CraftUser(this.uuid, this.name, this.craftcoins, this.rank, this.lastvote);
- }
- private void update(String key, String value) { Database.update("UPDATE CraftUsers SET " + key + "='" + value + "' WHERE uuid='" + this.uuid + "'"); }
- public String getName() { return this.name; }
- public int getCoins() { return this.craftcoins.intValue(); }
- public UUID getUUID() { return this.uuid; }
- public Rank getRank() { return this.rank; }
- public long getLastvote() { return this.lastvote.longValue(); }
- public boolean hasVoted() {
- if (this.lastvote == null || this.lastvote.longValue() == 0L) {
- return false;
- }
- Calendar voteCalendar = Calendar.getInstance();
- voteCalendar.setTimeInMillis(this.lastvote.longValue());
- int voteDay = voteCalendar.get(5);
- int voteMonth = voteCalendar.get(2);
- int voteYear = voteCalendar.get(1);
- Calendar calendar = Calendar.getInstance();
- int day = calendar.get(5);
- int month = calendar.get(2);
- int year = calendar.get(1);
- if (day == voteDay && month == voteMonth && year == voteYear) {
- return true;
- }
- return false;
- }
- public String getFullname() {
- if (this.rank == Rank.SPIELER) {
- return Rank.SPIELER.getColorcode() + this.name;
- }
- return this.rank.getPrefix() + this.name;
- }
- public Player getPlayer() { return Bukkit.getPlayer(this.uuid); }
- public ProxiedPlayer getProxiedPlayer() { return ProxyServer.getInstance().getPlayer(this.uuid); }
- public String getColorname() { return this.rank.getColorcode() + this.name; }
- public void getPlayerValue(String key, PlayerValueCallback callback) {
- Database.getResult("SELECT * FROM PlayerValues WHERE uuid='" + this.uuid + "' AND playerkey='" + key + "'", resultSet -> {
- if (resultSet.next()) {
- Object object = resultSet.getObject("playervalue");
- String objectString = object.toString();
- Object finalObject = object;
- if (ObjectUtils.isNumeric(objectString)) {
- if (objectString.contains(".")) {
- Float f = Float.valueOf(objectString);
- finalObject = f;
- if (f.floatValue() < Double.MAX_VALUE) {
- Double d = Double.valueOf(f.floatValue());
- finalObject = d;
- }
- } else {
- Long l = Long.valueOf(objectString);
- finalObject = l;
- if (l.longValue() < 2147483647L) {
- Integer integer = Integer.valueOf(objectString);
- finalObject = integer;
- }
- }
- } else if (objectString.equalsIgnoreCase("true") || objectString.equalsIgnoreCase("false")) {
- Boolean b = Boolean.valueOf(objectString.toLowerCase());
- finalObject = b;
- }
- callback.valueLoaded(finalObject);
- } else {
- callback.valueLoaded(null);
- }
- });
- }
- public void setName(String name) {
- this.name = name;
- update("name", name);
- }
- public void setRank(Rank rank) {
- this.rank = rank;
- update("rank", rank.toString());
- if (BUKKIT_LOADED) {
- BukkitCaller.callRankChangeEvent(getPlayer(), rank);
- } else {
- PluginMessageCallbackBungee.sendToSpigot(
- String.format("{ \"type\": \"RankChange\", \"uuid\": \"%s\" }", new Object[] { this.uuid }));
- if (getProxiedPlayer() == null) {
- return;
- }
- }
- }
- public void setPlayerValue(String key, Object object) {
- Database.getResult("SELECT * FROM PlayerValues WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'", resultSet -> {
- if (resultSet.next()) {
- Database.update("UPDATE PlayerValues SET playervalue='" + object.toString() + "' WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'");
- } else {
- Database.update("INSERT INTO PlayerValues (uuid, playerkey, playervalue) VALUES ('" + this.uuid + "','" + key + "','" + object
- .toString() + "')");
- }
- });
- }
- public static void createPlayerValue(){
- try{
- PreparedStatement ps = Database.getStatement("CREATE TABLE IF NOT EXISTS PlayerValues (uuid VARCHAR(100), playerkey VARCHAR(100), playervalue VARCHAR(100))");
- ps.executeUpdate();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- public void removePlayerValue(String key) { Database.update("DELETE FROM PlayerValues WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'"); }
- public void addCoins(int coins) {
- boolean added = true;
- if (coins < 0) {
- added = false;
- if (this.craftcoins.intValue() + coins < 0) {
- throw new IndexOutOfBoundsException("CraftUser can only have a positive number of coins");
- }
- }
- CraftUser craftUser = this; craftUser.craftcoins = Integer.valueOf(craftUser.craftcoins.intValue() + coins);
- update("craftcoins", String.valueOf(this.craftcoins));
- if (BUKKIT_LOADED) {
- PacketManager.callCoinEvent(getPlayer(), this.craftcoins, Integer.valueOf(coins), added);
- }
- }
- public void removeCoins(int coins) { addCoins(-coins); }
- public void updateLastvote() {
- this.lastvote = Long.valueOf(System.currentTimeMillis());
- update("lastvote", String.valueOf(this.lastvote));
- }
- public void sendToServer(String server) {
- Player player = Bukkit.getPlayer(this.uuid);
- if (player == null) {
- return;
- }
- ByteArrayDataOutput out = ByteStreams.newDataOutput();
- out.writeUTF("Connect");
- out.writeUTF(server);
- BukkitCaller.sendPluginMessage(player, "BungeeCord", out.toByteArray());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement