Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. package de.core2.user;
  2.  
  3. import com.google.common.io.ByteArrayDataOutput;
  4. import com.google.common.io.ByteStreams;
  5.  
  6. import java.sql.PreparedStatement;
  7. import java.util.Calendar;
  8. import java.util.UUID;
  9.  
  10. import de.core2.communication.PluginMessageCallbackBungee;
  11. import de.core2.database.Database;
  12. import de.core2.manager.BukkitCaller;
  13. import de.core2.manager.ObjectUtils;
  14. import de.core2.manager.PacketManager;
  15. import net.md_5.bungee.api.ProxyServer;
  16. import net.md_5.bungee.api.connection.ProxiedPlayer;
  17. import org.bukkit.Bukkit;
  18. import org.bukkit.entity.Player;
  19.  
  20. public class CraftUser {
  21.  
  22. public static boolean BUKKIT_LOADED = false;
  23. private UUID uuid;
  24. private String name;
  25. private Integer craftcoins;
  26.  
  27. public CraftUser(UUID uuid, String name, Integer coins, Rank rank, Long lastvote) {
  28. this.uuid = uuid;
  29. this.name = name;
  30. this.craftcoins = coins;
  31. this.rank = rank;
  32. this.lastvote = lastvote;
  33. }
  34. private Rank rank;
  35. private Long lastvote;
  36. public CraftUser create() {
  37. if (this.uuid == null || this.name == null) {
  38. return this;
  39. }
  40. this.craftcoins = Integer.valueOf(100);
  41. this.rank = Rank.SPIELER;
  42. this.lastvote = Long.valueOf(0L);
  43.  
  44. 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);
  45.  
  46.  
  47. return new CraftUser(this.uuid, this.name, this.craftcoins, this.rank, this.lastvote);
  48. }
  49.  
  50.  
  51. private void update(String key, String value) { Database.update("UPDATE CraftUsers SET " + key + "='" + value + "' WHERE uuid='" + this.uuid + "'"); }
  52.  
  53.  
  54.  
  55. public String getName() { return this.name; }
  56.  
  57.  
  58.  
  59. public int getCoins() { return this.craftcoins.intValue(); }
  60.  
  61. public UUID getUUID() { return this.uuid; }
  62.  
  63. public Rank getRank() { return this.rank; }
  64.  
  65. public long getLastvote() { return this.lastvote.longValue(); }
  66.  
  67. public boolean hasVoted() {
  68. if (this.lastvote == null || this.lastvote.longValue() == 0L) {
  69. return false;
  70. }
  71.  
  72. Calendar voteCalendar = Calendar.getInstance();
  73. voteCalendar.setTimeInMillis(this.lastvote.longValue());
  74. int voteDay = voteCalendar.get(5);
  75. int voteMonth = voteCalendar.get(2);
  76. int voteYear = voteCalendar.get(1);
  77.  
  78.  
  79. Calendar calendar = Calendar.getInstance();
  80. int day = calendar.get(5);
  81. int month = calendar.get(2);
  82. int year = calendar.get(1);
  83.  
  84. if (day == voteDay && month == voteMonth && year == voteYear) {
  85. return true;
  86. }
  87. return false;
  88. }
  89.  
  90.  
  91. public String getFullname() {
  92. if (this.rank == Rank.SPIELER) {
  93. return Rank.SPIELER.getColorcode() + this.name;
  94. }
  95. return this.rank.getPrefix() + this.name;
  96. }
  97.  
  98.  
  99. public Player getPlayer() { return Bukkit.getPlayer(this.uuid); }
  100.  
  101.  
  102.  
  103. public ProxiedPlayer getProxiedPlayer() { return ProxyServer.getInstance().getPlayer(this.uuid); }
  104.  
  105.  
  106.  
  107. public String getColorname() { return this.rank.getColorcode() + this.name; }
  108.  
  109.  
  110. public void getPlayerValue(String key, PlayerValueCallback callback) {
  111. Database.getResult("SELECT * FROM PlayerValues WHERE uuid='" + this.uuid + "' AND playerkey='" + key + "'", resultSet -> {
  112. if (resultSet.next()) {
  113. Object object = resultSet.getObject("playervalue");
  114. String objectString = object.toString();
  115. Object finalObject = object;
  116.  
  117.  
  118. if (ObjectUtils.isNumeric(objectString)) {
  119. if (objectString.contains(".")) {
  120. Float f = Float.valueOf(objectString);
  121. finalObject = f;
  122. if (f.floatValue() < Double.MAX_VALUE) {
  123. Double d = Double.valueOf(f.floatValue());
  124. finalObject = d;
  125. }
  126. } else {
  127. Long l = Long.valueOf(objectString);
  128. finalObject = l;
  129. if (l.longValue() < 2147483647L) {
  130. Integer integer = Integer.valueOf(objectString);
  131. finalObject = integer;
  132. }
  133.  
  134. }
  135. } else if (objectString.equalsIgnoreCase("true") || objectString.equalsIgnoreCase("false")) {
  136. Boolean b = Boolean.valueOf(objectString.toLowerCase());
  137. finalObject = b;
  138. }
  139.  
  140.  
  141. callback.valueLoaded(finalObject);
  142. } else {
  143. callback.valueLoaded(null);
  144. }
  145. });
  146. }
  147.  
  148. public void setName(String name) {
  149. this.name = name;
  150. update("name", name);
  151. }
  152.  
  153. public void setRank(Rank rank) {
  154. this.rank = rank;
  155. update("rank", rank.toString());
  156.  
  157. if (BUKKIT_LOADED) {
  158. BukkitCaller.callRankChangeEvent(getPlayer(), rank);
  159. } else {
  160. PluginMessageCallbackBungee.sendToSpigot(
  161. String.format("{ \"type\": \"RankChange\", \"uuid\": \"%s\" }", new Object[] { this.uuid }));
  162.  
  163. if (getProxiedPlayer() == null) {
  164. return;
  165. }
  166.  
  167. }
  168. }
  169.  
  170.  
  171. public void setPlayerValue(String key, Object object) {
  172. Database.getResult("SELECT * FROM PlayerValues WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'", resultSet -> {
  173. if (resultSet.next()) {
  174. Database.update("UPDATE PlayerValues SET playervalue='" + object.toString() + "' WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'");
  175. } else {
  176.  
  177. Database.update("INSERT INTO PlayerValues (uuid, playerkey, playervalue) VALUES ('" + this.uuid + "','" + key + "','" + object
  178. .toString() + "')");
  179. }
  180. });
  181. }
  182.  
  183. public static void createPlayerValue(){
  184. try{
  185. PreparedStatement ps = Database.getStatement("CREATE TABLE IF NOT EXISTS PlayerValues (uuid VARCHAR(100), playerkey VARCHAR(100), playervalue VARCHAR(100))");
  186. ps.executeUpdate();
  187. }catch(Exception ex){
  188. ex.printStackTrace();
  189. }
  190. }
  191.  
  192.  
  193.  
  194. public void removePlayerValue(String key) { Database.update("DELETE FROM PlayerValues WHERE playerkey='" + key + "' AND uuid='" + this.uuid + "'"); }
  195.  
  196.  
  197. public void addCoins(int coins) {
  198. boolean added = true;
  199. if (coins < 0) {
  200. added = false;
  201. if (this.craftcoins.intValue() + coins < 0) {
  202. throw new IndexOutOfBoundsException("CraftUser can only have a positive number of coins");
  203. }
  204. }
  205.  
  206. CraftUser craftUser = this; craftUser.craftcoins = Integer.valueOf(craftUser.craftcoins.intValue() + coins);
  207. update("craftcoins", String.valueOf(this.craftcoins));
  208. if (BUKKIT_LOADED) {
  209. PacketManager.callCoinEvent(getPlayer(), this.craftcoins, Integer.valueOf(coins), added);
  210. }
  211. }
  212.  
  213.  
  214.  
  215. public void removeCoins(int coins) { addCoins(-coins); }
  216.  
  217.  
  218. public void updateLastvote() {
  219. this.lastvote = Long.valueOf(System.currentTimeMillis());
  220. update("lastvote", String.valueOf(this.lastvote));
  221. }
  222.  
  223. public void sendToServer(String server) {
  224. Player player = Bukkit.getPlayer(this.uuid);
  225. if (player == null) {
  226. return;
  227. }
  228.  
  229. ByteArrayDataOutput out = ByteStreams.newDataOutput();
  230. out.writeUTF("Connect");
  231. out.writeUTF(server);
  232.  
  233. BukkitCaller.sendPluginMessage(player, "BungeeCord", out.toByteArray());
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement