diff --git a/src/main/java/world/bentobox/upgrades/UpgradesAddon.java b/src/main/java/world/bentobox/upgrades/UpgradesAddon.java index b1907c9..dc23006 100644 --- a/src/main/java/world/bentobox/upgrades/UpgradesAddon.java +++ b/src/main/java/world/bentobox/upgrades/UpgradesAddon.java @@ -18,217 +18,216 @@ import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.level.Level; +import world.bentobox.limits.Limits; import world.bentobox.upgrades.api.Upgrade; import world.bentobox.upgrades.command.PlayerUpgradeCommand; import world.bentobox.upgrades.config.Settings; import world.bentobox.upgrades.dataobjects.UpgradesData; import world.bentobox.upgrades.listeners.IslandChangeListener; -import world.bentobox.upgrades.listeners.JoinPermCheckListener; import world.bentobox.upgrades.upgrades.BlockLimitsUpgrade; import world.bentobox.upgrades.upgrades.CommandUpgrade; import world.bentobox.upgrades.upgrades.EntityGroupLimitsUpgrade; import world.bentobox.upgrades.upgrades.EntityLimitsUpgrade; import world.bentobox.upgrades.upgrades.RangeUpgrade; -import world.bentobox.level.Level; -import world.bentobox.limits.Limits; public class UpgradesAddon extends Addon { - @Override - public void onLoad() { - super.onLoad(); - this.saveDefaultConfig(); - this.settings = new Settings(this); - } - - @Override - public void onEnable() { - if (this.getState().equals(State.DISABLED)) { - this.logWarning("Upgrades Addon is not available or disabled!"); - return; - } - - List hookedGameModes = new ArrayList<>(); - - getPlugin().getAddonsManager().getGameModeAddons().stream() - .filter(g -> !settings.getDisabledGameModes().contains(g.getDescription().getName())) - .forEach(g -> { - if (g.getPlayerCommand().isPresent()) { - - new PlayerUpgradeCommand(this, g.getPlayerCommand().get()); - - UpgradesAddon.UPGRADES_RANK_RIGHT.addGameModeAddon(g); - - this.hooked = true; - hookedGameModes.add(g.getDescription().getName()); - } - }); - - if (this.hooked) { - this.upgradesManager = new UpgradesManager(this); - this.upgradesManager.addGameModes(hookedGameModes); - - this.upgrade = new HashSet<>(); - - this.database = new Database<>(this, UpgradesData.class); - this.upgradesCache = new HashMap<>(); - - Optional level = this.getAddonByName("Level"); - - if (!level.isPresent()) { - this.logWarning("Level addon not found so Upgrades won't look for Island Level"); - this.levelAddon = null; - } else - this.levelAddon = (Level) level.get(); - - Optional limits = this.getAddonByName("Limits"); - - if (!limits.isPresent()) { - this.logWarning("Limits addon not found so Island Upgrade won't look for IslandLevel"); - this.limitsAddon = null; - } else - this.limitsAddon = (Limits) limits.get(); - - Optional vault = this.getPlugin().getVault(); - if (!vault.isPresent()) { - this.logWarning("Vault plugin not found so Upgrades won't look for money"); - this.vault = null; - } else - this.vault = vault.get(); - - if (this.isLimitsProvided()) { - this.getSettings().getEntityLimitsUpgrade().forEach(ent -> this.registerUpgrade(new EntityLimitsUpgrade(this, ent))); - this.getSettings().getEntityGroupLimitsUpgrade().forEach(group -> this.registerUpgrade(new EntityGroupLimitsUpgrade(this, group))); - this.getSettings().getMaterialsLimitsUpgrade().forEach(mat -> this.registerUpgrade(new BlockLimitsUpgrade(this, mat))); - } - - this.getSettings().getCommandUpgrade().forEach(cmd -> this.registerUpgrade(new CommandUpgrade(this, cmd, this.getSettings().getCommandIcon(cmd)))); - - if (this.getSettings().getHasRangeUpgrade()) - this.registerUpgrade(new RangeUpgrade(this)); - - this.registerListener(new IslandChangeListener(this)); - - if (this.isLimitsProvided()) - this.registerListener(new JoinPermCheckListener(this)); - - getPlugin().getFlagsManager().registerFlag(UpgradesAddon.UPGRADES_RANK_RIGHT); - - this.log("Upgrades addon enabled"); - } else { - this.logError("Upgrades addon could not hook into any GameMode and therefore will not do anything"); - this.setState(State.DISABLED); - } - } - - @Override - public void onDisable() { - if (this.upgradesCache != null) - this.upgradesCache.values().forEach(this.database::saveObjectAsync); - } - - @Override - public void onReload() { - super.onReload(); - - if (this.hooked) - this.settings = new Settings(this); - this.log("Island upgrade addon reloaded"); - } - - /** - * @return the settings - */ - public Settings getSettings() { - return settings; - } - - /** - * @return the islandUpgradesManager - */ - public UpgradesManager getUpgradesManager() { - return upgradesManager; - } - - public Database getDatabase() { - return this.database; - } - - public UpgradesData getUpgradesLevels(@NonNull String targetIsland) { - UpgradesData upgradesData = this.upgradesCache.get(targetIsland); - if (upgradesData != null) - return upgradesData; - UpgradesData data = this.database.objectExists(targetIsland) ? - Optional.ofNullable(this.database.loadObject(targetIsland)).orElse(new UpgradesData(targetIsland)) : - new UpgradesData(targetIsland); - this.upgradesCache.put(targetIsland, data); - return data; - } - - public void uncacheIsland(@Nullable String targetIsland, boolean save) { - UpgradesData data = this.upgradesCache.remove(targetIsland); - if (data == null) - return; - if (save) - this.database.saveObjectAsync(data); - } - - public Level getLevelAddon() { - return this.levelAddon; - } - - public Limits getLimitsAddon() { - return this.limitsAddon; - } - - public VaultHook getVaultHook() { - return this.vault; - } - - public boolean isLevelProvided() { - return this.levelAddon != null; - } - - public boolean isLimitsProvided() { - return this.limitsAddon != null; - } - - public boolean isVaultProvided() { - return this.vault != null; - } - - public Set getAvailableUpgrades() { - return this.upgrade; - } - - public void registerUpgrade(Upgrade upgrade) { - this.upgrade.add(upgrade); - } - - private Settings settings; - - private boolean hooked; - - private UpgradesManager upgradesManager; - - private Set upgrade; - - private Database database; - - private Map upgradesCache; - - private Level levelAddon; - - private Limits limitsAddon; - - private VaultHook vault; - - public final static Flag UPGRADES_RANK_RIGHT = - new Flag.Builder("UPGRADES_RANK_RIGHT", Material.GOLD_INGOT) - .type(Flag.Type.PROTECTION) - .mode(Flag.Mode.BASIC) - .clickHandler(new CycleClick("UPGRADES_RANK_RIGHT", RanksManager.MEMBER_RANK, RanksManager.OWNER_RANK)) - .defaultRank(RanksManager.MEMBER_RANK) - .build(); - + @Override + public void onLoad() { + super.onLoad(); + this.saveDefaultConfig(); + this.settings = new Settings(this); + } + + @Override + public void onEnable() { + if (this.getState().equals(State.DISABLED)) { + this.logWarning("Upgrades Addon is not available or disabled!"); + return; + } + + List hookedGameModes = new ArrayList<>(); + + getPlugin().getAddonsManager().getGameModeAddons().stream() + .filter(g -> !settings.getDisabledGameModes().contains(g.getDescription().getName())) + .forEach(g -> { + if (g.getPlayerCommand().isPresent()) { + + new PlayerUpgradeCommand(this, g.getPlayerCommand().get()); + + UpgradesAddon.UPGRADES_RANK_RIGHT.addGameModeAddon(g); + + this.hooked = true; + hookedGameModes.add(g.getDescription().getName()); + } + }); + + if (this.hooked) { + this.upgradesManager = new UpgradesManager(this); + this.upgradesManager.addGameModes(hookedGameModes); + + this.upgrade = new HashSet<>(); + + this.database = new Database<>(this, UpgradesData.class); + this.upgradesCache = new HashMap<>(); + + Optional level = this.getAddonByName("Level"); + + if (!level.isPresent()) { + this.logWarning("Level addon not found so Upgrades won't look for Island Level"); + this.levelAddon = null; + } else + this.levelAddon = (Level) level.get(); + + Optional limits = this.getAddonByName("Limits"); + + if (!limits.isPresent()) { + this.logWarning("Limits addon not found so Island Upgrade won't look for IslandLevel"); + this.limitsAddon = null; + } else + this.limitsAddon = (Limits) limits.get(); + + Optional vault = this.getPlugin().getVault(); + if (!vault.isPresent()) { + this.logWarning("Vault plugin not found so Upgrades won't look for money"); + this.vault = null; + } else + this.vault = vault.get(); + + if (this.isLimitsProvided()) { + this.getSettings().getEntityLimitsUpgrade().forEach(ent -> this.registerUpgrade(new EntityLimitsUpgrade(this, ent))); + this.getSettings().getEntityGroupLimitsUpgrade().forEach(group -> this.registerUpgrade(new EntityGroupLimitsUpgrade(this, group))); + this.getSettings().getMaterialsLimitsUpgrade().forEach(mat -> this.registerUpgrade(new BlockLimitsUpgrade(this, mat))); + } + + this.getSettings().getCommandUpgrade().forEach(cmd -> this.registerUpgrade(new CommandUpgrade(this, cmd, this.getSettings().getCommandIcon(cmd)))); + + if (this.getSettings().getHasRangeUpgrade()) + this.registerUpgrade(new RangeUpgrade(this)); + + this.registerListener(new IslandChangeListener(this)); + + //if (this.isLimitsProvided()) + //this.registerListener(new JoinPermCheckListener(this)); + + getPlugin().getFlagsManager().registerFlag(UpgradesAddon.UPGRADES_RANK_RIGHT); + + this.log("Upgrades addon enabled"); + } else { + this.logError("Upgrades addon could not hook into any GameMode and therefore will not do anything"); + this.setState(State.DISABLED); + } + } + + @Override + public void onDisable() { + if (this.upgradesCache != null) + this.upgradesCache.values().forEach(this.database::saveObjectAsync); + } + + @Override + public void onReload() { + super.onReload(); + + if (this.hooked) + this.settings = new Settings(this); + this.log("Island upgrade addon reloaded"); + } + + /** + * @return the settings + */ + public Settings getSettings() { + return settings; + } + + /** + * @return the islandUpgradesManager + */ + public UpgradesManager getUpgradesManager() { + return upgradesManager; + } + + public Database getDatabase() { + return this.database; + } + + public UpgradesData getUpgradesLevels(@NonNull String targetIsland) { + UpgradesData upgradesData = this.upgradesCache.get(targetIsland); + if (upgradesData != null) + return upgradesData; + UpgradesData data = this.database.objectExists(targetIsland) ? + Optional.ofNullable(this.database.loadObject(targetIsland)).orElse(new UpgradesData(targetIsland)) : + new UpgradesData(targetIsland); + this.upgradesCache.put(targetIsland, data); + return data; + } + + public void uncacheIsland(@Nullable String targetIsland, boolean save) { + UpgradesData data = this.upgradesCache.remove(targetIsland); + if (data == null) + return; + if (save) + this.database.saveObjectAsync(data); + } + + public Level getLevelAddon() { + return this.levelAddon; + } + + public Limits getLimitsAddon() { + return this.limitsAddon; + } + + public VaultHook getVaultHook() { + return this.vault; + } + + public boolean isLevelProvided() { + return this.levelAddon != null; + } + + public boolean isLimitsProvided() { + return this.limitsAddon != null; + } + + public boolean isVaultProvided() { + return this.vault != null; + } + + public Set getAvailableUpgrades() { + return this.upgrade; + } + + public void registerUpgrade(Upgrade upgrade) { + this.upgrade.add(upgrade); + } + + private Settings settings; + + private boolean hooked; + + private UpgradesManager upgradesManager; + + private Set upgrade; + + private Database database; + + private Map upgradesCache; + + private Level levelAddon; + + private Limits limitsAddon; + + private VaultHook vault; + + public final static Flag UPGRADES_RANK_RIGHT = + new Flag.Builder("UPGRADES_RANK_RIGHT", Material.GOLD_INGOT) + .type(Flag.Type.PROTECTION) + .mode(Flag.Mode.BASIC) + .clickHandler(new CycleClick("UPGRADES_RANK_RIGHT", RanksManager.MEMBER_RANK, RanksManager.OWNER_RANK)) + .defaultRank(RanksManager.MEMBER_RANK) + .build(); + } \ 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 7b1f94c..b765a97 100644 --- a/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java +++ b/src/main/java/world/bentobox/upgrades/listeners/IslandChangeListener.java @@ -4,30 +4,30 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeleteEvent; +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 { - - 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) { - Island island = e.getIsland(); - this.addon.uncacheIsland(island.getUniqueId(), false); - this.addon.getDatabase().deleteID(island.getUniqueId()); - } - - private UpgradesAddon addon; + + 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) { + Island island = e.getIsland(); + this.addon.uncacheIsland(island.getUniqueId(), false); + this.addon.getDatabase().deleteID(island.getUniqueId()); + } + + private UpgradesAddon addon; } diff --git a/src/main/java/world/bentobox/upgrades/upgrades/BlockLimitsUpgrade.java b/src/main/java/world/bentobox/upgrades/upgrades/BlockLimitsUpgrade.java index 99ebbac..ae625c1 100644 --- a/src/main/java/world/bentobox/upgrades/upgrades/BlockLimitsUpgrade.java +++ b/src/main/java/world/bentobox/upgrades/upgrades/BlockLimitsUpgrade.java @@ -18,147 +18,133 @@ public class BlockLimitsUpgrade extends Upgrade { - public BlockLimitsUpgrade(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 = upgradeAddon.getUpgradesManager().getIslandLevel(island); - - Map upgradeInfos = upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeInfos(this.block, - upgradeLevel, islandLevel, numberPeople, island.getWorld()); - UpgradeValues upgrade; - - if (upgradeInfos == null) { - upgrade = null; - } else { - // Get new description - String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", - "[name]", upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeTierName(this.block, upgradeLevel, island.getWorld()), - "[current]", Integer.toString(upgradeLevel), - "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeMax(this.block, island.getWorld()))); - - // Set new description - this.setOwnDescription(user, description); - - upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), - upgradeInfos.get("upgrade")); - } - - this.setUpgradeValues(user, 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 isShowed(User user, Island island) { - // Get the addon - UpgradesAddon upgradesAddon = this.getUpgradesAddon(); - // Get the data from upgrades - UpgradesData islandData = upgradesAddon.getUpgradesLevels(island.getUniqueId()); - // Get level of the upgrade - int upgradeLevel = islandData.getUpgradeLevel(this.getName()); - // Permission level required - int permissionLevel = upgradesAddon.getUpgradesManager().getBlockLimitsPermissionLevel(this.block, upgradeLevel, - island.getWorld()); - - // If default permission, then true - if (permissionLevel == 0) - return true; - - Player player = user.getPlayer(); - String gamemode = island.getGameMode(); - String permissionStart = gamemode + ".upgrades." + this.getName() + "."; - permissionStart = permissionStart.toLowerCase(); - - // For each permission of the player - for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { - - // If permission is the one we search - if (!perms.getValue() || !perms.getPermission().startsWith(permissionStart)) - continue; - - if (perms.getPermission().contains(permissionStart + "*")) { - this.logError(player.getName(), perms.getPermission(), "Wildcards are not allowed."); - return false; - } - - String[] split = perms.getPermission().split("\\."); - if (split.length != 4) { - logError(player.getName(), perms.getPermission(), "format must be '" + permissionStart + "LEVEL'"); - return false; - } - - if (!NumberUtils.isDigits(split[3])) { - logError(player.getName(), perms.getPermission(), "The last part must be a number"); - return false; - } - - if (permissionLevel <= Integer.parseInt(split[3])) - return true; - } - - return false; - } - - private void logError(String name, String perm, String error) { - this.getUpgradesAddon() - .logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); - } - - @Override - public boolean doUpgrade(User user, Island island) { - UpgradesAddon islandAddon = this.getUpgradesAddon(); - - if (!islandAddon.isLimitsProvided()) - return false; - - BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener(); - IslandBlockCount isb = bLListener.getIsland(island.getUniqueId()); - Map 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 (isb == null) { - user.sendMessage("upgrades.error.placeblock"); - return false; - } - - if (!super.doUpgrade(user, island)) - return false; - - int oldCount = materialLimits.get(this.block); - int newCount = (int) (oldCount + this.getUpgradeValues(user).getUpgradeValue()); - - isb.setBlockLimit(this.block, newCount); - - user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone", "[block]", this.block.toString(), "[level]", - Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); - - return true; - } - - private Material block; + public BlockLimitsUpgrade(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 = upgradeAddon.getUpgradesManager().getIslandLevel(island); + + Map upgradeInfos = upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeInfos(this.block, + upgradeLevel, islandLevel, numberPeople, island.getWorld()); + UpgradeValues upgrade; + + if (upgradeInfos == null) { + upgrade = null; + } else { + // Get new description + String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", + "[name]", upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeTierName(this.block, upgradeLevel, island.getWorld()), + "[current]", Integer.toString(upgradeLevel), + "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getBlockLimitsUpgradeMax(this.block, island.getWorld()))); + + // Set new description + this.setOwnDescription(user, description); + + upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), + upgradeInfos.get("upgrade")); + } + + this.setUpgradeValues(user, 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 isShowed(User user, Island island) { + // Get the addon + UpgradesAddon upgradesAddon = this.getUpgradesAddon(); + // Get the data from upgrades + UpgradesData islandData = upgradesAddon.getUpgradesLevels(island.getUniqueId()); + // Get level of the upgrade + int upgradeLevel = islandData.getUpgradeLevel(this.getName()); + // Permission level required + int permissionLevel = upgradesAddon.getUpgradesManager().getBlockLimitsPermissionLevel(this.block, upgradeLevel, + island.getWorld()); + + // If default permission, then true + if (permissionLevel == 0) + return true; + + Player player = user.getPlayer(); + String gamemode = island.getGameMode(); + String permissionStart = gamemode + ".upgrades." + this.getName() + "."; + permissionStart = permissionStart.toLowerCase(); + + // For each permission of the player + for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { + + // If permission is the one we search + if (!perms.getValue() || !perms.getPermission().startsWith(permissionStart)) + continue; + + if (perms.getPermission().contains(permissionStart + "*")) { + this.logError(player.getName(), perms.getPermission(), "Wildcards are not allowed."); + return false; + } + + String[] split = perms.getPermission().split("\\."); + if (split.length != 4) { + logError(player.getName(), perms.getPermission(), "format must be '" + permissionStart + "LEVEL'"); + return false; + } + + if (!NumberUtils.isDigits(split[3])) { + logError(player.getName(), perms.getPermission(), "The last part must be a number"); + return false; + } + + if (permissionLevel <= Integer.parseInt(split[3])) + return true; + } + + return false; + } + + private void logError(String name, String perm, String error) { + this.getUpgradesAddon() + .logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); + } + + @Override + public boolean doUpgrade(User user, Island island) { + UpgradesAddon islandAddon = this.getUpgradesAddon(); + + if (!islandAddon.isLimitsProvided()) + return false; + + BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener(); + IslandBlockCount isb = bLListener.getIsland(island.getUniqueId()); + + if (!super.doUpgrade(user, island)) + return false; + + int oldCount = isb.getBlockLimitsOffset().getOrDefault(block, 0); + int newCount = oldCount + this.getUpgradeValues(user).getUpgradeValue(); + isb.setBlockLimitsOffset(block, newCount); + + user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone", "[block]", this.block.toString(), "[level]", + Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); + + return true; + } + + private Material block; } diff --git a/src/main/java/world/bentobox/upgrades/upgrades/EntityGroupLimitsUpgrade.java b/src/main/java/world/bentobox/upgrades/upgrades/EntityGroupLimitsUpgrade.java index 53735a7..c3eb008 100644 --- a/src/main/java/world/bentobox/upgrades/upgrades/EntityGroupLimitsUpgrade.java +++ b/src/main/java/world/bentobox/upgrades/upgrades/EntityGroupLimitsUpgrade.java @@ -38,30 +38,30 @@ public void updateUpgradeValue(User user, Island island) { UpgradeValues upgrade; if (upgradeInfos == null) { - upgrade = null; - } else { - // Get new description - String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", - "[name]", upgradeAddon.getUpgradesManager().getEntityGroupLimitsUpgradeTierName(this.group, upgradeLevel, island.getWorld()), - "[current]", Integer.toString(upgradeLevel), - "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getEntityGroupLimitsUpgradeMax(this.group, island.getWorld()))); - - // Set new description - this.setOwnDescription(user, description); + upgrade = null; + } else { + // Get new description + String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", + "[name]", upgradeAddon.getUpgradesManager().getEntityGroupLimitsUpgradeTierName(this.group, upgradeLevel, island.getWorld()), + "[current]", Integer.toString(upgradeLevel), + "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getEntityGroupLimitsUpgradeMax(this.group, island.getWorld()))); + + // Set new description + this.setOwnDescription(user, description); upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgrade")); - } - + } + this.setUpgradeValues(user, upgrade); String newDisplayName; if (upgrade == null) { newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.nolimitsupgrade", - "[block]", this.group); + "[block]", this.group); } else { newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.limitsupgrade", - "[block]", this.group, "[level]", Integer.toString(upgrade.getUpgradeValue())); + "[block]", this.group, "[level]", Integer.toString(upgrade.getUpgradeValue())); } this.setDisplayName(newDisplayName); @@ -90,7 +90,7 @@ public boolean isShowed(User user, Island island) { // For each permission of the player for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { - // If permission is the one we search + // If permission is the one we search if (!perms.getValue() || !perms.getPermission().startsWith(permissionStart)) continue; @@ -130,29 +130,16 @@ public boolean doUpgrade(User user, Island island) { BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener(); IslandBlockCount isb = bLListener.getIsland(island.getUniqueId()); - Map entityGroupLimits = islandAddon.getUpgradesManager().getEntityGroupLimits(island); - - if (!entityGroupLimits.containsKey(this.group) || entityGroupLimits.get(this.group) == -1) { - this.getUpgradesAddon().logWarning("User tried to upgrade " + this.group + " limits but it has no limits. This is probably a configuration problem."); - user.sendMessage("upgrades.error.increasenolimits"); - return false; - } - - if (isb == null) { - user.sendMessage("upgrades.error.placeblock"); - return false; - } - - if (!super.doUpgrade(user, island)) return false; - int newCount = (int) (entityGroupLimits.get(this.group) + this.getUpgradeValues(user).getUpgradeValue()); - - isb.setEntityGroupLimit(this.group, newCount); + int oldCount = isb.getEntityGroupLimitsOffset().getOrDefault(this.group, 0); + int newCount = oldCount + this.getUpgradeValues(user).getUpgradeValue(); + + isb.setEntityGroupLimitsOffset(this.group, newCount); user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone", - "[block]", this.group, "[level]", Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); + "[block]", this.group, "[level]", Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); return true; } diff --git a/src/main/java/world/bentobox/upgrades/upgrades/EntityLimitsUpgrade.java b/src/main/java/world/bentobox/upgrades/upgrades/EntityLimitsUpgrade.java index bdf025f..79024b8 100644 --- a/src/main/java/world/bentobox/upgrades/upgrades/EntityLimitsUpgrade.java +++ b/src/main/java/world/bentobox/upgrades/upgrades/EntityLimitsUpgrade.java @@ -17,151 +17,137 @@ public class EntityLimitsUpgrade extends Upgrade { - public EntityLimitsUpgrade(UpgradesAddon addon, EntityType entity) { - super(addon, "LimitsUpgrade-" + entity.toString(), entity.toString() + " limits Upgrade", - addon.getSettings().getEntityIcon(entity)); - this.entity = entity; - } - - @Override - public void updateUpgradeValue(User user, Island island) { - UpgradesAddon upgradeAddon = this.getUpgradesAddon(); - UpgradesData islandData = upgradeAddon.getUpgradesLevels(island.getUniqueId()); - int upgradeLevel = islandData.getUpgradeLevel(this.getName()); - int numberPeople = island.getMemberSet().size(); - int islandLevel; - - if (upgradeAddon.isLevelProvided()) - islandLevel = upgradeAddon.getUpgradesManager().getIslandLevel(island); - else - islandLevel = 0; - - Map upgradeInfos = upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeInfos(this.entity, - upgradeLevel, islandLevel, numberPeople, island.getWorld()); - UpgradeValues upgrade; - - if (upgradeInfos == null) { - upgrade = null; - } else { - // Get new description - String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", - "[name]", upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeTierName(this.entity, upgradeLevel, island.getWorld()), - "[current]", Integer.toString(upgradeLevel), - "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeMax(this.entity, island.getWorld()))); - - // Set new description - this.setOwnDescription(user, description); - - upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgrade")); - } - - this.setUpgradeValues(user, upgrade); - - String newDisplayName; - - if (upgrade == null) { - newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.nolimitsupgrade", "[block]", - this.entity.toString()); - } else { - newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.limitsupgrade", "[block]", - this.entity.toString(), "[level]", Integer.toString(upgrade.getUpgradeValue())); - } - - this.setDisplayName(newDisplayName); - } - - @Override - public boolean isShowed(User user, Island island) { - // Get the addon - UpgradesAddon upgradesAddon = this.getUpgradesAddon(); - // Get the data from upgrades - UpgradesData islandData = upgradesAddon.getUpgradesLevels(island.getUniqueId()); - // Get level of the upgrade - int upgradeLevel = islandData.getUpgradeLevel(this.getName()); - // Permission level required - int permissionLevel = upgradesAddon.getUpgradesManager().getEntityLimitsPermissionLevel(this.entity, - upgradeLevel, island.getWorld()); - - // If default permission, then true - if (permissionLevel == 0) - return true; - - Player player = user.getPlayer(); - String gamemode = island.getGameMode(); - String permissionStart = gamemode + ".upgrades." + this.getName() + "."; - permissionStart = permissionStart.toLowerCase(); - - // For each permission of the player - for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { - - // If permission is the one we search - if (!perms.getValue() || !perms.getPermission().startsWith(permissionStart)) - continue; - - if (perms.getPermission().contains(permissionStart + "*")) { - this.logError(player.getName(), perms.getPermission(), "Wildcards are not allowed."); - return false; - } - - String[] split = perms.getPermission().split("\\."); - if (split.length != 4) { - logError(player.getName(), perms.getPermission(), "format must be '" + permissionStart + "LEVEL'"); - return false; - } - - if (!NumberUtils.isDigits(split[3])) { - logError(player.getName(), perms.getPermission(), "The last part must be a number"); - return false; - } - - if (permissionLevel <= Integer.parseInt(split[3])) - return true; - } - - return false; - } - - private void logError(String name, String perm, String error) { - this.getUpgradesAddon() - .logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); - } - - @Override - public boolean doUpgrade(User user, Island island) { - UpgradesAddon islandAddon = this.getUpgradesAddon(); - - if (!islandAddon.isLimitsProvided()) - return false; - - BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener(); - IslandBlockCount isb = bLListener.getIsland(island.getUniqueId()); - Map entityLimits = islandAddon.getUpgradesManager().getEntityLimits(island); - - if (!entityLimits.containsKey(this.entity) || entityLimits.get(this.entity) == -1) { - this.getUpgradesAddon().logWarning("User tried to upgrade " + this.entity.toString() - + " limits but it has no limits. This is probably a configuration problem."); - user.sendMessage("upgrades.error.increasenolimits"); - return false; - } - - if (isb == null) { - user.sendMessage("upgrades.error.placeblock"); - return false; - } - - if (!super.doUpgrade(user, island)) - return false; - - int newCount = (int) (entityLimits.get(this.entity) + this.getUpgradeValues(user).getUpgradeValue()); - - isb.setEntityLimit(this.entity, newCount); - - user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone", "[block]", this.entity.toString(), "[level]", - Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); - - return true; - } - - private EntityType entity; + public EntityLimitsUpgrade(UpgradesAddon addon, EntityType entity) { + super(addon, "LimitsUpgrade-" + entity.toString(), entity.toString() + " limits Upgrade", + addon.getSettings().getEntityIcon(entity)); + this.entity = entity; + } + + @Override + public void updateUpgradeValue(User user, Island island) { + UpgradesAddon upgradeAddon = this.getUpgradesAddon(); + UpgradesData islandData = upgradeAddon.getUpgradesLevels(island.getUniqueId()); + int upgradeLevel = islandData.getUpgradeLevel(this.getName()); + int numberPeople = island.getMemberSet().size(); + int islandLevel; + + if (upgradeAddon.isLevelProvided()) + islandLevel = upgradeAddon.getUpgradesManager().getIslandLevel(island); + else + islandLevel = 0; + + Map upgradeInfos = upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeInfos(this.entity, + upgradeLevel, islandLevel, numberPeople, island.getWorld()); + UpgradeValues upgrade; + + if (upgradeInfos == null) { + upgrade = null; + } else { + // Get new description + String description = user.getTranslation("upgrades.ui.upgradepanel.tiernameandlevel", + "[name]", upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeTierName(this.entity, upgradeLevel, island.getWorld()), + "[current]", Integer.toString(upgradeLevel), + "[max]", Integer.toString(upgradeAddon.getUpgradesManager().getEntityLimitsUpgradeMax(this.entity, island.getWorld()))); + + // Set new description + this.setOwnDescription(user, description); + + upgrade = new UpgradeValues(upgradeInfos.get("islandMinLevel"), upgradeInfos.get("vaultCost"), upgradeInfos.get("upgrade")); + } + + this.setUpgradeValues(user, upgrade); + + String newDisplayName; + + if (upgrade == null) { + newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.nolimitsupgrade", "[block]", + this.entity.toString()); + } else { + newDisplayName = user.getTranslation("upgrades.ui.upgradepanel.limitsupgrade", "[block]", + this.entity.toString(), "[level]", Integer.toString(upgrade.getUpgradeValue())); + } + + this.setDisplayName(newDisplayName); + } + + @Override + public boolean isShowed(User user, Island island) { + // Get the addon + UpgradesAddon upgradesAddon = this.getUpgradesAddon(); + // Get the data from upgrades + UpgradesData islandData = upgradesAddon.getUpgradesLevels(island.getUniqueId()); + // Get level of the upgrade + int upgradeLevel = islandData.getUpgradeLevel(this.getName()); + // Permission level required + int permissionLevel = upgradesAddon.getUpgradesManager().getEntityLimitsPermissionLevel(this.entity, + upgradeLevel, island.getWorld()); + + // If default permission, then true + if (permissionLevel == 0) + return true; + + Player player = user.getPlayer(); + String gamemode = island.getGameMode(); + String permissionStart = gamemode + ".upgrades." + this.getName() + "."; + permissionStart = permissionStart.toLowerCase(); + + // For each permission of the player + for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { + + // If permission is the one we search + if (!perms.getValue() || !perms.getPermission().startsWith(permissionStart)) + continue; + + if (perms.getPermission().contains(permissionStart + "*")) { + this.logError(player.getName(), perms.getPermission(), "Wildcards are not allowed."); + return false; + } + + String[] split = perms.getPermission().split("\\."); + if (split.length != 4) { + logError(player.getName(), perms.getPermission(), "format must be '" + permissionStart + "LEVEL'"); + return false; + } + + if (!NumberUtils.isDigits(split[3])) { + logError(player.getName(), perms.getPermission(), "The last part must be a number"); + return false; + } + + if (permissionLevel <= Integer.parseInt(split[3])) + return true; + } + + return false; + } + + private void logError(String name, String perm, String error) { + this.getUpgradesAddon() + .logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); + } + + @Override + public boolean doUpgrade(User user, Island island) { + UpgradesAddon islandAddon = this.getUpgradesAddon(); + + if (!islandAddon.isLimitsProvided()) + return false; + + BlockLimitsListener bLListener = islandAddon.getLimitsAddon().getBlockLimitListener(); + IslandBlockCount isb = bLListener.getIsland(island.getUniqueId()); + if (!super.doUpgrade(user, island)) + return false; + int oldCount = isb.getEntityLimitsOffset().getOrDefault(entity, 0); + int newCount = oldCount + this.getUpgradeValues(user).getUpgradeValue(); + + isb.setEntityLimitsOffset(this.entity, newCount); + + user.sendMessage("upgrades.ui.upgradepanel.limitsupgradedone", "[block]", this.entity.toString(), "[level]", + Integer.toString(this.getUpgradeValues(user).getUpgradeValue())); + + return true; + } + + private EntityType entity; } diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index a224fa1..10eab33 100644 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -1,7 +1,7 @@ name: Upgrades main: world.bentobox.upgrades.UpgradesAddon version: ${version}${build.number} -api-version: '1.14' +api-version: '1.16' repository: "BentoBoxWorld/Upgrades" metrics: true icon: GOLD_INGOT