Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.hugo4715.fachub.storage;
- import static org.bukkit.ChatColor.GOLD;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Optional;
- import org.apache.commons.lang.Validate;
- import org.bukkit.ChatColor;
- import org.bukkit.block.Block;
- import org.bukkit.configuration.serialization.ConfigurationSerializable;
- import fr.hugo4715.fachub.FacHubConfig;
- import fr.hugo4715.fachub.FacHubPlugin;
- import fr.hugo4715.fachub.util.Util;
- public class FacInfo implements ConfigurationSerializable,Comparable<FacInfo> {
- private String name;
- private Block hub;
- private int points;
- private int invest;
- // 0 if the hub is spawned, currentTimeMillis if destroyed
- private long lastHubBreak;
- private long lastPointLoss;
- private long lastHubMove;
- public FacInfo(String name) {
- this.name = name;
- }
- public FacInfo(String name, Map<String, Object> map) {
- this.name = name;
- this.points = (Integer) map.getOrDefault("points", "0");
- this.invest = (Integer) map.getOrDefault("invest", "0");
- this.hub = map.containsKey("hub") ? Util.fromString((String) map.get("hub")) : null;
- this.lastHubBreak = (long) map.getOrDefault("last-hub-break", 0L);
- this.lastPointLoss = (long) map.getOrDefault("last-point-loss", System.currentTimeMillis());
- this.lastHubMove = (long) map.getOrDefault("last-hub-move", 0L);
- }
- public long getLastHubMove() {
- return lastHubMove;
- }
- public void setLastHubMove(long lastHubMove) {
- this.lastHubMove = lastHubMove;
- }
- public long getLastPointLoss() {
- return lastPointLoss;
- }
- public void setLastPointLoss(long lastPointLoss) {
- this.lastPointLoss = lastPointLoss;
- }
- public int getLevel() {
- FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
- int p = this.points;
- int lvl = 0;
- while (p > 0) {
- p -= config.getStartingPoints() * Math.pow(config.getPointsMultiplier(), lvl);
- if (p >= 0) {
- lvl++;
- }
- }
- return lvl;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- Validate.notNull(name);
- this.name = name;
- }
- public void setHub(Block hub) {
- this.hub = hub;
- }
- public Optional<Block> getHub() {
- return Optional.ofNullable(hub);
- }
- public int getPoints() {
- return points;
- }
- public void addPoints(int amount) {
- if (amount < -points) throw new IllegalArgumentException();
- this.points += amount;
- }
- public void setLastHubBreak(long lastHubBreak) {
- this.lastHubBreak = lastHubBreak;
- }
- public long getLastHubBreak() {
- return lastHubBreak;
- }
- public int getPointsUntilNextLevel() {
- FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
- int lvl = this.getLevel();
- int pointsForNextLevel = (int) Math.round((config.getStartingPoints() * ((1 - Math.pow(config.getPointsMultiplier(), lvl + 1)) / (1 - config.getPointsMultiplier()))));
- return getPointsForCurrentLevel() - (pointsForNextLevel - this.points);
- }
- public int getPointsForCurrentLevel() {
- return getPointsForLevel(getLevel());
- }
- private int getPointsForLevel(int level) {
- FacHubConfig config = FacHubPlugin.get().getFacHubConfig();
- return (int) Math.round(config.getStartingPoints() * Math.pow(config.getPointsMultiplier(), level));
- }
- public Map<String, Object> serialize() {
- return new HashMap<String, Object>() {{
- put("points", points);
- put("invest", invest);
- if(lastPointLoss != 0){
- put("last-point-loss", lastPointLoss);
- }
- if (lastHubBreak != 0) {
- put("last-hub-break", lastHubBreak);
- }
- if (hub != null) {
- put("hub", Util.toString(hub));
- }
- if(lastHubMove != 0){
- put("last-hub-move", lastHubMove);
- }
- }};
- }
- public void setPoints(int points) {
- Validate.isTrue(points >= 0);
- this.points = points;
- }
- public void setInvest(int invest) {
- Validate.isTrue(points >= 0);
- this.invest = invest;
- }
- public void removeInvest(int toRemove) {
- Validate.isTrue(toRemove <= this.invest);
- this.invest -= toRemove;
- }
- public int getInvest() {
- return invest;
- }
- public void removePoints(int toRemove) {
- Validate.isTrue(toRemove <= this.points);
- this.points -= toRemove;
- }
- public String getLevelAndPoints() {
- return ChatColor.GREEN.toString() + getLevel() + GOLD + " [" + getPointsUntilNextLevel() + "/" + getPointsForCurrentLevel() + "]";
- }
- @Override
- public int compareTo(FacInfo other) {
- if(other == null)return 1;
- return Integer.compare(this.points, other.points);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement