Skip to content

Commit

Permalink
Adding tier / Level / MaxLevel to description of upgrade resolve #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume-Lebegue committed Jun 8, 2020
1 parent 2b37f05 commit c1876b4
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 11 deletions.
39 changes: 39 additions & 0 deletions src/main/java/world/bentobox/upgrades/UpgradesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ public Map<String, Integer> getRangeUpgradeInfos(int rangeLevel, int islandLevel
return info;
}

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);
return this.addon.getSettings().getMaxRangeUpgrade(name);
}

public Map<String, Integer> getBlockLimitsUpgradeInfos(Material mat, int limitsLevel, int islandLevel, int numberPeople, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getBlockLimitsUpgradeTier(mat, limitsLevel, world);
if (limitsUpgradeTier == null) {
Expand All @@ -239,6 +252,19 @@ public Map<String, Integer> getBlockLimitsUpgradeInfos(Material mat, int limitsL
return info;
}

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);
return this.addon.getSettings().getMaxBlockLimitsUpgrade(mat, name);
}

public Map<String, Integer> getEntityLimitsUpgradeInfos(EntityType ent, int limitsLevel, int islandLevel, int numberPeople, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getEntityLimitsUpgradeTier(ent, limitsLevel, world);
if (limitsUpgradeTier == null) {
Expand All @@ -254,6 +280,19 @@ public Map<String, Integer> getEntityLimitsUpgradeInfos(EntityType ent, int limi
return info;
}

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 int getEntityLimitsUpgradeMax(EntityType ent, World world) {
String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null);
return this.addon.getSettings().getMaxEntityLimitsUpgrade(ent, name);
}

public Map<EntityType, Integer> getEntityLimits(Island island) {
if (!this.addon.isLimitsProvided())
return Collections.emptyMap();
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/world/bentobox/upgrades/api/Upgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Upgrade(Addon addon, String name, String displayName, Material icon) {
this.addon = addon;

this.playerCache = new HashMap<>();
this.ownDescription = new HashMap<>();

Optional<Addon> islandUpgrade = this.addon.getAddonByName("upgrades");
if (!islandUpgrade.isPresent()) {
Expand Down Expand Up @@ -145,6 +146,25 @@ 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
*/
Expand Down Expand Up @@ -194,6 +214,7 @@ public boolean equals(Object obj) {
private Addon addon;
private UpgradesAddon upgradesAddon;
private Map<UUID, UpgradeValues> playerCache;
private Map<UUID, String> ownDescription;

public class UpgradeValues {

Expand Down
95 changes: 85 additions & 10 deletions src/main/java/world/bentobox/upgrades/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ public Settings(UpgradesAddon addon) {
if (this.addon.getConfig().isSet("range-upgrade")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("range-upgrade");
for (String key : Objects.requireNonNull(section).getKeys(false)) {
this.rangeUpgradeTierMap.put(key, addUpgradeSection(section, key));
UpgradeTier newUpgrade = addUpgradeSection(section, key);

if (this.maxRangeUpgrade < newUpgrade.getMaxLevel())
this.maxRangeUpgrade = newUpgrade.getMaxLevel();
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);
this.blockLimitsUpgradeTierMap = this.loadBlockLimits(section, null);
}

if (this.addon.getConfig().isSet("entity-icon")) {
Expand All @@ -55,7 +59,7 @@ else if (mat == null)

if (this.addon.getConfig().isSet("entity-limits-upgrade")) {
ConfigurationSection section = this.addon.getConfig().getConfigurationSection("entity-limits-upgrade");
this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section);
this.entityLimitsUpgradeTierMap = this.loadEntityLimits(section, null);
}

if (this.addon.getConfig().isSet("gamemodes")) {
Expand All @@ -67,34 +71,54 @@ else if (mat == null)
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.customRangeUpgradeTierMap.computeIfAbsent(gameMode, k -> new HashMap<>()).put(key, addUpgradeSection(lowSection, key));
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));
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));
this.customEntityLimitsUpgradeTierMap.computeIfAbsent(gameMode, k -> loadEntityLimits(lowSection, gameMode));
}
}
}

}

private Map<Material, Map<String, UpgradeTier>> loadBlockLimits(ConfigurationSection section) {
private Map<Material, Map<String, UpgradeTier>> loadBlockLimits(ConfigurationSection section, String gameMode) {
Map<Material, Map<String, UpgradeTier>> mats = new EnumMap<>(Material.class);
for (String material: Objects.requireNonNull(section).getKeys(false)) {
Material mat = Material.getMaterial(material);
if (mat != null && mat.isBlock()) {
Map<String, UpgradeTier> tier = new HashMap<>();
ConfigurationSection matSection = section.getConfigurationSection(material);
for (String key : Objects.requireNonNull(matSection).getKeys(false)) {
tier.put(key, addUpgradeSection(matSection, key));
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<Material, Integer> 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 {
Expand All @@ -104,15 +128,31 @@ private Map<Material, Map<String, UpgradeTier>> loadBlockLimits(ConfigurationSec
return mats;
}

private Map<EntityType, Map<String, UpgradeTier>> loadEntityLimits(ConfigurationSection section) {
private Map<EntityType, Map<String, UpgradeTier>> loadEntityLimits(ConfigurationSection section, String gameMode) {
Map<EntityType, Map<String, UpgradeTier>> 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<String, UpgradeTier> tier = new HashMap<>();
ConfigurationSection entSection = section.getConfigurationSection(entity);
for (String key : Objects.requireNonNull(entSection).getKeys(false)) {
tier.put(key, addUpgradeSection(entSection, key));
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<EntityType, Integer> 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 {
Expand All @@ -129,6 +169,7 @@ private Map<EntityType, Map<String, UpgradeTier>> loadEntityLimits(Configuration
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()));

Expand All @@ -153,6 +194,10 @@ public Set<String> getDisabledGameModes() {
return disabledGameModes;
}

public int getMaxRangeUpgrade(String addon) {
return this.customMaxRangeUpgrade.getOrDefault(addon, this.maxRangeUpgrade);
}

public Map<String, UpgradeTier> getDefaultRangeUpgradeTierMap() {
return this.rangeUpgradeTierMap;
}
Expand All @@ -164,6 +209,10 @@ public Map<String, UpgradeTier> 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<Material, Map<String, UpgradeTier>> getDefaultBlockLimitsUpgradeTierMap() {
return this.blockLimitsUpgradeTierMap;
}
Expand All @@ -190,6 +239,10 @@ public Material getEntityIcon(EntityType entity) {
return this.entityIcon.getOrDefault(entity, null);
}

public int getMaxEntityLimitsUpgrade(EntityType entity, String addon) {
return this.customMaxEntityLimitsUpgrade.getOrDefault(addon, this.maxEntityLimitsUpgrade).getOrDefault(entity, 0);
}

public Map<EntityType, Map<String, UpgradeTier>> getDefaultEntityLimitsUpgradeTierMap() {
return this.entityLimitsUpgradeTierMap;
}
Expand Down Expand Up @@ -220,16 +273,28 @@ private EntityType getEntityType(String key) {

private Set<String> disabledGameModes;

private int maxRangeUpgrade = 0;

private Map<String, Integer> customMaxRangeUpgrade = new HashMap<>();

private Map<String, UpgradeTier> rangeUpgradeTierMap = new HashMap<>();

private Map<String, Map<String, UpgradeTier>> customRangeUpgradeTierMap = new HashMap<>();

private Map<Material, Integer> maxBlockLimitsUpgrade = new EnumMap<>(Material.class);

private Map<String, Map<Material, Integer>> customMaxBlockLimitsUpgrade = new HashMap<>();

private Map<Material, Map<String, UpgradeTier>> blockLimitsUpgradeTierMap = new EnumMap<>(Material.class);

private Map<String, Map<Material, Map<String, UpgradeTier>>> customBlockLimitsUpgradeTierMap = new HashMap<>();

private Map<EntityType, Material> entityIcon = new EnumMap<>(EntityType.class);

private Map<EntityType, Integer> maxEntityLimitsUpgrade = new EnumMap<>(EntityType.class);

private Map<String, Map<EntityType, Integer>> customMaxEntityLimitsUpgrade = new HashMap<>();

private Map<EntityType, Map<String, UpgradeTier>> entityLimitsUpgradeTierMap = new EnumMap<>(EntityType.class);

private Map<String, Map<EntityType, Map<String, UpgradeTier>>> customEntityLimitsUpgradeTierMap = new HashMap<>();
Expand Down Expand Up @@ -265,6 +330,14 @@ public String getId() {
return id;
}

public String getTierName() {
return this.tierName;
}

public void setTierName(String tierName) {
this.tierName = tierName;
}

/**
* @return the maxLevel
*/
Expand Down Expand Up @@ -364,6 +437,8 @@ public double calculateVaultCost(double level, double islandLevel, double number
private final String id;

private int maxLevel = -1;

private String tierName;

private Expression upgrade;

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/world/bentobox/upgrades/ui/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ public void showPanel(User user) {

this.addon.getAvailableUpgrades().forEach(upgrade -> {
upgrade.updateUpgradeValue(user, this.island);
String ownDescription = upgrade.getOwnDescription(user);
List<String> fullDescription = new ArrayList<>();

if (ownDescription != null)
fullDescription.add(ownDescription);
fullDescription.addAll(this.getDescription(user, upgrade, islandLevel));

pb.item(new PanelItemBuilder()
.name(upgrade.getDisplayName())
.icon(upgrade.getIcon())
.description(this.getDescription(user, upgrade, islandLevel))
.description(fullDescription)
.clickHandler(new PanelClick(upgrade, this.island))
.build());
});
Expand All @@ -39,6 +45,7 @@ public void showPanel(User user) {

private List<String> getDescription(User user, Upgrade upgrade, int islandLevel) {
List<String> descrip = new ArrayList<>();

if (upgrade.getUpgradeValues(user) == null)
descrip.add(user.getTranslation("upgrades.ui.upgradepanel.maxlevel"));
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void updateUpgradeValue(User user, Island island) {
Map<String, Integer> upgradeInfos = upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeInfos(this.block, upgradeLevel, islandLevel, numberPeople, island.getWorld());
UpgradeValues upgrade;

// Get new description
String description = upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeTierName(this.block, upgradeLevel, island.getWorld()) + " (" + upgradeLevel + "/" +
upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeMax(this.block, island.getWorld()) + ")";
// Set new description
this.setOwnDescription(user, description);

if (upgradeInfos == null)
upgrade = null;
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public void updateUpgradeValue(User user, Island island) {
Map<String, Integer> upgradeInfos = upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeInfos(this.entity, upgradeLevel, islandLevel, numberPeople, island.getWorld());
UpgradeValues upgrade;

// Get new description
String description = upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeTierName(this.entity, upgradeLevel, island.getWorld()) + " (" + upgradeLevel + "/" +
upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeMax(this.entity, island.getWorld()) + ")";
// Set new description
this.setOwnDescription(user, description);

if (upgradeInfos == null)
upgrade = null;
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public void updateUpgradeValue(User user, Island island) {
Map<String, Integer> upgradeInfos = islandAddon.getUpgradesManager().getRangeUpgradeInfos(upgradeLevel, islandLevel, numberPeople, island.getWorld());
UpgradeValues upgrade;

// Get new description
String description = islandAddon.getUpgradesManager().getRangeUpgradeTierName(upgradeLevel, island.getWorld()) + " (" + upgradeLevel + "/" +
islandAddon.getUpgradesManager().getRangeUpgradeMax(island.getWorld()) + ")";
// Set new description
this.setOwnDescription(user, description);

// If null -> no next upgrades
if (upgradeInfos == null)
upgrade = null;
Expand Down

0 comments on commit c1876b4

Please sign in to comment.