Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. package fr.hugo4715.fachub.storage;
  2.  
  3. import static org.bukkit.ChatColor.GOLD;
  4.  
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Optional;
  8.  
  9. import org.apache.commons.lang.Validate;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.configuration.serialization.ConfigurationSerializable;
  13.  
  14. import fr.hugo4715.fachub.FacHubConfig;
  15. import fr.hugo4715.fachub.FacHubPlugin;
  16. import fr.hugo4715.fachub.util.Util;
  17.  
  18.  
  19. public class FacInfo implements ConfigurationSerializable,Comparable<FacInfo> {
  20.  
  21. private String name;
  22. private Block hub;
  23. private int points;
  24. private int invest;
  25.  
  26. // 0 if the hub is spawned, currentTimeMillis if destroyed
  27. private long lastHubBreak;
  28.  
  29. private long lastPointLoss;
  30.  
  31. private long lastHubMove;
  32.  
  33. public FacInfo(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public FacInfo(String name, Map<String, Object> map) {
  38. this.name = name;
  39. this.points = (Integer) map.getOrDefault("points", "0");
  40. this.invest = (Integer) map.getOrDefault("invest", "0");
  41. this.hub = map.containsKey("hub") ? Util.fromString((String) map.get("hub")) : null;
  42. this.lastHubBreak = (long) map.getOrDefault("last-hub-break", 0L);
  43. this.lastPointLoss = (long) map.getOrDefault("last-point-loss", System.currentTimeMillis());
  44. this.lastHubMove = (long) map.getOrDefault("last-hub-move", 0L);
  45. }
  46.  
  47. public long getLastHubMove() {
  48. return lastHubMove;
  49. }
  50.  
  51. public void setLastHubMove(long lastHubMove) {
  52. this.lastHubMove = lastHubMove;
  53. }
  54.  
  55. public long getLastPointLoss() {
  56. return lastPointLoss;
  57. }
  58.  
  59. public void setLastPointLoss(long lastPointLoss) {
  60. this.lastPointLoss = lastPointLoss;
  61. }
  62.  
  63. public int getLevel() {
  64. FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
  65.  
  66. int p = this.points;
  67. int lvl = 0;
  68. while (p > 0) {
  69. p -= config.getStartingPoints() * Math.pow(config.getPointsMultiplier(), lvl);
  70.  
  71. if (p >= 0) {
  72. lvl++;
  73. }
  74. }
  75. return lvl;
  76. }
  77.  
  78.  
  79. public String getName() {
  80. return name;
  81. }
  82.  
  83. public void setName(String name) {
  84. Validate.notNull(name);
  85. this.name = name;
  86. }
  87.  
  88. public void setHub(Block hub) {
  89. this.hub = hub;
  90. }
  91.  
  92. public Optional<Block> getHub() {
  93. return Optional.ofNullable(hub);
  94. }
  95.  
  96. public int getPoints() {
  97. return points;
  98. }
  99.  
  100. public void addPoints(int amount) {
  101. if (amount < -points) throw new IllegalArgumentException();
  102. this.points += amount;
  103. }
  104.  
  105. public void setLastHubBreak(long lastHubBreak) {
  106. this.lastHubBreak = lastHubBreak;
  107. }
  108.  
  109. public long getLastHubBreak() {
  110. return lastHubBreak;
  111. }
  112.  
  113. public int getPointsUntilNextLevel() {
  114. FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
  115. int lvl = this.getLevel();
  116. int pointsForNextLevel = (int) Math.round((config.getStartingPoints() * ((1 - Math.pow(config.getPointsMultiplier(), lvl + 1)) / (1 - config.getPointsMultiplier()))));
  117. return getPointsForCurrentLevel() - (pointsForNextLevel - this.points);
  118. }
  119.  
  120. public int getPointsForCurrentLevel() {
  121. return getPointsForLevel(getLevel());
  122. }
  123.  
  124. private int getPointsForLevel(int level) {
  125. FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
  126. return (int) Math.round(config.getStartingPoints() * Math.pow(config.getPointsMultiplier(), level));
  127. }
  128.  
  129. public Map<String, Object> serialize() {
  130. return new HashMap<String, Object>() {{
  131. put("points", points);
  132. put("invest", invest);
  133. if(lastPointLoss != 0){
  134. put("last-point-loss", lastPointLoss);
  135. }
  136. if (lastHubBreak != 0) {
  137. put("last-hub-break", lastHubBreak);
  138. }
  139. if (hub != null) {
  140. put("hub", Util.toString(hub));
  141. }
  142.  
  143. if(lastHubMove != 0){
  144. put("last-hub-move", lastHubMove);
  145. }
  146. }};
  147. }
  148.  
  149. public void setPoints(int points) {
  150. Validate.isTrue(points >= 0);
  151. this.points = points;
  152. }
  153. public void setInvest(int invest) {
  154. Validate.isTrue(points >= 0);
  155. this.invest = invest;
  156. }
  157.  
  158. public void removeInvest(int toRemove) {
  159. Validate.isTrue(toRemove <= this.invest);
  160. this.invest -= toRemove;
  161. }
  162. public int getInvest() {
  163. return invest;
  164. }
  165. public void removePoints(int toRemove) {
  166. Validate.isTrue(toRemove <= this.points);
  167. this.points -= toRemove;
  168. }
  169.  
  170. public String getLevelAndPoints() {
  171. return ChatColor.GREEN.toString() + getLevel() + GOLD + " [" + getPointsUntilNextLevel() + "/" + getPointsForCurrentLevel() + "]";
  172. }
  173.  
  174. @Override
  175. public int compareTo(FacInfo other) {
  176. if(other == null)return 1;
  177. return Integer.compare(this.points, other.points);
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement