funkemunky

Untitled

Jul 23rd, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. package net.raauhh.uhc.manager;
  2.  
  3. import lombok.Data;
  4. import lombok.Getter;
  5. import lombok.Setter;
  6. import net.raauhh.uhc.UHC;
  7. import net.raauhh.uhc.manager.assignation.Assignation;
  8. import net.raauhh.uhc.manager.logger.Logger;
  9. import net.raauhh.uhc.menu.submenu.cosmetics.ArrowTrail;
  10. import net.raauhh.uhc.menu.submenu.cosmetics.RodTrail;
  11. import net.raauhh.uhc.util.Cooldown;
  12. import net.raauhh.uhc.util.Skin;
  13. import org.bukkit.Bukkit;
  14. import org.bukkit.Location;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.inventory.ItemStack;
  17.  
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.text.DecimalFormat;
  23. import java.util.*;
  24.  
  25. import static org.bukkit.GameMode.SURVIVAL;
  26.  
  27. @Getter
  28. @Setter
  29. public class UHCPlayer {
  30.  
  31. @Getter
  32. private static Map<UUID, UHCPlayer> players = new HashMap<>();
  33. @Getter
  34. private static Map<String, UHCPlayer> playersNames = new HashMap<>();
  35.  
  36. private Skin skin;
  37. private String name;
  38. private UUID uuid;
  39.  
  40. private UHCTeam uhcTeam;
  41. private Logger logger;
  42.  
  43. private State state;
  44.  
  45. private ArrowTrail arrowTrail;
  46. private RodTrail rodTrail;
  47.  
  48. private int coins;
  49.  
  50. private ItemStack[] armor;
  51. private ItemStack[] inventory;
  52. private Location deadLocation;
  53. private boolean respawned;
  54. private boolean lateScattered;
  55.  
  56. private boolean givenGoneFishing;
  57.  
  58. private Location scatterLocation;
  59.  
  60. private Cooldown helpopCooldown = new Cooldown(0);
  61. private Cooldown noCleanCooldown = new Cooldown(0);
  62. private Cooldown lastAssignation = new Cooldown(0);
  63. private Cooldown deadCooldown = new Cooldown(0);
  64. private Cooldown savingCooldown = new Cooldown(0);
  65.  
  66. private Statistics statistics = new Statistics();
  67. private Assignation assignation;
  68.  
  69. private boolean freezed;
  70. private boolean hasPlayed;
  71.  
  72. private boolean seeSpectators = false;
  73. private boolean goldAlerts = false;
  74. private boolean diamondAlerts = true;
  75. private boolean pvpAlerts = false;
  76. private boolean pveAlerts = false;
  77.  
  78. private boolean inPractice = false;
  79. private boolean teamChat = false;
  80.  
  81. private Stat whitelists = new Stat();
  82. private List<String> whitelistedPlayers = new ArrayList<>();
  83.  
  84. private Stat kills = new Stat(), diamond = new Stat(), gold = new Stat();
  85.  
  86. public UHCPlayer(String name, UUID uuid) {
  87. this.name = name;
  88. this.uuid = uuid;
  89. this.state = State.WAITING;
  90.  
  91. players.put(uuid, this);
  92. playersNames.put(name, this);
  93. }
  94.  
  95. public Player getPlayer() {
  96. return Bukkit.getPlayer(this.uuid);
  97. }
  98.  
  99. public static UHCPlayer getByName(String name) {
  100. return playersNames.get(name);
  101. }
  102.  
  103. public static UHCPlayer getByUuid(UUID uuid) {
  104. return players.get(uuid);
  105. }
  106.  
  107. public String getKDR() {
  108. double kills = this.statistics.getKills().getAmount();
  109. double deaths = this.statistics.getDeaths().getAmount();
  110. double kdr = deaths == 0 ? kills : kills / deaths;
  111. return new DecimalFormat("#.##").format(kdr).replace(",", ".");
  112. }
  113.  
  114. public boolean isOnline() {
  115. return Bukkit.getPlayer(this.uuid) != null;
  116. }
  117.  
  118. public boolean isAlive() {
  119. return this.state == State.PLAYING;
  120. }
  121.  
  122. public boolean isWaiting() {
  123. return this.state == State.WAITING;
  124. }
  125.  
  126. public boolean isSpectating() {
  127. return this.state == State.SPECTATING;
  128. }
  129.  
  130. public boolean isAssigned() {
  131. return this.assignation != null;
  132. }
  133.  
  134. public void setState(State state) {
  135. this.state = state;
  136.  
  137. if(!this.isOnline()) return;
  138.  
  139. Player player = this.getPlayer();
  140. if (state != State.SPECTATING && player.getGameMode() != SURVIVAL) player.setGameMode(SURVIVAL);
  141.  
  142. if (state == State.PLAYING) { // Fix some weird occurrences with Superheroes
  143. player.setMaxHealth(20.0D);
  144. }
  145. }
  146.  
  147. public enum State {
  148. WAITING, PLAYING, SPECTATING, DEAD
  149. }
  150.  
  151. @Getter
  152. @Setter
  153. public class Statistics {
  154. private boolean loaded;
  155.  
  156. private Stat kills = new Stat(), deaths = new Stat(), wins = new Stat(), kdr = new Stat();
  157. private Stat goldenHeadsEaten = new Stat(), goldenApplesEaten = new Stat();
  158. private Stat coalMined = new Stat(), ironMined = new Stat(), goldMined = new Stat(), lapisMined = new Stat(), redstoneMined = new Stat(), diamondMined = new Stat(), emeraldMined = new Stat();
  159.  
  160. public void load() {
  161. Connection connection = UHC.getInstance().getDb().getConnection();
  162. PreparedStatement statement = null;
  163. ResultSet resultSet;
  164.  
  165. try {
  166. statement = connection.prepareStatement("SELECT * FROM `" + UHC.getInstance().getDb().getTable() + "` WHERE `uuid` = ? LIMIT 1;");
  167. statement.setString(1, getUuid().toString());
  168. statement.executeQuery();
  169.  
  170. resultSet = statement.getResultSet();
  171.  
  172. if (resultSet.next()) {
  173. this.wins.setAmount(resultSet.getInt("wins"));
  174.  
  175. this.kills.setAmount(resultSet.getInt("kills"));
  176. this.deaths.setAmount(resultSet.getInt("deaths"));
  177.  
  178. this.goldenApplesEaten.setAmount(resultSet.getInt("goldenapples-eaten"));
  179. this.goldenHeadsEaten.setAmount(resultSet.getInt("goldenheads-eaten"));
  180.  
  181. this.coalMined.setAmount(resultSet.getInt("coal-mined"));
  182. this.ironMined.setAmount(resultSet.getInt("iron-mined"));
  183. this.goldMined.setAmount(resultSet.getInt("gold-mined"));
  184. this.lapisMined.setAmount(resultSet.getInt("lapis-mined"));
  185. this.redstoneMined.setAmount(resultSet.getInt("redstone-mined"));
  186. this.diamondMined.setAmount(resultSet.getInt("diamond-mined"));
  187. this.emeraldMined.setAmount(resultSet.getInt("emerald-mined"));
  188.  
  189. if (resultSet.getString("arrow-trail") == null) {
  190. arrowTrail = ArrowTrail.valueOf("NONE");
  191. } else {
  192. arrowTrail = ArrowTrail.valueOf(resultSet.getString("arrow-trail").toUpperCase());
  193. }
  194.  
  195. if (resultSet.getString("rod-trail") == null) {
  196. rodTrail = RodTrail.valueOf("NONE");
  197. } else {
  198. rodTrail = RodTrail.valueOf(resultSet.getString("rod-trail").toUpperCase());
  199. }
  200.  
  201. } else {
  202. statement = connection.prepareStatement("INSERT INTO `" + UHC.getInstance().getDb().getTable() + "` (uuid, `name`) VALUES (?, ?);");
  203. statement.setString(1, uuid.toString());
  204. statement.setString(2, name);
  205. statement.executeUpdate();
  206. statement.close();
  207. }
  208.  
  209. this.loaded = true;
  210. UHC.getInstance().getLogger().info(getName() + "'s data was successfully loaded.");
  211. } catch (SQLException ex) {
  212. this.loaded = false;
  213. ex.printStackTrace();
  214. } finally {
  215. try {
  216. if (statement != null) statement.close();
  217. } catch (SQLException e) {
  218. e.printStackTrace();
  219. }
  220. }
  221. }
  222.  
  223. public void save() {
  224. try {
  225. Connection connection = UHC.getInstance().getDb().getConnection();
  226. PreparedStatement statement = connection.prepareStatement("UPDATE " + UHC.getInstance().getDb().getTable() + " SET" +
  227. " `name` = ?" +
  228. ", `wins` = " + wins.getAmount() +
  229. ", `kills` = " + kills.getAmount() +
  230. ", `deaths` = " + deaths.getAmount() +
  231. ", `goldenapples-eaten` = " + goldenApplesEaten.getAmount() +
  232. ", `goldenheads-eaten` = " + goldenHeadsEaten.getAmount() +
  233. ", `coal-mined` = " + coalMined.getAmount() +
  234. ", `iron-mined` = " + ironMined.getAmount() +
  235. ", `gold-mined` = " + goldMined.getAmount() +
  236. ", `lapis-mined` = " + lapisMined.getAmount() +
  237. ", `redstone-mined` = " + redstoneMined.getAmount() +
  238. ", `diamond-mined` = " + diamondMined.getAmount() +
  239. ", `emerald-mined` = " + emeraldMined.getAmount() +
  240. " WHERE `uuid` = ?;");
  241.  
  242. statement.setString(1, name);
  243. statement.setString(2, uuid.toString());
  244. statement.executeUpdate();
  245. statement.close();
  246. } catch (SQLException ex) {
  247. ex.printStackTrace();
  248. }
  249. }
  250. }
  251.  
  252. @Data
  253. public class Stat {
  254.  
  255. @Setter
  256. private int amount;
  257.  
  258. public void increment() {
  259. this.amount++;
  260. }
  261.  
  262. public void decrement() {
  263. this.amount--;
  264. }
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment