diff --git a/src/main/java/world/bentobox/upgrades/UpgradesManager.java b/src/main/java/world/bentobox/upgrades/UpgradesManager.java index ecceede..881e1f8 100644 --- a/src/main/java/world/bentobox/upgrades/UpgradesManager.java +++ b/src/main/java/world/bentobox/upgrades/UpgradesManager.java @@ -4,12 +4,12 @@ import java.util.Collections; import java.util.Comparator; import java.util.EnumMap; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.TreeMap; import java.util.stream.Collectors; import org.bukkit.Material; @@ -27,74 +27,79 @@ public UpgradesManager(UpgradesAddon addon) { this.addon = addon; this.hookedGameModes = new HashSet<>(); } - + protected void addGameModes(List gameModes) { this.hookedGameModes.addAll(gameModes); } - + public boolean canOperateInWorld(World world) { Optional addon = this.addon.getPlugin().getIWM().getAddon(world); - + return addon.isPresent() && this.hookedGameModes.contains(addon.get().getDescription().getName()); } - + public int getIslandLevel(Island island) { if (!this.addon.isLevelProvided()) return 0; - + if (island == null) { this.addon.logError("Island couldn't be found"); return 0; } - + int islandLevel = (int) this.addon.getLevelAddon().getIslandLevel(island.getWorld(), island.getOwner()); - + if (islandLevel < 0) { this.addon.logWarning("Island " + island.getUniqueId() + " has an invalid level: " + islandLevel); islandLevel = 0; } - + return islandLevel; } - + public List getAllRangeUpgradeTiers(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); - if (name == null) return Collections.emptyList(); - + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); + if (name == null) + return Collections.emptyList(); + Map defaultTiers = this.addon.getSettings().getDefaultRangeUpgradeTierMap(); Map customAddonTiers = this.addon.getSettings().getAddonRangeUpgradeTierMap(name); - + List tierList; - + if (customAddonTiers.isEmpty()) tierList = new ArrayList<>(defaultTiers.values()); else { Set uniqueIDSet = new HashSet<>(customAddonTiers.keySet()); uniqueIDSet.addAll(defaultTiers.keySet()); tierList = new ArrayList<>(uniqueIDSet.size()); - + uniqueIDSet.forEach(id -> tierList.add(customAddonTiers.getOrDefault(id, defaultTiers.get(id)))); } - + if (tierList.isEmpty()) return Collections.emptyList(); - + tierList.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel)); - + return tierList; } - + public Map> getAllBlockLimitsUpgradeTiers(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); if (name == null) { return Collections.emptyMap(); } - - Map> defaultTiers = this.addon.getSettings().getDefaultBlockLimitsUpgradeTierMap(); - Map> customAddonTiers = this.addon.getSettings().getAddonBlockLimitsUpgradeTierMap(name); - + + Map> defaultTiers = this.addon.getSettings() + .getDefaultBlockLimitsUpgradeTierMap(); + Map> customAddonTiers = this.addon.getSettings() + .getAddonBlockLimitsUpgradeTierMap(name); + Map> tierList = new EnumMap<>(Material.class); - + if (customAddonTiers.isEmpty()) { defaultTiers.forEach((mat, tiers) -> tierList.put(mat, new ArrayList<>(tiers.values()))); } else { @@ -103,34 +108,37 @@ public Map> getAllBlockLimitsUpgradeTiers(W if (defaultTiers.containsKey(mat)) uniqueIDSet.addAll(defaultTiers.get(mat).keySet()); List matTier = new ArrayList<>(uniqueIDSet.size()); - + uniqueIDSet.forEach(id -> matTier.add(tiers.getOrDefault(id, defaultTiers.get(mat).get(id)))); tierList.put(mat, matTier); }); - + defaultTiers.forEach((mat, tiers) -> tierList.putIfAbsent(mat, new ArrayList<>(tiers.values()))); } - + if (tierList.isEmpty()) { return Collections.emptyMap(); } - + tierList.forEach((mat, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel))); - + return tierList; } - + public Map> getAllEntityLimitsUpgradeTiers(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); if (name == null) { return Collections.emptyMap(); } - - Map> defaultTiers = this.addon.getSettings().getDefaultEntityLimitsUpgradeTierMap(); - Map> customAddonTiers = this.addon.getSettings().getAddonEntityLimitsUpgradeTierMap(name); - + + Map> defaultTiers = this.addon.getSettings() + .getDefaultEntityLimitsUpgradeTierMap(); + Map> customAddonTiers = this.addon.getSettings() + .getAddonEntityLimitsUpgradeTierMap(name); + Map> tierList = new EnumMap<>(EntityType.class); - + if (customAddonTiers.isEmpty()) { defaultTiers.forEach((ent, tiers) -> tierList.put(ent, new ArrayList<>(tiers.values()))); } else { @@ -139,34 +147,37 @@ public Map> getAllEntityLimitsUpgradeTier if (defaultTiers.containsKey(ent)) uniqueIDSet.addAll(defaultTiers.get(ent).keySet()); List entTier = new ArrayList<>(uniqueIDSet.size()); - + uniqueIDSet.forEach(id -> entTier.add(tiers.getOrDefault(id, defaultTiers.get(ent).get(id)))); tierList.put(ent, entTier); }); - + defaultTiers.forEach((ent, tiers) -> tierList.putIfAbsent(ent, new ArrayList<>(tiers.values()))); } - + if (tierList.isEmpty()) { return Collections.emptyMap(); } - + tierList.forEach((ent, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel))); - + return tierList; } - + public Map> getAllEntityGroupLimitsUpgradeTiers(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); if (name == null) { return Collections.emptyMap(); } - - Map> defaultTiers = this.addon.getSettings().getDefaultEntityGroupLimitsUpgradeTierMap(); - Map> customAddonTiers = this.addon.getSettings().getAddonEntityGroupLimitsUpgradeTierMap(name); - - Map> tierList = new HashMap<>(); - + + Map> defaultTiers = this.addon.getSettings() + .getDefaultEntityGroupLimitsUpgradeTierMap(); + Map> customAddonTiers = this.addon.getSettings() + .getAddonEntityGroupLimitsUpgradeTierMap(name); + + Map> tierList = new TreeMap<>(); + if (customAddonTiers.isEmpty()) { defaultTiers.forEach((ent, tiers) -> tierList.put(ent, new ArrayList<>(tiers.values()))); } else { @@ -175,34 +186,37 @@ public Map> getAllEntityGroupLimitsUpgradeTie if (defaultTiers.containsKey(ent)) uniqueIDSet.addAll(defaultTiers.get(ent).keySet()); List entTier = new ArrayList<>(uniqueIDSet.size()); - + uniqueIDSet.forEach(id -> entTier.add(tiers.getOrDefault(id, defaultTiers.get(ent).get(id)))); tierList.put(ent, entTier); }); - + defaultTiers.forEach((ent, tiers) -> tierList.putIfAbsent(ent, new ArrayList<>(tiers.values()))); } - + if (tierList.isEmpty()) { return Collections.emptyMap(); } - + tierList.forEach((ent, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel))); - + return tierList; } - + public Map> getAllCommandUpgradeTiers(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); if (name == null) { return Collections.emptyMap(); } - - Map> defaultTiers = this.addon.getSettings().getDefaultCommandUpgradeTierMap(); - Map> customAddonTiers = this.addon.getSettings().getAddonCommandUpgradeTierMap(name); - - Map> tierList = new HashMap<>(); - + + Map> defaultTiers = this.addon.getSettings() + .getDefaultCommandUpgradeTierMap(); + Map> customAddonTiers = this.addon.getSettings() + .getAddonCommandUpgradeTierMap(name); + + Map> tierList = new TreeMap<>(); + if (customAddonTiers.isEmpty()) { defaultTiers.forEach((cmd, tiers) -> tierList.put(cmd, new ArrayList<>(tiers.values()))); } else { @@ -211,345 +225,360 @@ public Map> getAllCommandUpgradeTiers( if (defaultTiers.containsKey(cmd)) uniqueIDSet.addAll(defaultTiers.get(cmd).keySet()); List cmdTier = new ArrayList<>(uniqueIDSet.size()); - + uniqueIDSet.forEach(id -> cmdTier.add(tiers.getOrDefault(id, defaultTiers.get(cmd).get(id)))); tierList.put(cmd, cmdTier); }); - + defaultTiers.forEach((cmd, tiers) -> tierList.putIfAbsent(cmd, new ArrayList<>(tiers.values()))); } - + if (tierList.isEmpty()) { return Collections.emptyMap(); } - + tierList.forEach((cmd, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel))); - + return tierList; } - + public Settings.UpgradeTier getRangeUpgradeTier(int rangeLevel, World world) { List tierList = this.getAllRangeUpgradeTiers(world); - + if (tierList.isEmpty()) return null; - + Settings.UpgradeTier rangeUpgradeTier = tierList.get(0); - + if (rangeUpgradeTier.getMaxLevel() < 0) return rangeUpgradeTier; - + for (int i = 0; i < tierList.size(); i++) { if (rangeLevel <= tierList.get(i).getMaxLevel()) return tierList.get(i); } - + return null; } - + public Settings.UpgradeTier getBlockLimitsUpgradeTier(Material mat, int limitsLevel, World world) { Map> matTierList = this.getAllBlockLimitsUpgradeTiers(world); - + if (matTierList.isEmpty()) { return null; } - + if (!matTierList.containsKey(mat)) { return null; } - + List tierList = matTierList.get(mat); - + for (int i = 0; i < tierList.size(); i++) { if (limitsLevel <= tierList.get(i).getMaxLevel()) return tierList.get(i); } return null; } - + public Settings.UpgradeTier getEntityLimitsUpgradeTier(EntityType ent, int limitsLevel, World world) { Map> entTierList = this.getAllEntityLimitsUpgradeTiers(world); - + if (entTierList.isEmpty()) { return null; } - + if (!entTierList.containsKey(ent)) { return null; } - + List tierList = entTierList.get(ent); - + for (int i = 0; i < tierList.size(); i++) { if (limitsLevel <= tierList.get(i).getMaxLevel()) return tierList.get(i); } - + return null; } - + public Settings.UpgradeTier getEntityGroupLimitsUpgradeTier(String group, int limitsLevel, World world) { Map> entTierList = this.getAllEntityGroupLimitsUpgradeTiers(world); - + if (entTierList.isEmpty()) { return null; } - + if (!entTierList.containsKey(group)) { return null; } - + List tierList = entTierList.get(group); - + for (int i = 0; i < tierList.size(); i++) { if (limitsLevel <= tierList.get(i).getMaxLevel()) return tierList.get(i); } - + return null; } - + public Settings.CommandUpgradeTier getCommandUpgradeTier(String cmd, int cmdLevel, World world) { Map> cmdTierList = this.getAllCommandUpgradeTiers(world); - + if (cmdTierList.isEmpty()) { return null; } - + if (!cmdTierList.containsKey(cmd)) { return null; } - + List tierList = cmdTierList.get(cmd); - + for (int i = 0; i < tierList.size(); i++) { if (cmdLevel <= tierList.get(i).getMaxLevel()) return tierList.get(i); } - + return null; } - + public Map getRangeUpgradeInfos(int rangeLevel, int islandLevel, int numberPeople, World world) { Settings.UpgradeTier rangeUpgradeTier = this.getRangeUpgradeTier(rangeLevel, world); - + if (rangeUpgradeTier == null) return null; - - Map info = new HashMap<>(); - - info.put("islandMinLevel", (int) rangeUpgradeTier.calculateIslandMinLevel(rangeLevel, islandLevel, numberPeople)); + + Map info = new TreeMap<>(); + + info.put("islandMinLevel", + (int) rangeUpgradeTier.calculateIslandMinLevel(rangeLevel, islandLevel, numberPeople)); info.put("vaultCost", (int) rangeUpgradeTier.calculateVaultCost(rangeLevel, islandLevel, numberPeople)); info.put("upgrade", (int) rangeUpgradeTier.calculateUpgrade(rangeLevel, islandLevel, numberPeople)); - + return info; } - + public int getRangePermissionLevel(int rangeLevel, World world) { Settings.UpgradeTier rangeUpgradeTier = this.getRangeUpgradeTier(rangeLevel, world); - + if (rangeUpgradeTier == null) return 0; return rangeUpgradeTier.getPermissionLevel(); } - + public String getRangeUpgradeTierName(int rangeLevel, World world) { Settings.UpgradeTier rangeUpgradeTier = this.getRangeUpgradeTier(rangeLevel, world); - + if (rangeUpgradeTier == null) return null; return rangeUpgradeTier.getTierName(); } - + public int getRangeUpgradeMax(World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); return this.addon.getSettings().getMaxRangeUpgrade(name); } - - public Map getBlockLimitsUpgradeInfos(Material mat, int limitsLevel, int islandLevel, int numberPeople, World world) { + + public Map getBlockLimitsUpgradeInfos(Material mat, int limitsLevel, int islandLevel, + int numberPeople, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getBlockLimitsUpgradeTier(mat, limitsLevel, world); if (limitsUpgradeTier == null) { return null; } - - Map info = new HashMap<>(); - - info.put("islandMinLevel", (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); + + Map info = new TreeMap<>(); + + info.put("islandMinLevel", + (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); info.put("vaultCost", (int) limitsUpgradeTier.calculateVaultCost(limitsLevel, islandLevel, numberPeople)); info.put("upgrade", (int) limitsUpgradeTier.calculateUpgrade(limitsLevel, islandLevel, numberPeople)); - + return info; } - + public int getBlockLimitsPermissionLevel(Material mat, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getBlockLimitsUpgradeTier(mat, limitsLevel, world); - + if (limitsUpgradeTier == null) return 0; return limitsUpgradeTier.getPermissionLevel(); } - + public String getBlockLimitsUpgradeTierName(Material mat, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getBlockLimitsUpgradeTier(mat, limitsLevel, world); - + if (limitsUpgradeTier == null) return null; return limitsUpgradeTier.getTierName(); } - + public int getBlockLimitsUpgradeMax(Material mat, World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); return this.addon.getSettings().getMaxBlockLimitsUpgrade(mat, name); } - - public Map getEntityLimitsUpgradeInfos(EntityType ent, int limitsLevel, int islandLevel, int numberPeople, World world) { + + public Map getEntityLimitsUpgradeInfos(EntityType ent, int limitsLevel, int islandLevel, + int numberPeople, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world); if (limitsUpgradeTier == null) { return null; } - - Map info = new HashMap<>(); - - info.put("islandMinLevel", (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); + + Map info = new TreeMap<>(); + + info.put("islandMinLevel", + (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); info.put("vaultCost", (int) limitsUpgradeTier.calculateVaultCost(limitsLevel, islandLevel, numberPeople)); info.put("upgrade", (int) limitsUpgradeTier.calculateUpgrade(limitsLevel, islandLevel, numberPeople)); - + return info; } - - public Map getEntityGroupLimitsUpgradeInfos(String group, int limitsLevel, int islandLevel, int numberPeople, World world) { + + public Map getEntityGroupLimitsUpgradeInfos(String group, int limitsLevel, int islandLevel, + int numberPeople, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world); if (limitsUpgradeTier == null) { return null; } - - Map info = new HashMap<>(); - - info.put("islandMinLevel", (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); + + Map info = new TreeMap<>(); + + info.put("islandMinLevel", + (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople)); info.put("vaultCost", (int) limitsUpgradeTier.calculateVaultCost(limitsLevel, islandLevel, numberPeople)); info.put("upgrade", (int) limitsUpgradeTier.calculateUpgrade(limitsLevel, islandLevel, numberPeople)); - + return info; } - + public int getEntityLimitsPermissionLevel(EntityType ent, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world); - + if (limitsUpgradeTier == null) return 0; return limitsUpgradeTier.getPermissionLevel(); } - + public int getEntityGroupLimitsPermissionLevel(String group, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world); - + if (limitsUpgradeTier == null) return 0; return limitsUpgradeTier.getPermissionLevel(); } - + public String getEntityLimitsUpgradeTierName(EntityType ent, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world); - + if (limitsUpgradeTier == null) return null; return limitsUpgradeTier.getTierName(); } - + public String getEntityGroupLimitsUpgradeTierName(String group, int limitsLevel, World world) { Settings.UpgradeTier limitsUpgradeTier = this.getEntityGroupLimitsUpgradeTier(group, limitsLevel, world); - + if (limitsUpgradeTier == null) return null; return limitsUpgradeTier.getTierName(); } - + public int getEntityLimitsUpgradeMax(EntityType ent, World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); return this.addon.getSettings().getMaxEntityLimitsUpgrade(ent, name); } - + public int getEntityGroupLimitsUpgradeMax(String group, World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); return this.addon.getSettings().getMaxEntityGroupLimitsUpgrade(group, name); } - - public Map getCommandUpgradeInfos(String cmd, int cmdLevel, int islandLevel, int numberPeople, World world) { + + public Map getCommandUpgradeInfos(String cmd, int cmdLevel, int islandLevel, int numberPeople, + World world) { Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world); if (cmdUpgradeTier == null) { return null; } - - Map info = new HashMap<>(); - + + Map info = new TreeMap<>(); + info.put("islandMinLevel", (int) cmdUpgradeTier.calculateIslandMinLevel(cmdLevel, islandLevel, numberPeople)); info.put("vaultCost", (int) cmdUpgradeTier.calculateVaultCost(cmdLevel, islandLevel, numberPeople)); info.put("upgrade", (int) cmdUpgradeTier.calculateUpgrade(cmdLevel, islandLevel, numberPeople)); - + return info; } - + public int getCommandPermissionLevel(String cmd, int cmdLevel, World world) { Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world); - + if (cmdUpgradeTier == null) return 0; return cmdUpgradeTier.getPermissionLevel(); } - + public String getCommandUpgradeTierName(String cmd, int cmdLevel, World world) { Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world); - + if (cmdUpgradeTier == null) return null; return cmdUpgradeTier.getTierName(); } - + public int getCommandUpgradeMax(String cmd, World world) { - String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null); + String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()) + .orElse(null); return this.addon.getSettings().getMaxCommandUpgrade(cmd, name); } - + public List getCommandList(String cmd, int cmdLevel, Island island, String playerName) { Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, island.getWorld()); - + if (cmdUpgradeTier == null) return Collections.emptyList(); return cmdUpgradeTier.getCommandList(playerName, island, cmdLevel); } - + public Boolean isCommantConsole(String cmd, int cmdLevel, World world) { Settings.CommandUpgradeTier cmdUpgradeTier = this.getCommandUpgradeTier(cmd, cmdLevel, world); - + if (cmdUpgradeTier == null) return false; return cmdUpgradeTier.getConsole(); } - + public Map getEntityLimits(Island island) { if (!this.addon.isLimitsProvided()) return Collections.emptyMap(); - - Map entityLimits = new HashMap<>(this.addon.getLimitsAddon().getSettings().getLimits()); + + Map entityLimits = new TreeMap<>(this.addon.getLimitsAddon().getSettings().getLimits()); IslandBlockCount ibc = this.addon.getLimitsAddon().getBlockLimitListener().getIsland(island.getUniqueId()); - if (ibc != null) ibc.getEntityLimits().forEach(entityLimits::put); + if (ibc != null) + ibc.getEntityLimits().forEach(entityLimits::put); return entityLimits; } - + public Map getEntityGroupLimits(Island island) { if (!this.addon.isLimitsProvided()) return Collections.emptyMap(); - - Map entityGroupLimits = new HashMap<>(this.addon.getLimitsAddon().getSettings().getGroupLimits().values() - .stream().flatMap(e -> e.stream()).distinct() - .collect(Collectors.toMap(e -> e.getName(), e -> e.getLimit()))); + + Map entityGroupLimits = new TreeMap<>( + this.addon.getLimitsAddon().getSettings().getGroupLimits().values().stream().flatMap(e -> e.stream()) + .distinct().collect(Collectors.toMap(e -> e.getName(), e -> e.getLimit()))); IslandBlockCount ibc = this.addon.getLimitsAddon().getBlockLimitListener().getIsland(island.getUniqueId()); - if (ibc != null) ibc.getEntityGroupLimits().forEach(entityGroupLimits::put); + if (ibc != null) + ibc.getEntityGroupLimits().forEach(entityGroupLimits::put); return entityGroupLimits; } - + private UpgradesAddon addon; - + private Set hookedGameModes; - + } diff --git a/src/main/java/world/bentobox/upgrades/api/Upgrade.java b/src/main/java/world/bentobox/upgrades/api/Upgrade.java index 3e6b4c1..08850c0 100644 --- a/src/main/java/world/bentobox/upgrades/api/Upgrade.java +++ b/src/main/java/world/bentobox/upgrades/api/Upgrade.java @@ -1,10 +1,9 @@ package world.bentobox.upgrades.api; - -import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.TreeMap; import java.util.UUID; import org.bukkit.Material; @@ -25,254 +24,259 @@ */ public abstract class Upgrade { - /** - * Initialize the upgrade object - * you should call it in your init methode - * - * @param addon This should be your addon - * @param name This is the name for the upgrade that will be used in the DataBase - * @param displayName This is the name that is shown to the user - * @param icon This is the icon shown to the user - */ - public Upgrade(Addon addon, String name, String displayName, Material icon) { - this.name = name; - this.displayName = displayName; - this.icon = icon; - this.addon = addon; - - this.playerCache = new HashMap<>(); - this.ownDescription = new HashMap<>(); - - Optional islandUpgrade = this.addon.getAddonByName("upgrades"); - if (!islandUpgrade.isPresent()) { - this.addon.logError("Island Upgrade Addon couldn't be found"); - this.addon.setState(State.DISABLED); - } else { - this.upgradesAddon = (UpgradesAddon) islandUpgrade.get(); - this.addon.log("Added upgrade -> " + name); - } - } - - /** - * This function is called every times a user open the interface - * You should make it update the upgradeValues - * - * @param user This is the user that ask for the interface - * @param island This is the island concerned by the interface - */ - public abstract void updateUpgradeValue(User user, Island island); - - /** - * This function is called every times a user open the interface - * If it return false, the upgrade won't be showed to the user - * - * @param user This is the user that ask for the interface - * @param island This is the island concerned by the interface - * @return If true, then upgrade is shown else, it is hided - */ - public boolean isShowed(User user, Island island) { - return true; - } - - /** - * This function return true if the user can upgrade for this island. - * You can override it and call the super. - * - * The super test for islandLevel and for money - * - * @param user This is the user that try to upgrade - * @param island This is the island that is concerned - * @return Can upgrade - */ - public boolean canUpgrade(User user, Island island) { - UpgradeValues upgradeValues = this.getUpgradeValues(user); - boolean can = true; - - if (this.upgradesAddon.isLevelProvided() && - this.upgradesAddon.getUpgradesManager().getIslandLevel(island) < upgradeValues.getIslandLevel()) { - - can = false; - } - - if (this.upgradesAddon.isVaultProvided() && - !this.upgradesAddon.getVaultHook().has(user, upgradeValues.getMoneyCost())) { - - can = false; - } - - return can; - } - - /** - * This function is called when the user is upgrading for the island - * It is called after the canUpgrade function - * - * You should call the super to update the balance of the user as well as the level is the island - * - * @param user This is the user that do the upgrade - * @param island This is the island that is concerned - * @return If upgrade was successful - */ - public boolean doUpgrade(User user, Island island) { - UpgradeValues upgradeValues = this.getUpgradeValues(user); - - if (this.upgradesAddon.isVaultProvided()) { - EconomyResponse response = this.upgradesAddon.getVaultHook().withdraw(user, upgradeValues.getMoneyCost()); - if (!response.transactionSuccess()) { - this.addon.logWarning("User Money withdrawing failed user: " + user.getName() + " reason: " + response.errorMessage); - user.sendMessage("upgrades.error.costwithdraw"); - return false; - } - } - - UpgradesData data = this.upgradesAddon.getUpgradesLevels(island.getUniqueId()); - data.setUpgradeLevel(this.name, data.getUpgradeLevel(this.name) + 1); - - return true; - } - - /** - * @return The name that is used for the DataBase - */ - public String getName() { - return this.name; - } - - /** - * @return The name that is displayed to the user - */ - public String getDisplayName() { - return this.displayName; - } - - /** - * @param displayName To update the name to display to the user - */ - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - /** - * @return The icon that is displayed to the user - */ - public Material getIcon() { - return this.icon; - } - - public int getUpgradeLevel(Island island) { - return this.upgradesAddon.getUpgradesLevels(island.getUniqueId()).getUpgradeLevel(this.name); - } - - /** - * @return The actual description for the user - */ - public String getOwnDescription(User user) { - return this.ownDescription.get(user.getUniqueId()); - } - - /** - * @param user User to set the description - * @param description Description to set - */ - public void setOwnDescription(User user, String description) { - this.ownDescription.put(user.getUniqueId(), description); - } - - /** - * @return The actual upgradeValues - */ - public UpgradeValues getUpgradeValues(User user) { - return this.playerCache.get(user.getUniqueId()); - } - - /** - * @param upgrade Values to upgrades - */ - public void setUpgradeValues(User user, UpgradeValues upgrade) { - this.playerCache.put(user.getUniqueId(), upgrade); - } - - /** - * Function that get the upgrades addon - * You should use it to use the upgrades addon methods - * - * @return UpgradesAddon - */ - public UpgradesAddon getUpgradesAddon() { - return this.upgradesAddon; - } - - /** - * You shouldn't override this function - */ - @Override - public int hashCode() { - return Objects.hash(name); - } - - /** - * You shouldn't override this function - */ - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (!(obj instanceof Upgrade)) { - return false; - } - Upgrade other = (Upgrade) obj; - return Objects.equals(name, other.name); - } - - private final String name; - private String displayName; - private Material icon; - private Addon addon; - private UpgradesAddon upgradesAddon; - private Map playerCache; - private Map ownDescription; - - public class UpgradeValues { - - public UpgradeValues(Integer islandLevel, Integer moneyCost, Integer upgradeValue) { - this.islandLevel = islandLevel; - this.moneyCost = moneyCost; - this.upgradeValue = upgradeValue; - } - - public int getIslandLevel() { - return islandLevel; - } - - public void setIslandLevel(int islandLevel) { - this.islandLevel = islandLevel; - } - - public int getMoneyCost() { - return moneyCost; - } - - public void setMoneyCost(int moneyCost) { - this.moneyCost = moneyCost; - } - - public int getUpgradeValue() { - return upgradeValue; - } - - public void setUpgradeValue(int upgradeValue) { - this.upgradeValue = upgradeValue; - } - - private int islandLevel; - private int moneyCost; - private int upgradeValue; - } - - + /** + * Initialize the upgrade object you should call it in your init methode + * + * @param addon This should be your addon + * @param name This is the name for the upgrade that will be used in the + * DataBase + * @param displayName This is the name that is shown to the user + * @param icon This is the icon shown to the user + */ + public Upgrade(Addon addon, String name, String displayName, Material icon) { + this.name = name; + this.displayName = displayName; + this.icon = icon; + this.addon = addon; + + this.playerCache = new TreeMap<>(); + this.ownDescription = new TreeMap<>(); + + Optional islandUpgrade = this.addon.getAddonByName("upgrades"); + if (!islandUpgrade.isPresent()) { + this.addon.logError("Island Upgrade Addon couldn't be found"); + this.addon.setState(State.DISABLED); + } else { + this.upgradesAddon = (UpgradesAddon) islandUpgrade.get(); + this.addon.log("Added upgrade -> " + name); + } + } + + /** + * This function is called every times a user open the interface You should make + * it update the upgradeValues + * + * @param user This is the user that ask for the interface + * @param island This is the island concerned by the interface + */ + public abstract void updateUpgradeValue(User user, Island island); + + /** + * This function is called every times a user open the interface If it return + * false, the upgrade won't be showed to the user + * + * @param user This is the user that ask for the interface + * @param island This is the island concerned by the interface + * @return If true, then upgrade is shown else, it is hided + */ + public boolean isShowed(User user, Island island) { + return true; + } + + /** + * This function return true if the user can upgrade for this island. You can + * override it and call the super. + * + * The super test for islandLevel and for money + * + * @param user This is the user that try to upgrade + * @param island This is the island that is concerned + * @return Can upgrade + */ + public boolean canUpgrade(User user, Island island) { + UpgradeValues upgradeValues = this.getUpgradeValues(user); + boolean can = true; + + if (this.upgradesAddon.isLevelProvided() + && this.upgradesAddon.getUpgradesManager().getIslandLevel(island) < upgradeValues.getIslandLevel()) { + + can = false; + } + + if (this.upgradesAddon.isVaultProvided() + && !this.upgradesAddon.getVaultHook().has(user, upgradeValues.getMoneyCost())) { + + can = false; + } + + return can; + } + + /** + * This function is called when the user is upgrading for the island It is + * called after the canUpgrade function + * + * You should call the super to update the balance of the user as well as the + * level is the island + * + * @param user This is the user that do the upgrade + * @param island This is the island that is concerned + * @return If upgrade was successful + */ + public boolean doUpgrade(User user, Island island) { + UpgradeValues upgradeValues = this.getUpgradeValues(user); + + if (this.upgradesAddon.isVaultProvided()) { + EconomyResponse response = this.upgradesAddon.getVaultHook().withdraw(user, upgradeValues.getMoneyCost()); + if (!response.transactionSuccess()) { + this.addon.logWarning( + "User Money withdrawing failed user: " + user.getName() + " reason: " + response.errorMessage); + user.sendMessage("upgrades.error.costwithdraw"); + return false; + } + } + + UpgradesData data = this.upgradesAddon.getUpgradesLevels(island.getUniqueId()); + data.setUpgradeLevel(this.name, data.getUpgradeLevel(this.name) + 1); + + return true; + } + + /** + * @return The name that is used for the DataBase + */ + public String getName() { + return this.name; + } + + /** + * @return The name that is displayed to the user + */ + public String getDisplayName() { + return this.displayName; + } + + /** + * @param displayName To update the name to display to the user + */ + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + /** + * @return The icon that is displayed to the user + */ + public Material getIcon() { + return this.icon; + } + + public int getUpgradeLevel(Island island) { + return this.upgradesAddon.getUpgradesLevels(island.getUniqueId()).getUpgradeLevel(this.name); + } + + /** + * @return The actual description for the user + */ + public String getOwnDescription(User user) { + return this.ownDescription.get(user.getUniqueId()); + } + + /** + * @param user User to set the description + * @param description Description to set + */ + public void setOwnDescription(User user, String description) { + this.ownDescription.put(user.getUniqueId(), description); + } + + /** + * @return The actual upgradeValues + */ + public UpgradeValues getUpgradeValues(User user) { + return this.playerCache.get(user.getUniqueId()); + } + + /** + * @param upgrade Values to upgrades + */ + public void setUpgradeValues(User user, UpgradeValues upgrade) { + this.playerCache.put(user.getUniqueId(), upgrade); + } + + /** + * Function that get the upgrades addon You should use it to use the upgrades + * addon methods + * + * @return UpgradesAddon + */ + public UpgradesAddon getUpgradesAddon() { + return this.upgradesAddon; + } + + /** + * You shouldn't override this function + */ + @Override + public int hashCode() { + return Objects.hash(name); + } + + /** + * You shouldn't override this function + */ + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (this == obj) { + return true; + } + if (!(obj instanceof Upgrade)) { + return false; + } + Upgrade other = (Upgrade) obj; + return Objects.equals(name, other.name); + } + + private final String name; + private String displayName; + private Material icon; + private Addon addon; + private UpgradesAddon upgradesAddon; + private Map playerCache; + private Map ownDescription; + + public class UpgradeValues { + + public UpgradeValues(Integer islandLevel, Integer moneyCost, Integer upgradeValue) { + this.islandLevel = islandLevel; + this.moneyCost = moneyCost; + this.upgradeValue = upgradeValue; + } + + public int getIslandLevel() { + return islandLevel; + } + + public void setIslandLevel(int islandLevel) { + this.islandLevel = islandLevel; + } + + public int getMoneyCost() { + return moneyCost; + } + + public void setMoneyCost(int moneyCost) { + this.moneyCost = moneyCost; + } + + public int getUpgradeValue() { + return upgradeValue; + } + + public void setUpgradeValue(int upgradeValue) { + this.upgradeValue = upgradeValue; + } + + private int islandLevel; + private int moneyCost; + private int upgradeValue; + } + + @Override + public String toString() { + return "Upgrade [name=" + name + ", displayName=" + displayName + ", icon=" + icon + "]"; + } } diff --git a/src/main/java/world/bentobox/upgrades/config/Settings.java b/src/main/java/world/bentobox/upgrades/config/Settings.java index 31ddd9e..8fddfc2 100644 --- a/src/main/java/world/bentobox/upgrades/config/Settings.java +++ b/src/main/java/world/bentobox/upgrades/config/Settings.java @@ -4,881 +4,915 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumMap; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.EntityType; import org.eclipse.jdt.annotation.NonNull; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.upgrades.UpgradesAddon; public class Settings { - public Settings(UpgradesAddon addon) { - this.addon = addon; - this.addon.saveDefaultConfig(); - - this.hasRangeUpgrade = false; - - this.disabledGameModes = new HashSet<>(this.addon.getConfig().getStringList("disabled-gamemodes")); - - if (this.addon.getConfig().isSet("range-upgrade")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("range-upgrade"); - for (String key : Objects.requireNonNull(section).getKeys(false)) { - UpgradeTier newUpgrade = addUpgradeSection(section, key); - - if (this.maxRangeUpgrade < newUpgrade.getMaxLevel()) - this.maxRangeUpgrade = newUpgrade.getMaxLevel(); - - this.hasRangeUpgrade = true; - this.rangeUpgradeTierMap.put(key, newUpgrade); - } - } - - if (this.addon.getConfig().isSet("block-limits-upgrade")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("block-limits-upgrade"); - this.blockLimitsUpgradeTierMap = this.loadBlockLimits(section, null); - } - - if (this.addon.getConfig().isSet("entity-icon")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-icon"); - for (String entity : Objects.requireNonNull(section).getKeys(false)) { - String material = section.getString(entity); - EntityType ent = this.getEntityType(entity); - Material mat = Material.getMaterial(material); - if (ent == null) - this.addon.logError("Config: EntityType " + entity + " is not valid in icon"); - else if (mat == null) - this.addon.logError("Config: Material " + material + " is not a valid material"); - else - this.entityIcon.put(ent, mat); - } - } - - if (this.addon.getConfig().isSet("entity-group-icon")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-group-icon"); - for (String group : Objects.requireNonNull(section).getKeys(false)) { - String material = section.getString(group); - Material mat = Material.getMaterial(material); - if (mat == null) - this.addon.logError("Config: Material " + material + " is not a valid material"); - else - this.entityGroupIcon.put(group, mat); - } - } - - if (this.addon.getConfig().isSet("entity-limits-upgrade")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-limits-upgrade"); - this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section, null); - } - - if (this.addon.getConfig().isSet("entity-group-limits-upgrade")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-group-limits-upgrade"); - this.entityGroupLimitsUpgradeTierMap = this.loadEntityGroupLimits(section, null); - } - - if (this.addon.getConfig().isSet("command-icon")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-icon"); - for (String commandId: Objects.requireNonNull(section).getKeys(false)) { - String material = section.getString(commandId); - Material mat = Material.getMaterial(material); - if (mat == null) - this.addon.logError("Config: Material " + material + " is not a valid material"); - else - this.commandIcon.put(commandId, mat); - } - } - - if (this.addon.getConfig().isSet("command-upgrade")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-upgrade"); - this.commandUpgradeTierMap = this.loadCommand(section, null); - } - - if (this.addon.getConfig().isSet("gamemodes")) { - ConfigurationSection section = this.addon.getConfig().getConfigurationSection("gamemodes"); - - for (String gameMode : Objects.requireNonNull(section).getKeys(false)) { - ConfigurationSection gameModeSection = section.getConfigurationSection(gameMode); - - if (gameModeSection.isSet("range-upgrade")) { - ConfigurationSection lowSection = gameModeSection.getConfigurationSection("range-upgrade"); - for (String key : Objects.requireNonNull(lowSection).getKeys(false)) { - UpgradeTier newUpgrade = addUpgradeSection(lowSection, key); - - if (this.customMaxRangeUpgrade.get(gameMode) == null || this.customMaxRangeUpgrade.get(gameMode) < newUpgrade.getMaxLevel()) - this.customMaxRangeUpgrade.put(gameMode, newUpgrade.getMaxLevel()); - - this.hasRangeUpgrade = true; - - this.customRangeUpgradeTierMap.computeIfAbsent(gameMode, k -> new HashMap<>()).put(key, newUpgrade); - } - } - - if (gameModeSection.isSet("block-limits-upgrade")) { - ConfigurationSection lowSection = gameModeSection.getConfigurationSection("block-limits-upgrade"); - this.customBlockLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadBlockLimits(lowSection, gameMode)); - } - - if (gameModeSection.isSet("entity-limits-upgrade")) { - ConfigurationSection lowSection = gameModeSection.getConfigurationSection("entity-limits-upgrade"); - this.customEntityLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityLimits(lowSection, gameMode)); - } - - if (gameModeSection.isSet("entity-group-limits-upgrade")) { - ConfigurationSection lowSection = gameModeSection.getConfigurationSection("entity-group-limits-upgrade"); - this.customEntityGroupLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityGroupLimits(lowSection, gameMode)); - } - - if (gameModeSection.isSet("command-upgrade")) { - ConfigurationSection lowSection = gameModeSection.getConfigurationSection("command-upgrade"); - this.customCommandUpgradeTierMap.computeIfAbsent(gameMode, k -> loadCommand(lowSection, gameMode)); - } - } - } - - } - - private Map> loadBlockLimits(ConfigurationSection section, String gameMode) { - Map> mats = new EnumMap<>(Material.class); - for (String material: Objects.requireNonNull(section).getKeys(false)) { - Material mat = Material.getMaterial(material); - if (mat != null && mat.isBlock()) { - Map tier = new HashMap<>(); - ConfigurationSection matSection = section.getConfigurationSection(material); - for (String key : Objects.requireNonNull(matSection).getKeys(false)) { - UpgradeTier newUpgrade = addUpgradeSection(matSection, key); - - if (gameMode == null) { - if (this.maxBlockLimitsUpgrade.get(mat) == null || this.maxBlockLimitsUpgrade.get(mat) < newUpgrade.getMaxLevel()) - this.maxBlockLimitsUpgrade.put(mat, newUpgrade.getMaxLevel()); - } else { - if (this.customMaxBlockLimitsUpgrade.get(gameMode) == null) { - Map newMap = new EnumMap<>(Material.class); - newMap.put(mat, newUpgrade.getMaxLevel()); - this.customMaxBlockLimitsUpgrade.put(gameMode, newMap); - } else { - if (this.customMaxBlockLimitsUpgrade.get(gameMode).get(mat) == null || this.customMaxBlockLimitsUpgrade.get(gameMode).get(mat) < newUpgrade.getMaxLevel()) - this.customMaxBlockLimitsUpgrade.get(gameMode).put(mat, newUpgrade.getMaxLevel()); - } - } - - tier.put(key, newUpgrade); - } - mats.put(mat, tier); - } else { - this.addon.logError("Material " + material + " is not a valid material. Skipping..."); - } - } - return mats; - } - - private Map> loadEntityLimits(ConfigurationSection section, String gameMode) { - Map> ents = new EnumMap<>(EntityType.class); - for (String entity: Objects.requireNonNull(section).getKeys(false)) { - EntityType ent = this.getEntityType(entity); - if (ent != null && this.entityIcon.containsKey(ent)) { - Map tier = new HashMap<>(); - ConfigurationSection entSection = section.getConfigurationSection(entity); - for (String key : Objects.requireNonNull(entSection).getKeys(false)) { - UpgradeTier newUpgrade = addUpgradeSection(entSection, key); - - if (gameMode == null) { - if (this.maxEntityLimitsUpgrade.get(ent) == null || this.maxEntityLimitsUpgrade.get(ent) < newUpgrade.getMaxLevel()) - this.maxEntityLimitsUpgrade.put(ent, newUpgrade.getMaxLevel()); - } else { - if (this.customMaxEntityLimitsUpgrade.get(gameMode) == null) { - Map newMap = new EnumMap<>(EntityType.class); - newMap.put(ent, newUpgrade.getMaxLevel()); - this.customMaxEntityLimitsUpgrade.put(gameMode, newMap); - } else { - if (this.customMaxEntityLimitsUpgrade.get(gameMode).get(ent) == null || this.customMaxEntityLimitsUpgrade.get(gameMode).get(ent) < newUpgrade.getMaxLevel()) - this.customMaxEntityLimitsUpgrade.get(gameMode).put(ent, newUpgrade.getMaxLevel()); - } - } - - tier.put(key, newUpgrade); - } - ents.put(ent, tier); - } else { - if (ent != null) - this.addon.logError("Entity " + entity+ " is not a valid entity. Skipping..."); - else - this.addon.logError("Entity " + entity+ " is missing a corresponding icon. Skipping..."); - } - } - return ents; - } - - private Map> loadEntityGroupLimits(ConfigurationSection section, String gameMode) { - Map> ents = new HashMap<>(); - for (String entitygroup : Objects.requireNonNull(section).getKeys(false)) { - Map tier = new HashMap<>(); - ConfigurationSection entSection = section.getConfigurationSection(entitygroup); - for (String key : Objects.requireNonNull(entSection).getKeys(false)) { - UpgradeTier newUpgrade = addUpgradeSection(entSection, key); - - if (gameMode == null) { - if (this.maxEntityGroupLimitsUpgrade.get(entitygroup) == null || this.maxEntityGroupLimitsUpgrade.get(entitygroup) < newUpgrade.getMaxLevel()) - this.maxEntityGroupLimitsUpgrade.put(entitygroup, newUpgrade.getMaxLevel()); - } else { - if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode) == null) { - Map newMap = new HashMap<>(); - newMap.put(entitygroup, newUpgrade.getMaxLevel()); - this.customMaxEntityGroupLimitsUpgrade.put(gameMode, newMap); - } else { - if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) == null || this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) < newUpgrade.getMaxLevel()) - this.customMaxEntityGroupLimitsUpgrade.get(gameMode).put(entitygroup, newUpgrade.getMaxLevel()); - } - } - - tier.put(key, newUpgrade); - } - ents.put(entitygroup, tier); - } - return ents; - } - - private Map> loadCommand(ConfigurationSection section, String gamemode) { - Map> commands = new HashMap<>(); - - for (String commandId: Objects.requireNonNull(section).getKeys(false)) { - if (this.commandIcon.containsKey(commandId)) { - String name = commandId; - Map tier = new HashMap<>(); - ConfigurationSection cmdSection = section.getConfigurationSection(commandId); - for (String key: Objects.requireNonNull(cmdSection).getKeys(false)) { - if (key.equals("name")) { - name = cmdSection.getString(key); - } else { - CommandUpgradeTier newUpgrade = addCommandUpgradeSection(cmdSection, key); - - if (gamemode == null) { - if (this.maxCommandUpgrade.get(commandId) == null || this.maxCommandUpgrade.get(commandId) < newUpgrade.getMaxLevel()) { - this.maxCommandUpgrade.put(commandId, newUpgrade.getMaxLevel()); - } - } else { - if (this.customMaxCommandUpgrade.get(gamemode) == null) { - Map newMap = new HashMap<>(); - newMap.put(commandId, newUpgrade.getMaxLevel()); - this.customMaxCommandUpgrade.put(gamemode, newMap); - } else { - if (this.customMaxCommandUpgrade.get(gamemode).get(commandId) == null || this.customMaxCommandUpgrade.get(gamemode).get(commandId) < newUpgrade.getMaxLevel()) - this.customMaxCommandUpgrade.get(gamemode).put(commandId, newUpgrade.getMaxLevel()); - } - } - - tier.put(key, newUpgrade); - } - } - if (!this.commandName.containsKey(commandId) || !name.equals(commandId)) - this.commandName.put(commandId, name); - commands.put(commandId, tier); - } else { - this.addon.logError("Command " + commandId + " is missing a corresponding icon. Skipping..."); - } - } - - return commands; - } - - @NonNull - private UpgradeTier addUpgradeSection(ConfigurationSection section, String key) { - ConfigurationSection tierSection = section.getConfigurationSection(key); - UpgradeTier upgradeTier = new UpgradeTier(key); - upgradeTier.setTierName(tierSection.getName()); - upgradeTier.setMaxLevel(tierSection.getInt("max-level")); - upgradeTier.setUpgrade(parse(tierSection.getString("upgrade"), upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("island-min-level")) - upgradeTier.setIslandMinLevel(parse(tierSection.getString("island-min-level"), upgradeTier.getExpressionVariable())); - else - upgradeTier.setIslandMinLevel(parse("0", upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("vault-cost")) - upgradeTier.setVaultCost(parse(tierSection.getString("vault-cost"), upgradeTier.getExpressionVariable())); - else - upgradeTier.setVaultCost(parse("0", upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("permission-level")) - upgradeTier.setPermissionLevel(tierSection.getInt("permission-level")); - else - upgradeTier.setPermissionLevel(0); - - return upgradeTier; - - } - - @NonNull - private CommandUpgradeTier addCommandUpgradeSection(ConfigurationSection section, String key) { - ConfigurationSection tierSection = section.getConfigurationSection(key); - CommandUpgradeTier upgradeTier = new CommandUpgradeTier(key); - upgradeTier.setTierName(tierSection.getName()); - upgradeTier.setMaxLevel(tierSection.getInt("max-level")); - upgradeTier.setUpgrade(parse("0", upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("island-min-level")) - upgradeTier.setIslandMinLevel(parse(tierSection.getString("island-min-level"), upgradeTier.getExpressionVariable())); - else - upgradeTier.setIslandMinLevel(parse("0", upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("vault-cost")) - upgradeTier.setVaultCost(parse(tierSection.getString("vault-cost"), upgradeTier.getExpressionVariable())); - else - upgradeTier.setVaultCost(parse("0", upgradeTier.getExpressionVariable())); - - if (tierSection.isSet("permission-level")) - upgradeTier.setPermissionLevel(tierSection.getInt("permission-level")); - else - upgradeTier.setPermissionLevel(0); - - if (tierSection.isSet("console") && tierSection.isBoolean("console")) - upgradeTier.setConsole(tierSection.getBoolean("console")); - else - upgradeTier.setConsole(false); - - if (tierSection.isSet("command")) - upgradeTier.setCommandList(tierSection.getStringList("command")); - - return upgradeTier; - - } - - /** - * @return the disabledGameModes - */ - public Set getDisabledGameModes() { - return disabledGameModes; - } - - public boolean getHasRangeUpgrade() { - return this.hasRangeUpgrade; - } - - public int getMaxRangeUpgrade(String addon) { - return this.customMaxRangeUpgrade.getOrDefault(addon, this.maxRangeUpgrade); - } - - public Map getDefaultRangeUpgradeTierMap() { - return this.rangeUpgradeTierMap; - } - - /** - * @return the rangeUpgradeTierMap - */ - public Map getAddonRangeUpgradeTierMap(String addon) { - return this.customRangeUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); - } - - public int getMaxBlockLimitsUpgrade(Material mat, String addon) { - return this.customMaxBlockLimitsUpgrade.getOrDefault(addon, this.maxBlockLimitsUpgrade).getOrDefault(mat, 0); - } - - public Map> getDefaultBlockLimitsUpgradeTierMap() { - return this.blockLimitsUpgradeTierMap; - } - - /** - * @return the rangeUpgradeTierMap - */ - public Map> getAddonBlockLimitsUpgradeTierMap(String addon) { - return this.customBlockLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); - } - - public Set getMaterialsLimitsUpgrade() { - Set materials = new HashSet<>(); - - this.customBlockLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { - materials.addAll(addonUpgrade.keySet()); - }); - materials.addAll(this.blockLimitsUpgradeTierMap.keySet()); - - return materials; - } - - public Material getEntityIcon(EntityType entity) { - return this.entityIcon.getOrDefault(entity, null); - } - - public Material getEntityGroupIcon(String group) { - return this.entityGroupIcon.getOrDefault(group, null); - } - - public int getMaxEntityLimitsUpgrade(EntityType entity, String addon) { - return this.customMaxEntityLimitsUpgrade.getOrDefault(addon, this.maxEntityLimitsUpgrade).getOrDefault(entity, 0); - } - - public int getMaxEntityGroupLimitsUpgrade(String group, String addon) { - return this.customMaxEntityGroupLimitsUpgrade.getOrDefault(addon, this.maxEntityGroupLimitsUpgrade).getOrDefault(group, 0); - } - - public Map> getDefaultEntityLimitsUpgradeTierMap() { - return this.entityLimitsUpgradeTierMap; - } - - public Map> getDefaultEntityGroupLimitsUpgradeTierMap() { - return this.entityGroupLimitsUpgradeTierMap; - } + public Settings(UpgradesAddon addon) { + this.addon = addon; + this.addon.saveDefaultConfig(); + + this.hasRangeUpgrade = false; + + this.disabledGameModes = new HashSet<>(this.addon.getConfig().getStringList("disabled-gamemodes")); + + if (this.addon.getConfig().isSet("range-upgrade")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("range-upgrade"); + for (String key : Objects.requireNonNull(section).getKeys(false)) { + UpgradeTier newUpgrade = addUpgradeSection(section, key); + + if (this.maxRangeUpgrade < newUpgrade.getMaxLevel()) + this.maxRangeUpgrade = newUpgrade.getMaxLevel(); + + this.hasRangeUpgrade = true; + this.rangeUpgradeTierMap.put(key, newUpgrade); + } + } + + if (this.addon.getConfig().isSet("block-limits-upgrade")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("block-limits-upgrade"); + this.blockLimitsUpgradeTierMap = this.loadBlockLimits(section, null); + } + + if (this.addon.getConfig().isSet("entity-icon")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-icon"); + for (String entity : Objects.requireNonNull(section).getKeys(false)) { + String material = section.getString(entity); + EntityType ent = this.getEntityType(entity); + Material mat = Material.getMaterial(material); + if (ent == null) + this.addon.logError("Config: EntityType " + entity + " is not valid in icon"); + else if (mat == null) + this.addon.logError("Config: Material " + material + " is not a valid material"); + else + this.entityIcon.put(ent, mat); + } + } + + if (this.addon.getConfig().isSet("entity-group-icon")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-group-icon"); + for (String group : Objects.requireNonNull(section).getKeys(false)) { + String material = section.getString(group); + Material mat = Material.getMaterial(material); + if (mat == null) + this.addon.logError("Config: Material " + material + " is not a valid material"); + else + this.entityGroupIcon.put(group, mat); + } + } + + if (this.addon.getConfig().isSet("entity-limits-upgrade")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-limits-upgrade"); + this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section, null); + } + + if (this.addon.getConfig().isSet("entity-group-limits-upgrade")) { + ConfigurationSection section = this.addon.getConfig() + .getConfigurationSection("entity-group-limits-upgrade"); + this.entityGroupLimitsUpgradeTierMap = this.loadEntityGroupLimits(section, null); + } + + if (this.addon.getConfig().isSet("command-icon")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-icon"); + for (String commandId : Objects.requireNonNull(section).getKeys(false)) { + String material = section.getString(commandId); + Material mat = Material.getMaterial(material); + if (mat == null) + this.addon.logError("Config: Material " + material + " is not a valid material"); + else + this.commandIcon.put(commandId, mat); + } + } + + if (this.addon.getConfig().isSet("command-upgrade")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("command-upgrade"); + this.commandUpgradeTierMap = this.loadCommand(section, null); + } + + if (this.addon.getConfig().isSet("gamemodes")) { + ConfigurationSection section = this.addon.getConfig().getConfigurationSection("gamemodes"); + + for (String gameMode : Objects.requireNonNull(section).getKeys(false)) { + ConfigurationSection gameModeSection = section.getConfigurationSection(gameMode); + + if (gameModeSection.isSet("range-upgrade")) { + ConfigurationSection lowSection = gameModeSection.getConfigurationSection("range-upgrade"); + for (String key : Objects.requireNonNull(lowSection).getKeys(false)) { + UpgradeTier newUpgrade = addUpgradeSection(lowSection, key); + + if (this.customMaxRangeUpgrade.get(gameMode) == null + || this.customMaxRangeUpgrade.get(gameMode) < newUpgrade.getMaxLevel()) + this.customMaxRangeUpgrade.put(gameMode, newUpgrade.getMaxLevel()); + + this.hasRangeUpgrade = true; + + this.customRangeUpgradeTierMap.computeIfAbsent(gameMode, k -> new TreeMap<>()).put(key, + newUpgrade); + } + } + + if (gameModeSection.isSet("block-limits-upgrade")) { + ConfigurationSection lowSection = gameModeSection.getConfigurationSection("block-limits-upgrade"); + this.customBlockLimitsUpgradeTierMap.computeIfAbsent(gameMode, + k -> loadBlockLimits(lowSection, gameMode)); + } + + if (gameModeSection.isSet("entity-limits-upgrade")) { + ConfigurationSection lowSection = gameModeSection.getConfigurationSection("entity-limits-upgrade"); + this.customEntityLimitsUpgradeTierMap.computeIfAbsent(gameMode, + k -> loadEntityLimits(lowSection, gameMode)); + } + + if (gameModeSection.isSet("entity-group-limits-upgrade")) { + ConfigurationSection lowSection = gameModeSection + .getConfigurationSection("entity-group-limits-upgrade"); + this.customEntityGroupLimitsUpgradeTierMap.computeIfAbsent(gameMode, + k -> loadEntityGroupLimits(lowSection, gameMode)); + } + + if (gameModeSection.isSet("command-upgrade")) { + ConfigurationSection lowSection = gameModeSection.getConfigurationSection("command-upgrade"); + this.customCommandUpgradeTierMap.computeIfAbsent(gameMode, k -> loadCommand(lowSection, gameMode)); + } + } + } + + } + + private Map> loadBlockLimits(ConfigurationSection section, String gameMode) { + Map> mats = new EnumMap<>(Material.class); + for (String material : Objects.requireNonNull(section).getKeys(false)) { + Material mat = Material.getMaterial(material); + if (mat != null && mat.isBlock()) { + Map tier = new TreeMap<>(); + ConfigurationSection matSection = section.getConfigurationSection(material); + for (String key : Objects.requireNonNull(matSection).getKeys(false)) { + UpgradeTier newUpgrade = addUpgradeSection(matSection, key); + + if (gameMode == null) { + if (this.maxBlockLimitsUpgrade.get(mat) == null + || this.maxBlockLimitsUpgrade.get(mat) < newUpgrade.getMaxLevel()) + this.maxBlockLimitsUpgrade.put(mat, newUpgrade.getMaxLevel()); + } else { + if (this.customMaxBlockLimitsUpgrade.get(gameMode) == null) { + Map newMap = new EnumMap<>(Material.class); + newMap.put(mat, newUpgrade.getMaxLevel()); + this.customMaxBlockLimitsUpgrade.put(gameMode, newMap); + } else { + if (this.customMaxBlockLimitsUpgrade.get(gameMode).get(mat) == null + || this.customMaxBlockLimitsUpgrade.get(gameMode).get(mat) < newUpgrade + .getMaxLevel()) + this.customMaxBlockLimitsUpgrade.get(gameMode).put(mat, newUpgrade.getMaxLevel()); + } + } + + tier.put(key, newUpgrade); + } + mats.put(mat, tier); + } else { + this.addon.logError("Material " + material + " is not a valid material. Skipping..."); + } + } + return mats; + } + + private Map> loadEntityLimits(ConfigurationSection section, String gameMode) { + Map> ents = new EnumMap<>(EntityType.class); + for (String entity : Objects.requireNonNull(section).getKeys(false)) { + EntityType ent = this.getEntityType(entity); + if (ent != null && this.entityIcon.containsKey(ent)) { + Map tier = new TreeMap<>(); + ConfigurationSection entSection = section.getConfigurationSection(entity); + for (String key : Objects.requireNonNull(entSection).getKeys(false)) { + UpgradeTier newUpgrade = addUpgradeSection(entSection, key); + + if (gameMode == null) { + if (this.maxEntityLimitsUpgrade.get(ent) == null + || this.maxEntityLimitsUpgrade.get(ent) < newUpgrade.getMaxLevel()) + this.maxEntityLimitsUpgrade.put(ent, newUpgrade.getMaxLevel()); + } else { + if (this.customMaxEntityLimitsUpgrade.get(gameMode) == null) { + Map newMap = new EnumMap<>(EntityType.class); + newMap.put(ent, newUpgrade.getMaxLevel()); + this.customMaxEntityLimitsUpgrade.put(gameMode, newMap); + } else { + if (this.customMaxEntityLimitsUpgrade.get(gameMode).get(ent) == null + || this.customMaxEntityLimitsUpgrade.get(gameMode).get(ent) < newUpgrade + .getMaxLevel()) + this.customMaxEntityLimitsUpgrade.get(gameMode).put(ent, newUpgrade.getMaxLevel()); + } + } + + tier.put(key, newUpgrade); + } + tier.keySet().forEach(k -> BentoBox.getInstance().logDebug("Key - " + k)); + ents.put(ent, tier); + } else { + if (ent != null) + this.addon.logError("Entity " + entity + " is not a valid entity. Skipping..."); + else + this.addon.logError("Entity " + entity + " is missing a corresponding icon. Skipping..."); + } + } + return ents; + } + + private Map> loadEntityGroupLimits(ConfigurationSection section, String gameMode) { + Map> ents = new TreeMap<>(); + for (String entitygroup : Objects.requireNonNull(section).getKeys(false)) { + Map tier = new TreeMap<>(); + ConfigurationSection entSection = section.getConfigurationSection(entitygroup); + for (String key : Objects.requireNonNull(entSection).getKeys(false)) { + UpgradeTier newUpgrade = addUpgradeSection(entSection, key); + + if (gameMode == null) { + if (this.maxEntityGroupLimitsUpgrade.get(entitygroup) == null + || this.maxEntityGroupLimitsUpgrade.get(entitygroup) < newUpgrade.getMaxLevel()) + this.maxEntityGroupLimitsUpgrade.put(entitygroup, newUpgrade.getMaxLevel()); + } else { + if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode) == null) { + Map newMap = new TreeMap<>(); + newMap.put(entitygroup, newUpgrade.getMaxLevel()); + this.customMaxEntityGroupLimitsUpgrade.put(gameMode, newMap); + } else { + if (this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) == null + || this.customMaxEntityGroupLimitsUpgrade.get(gameMode).get(entitygroup) < newUpgrade + .getMaxLevel()) + this.customMaxEntityGroupLimitsUpgrade.get(gameMode).put(entitygroup, + newUpgrade.getMaxLevel()); + } + } + + tier.put(key, newUpgrade); + } + ents.put(entitygroup, tier); + } + return ents; + } + + private Map> loadCommand(ConfigurationSection section, String gamemode) { + Map> commands = new TreeMap<>(); + + for (String commandId : Objects.requireNonNull(section).getKeys(false)) { + if (this.commandIcon.containsKey(commandId)) { + String name = commandId; + Map tier = new TreeMap<>(); + ConfigurationSection cmdSection = section.getConfigurationSection(commandId); + for (String key : Objects.requireNonNull(cmdSection).getKeys(false)) { + if (key.equals("name")) { + name = cmdSection.getString(key); + } else { + CommandUpgradeTier newUpgrade = addCommandUpgradeSection(cmdSection, key); + + if (gamemode == null) { + if (this.maxCommandUpgrade.get(commandId) == null + || this.maxCommandUpgrade.get(commandId) < newUpgrade.getMaxLevel()) { + this.maxCommandUpgrade.put(commandId, newUpgrade.getMaxLevel()); + } + } else { + if (this.customMaxCommandUpgrade.get(gamemode) == null) { + Map newMap = new TreeMap<>(); + newMap.put(commandId, newUpgrade.getMaxLevel()); + this.customMaxCommandUpgrade.put(gamemode, newMap); + } else { + if (this.customMaxCommandUpgrade.get(gamemode).get(commandId) == null + || this.customMaxCommandUpgrade.get(gamemode).get(commandId) < newUpgrade + .getMaxLevel()) + this.customMaxCommandUpgrade.get(gamemode).put(commandId, newUpgrade.getMaxLevel()); + } + } + + tier.put(key, newUpgrade); + } + } + if (!this.commandName.containsKey(commandId) || !name.equals(commandId)) + this.commandName.put(commandId, name); + commands.put(commandId, tier); + } else { + this.addon.logError("Command " + commandId + " is missing a corresponding icon. Skipping..."); + } + } + + return commands; + } + + @NonNull + private UpgradeTier addUpgradeSection(ConfigurationSection section, String key) { + ConfigurationSection tierSection = section.getConfigurationSection(key); + UpgradeTier upgradeTier = new UpgradeTier(key); + upgradeTier.setTierName(tierSection.getName()); + upgradeTier.setMaxLevel(tierSection.getInt("max-level")); + upgradeTier.setUpgrade(parse(tierSection.getString("upgrade"), upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("island-min-level")) + upgradeTier.setIslandMinLevel( + parse(tierSection.getString("island-min-level"), upgradeTier.getExpressionVariable())); + else + upgradeTier.setIslandMinLevel(parse("0", upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("vault-cost")) + upgradeTier.setVaultCost(parse(tierSection.getString("vault-cost"), upgradeTier.getExpressionVariable())); + else + upgradeTier.setVaultCost(parse("0", upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("permission-level")) + upgradeTier.setPermissionLevel(tierSection.getInt("permission-level")); + else + upgradeTier.setPermissionLevel(0); + + return upgradeTier; + + } + + @NonNull + private CommandUpgradeTier addCommandUpgradeSection(ConfigurationSection section, String key) { + ConfigurationSection tierSection = section.getConfigurationSection(key); + CommandUpgradeTier upgradeTier = new CommandUpgradeTier(key); + upgradeTier.setTierName(tierSection.getName()); + upgradeTier.setMaxLevel(tierSection.getInt("max-level")); + upgradeTier.setUpgrade(parse("0", upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("island-min-level")) + upgradeTier.setIslandMinLevel( + parse(tierSection.getString("island-min-level"), upgradeTier.getExpressionVariable())); + else + upgradeTier.setIslandMinLevel(parse("0", upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("vault-cost")) + upgradeTier.setVaultCost(parse(tierSection.getString("vault-cost"), upgradeTier.getExpressionVariable())); + else + upgradeTier.setVaultCost(parse("0", upgradeTier.getExpressionVariable())); + + if (tierSection.isSet("permission-level")) + upgradeTier.setPermissionLevel(tierSection.getInt("permission-level")); + else + upgradeTier.setPermissionLevel(0); + + if (tierSection.isSet("console") && tierSection.isBoolean("console")) + upgradeTier.setConsole(tierSection.getBoolean("console")); + else + upgradeTier.setConsole(false); + + if (tierSection.isSet("command")) + upgradeTier.setCommandList(tierSection.getStringList("command")); + + return upgradeTier; + + } + + /** + * @return the disabledGameModes + */ + public Set getDisabledGameModes() { + return disabledGameModes; + } + + public boolean getHasRangeUpgrade() { + return this.hasRangeUpgrade; + } + + public int getMaxRangeUpgrade(String addon) { + return this.customMaxRangeUpgrade.getOrDefault(addon, this.maxRangeUpgrade); + } + + public Map getDefaultRangeUpgradeTierMap() { + return this.rangeUpgradeTierMap; + } + + /** + * @return the rangeUpgradeTierMap + */ + public Map getAddonRangeUpgradeTierMap(String addon) { + return this.customRangeUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); + } + + public int getMaxBlockLimitsUpgrade(Material mat, String addon) { + return this.customMaxBlockLimitsUpgrade.getOrDefault(addon, this.maxBlockLimitsUpgrade).getOrDefault(mat, 0); + } + + public Map> getDefaultBlockLimitsUpgradeTierMap() { + return this.blockLimitsUpgradeTierMap; + } + + /** + * @return the rangeUpgradeTierMap + */ + public Map> getAddonBlockLimitsUpgradeTierMap(String addon) { + return this.customBlockLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); + } + + public Set getMaterialsLimitsUpgrade() { + Set materials = new HashSet<>(); + + this.customBlockLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { + materials.addAll(addonUpgrade.keySet()); + }); + materials.addAll(this.blockLimitsUpgradeTierMap.keySet()); + + return materials; + } + + public Material getEntityIcon(EntityType entity) { + return this.entityIcon.getOrDefault(entity, null); + } + + public Material getEntityGroupIcon(String group) { + return this.entityGroupIcon.getOrDefault(group, null); + } + + public int getMaxEntityLimitsUpgrade(EntityType entity, String addon) { + return this.customMaxEntityLimitsUpgrade.getOrDefault(addon, this.maxEntityLimitsUpgrade).getOrDefault(entity, + 0); + } + + public int getMaxEntityGroupLimitsUpgrade(String group, String addon) { + return this.customMaxEntityGroupLimitsUpgrade.getOrDefault(addon, this.maxEntityGroupLimitsUpgrade) + .getOrDefault(group, 0); + } - /** - * @return the rangeUpgradeTierMap - */ - public Map> getAddonEntityLimitsUpgradeTierMap(String addon) { - return this.customEntityLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); - } + public Map> getDefaultEntityLimitsUpgradeTierMap() { + return this.entityLimitsUpgradeTierMap; + } - public Map> getAddonEntityGroupLimitsUpgradeTierMap(String addon) { - return this.customEntityGroupLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); - } + public Map> getDefaultEntityGroupLimitsUpgradeTierMap() { + return this.entityGroupLimitsUpgradeTierMap; + } - public Set getEntityLimitsUpgrade() { - Set entity = new HashSet<>(); + /** + * @return the rangeUpgradeTierMap + */ + public Map> getAddonEntityLimitsUpgradeTierMap(String addon) { + return this.customEntityLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); + } - this.customEntityLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { - entity.addAll(addonUpgrade.keySet()); - }); - entity.addAll(this.entityLimitsUpgradeTierMap.keySet()); + public Map> getAddonEntityGroupLimitsUpgradeTierMap(String addon) { + return this.customEntityGroupLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); + } - return entity; - } + public Set getEntityLimitsUpgrade() { + Set entity = new HashSet<>(); - public Set getEntityGroupLimitsUpgrade() { - Set groups = new HashSet<>(); + this.customEntityLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { + entity.addAll(addonUpgrade.keySet()); + }); + entity.addAll(this.entityLimitsUpgradeTierMap.keySet()); - this.customEntityGroupLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { - groups.addAll(addonUpgrade.keySet()); - }); - groups.addAll(this.entityGroupLimitsUpgradeTierMap.keySet()); + return entity; + } - return groups; - } + public Set getEntityGroupLimitsUpgrade() { + Set groups = new HashSet<>(); - private EntityType getEntityType(String key) { - return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null); - } + this.customEntityGroupLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> { + groups.addAll(addonUpgrade.keySet()); + }); + groups.addAll(this.entityGroupLimitsUpgradeTierMap.keySet()); - public int getMaxCommandUpgrade(String commandUpgrade, String addon) { - if (this.customMaxCommandUpgrade.containsKey(addon)) { - if (this.customMaxCommandUpgrade.get(addon).containsKey(commandUpgrade)) { - return this.customMaxCommandUpgrade.get(addon).get(commandUpgrade); - } - } - return this.maxCommandUpgrade.getOrDefault(commandUpgrade, 0); - } + return groups; + } - public Map> getDefaultCommandUpgradeTierMap() { - return this.commandUpgradeTierMap; - } + private EntityType getEntityType(String key) { + return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null); + } - /** - * @return the rangeUpgradeTierMap - */ - public Map> getAddonCommandUpgradeTierMap(String addon) { - return this.customCommandUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); - } + public int getMaxCommandUpgrade(String commandUpgrade, String addon) { + if (this.customMaxCommandUpgrade.containsKey(addon)) { + if (this.customMaxCommandUpgrade.get(addon).containsKey(commandUpgrade)) { + return this.customMaxCommandUpgrade.get(addon).get(commandUpgrade); + } + } + return this.maxCommandUpgrade.getOrDefault(commandUpgrade, 0); + } - public Set getCommandUpgrade() { - Set command = new HashSet<>(); + public Map> getDefaultCommandUpgradeTierMap() { + return this.commandUpgradeTierMap; + } - this.customCommandUpgradeTierMap.forEach((addon, addonUpgrade) -> { - command.addAll(addonUpgrade.keySet()); - }); - command.addAll(this.commandUpgradeTierMap.keySet()); + /** + * @return the rangeUpgradeTierMap + */ + public Map> getAddonCommandUpgradeTierMap(String addon) { + return this.customCommandUpgradeTierMap.getOrDefault(addon, Collections.emptyMap()); + } - return command; - } + public Set getCommandUpgrade() { + Set command = new HashSet<>(); - public Material getCommandIcon(String command) { - return this.commandIcon.getOrDefault(command, null); - } + this.customCommandUpgradeTierMap.forEach((addon, addonUpgrade) -> { + command.addAll(addonUpgrade.keySet()); + }); + command.addAll(this.commandUpgradeTierMap.keySet()); - public String getCommandName(String command) { - return this.commandName.get(command); - } + return command; + } - private UpgradesAddon addon; + public Material getCommandIcon(String command) { + return this.commandIcon.getOrDefault(command, null); + } - private Set disabledGameModes; + public String getCommandName(String command) { + return this.commandName.get(command); + } - private int maxRangeUpgrade = 0; + private UpgradesAddon addon; - private boolean hasRangeUpgrade; + private Set disabledGameModes; - private Map customMaxRangeUpgrade = new HashMap<>(); + private int maxRangeUpgrade = 0; - private Map rangeUpgradeTierMap = new HashMap<>(); + private boolean hasRangeUpgrade; - private Map> customRangeUpgradeTierMap = new HashMap<>(); + private Map customMaxRangeUpgrade = new TreeMap<>(); - private Map maxBlockLimitsUpgrade = new EnumMap<>(Material.class); + private Map rangeUpgradeTierMap = new TreeMap<>(); - private Map> customMaxBlockLimitsUpgrade = new HashMap<>(); + private Map> customRangeUpgradeTierMap = new TreeMap<>(); - private Map> blockLimitsUpgradeTierMap = new EnumMap<>(Material.class); + private Map maxBlockLimitsUpgrade = new EnumMap<>(Material.class); - private Map>> customBlockLimitsUpgradeTierMap = new HashMap<>(); + private Map> customMaxBlockLimitsUpgrade = new TreeMap<>(); - private Map entityIcon = new EnumMap<>(EntityType.class); + private Map> blockLimitsUpgradeTierMap = new EnumMap<>(Material.class); - private Map entityGroupIcon = new HashMap<>(); + private Map>> customBlockLimitsUpgradeTierMap = new TreeMap<>(); - private Map maxEntityLimitsUpgrade = new EnumMap<>(EntityType.class); + private Map entityIcon = new EnumMap<>(EntityType.class); - private Map maxEntityGroupLimitsUpgrade = new HashMap<>(); + private Map entityGroupIcon = new TreeMap<>(); - private Map> customMaxEntityLimitsUpgrade = new HashMap<>(); + private Map maxEntityLimitsUpgrade = new EnumMap<>(EntityType.class); - private Map> customMaxEntityGroupLimitsUpgrade = new HashMap<>(); + private Map maxEntityGroupLimitsUpgrade = new TreeMap<>(); - private Map> entityLimitsUpgradeTierMap = new EnumMap<>(EntityType.class); + private Map> customMaxEntityLimitsUpgrade = new TreeMap<>(); - private Map> entityGroupLimitsUpgradeTierMap = new HashMap<>(); + private Map> customMaxEntityGroupLimitsUpgrade = new TreeMap<>(); - private Map>> customEntityLimitsUpgradeTierMap = new HashMap<>(); + private Map> entityLimitsUpgradeTierMap = new EnumMap<>(EntityType.class); - private Map>> customEntityGroupLimitsUpgradeTierMap = new HashMap<>(); + private Map> entityGroupLimitsUpgradeTierMap = new TreeMap<>(); - private Map maxCommandUpgrade = new HashMap<>(); + private Map>> customEntityLimitsUpgradeTierMap = new TreeMap<>(); - private Map> customMaxCommandUpgrade = new HashMap<>(); + private Map>> customEntityGroupLimitsUpgradeTierMap = new TreeMap<>(); - private Map> commandUpgradeTierMap = new HashMap<>(); + private Map maxCommandUpgrade = new TreeMap<>(); - private Map>> customCommandUpgradeTierMap = new HashMap<>(); + private Map> customMaxCommandUpgrade = new TreeMap<>(); - private Map commandIcon = new HashMap<>(); + private Map> commandUpgradeTierMap = new TreeMap<>(); - private Map commandName = new HashMap<>(); + private Map>> customCommandUpgradeTierMap = new TreeMap<>(); - // ------------------------------------------------------------------ - // Section: Private object - // ------------------------------------------------------------------ + private Map commandIcon = new TreeMap<>(); - public class UpgradeTier { - /** - * Constructor UpgradeTier create a new UpgradeTier instance - * and set expressionVariables to default value - * - * @param id - */ - public UpgradeTier(String id) { - this.id = id; - this.expressionVariables = new HashMap<>(); - this.expressionVariables.put("[level]", 0.0); - this.expressionVariables.put("[islandLevel]", 0.0); - this.expressionVariables.put("[numberPlayer]", 0.0); + private Map commandName = new TreeMap<>(); - } + // ------------------------------------------------------------------ + // Section: Private object + // ------------------------------------------------------------------ - // -------------------------------------------------------------- - // Section: Methods - // -------------------------------------------------------------- + public class UpgradeTier { + /** + * Constructor UpgradeTier create a new UpgradeTier instance and set + * expressionVariables to default value + * + * @param id + */ + public UpgradeTier(String id) { + this.id = id; + this.expressionVariables = new TreeMap<>(); + this.expressionVariables.put("[level]", 0.0); + this.expressionVariables.put("[islandLevel]", 0.0); + this.expressionVariables.put("[numberPlayer]", 0.0); - /** - * @return the id - */ - public String getId() { - return id; - } + } - public String getTierName() { - return this.tierName; - } + // -------------------------------------------------------------- + // Section: Methods + // -------------------------------------------------------------- - public void setTierName(String tierName) { - this.tierName = tierName; - } + /** + * @return the id + */ + public String getId() { + return id; + } - /** - * @return the maxLevel - */ - public int getMaxLevel() { - return maxLevel; - } + public String getTierName() { + return this.tierName; + } - /** - * @param maxLevel the maxLevel to set - */ - public void setMaxLevel(int maxLevel) { - this.maxLevel = maxLevel; - } + public void setTierName(String tierName) { + this.tierName = tierName; + } - /** - * @return the level of permission - */ - public Integer getPermissionLevel() { - return this.permissionLevel; - } + /** + * @return the maxLevel + */ + public int getMaxLevel() { + return maxLevel; + } - /** - * @param level of permission to set - */ - public void setPermissionLevel(Integer level) { - this.permissionLevel = level; - } + /** + * @param maxLevel the maxLevel to set + */ + public void setMaxLevel(int maxLevel) { + this.maxLevel = maxLevel; + } + + /** + * @return the level of permission + */ + public Integer getPermissionLevel() { + return this.permissionLevel; + } - /** - * @return the upgradeRange - */ - public Expression getUpgrade() { - return upgrade; - } - - /** - * @param upgrade the upgradeRange to set - */ - public void setUpgrade(Expression upgrade) { - this.upgrade = upgrade; - } - - /** - * @return the islandMinLevel - */ - public Expression getIslandMinLevel() { - return islandMinLevel; - } - - /** - * @param islandMinLevel the islandMinLevel to set - */ - public void setIslandMinLevel(Expression islandMinLevel) { - this.islandMinLevel = islandMinLevel; - } - - /** - * @return the vaultCost - */ - public Expression getVaultCost() { - return vaultCost; - } - - /** - * @param vaultCost the vaultCost to set - */ - public void setVaultCost(Expression vaultCost) { - this.vaultCost = vaultCost; - } - - /** - * Value to set for the math parser - * @param key - * @param value - */ - public void updateExpressionVariable(String key, double value) { - this.expressionVariables.put(key, value); - } - - public Map getExpressionVariable() { - return expressionVariables; - } - - public double calculateUpgrade(double level, double islandLevel, double numberPeople) { - this.updateExpressionVariable("[level]", level); - this.updateExpressionVariable("[islandLevel]", islandLevel); - this.updateExpressionVariable("[numberPlayer]", numberPeople); - return this.getUpgrade().eval(); - } - - public double calculateIslandMinLevel(double level, double islandLevel, double numberPeople) { - this.updateExpressionVariable("[level]", level); - this.updateExpressionVariable("[islandLevel]", islandLevel); - this.updateExpressionVariable("[numberPlayer]", numberPeople); - return this.getIslandMinLevel().eval(); - } - - public double calculateVaultCost(double level, double islandLevel, double numberPeople) { - this.updateExpressionVariable("[level]", level); - this.updateExpressionVariable("[islandLevel]", islandLevel); - this.updateExpressionVariable("[numberPlayer]", numberPeople); - return this.getVaultCost().eval(); - } - - - // ---------------------------------------------------------------------- - // Section: Variables - // ---------------------------------------------------------------------- - - - private final String id; - - private int maxLevel = -1; - - private String tierName; - - private Integer permissionLevel = 0; - - private Expression upgrade; - - private Expression islandMinLevel; - - private Expression vaultCost; - - private Map expressionVariables; - } - - public class CommandUpgradeTier extends UpgradeTier { - - public CommandUpgradeTier(String id) { - super(id); - this.commandList = new ArrayList(); - } - - public void setConsole(Boolean console) { - this.console = console; - } - - public Boolean getConsole() { - return this.console; - } - - public void setCommandList(List commandsList) { - this.commandList = commandsList; - } - - public List getCommandList(String playerName, Island island, int level) { - List formatedList = new ArrayList(this.commandList.size()); - String owner = island.getPlugin().getPlayers().getName(island.getOwner()); - - this.commandList.forEach(cmd -> { - String fcmd = cmd.replace("[player]", playerName) - .replace("[level]", Integer.toString(level)) - .replace("[owner]", owner); - formatedList.add(fcmd); - }); - return formatedList; - } - - private List commandList; - - private Boolean console; - - } - - - // ------------------------------------------------------------------------- - // Section: Arithmetic expressions Parser - // Thanks to Boann on StackOverflow - // Link: https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form - // ------------------------------------------------------------------------- - - @FunctionalInterface - interface Expression { - double eval(); - } - - public static Expression parse(final String str, Map variables) { - return new Object() { - int pos = -1, ch; - - void nextChar() { - ch = (++pos < str.length()) ? str.charAt(pos) : -1; - } - - boolean eat(int charToEat) { - while (ch == ' ') nextChar(); - if (ch == charToEat) { - nextChar(); - return true; - } - return false; - } - - Expression parse() { - nextChar(); - Expression x = parseExpression(); - if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch); - return x; - } - - // Grammar: - // expression = term | expression `+` term | expression `-` term - // term = factor | term `*` factor | term `/` factor - // factor = `+` factor | `-` factor | `(` expression `)` - // | number | functionName factor | factor `^` factor - - Expression parseExpression() { - Expression x = parseTerm(); - for (;;) { - if (eat('+')) { - Expression a = x, b = parseTerm(); - x = (() -> a.eval() + b.eval()); - } else if (eat('-')) { - Expression a = x, b = parseTerm(); - x = (() -> a.eval() - b.eval()); - } else - return x; - } - } - - Expression parseTerm() { - Expression x = parseFactor(); - for (;;) { - if (eat('*')) { - Expression a = x, b = parseFactor(); - x = (() -> a.eval() * b.eval()); - } else if (eat('/')) { - Expression a = x, b = parseFactor(); - x = (() -> a.eval() / b.eval()); - } else - return x; - } - } - - Expression parseFactor() { - if (eat('+')) return parseFactor(); // unary plus - if (eat('-')) { - Expression x = (() -> -parseFactor().eval()); - return x; // unary minus - } - - Expression x; - int startPos = this.pos; - if (eat('(')) { // parentheses - x = parseExpression(); - eat(')'); - } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers - while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); - final Integer innerPos = new Integer(this.pos); - x = (() -> Double.parseDouble(str.substring(startPos, innerPos))); - } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '[' || ch == ']') { // functions - while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '[' || ch == ']') nextChar(); - String func = str.substring(startPos, this.pos); - if (funct.contains(func)) { - Expression a = parseFactor(); - if (func.equals("sqrt")) x = (() -> Math.sqrt(a.eval())); - else if (func.equals("sin")) x = (() -> Math.sin(Math.toRadians(a.eval()))); - else if (func.equals("cos")) x = (() -> Math.cos(Math.toRadians(a.eval()))); - else if (func.equals("tan")) x = (() -> Math.tan(Math.toRadians(a.eval()))); - else throw new RuntimeException("Unknown function: " + func); - } else { - x = (() -> variables.get(func)); - } - } else { - throw new RuntimeException("Unexpected: " + (char)ch); - } - - if (eat('^')) { - Expression a = x, b = parseFactor(); - x = (() -> Math.pow(a.eval(), b.eval())); // exponentiation - } - - return x; - } - }.parse(); - } - - private static final List funct = new ArrayList<>(); - static { - funct.add("sqrt"); - funct.add("sin"); - funct.add("cos"); - funct.add("tan"); - } + /** + * @param level of permission to set + */ + public void setPermissionLevel(Integer level) { + this.permissionLevel = level; + } + + /** + * @return the upgradeRange + */ + public Expression getUpgrade() { + return upgrade; + } + + /** + * @param upgrade the upgradeRange to set + */ + public void setUpgrade(Expression upgrade) { + this.upgrade = upgrade; + } + + /** + * @return the islandMinLevel + */ + public Expression getIslandMinLevel() { + return islandMinLevel; + } + + /** + * @param islandMinLevel the islandMinLevel to set + */ + public void setIslandMinLevel(Expression islandMinLevel) { + this.islandMinLevel = islandMinLevel; + } + + /** + * @return the vaultCost + */ + public Expression getVaultCost() { + return vaultCost; + } + + /** + * @param vaultCost the vaultCost to set + */ + public void setVaultCost(Expression vaultCost) { + this.vaultCost = vaultCost; + } + + /** + * Value to set for the math parser + * + * @param key + * @param value + */ + public void updateExpressionVariable(String key, double value) { + this.expressionVariables.put(key, value); + } + + public Map getExpressionVariable() { + return expressionVariables; + } + + public double calculateUpgrade(double level, double islandLevel, double numberPeople) { + this.updateExpressionVariable("[level]", level); + this.updateExpressionVariable("[islandLevel]", islandLevel); + this.updateExpressionVariable("[numberPlayer]", numberPeople); + return this.getUpgrade().eval(); + } + + public double calculateIslandMinLevel(double level, double islandLevel, double numberPeople) { + this.updateExpressionVariable("[level]", level); + this.updateExpressionVariable("[islandLevel]", islandLevel); + this.updateExpressionVariable("[numberPlayer]", numberPeople); + return this.getIslandMinLevel().eval(); + } + + public double calculateVaultCost(double level, double islandLevel, double numberPeople) { + this.updateExpressionVariable("[level]", level); + this.updateExpressionVariable("[islandLevel]", islandLevel); + this.updateExpressionVariable("[numberPlayer]", numberPeople); + return this.getVaultCost().eval(); + } + + // ---------------------------------------------------------------------- + // Section: Variables + // ---------------------------------------------------------------------- + + private final String id; + + private int maxLevel = -1; + + private String tierName; + + private Integer permissionLevel = 0; + + private Expression upgrade; + + private Expression islandMinLevel; + + private Expression vaultCost; + + private Map expressionVariables; + } + + public class CommandUpgradeTier extends UpgradeTier { + + public CommandUpgradeTier(String id) { + super(id); + this.commandList = new ArrayList(); + } + + public void setConsole(Boolean console) { + this.console = console; + } + + public Boolean getConsole() { + return this.console; + } + + public void setCommandList(List commandsList) { + this.commandList = commandsList; + } + + public List getCommandList(String playerName, Island island, int level) { + List formatedList = new ArrayList(this.commandList.size()); + String owner = island.getPlugin().getPlayers().getName(island.getOwner()); + + this.commandList.forEach(cmd -> { + String fcmd = cmd.replace("[player]", playerName).replace("[level]", Integer.toString(level)) + .replace("[owner]", owner); + formatedList.add(fcmd); + }); + return formatedList; + } + + private List commandList; + + private Boolean console; + + } + + // ------------------------------------------------------------------------- + // Section: Arithmetic expressions Parser + // Thanks to Boann on StackOverflow + // Link: + // https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form + // ------------------------------------------------------------------------- + + @FunctionalInterface + interface Expression { + double eval(); + } + + public static Expression parse(final String str, Map variables) { + return new Object() { + int pos = -1, ch; + + void nextChar() { + ch = (++pos < str.length()) ? str.charAt(pos) : -1; + } + + boolean eat(int charToEat) { + while (ch == ' ') + nextChar(); + if (ch == charToEat) { + nextChar(); + return true; + } + return false; + } + + Expression parse() { + nextChar(); + Expression x = parseExpression(); + if (pos < str.length()) + throw new RuntimeException("Unexpected: " + (char) ch); + return x; + } + + // Grammar: + // expression = term | expression `+` term | expression `-` term + // term = factor | term `*` factor | term `/` factor + // factor = `+` factor | `-` factor | `(` expression `)` + // | number | functionName factor | factor `^` factor + + Expression parseExpression() { + Expression x = parseTerm(); + for (;;) { + if (eat('+')) { + Expression a = x, b = parseTerm(); + x = (() -> a.eval() + b.eval()); + } else if (eat('-')) { + Expression a = x, b = parseTerm(); + x = (() -> a.eval() - b.eval()); + } else + return x; + } + } + + Expression parseTerm() { + Expression x = parseFactor(); + for (;;) { + if (eat('*')) { + Expression a = x, b = parseFactor(); + x = (() -> a.eval() * b.eval()); + } else if (eat('/')) { + Expression a = x, b = parseFactor(); + x = (() -> a.eval() / b.eval()); + } else + return x; + } + } + + Expression parseFactor() { + if (eat('+')) + return parseFactor(); // unary plus + if (eat('-')) { + Expression x = (() -> -parseFactor().eval()); + return x; // unary minus + } + + Expression x; + int startPos = this.pos; + if (eat('(')) { // parentheses + x = parseExpression(); + eat(')'); + } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers + while ((ch >= '0' && ch <= '9') || ch == '.') + nextChar(); + final Integer innerPos = new Integer(this.pos); + x = (() -> Double.parseDouble(str.substring(startPos, innerPos))); + } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '[' || ch == ']') { // functions + while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '[' || ch == ']') + nextChar(); + String func = str.substring(startPos, this.pos); + if (funct.contains(func)) { + Expression a = parseFactor(); + if (func.equals("sqrt")) + x = (() -> Math.sqrt(a.eval())); + else if (func.equals("sin")) + x = (() -> Math.sin(Math.toRadians(a.eval()))); + else if (func.equals("cos")) + x = (() -> Math.cos(Math.toRadians(a.eval()))); + else if (func.equals("tan")) + x = (() -> Math.tan(Math.toRadians(a.eval()))); + else + throw new RuntimeException("Unknown function: " + func); + } else { + x = (() -> variables.get(func)); + } + } else { + throw new RuntimeException("Unexpected: " + (char) ch); + } + + if (eat('^')) { + Expression a = x, b = parseFactor(); + x = (() -> Math.pow(a.eval(), b.eval())); // exponentiation + } + + return x; + } + }.parse(); + } + + private static final List funct = new ArrayList<>(); + static { + funct.add("sqrt"); + funct.add("sin"); + funct.add("cos"); + funct.add("tan"); + } } \ No newline at end of file diff --git a/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java b/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java index b765a97..7464257 100644 --- a/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java +++ b/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java @@ -6,7 +6,6 @@ import world.bentobox.bentobox.api.events.island.IslandDeleteEvent; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.limits.events.LimitsJoinPermCheckEvent; import world.bentobox.upgrades.UpgradesAddon; public class IslandChangeListener implements Listener { @@ -15,11 +14,12 @@ public IslandChangeListener(UpgradesAddon addon) { this.addon = addon; } + /* @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) public void onLimitsJoinPermCheckEvent(LimitsJoinPermCheckEvent e) { // Stop LimitsJoinPermCheck else reset limits upgrades when player join //e.setCancelled(true); - } + }*/ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) public void onIslandDeleteEvent(IslandDeleteEvent e) { diff --git a/src/main/java/world/bentobox/upgrades/ui/Panel.java b/src/main/java/world/bentobox/upgrades/ui/Panel.java index 72f4121..3f875d5 100644 --- a/src/main/java/world/bentobox/upgrades/ui/Panel.java +++ b/src/main/java/world/bentobox/upgrades/ui/Panel.java @@ -11,70 +11,66 @@ import world.bentobox.upgrades.api.Upgrade; public class Panel { - + public Panel(UpgradesAddon addon, Island island) { super(); this.addon = addon; this.island = island; } - + public void showPanel(User user) { int islandLevel = this.addon.getUpgradesManager().getIslandLevel(this.island); - + PanelBuilder pb = new PanelBuilder().name(user.getTranslation("upgrades.ui.upgradepanel.title")); - + this.addon.getAvailableUpgrades().forEach(upgrade -> { upgrade.updateUpgradeValue(user, this.island); - + if (!upgrade.isShowed(user, this.island)) return; - + String ownDescription = upgrade.getOwnDescription(user); List fullDescription = new ArrayList<>(); - - if (ownDescription != null && upgrade.getUpgradeValues(user) != null) + + if (ownDescription != null && upgrade.getUpgradeValues(user) != null) { fullDescription.add(ownDescription); + } fullDescription.addAll(this.getDescription(user, upgrade, islandLevel)); - - pb.item(new PanelItemBuilder() - .name(upgrade.getDisplayName()) - .icon(upgrade.getIcon()) - .description(fullDescription) - .clickHandler(new PanelClick(upgrade, this.island)) - .build()); + + pb.item(new PanelItemBuilder().name(upgrade.getDisplayName()).icon(upgrade.getIcon()) + .description(fullDescription).clickHandler(new PanelClick(upgrade, this.island)).build()); }); - + pb.user(user).build(); } - + private List getDescription(User user, Upgrade upgrade, int islandLevel) { List descrip = new ArrayList<>(); - + if (upgrade.getUpgradeValues(user) == null) descrip.add(user.getTranslation("upgrades.ui.upgradepanel.maxlevel")); else { if (this.addon.isLevelProvided()) { - descrip.add((upgrade.getUpgradeValues(user).getIslandLevel() <= islandLevel ? "§a" : "§c") + - user.getTranslation("upgrades.ui.upgradepanel.islandneed", - "[islandlevel]", Integer.toString(upgrade.getUpgradeValues(user).getIslandLevel()))); + descrip.add((upgrade.getUpgradeValues(user).getIslandLevel() <= islandLevel ? "§a" : "§c") + + user.getTranslation("upgrades.ui.upgradepanel.islandneed", "[islandlevel]", + Integer.toString(upgrade.getUpgradeValues(user).getIslandLevel()))); } - + if (this.addon.isVaultProvided()) { boolean hasMoney = this.addon.getVaultHook().has(user, upgrade.getUpgradeValues(user).getMoneyCost()); - descrip.add((hasMoney ? "§a" : "§c") + - user.getTranslation("upgrades.ui.upgradepanel.moneycost", + descrip.add((hasMoney ? "§a" : "§c") + user.getTranslation("upgrades.ui.upgradepanel.moneycost", "[cost]", Integer.toString(upgrade.getUpgradeValues(user).getMoneyCost()))); } - + if (this.addon.isLevelProvided() && upgrade.getUpgradeValues(user).getIslandLevel() > islandLevel) { descrip.add("§8" + user.getTranslation("upgrades.ui.upgradepanel.tryreloadlevel")); } } - + return descrip; } - + private UpgradesAddon addon; private Island island; - + } diff --git a/src/main/java/world/bentobox/upgrades/upgrades/RangeUpgrade.java b/src/main/java/world/bentobox/upgrades/upgrades/RangeUpgrade.java index afe79b5..9250bd7 100644 --- a/src/main/java/world/bentobox/upgrades/upgrades/RangeUpgrade.java +++ b/src/main/java/world/bentobox/upgrades/upgrades/RangeUpgrade.java @@ -17,7 +17,7 @@ /** * Upgrade Object for range upgrade * - * @author Ikkino + * @author Ikkino, tastybento * */ public class RangeUpgrade extends Upgrade { @@ -142,7 +142,7 @@ private void logError(String name, String perm, String error) { @Override public boolean doUpgrade(User user, Island island) { // Get the new range - long newRange = (long)island.getProtectionRange() + this.getUpgradeValues(user).getUpgradeValue(); + int newRange = island.getProtectionRange() + this.getUpgradeValues(user).getUpgradeValue(); // If newRange is more than the authorized range (Config problem) if (newRange > island.getRange()) { @@ -159,12 +159,12 @@ public boolean doUpgrade(User user, Island island) { // Save oldRange for rangeChange event int oldRange = island.getProtectionRange(); - // Set range - island.setProtectionRange((int) newRange); + // Add range bonus + island.addBonusRange(this.getUpgradesAddon().getDescription().getName(), this.getUpgradeValues(user).getUpgradeValue(), ""); // Launch range change event IslandEvent.builder().island(island).location(island.getCenter()).reason(IslandEvent.Reason.RANGE_CHANGE) - .involvedPlayer(user.getUniqueId()).admin(false).protectionRange((int) newRange, oldRange).build(); + .involvedPlayer(user.getUniqueId()).admin(false).protectionRange(island.getProtectionRange(), oldRange).build(); user.sendMessage("upgrades.ui.upgradepanel.rangeupgradedone", "[rangelevel]", Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index 10eab33..0bb442b 100644 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -1,12 +1,13 @@ name: Upgrades main: world.bentobox.upgrades.UpgradesAddon version: ${version}${build.number} -api-version: '1.16' +api-version: '1.20' repository: "BentoBoxWorld/Upgrades" metrics: true icon: GOLD_INGOT authors: - Guillaume-Lebegue + - tastybento softdepend: BSkyBlock, AcidIsland, CaveBlock, SkyGrid, AOneBlock, Level, Limits \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 381f49c..6e63e74 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -169,4 +169,4 @@ entity-group-icon: group1: CHICKEN_SPAWN_EGG command-icon: - lambda-upgrade: GRASS + lambda-upgrade: GRASS_BLOCK