Skip to content

Commit

Permalink
Add Working Block Limits upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume-Lebegue committed May 24, 2020
1 parent ebf69f8 commit 246db4c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/main/java/world/bentobox/upgrades/UpgradesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Optional;
import java.util.Set;

import org.bukkit.Material;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -87,9 +86,9 @@ public void onEnable() {
this.vault = vault.get();

this.registerUpgrade(new RangeUpgrade(this));

if (this.isLimitsProvided())
this.registerUpgrade(new LimitsUpgrade(this, Material.HOPPER));
this.getSettings().getMaterialsLimitsUpgrade().forEach(mat -> this.registerUpgrade(new LimitsUpgrade(this, mat)));

this.log("Upgrades addon enabled");
} else {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/world/bentobox/upgrades/UpgradesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,18 @@ public List<Settings.UpgradeTier> getAllRangeUpgradeTiers(World world) {

public Map<Material, List<Settings.UpgradeTier>> getAllLimitsUpgradeTiers(World world) {
String name = this.addon.getPlugin().getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse(null);
if (name == null)
if (name == null) {
return Collections.emptyMap();
}

Map<Material, Map<String, Settings.UpgradeTier>> defaultTiers = this.addon.getSettings().getDefaultLimitsUpgradeTierMap();
Map<Material, Map<String, Settings.UpgradeTier>> customAddonTiers = this.addon.getSettings().getAddonLimitsUpgradeTierMap(name);

Map<Material, List<Settings.UpgradeTier>> tierList = new EnumMap<>(Material.class);

if (customAddonTiers.isEmpty())
if (customAddonTiers.isEmpty()) {
defaultTiers.forEach((mat, tiers) -> tierList.put(mat, new ArrayList<>(tiers.values())));
else {
} else {
customAddonTiers.forEach((mat, tiers) -> {
Set<String> uniqueIDSet = new HashSet<>(tiers.keySet());
if (defaultTiers.containsKey(mat))
Expand All @@ -95,8 +96,9 @@ public Map<Material, List<Settings.UpgradeTier>> getAllLimitsUpgradeTiers(World
defaultTiers.forEach((mat, tiers) -> tierList.putIfAbsent(mat, new ArrayList<>(tiers.values())));
}

if (tierList.isEmpty())
if (tierList.isEmpty()) {
return Collections.emptyMap();
}

tierList.forEach((mat, tiers) -> tiers.sort(Comparator.comparingInt(Settings.UpgradeTier::getMaxLevel)));

Expand Down Expand Up @@ -125,11 +127,13 @@ public Settings.UpgradeTier getRangeUpgradeTier(int rangeLevel, World world) {
public Settings.UpgradeTier getLimitsUpgradeTier(Material mat, int limitsLevel, World world) {
Map<Material, List<Settings.UpgradeTier>> matTierList = this.getAllLimitsUpgradeTiers(world);

if (matTierList.isEmpty())
if (matTierList.isEmpty()) {
return null;
}

if (!matTierList.containsKey(mat))
if (!matTierList.containsKey(mat)) {
return null;
}

List<Settings.UpgradeTier> tierList = matTierList.get(mat);

Expand All @@ -151,22 +155,22 @@ public Map<String, Integer> getRangeUpgradeInfos(int rangeLevel, int islandLevel

info.put("islandMinLevel", (int) rangeUpgradeTier.calculateIslandMinLevel(rangeLevel, islandLevel, numberPeople));
info.put("vaultCost", (int) rangeUpgradeTier.calculateVaultCost(rangeLevel, islandLevel, numberPeople));
info.put("upgradeRange", (int) rangeUpgradeTier.calculateUpgrade(rangeLevel, islandLevel, numberPeople));
info.put("upgrade", (int) rangeUpgradeTier.calculateUpgrade(rangeLevel, islandLevel, numberPeople));

return info;
}

public Map<String, Integer> getLimitsUpgradeInfos(Material mat, int limitsLevel, int islandLevel, int numberPeople, World world) {
Settings.UpgradeTier limitsUpgradeTier = this.getLimitsUpgradeTier(mat, limitsLevel, world);

if (limitsUpgradeTier == null)
if (limitsUpgradeTier == null) {
return null;
}

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

info.put("islandMinLevel", (int) limitsUpgradeTier.calculateIslandMinLevel(limitsLevel, islandLevel, numberPeople));
info.put("vaultCost", (int) limitsUpgradeTier.calculateVaultCost(limitsLevel, islandLevel, numberPeople));
info.put("upgradeRange", (int) limitsUpgradeTier.calculateUpgrade(limitsLevel, islandLevel, numberPeople));
info.put("upgrade", (int) limitsUpgradeTier.calculateUpgrade(limitsLevel, islandLevel, numberPeople));

return info;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/world/bentobox/upgrades/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ public Map<Material, Map<String, UpgradeTier>> getDefaultLimitsUpgradeTierMap()
public Map<Material, Map<String, UpgradeTier>> getAddonLimitsUpgradeTierMap(String addon) {
return this.customLimitsUpgradeTierMap.getOrDefault(addon, Collections.emptyMap());
}

public Set<Material> getMaterialsLimitsUpgrade() {
Set<Material> materials = new HashSet<>();

this.customLimitsUpgradeTierMap.forEach((addon, addonUpgrade) -> {
materials.addAll(addonUpgrade.keySet());
});
materials.addAll(this.limitsUpgradeTierMap.keySet());

return materials;
}

private UpgradesAddon addon;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void setUniqueId(String uniqueId) {
}

public int getUpgradeLevel(String name) {
this.upgradesLevels.putIfAbsent(name, 0);
this.upgradesLevels.putIfAbsent(name, 1);
return this.upgradesLevels.get(name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/upgrades/ui/PanelClick.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PanelClick(UpgradesAddon addon, Upgrade upgrade) {

@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
if (this.upgrade == null)
if (this.upgrade == null || this.upgrade.getUpgradeValues() == null)
return true;

Island island = this.addon.getIslands().getIsland(user.getWorld(), user);
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/world/bentobox/upgrades/upgrades/LimitsUpgrade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package world.bentobox.upgrades.upgrades;

import java.util.Map;

import org.bukkit.Material;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;


import world.bentobox.limits.listeners.BlockLimitsListener;
import world.bentobox.upgrades.UpgradesAddon;
import world.bentobox.upgrades.api.Upgrade;
import world.bentobox.upgrades.dataobjects.UpgradesData;

public class LimitsUpgrade extends Upgrade {

public LimitsUpgrade(UpgradesAddon addon, Material block) {
super(addon, "LimitsUpgrade-" + block.toString(), block.toString() + " limits Upgrade", block);
this.block = block;
}

@Override
public void updateUpgradeValue(User user, Island island) {
UpgradesAddon upgradeAddon = this.getUpgradesAddon();
UpgradesData islandData = upgradeAddon.getUpgradesLevels(island.getUniqueId());
int upgradeLevel = islandData.getUpgradeLevel(getName());
int numberPeople = island.getMemberSet().size();
int islandLevel;

if (upgradeAddon.isLevelProvided())
islandLevel = upgradeAddon.getUpgradesManager().getIslandLevel(island);
else
islandLevel = 0;

Map<String, Integer> upgradeInfos = upgradeAddon.getUpgradesManager().getLimitsUpgradeInfos(this.block, upgradeLevel, islandLevel, numberPeople, island.getWorld());
UpgradeValues upgrade;

if (upgradeInfos == null)
upgrade = null;
else
upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgrade"));

this.setUpgradeValues(upgrade);

String newDisplayName;

if (upgrade == null) {
newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.nolimitsupgrade",
"[block]", this.block.toString());
} else {
newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.limitsupgrade",
"[block]", this.block.toString(), "[level]", Integer.toString(upgrade.getUpgradeValue()));
}

this.setDisplayName(newDisplayName);
}

@Override
public boolean doUpgrade(User user, Island island) {
UpgradesAddon islandAddon = this.getUpgradesAddon();

if (!islandAddon.isLimitsProvided())
return false;

BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener();
Map<Material, Integer> materialLimits = bLListener.getMaterialLimits(island.getWorld(), island.getUniqueId());

if (!materialLimits.containsKey(this.block) || materialLimits.get(this.block) == -1) {
this.getUpgradesAddon().logWarning("User tried to upgrade " + this.block.toString() + " limits but it has no limits. This is probably a configuration problem.");
user.sendMessage("upgrades.error.increasenolimits");
return false;
}

if (!super.doUpgrade(user, island))
return false;

int oldCount = materialLimits.get(this.block);
int newCount = (int) (oldCount + this.getUpgradeValues().getUpgradeValue());

bLListener.getIsland(island.getUniqueId()).setBlockLimit(this.block, newCount);

user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone",
"[block]", this.block.toString(), "[level]", Integer.toString(this.getUpgradeValues().getUpgradeValue()));

return true;
}

private Material block;

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void updateUpgradeValue(User user, Island island) {
if (upgradeInfos == null)
upgrade = null;
else
upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgradeRange"));
upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgrade"));

// Update the upgrade values
this.setUpgradeValues(upgrade);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ limits-upgrade:
# List any tiers that you want to add
# You can overwrite a tier by using the same name
gamemodes:
BSkyblock:
BSkyBlock:
range-upgrade:
tier3:
max-level: 15
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ upgrades:
rangeupgrade: "Range upgrade of [rangelevel]"
norangeupgrade: "&7Range upgrade"
rangeupgradedone: "&aYour island range was upgraded of [rangelevel] blocks"
limitsupgrade: "[block] limits upgrade of [level]"
nolimitsupgrade: "&7[block] limits upgrade"
limitsupgradedone: "&aYour island [block] limits was upgraded of [level] blocks"
maxlevel: "Max level reached"
islandneed: "Island Min Level: [islandlevel]"
moneycost: "Money cost: [cost]"
tryreloadlevel: "Don't forget to update your island level with the level command"
error:
rangeovermax: "&cYou tried to upgrade you island range over the max? You should talk about this to an administrator"
increasenolimits: "&cYou can't increase the limits of something not limited. You should talk about this to an administrator"
costwithdraw: "&cCouldn't withdraw money. You should talk about this to an administrator"

0 comments on commit 246db4c

Please sign in to comment.