From ddbe806af5d58cf00ba5f79d68d35325644d35c4 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 21 Feb 2019 19:56:44 -0800 Subject: [PATCH] Fixes a load of bugs. https://github.com/BentoBoxWorld/Level/issues/41 https://github.com/BentoBoxWorld/Level/issues/44 https://github.com/BentoBoxWorld/Level/issues/45 --- pom.xml | 2 +- src/main/java/world/bentobox/level/Level.java | 18 +++++--- .../java/world/bentobox/level/TopTen.java | 2 +- .../level/calculators/CalcIslandLevel.java | 25 ++++++++++- .../level/calculators/PlayerLevel.java | 2 + .../commands/admin/AdminLevelCommand.java | 20 +++++++-- .../level/commands/admin/AdminTopCommand.java | 23 +--------- .../commands/island/IslandLevelCommand.java | 6 +-- .../commands/island/IslandTopCommand.java | 4 +- .../event/IslandLevelCalculatedEvent.java | 2 +- .../bentobox/level/objects/LevelsData.java | 43 ++++++++++++++----- .../level/requests/LevelRequestHandler.java | 9 ++-- .../bentobox/level/LevelPresenterTest.java | 2 +- 13 files changed, 103 insertions(+), 55 deletions(-) diff --git a/pom.xml b/pom.xml index 034e8f9..2417dc5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ world.bentobox level - 1.2.1-SNAPSHOT + 1.2.2-SNAPSHOT Level Level is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like SkyBlock or AcidIsland. diff --git a/src/main/java/world/bentobox/level/Level.java b/src/main/java/world/bentobox/level/Level.java index 9083a39..074e44e 100644 --- a/src/main/java/world/bentobox/level/Level.java +++ b/src/main/java/world/bentobox/level/Level.java @@ -147,10 +147,7 @@ public class Level extends Addon { // Register request handlers registerRequestHandler(new LevelRequestHandler(this)); - - // Done - } /** @@ -185,7 +182,7 @@ public class Level extends Addon { } /** - * Sets the initial island level + * Zeros the initial island level * @param island - island * @param level - initial calculated island level */ @@ -194,10 +191,19 @@ public class Level extends Addon { this.logError("Level: request to store a null (initial) " + island.getWorld() + " " + island.getOwner()); return; } - setIslandLevel(island.getWorld(), island.getOwner(), level); - levelsCache.get(island.getOwner()).setInitialIslandLevel(level); + setIslandLevel(island.getWorld(), island.getOwner(), 0L); + levelsCache.get(island.getOwner()).setInitialLevel(island.getWorld(), level); } + /** + * Get the initial island level + * @param island - island + * @return level or 0 by default + */ + public long getInitialIslandLevel(Island island) { + return levelsCache.containsKey(island.getOwner()) ? levelsCache.get(island.getOwner()).getInitialLevel(island.getWorld()) : 0L; + } + public Database getHandler() { return handler; } diff --git a/src/main/java/world/bentobox/level/TopTen.java b/src/main/java/world/bentobox/level/TopTen.java index 12e1f64..24b1576 100644 --- a/src/main/java/world/bentobox/level/TopTen.java +++ b/src/main/java/world/bentobox/level/TopTen.java @@ -14,12 +14,12 @@ import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.Listener; -import world.bentobox.level.objects.TopTenData; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.Database; +import world.bentobox.level.objects.TopTenData; /** * Handles all Top Ten List functions diff --git a/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java b/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java index 36e3683..2c6fe65 100644 --- a/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java +++ b/src/main/java/world/bentobox/level/calculators/CalcIslandLevel.java @@ -63,6 +63,9 @@ public class CalcIslandLevel { // Results go here result = new Results(); + // Set the initial island handicap + result.initialLevel = addon.getInitialIslandLevel(island); + // Get chunks to scan chunksToScan = getChunksToScan(island); @@ -229,7 +232,7 @@ public class CalcIslandLevel { blockAndDeathPoints -= this.result.deathHandicap * this.addon.getSettings().getDeathPenalty(); } - this.result.level = blockAndDeathPoints / this.addon.getSettings().getLevelCost() - this.island.getLevelHandicap(); + this.result.level = blockAndDeathPoints / this.addon.getSettings().getLevelCost() - this.island.getLevelHandicap() - result.initialLevel; // Calculate how many points are required to get to the next level @@ -253,6 +256,7 @@ public class CalcIslandLevel { reportLines.add("Total block value count = " + String.format("%,d",result.rawBlockCount)); reportLines.add("Level cost = " + addon.getSettings().getLevelCost()); reportLines.add("Deaths handicap = " + result.deathHandicap); + reportLines.add("Initial island level = " + (0L - result.initialLevel)); reportLines.add("Level calculated = " + result.level); reportLines.add(LINE_BREAK); int total = 0; @@ -335,6 +339,8 @@ public class CalcIslandLevel { private long level = 0; private int deathHandicap = 0; private long pointsToNextLevel = 0; + private long initialLevel = 0; + /** * @return the deathHandicap */ @@ -348,6 +354,13 @@ public class CalcIslandLevel { public List getReport() { return report; } + /** + * Set level + * @param level - level + */ + public void setLevel(int level) { + this.level = level; + } /** * @return the level */ @@ -361,6 +374,14 @@ public class CalcIslandLevel { return pointsToNextLevel; } + public long getInitialLevel() { + return initialLevel; + } + + public void setInitialLevel(long initialLevel) { + this.initialLevel = initialLevel; + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ @@ -369,7 +390,7 @@ public class CalcIslandLevel { return "Results [report=" + report + ", mdCount=" + mdCount + ", uwCount=" + uwCount + ", ncCount=" + ncCount + ", ofCount=" + ofCount + ", rawBlockCount=" + rawBlockCount + ", underWaterBlockCount=" + underWaterBlockCount + ", level=" + level + ", deathHandicap=" + deathHandicap - + ", pointsToNextLevel=" + pointsToNextLevel + "]"; + + ", pointsToNextLevel=" + pointsToNextLevel + ", initialLevel=" + initialLevel + "]"; } } diff --git a/src/main/java/world/bentobox/level/calculators/PlayerLevel.java b/src/main/java/world/bentobox/level/calculators/PlayerLevel.java index 70f501d..dee9e5a 100644 --- a/src/main/java/world/bentobox/level/calculators/PlayerLevel.java +++ b/src/main/java/world/bentobox/level/calculators/PlayerLevel.java @@ -56,6 +56,7 @@ public class PlayerLevel { // Fire post calculation event IslandLevelCalculatedEvent ilce = new IslandLevelCalculatedEvent(targetPlayer, island, calc.getResult()); addon.getServer().getPluginManager().callEvent(ilce); + // This exposes these values to plugins via the event Map keyValues = new HashMap<>(); keyValues.put("eventName", "IslandLevelCalculatedEvent"); keyValues.put("targetPlayer", targetPlayer); @@ -63,6 +64,7 @@ public class PlayerLevel { keyValues.put("level", calc.getResult().getLevel()); keyValues.put("pointsToNextLevel", calc.getResult().getPointsToNextLevel()); keyValues.put("deathHandicap", calc.getResult().getDeathHandicap()); + keyValues.put("initialLevel", calc.getResult().getInitialLevel()); addon.getServer().getPluginManager().callEvent(new AddonEvent().builder().addon(addon).keyValues(keyValues).build()); Results results = ilce.getResults(); // Save the results diff --git a/src/main/java/world/bentobox/level/commands/admin/AdminLevelCommand.java b/src/main/java/world/bentobox/level/commands/admin/AdminLevelCommand.java index f0fcee2..a532acf 100644 --- a/src/main/java/world/bentobox/level/commands/admin/AdminLevelCommand.java +++ b/src/main/java/world/bentobox/level/commands/admin/AdminLevelCommand.java @@ -1,12 +1,15 @@ package world.bentobox.level.commands.admin; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.Util; import world.bentobox.level.Level; -import java.util.List; -import java.util.UUID; - public class AdminLevelCommand extends CompositeCommand { private final Level levelPlugin; @@ -42,4 +45,15 @@ public class AdminLevelCommand extends CompositeCommand { return false; } } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; + if (args.isEmpty()) { + // Don't show every player on the server. Require at least the first letter + return Optional.empty(); + } + List options = new ArrayList<>(Util.getOnlinePlayerList(user)); + return Optional.of(Util.tabLimit(options, lastArg)); + } } diff --git a/src/main/java/world/bentobox/level/commands/admin/AdminTopCommand.java b/src/main/java/world/bentobox/level/commands/admin/AdminTopCommand.java index 5970e88..30598e7 100644 --- a/src/main/java/world/bentobox/level/commands/admin/AdminTopCommand.java +++ b/src/main/java/world/bentobox/level/commands/admin/AdminTopCommand.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.bukkit.World; - import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -29,26 +27,9 @@ public class AdminTopCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { - // Get world - World world; - if (args.isEmpty()) { - if (getPlugin().getIWM().getOverWorlds().size() == 1) { - world = getPlugin().getIWM().getOverWorlds().get(0); - } else { - showHelp(this, user); - return false; - } - } else { - world = getPlugin().getIWM().getIslandWorld(args.get(0)); - if (world == null) { - user.sendMessage("commands.admin.top.unknown-world"); - return false; - } - - } int rank = 0; - for (Map.Entry topTen : levelPlugin.getTopTen().getTopTenList(world).getTopTen().entrySet()) { - Island island = getPlugin().getIslands().getIsland(world, topTen.getKey()); + for (Map.Entry topTen : levelPlugin.getTopTen().getTopTenList(getWorld()).getTopTen().entrySet()) { + Island island = getPlugin().getIslands().getIsland(getWorld(), topTen.getKey()); if (island != null) { rank++; user.sendMessage("admin.top.display", diff --git a/src/main/java/world/bentobox/level/commands/island/IslandLevelCommand.java b/src/main/java/world/bentobox/level/commands/island/IslandLevelCommand.java index b2390a3..48314f9 100644 --- a/src/main/java/world/bentobox/level/commands/island/IslandLevelCommand.java +++ b/src/main/java/world/bentobox/level/commands/island/IslandLevelCommand.java @@ -1,12 +1,12 @@ package world.bentobox.level.commands.island; +import java.util.List; +import java.util.UUID; + import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.level.Level; -import java.util.List; -import java.util.UUID; - public class IslandLevelCommand extends CompositeCommand { private final Level levelPlugin; diff --git a/src/main/java/world/bentobox/level/commands/island/IslandTopCommand.java b/src/main/java/world/bentobox/level/commands/island/IslandTopCommand.java index 1f8db3d..a80a3c1 100644 --- a/src/main/java/world/bentobox/level/commands/island/IslandTopCommand.java +++ b/src/main/java/world/bentobox/level/commands/island/IslandTopCommand.java @@ -1,11 +1,11 @@ package world.bentobox.level.commands.island; +import java.util.List; + import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.level.Level; -import java.util.List; - public class IslandTopCommand extends CompositeCommand { private final Level plugin; diff --git a/src/main/java/world/bentobox/level/event/IslandLevelCalculatedEvent.java b/src/main/java/world/bentobox/level/event/IslandLevelCalculatedEvent.java index c6b100a..73a9ed8 100644 --- a/src/main/java/world/bentobox/level/event/IslandLevelCalculatedEvent.java +++ b/src/main/java/world/bentobox/level/event/IslandLevelCalculatedEvent.java @@ -2,9 +2,9 @@ package world.bentobox.level.event; import java.util.UUID; -import world.bentobox.level.calculators.CalcIslandLevel.Results; import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.level.calculators.CalcIslandLevel.Results; /** * This event is fired after the island level is calculated and before the results are saved. diff --git a/src/main/java/world/bentobox/level/objects/LevelsData.java b/src/main/java/world/bentobox/level/objects/LevelsData.java index a45c15f..45c4876 100644 --- a/src/main/java/world/bentobox/level/objects/LevelsData.java +++ b/src/main/java/world/bentobox/level/objects/LevelsData.java @@ -16,11 +16,16 @@ public class LevelsData implements DataObject { @Expose private String uniqueId = ""; - // Map - world name, level + /** + * Map of world name and island level + */ @Expose private Map levels = new HashMap<>(); + /** + * Map of world name to island initial level + */ @Expose - private long initialIslandLevel = 0; + private Map initialLevel = new HashMap<>(); public LevelsData() {} // For Bean loading @@ -54,10 +59,10 @@ public class LevelsData implements DataObject { /** * Get the island level for this world * @param world - world - * @return island level, less the initialIslandLevel + * @return island level */ public Long getLevel(World world) { - return world == null ? -initialIslandLevel : levels.getOrDefault(world.getName(), 0L) - initialIslandLevel; + return world == null ? 0L : levels.getOrDefault(world.getName(), 0L); } /** @@ -79,16 +84,34 @@ public class LevelsData implements DataObject { } /** - * @return the initialIslandLevel + * Set the initial level of the island for this world + * @param world - world + * @param level - level */ - public long getInitialIslandLevel() { - return initialIslandLevel; + public void setInitialLevel(World world, long level) { + this.initialLevel.put(world.getName(), level); } /** - * @param initialIslandLevel the initialIslandLevel to set + * @return the initialLevel */ - public void setInitialIslandLevel(long initialIslandLevel) { - this.initialIslandLevel = initialIslandLevel; + public Map getInitialLevel() { + return initialLevel; + } + + /** + * @param initialLevel the initialLevel to set + */ + public void setInitialLevel(Map initialLevel) { + this.initialLevel = initialLevel; + } + + /** + * Get the initial island level for this world + * @param world - world + * @return initial island level or 0 by default + */ + public long getInitialLevel(World world) { + return initialLevel.getOrDefault(world.getName(), 0L); } } diff --git a/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java b/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java index 7626050..747bda0 100644 --- a/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java +++ b/src/main/java/world/bentobox/level/requests/LevelRequestHandler.java @@ -1,12 +1,13 @@ package world.bentobox.level.requests; -import org.bukkit.Bukkit; -import world.bentobox.bentobox.api.addons.request.AddonRequestHandler; -import world.bentobox.level.Level; - import java.util.Map; import java.util.UUID; +import org.bukkit.Bukkit; + +import world.bentobox.bentobox.api.addons.request.AddonRequestHandler; +import world.bentobox.level.Level; + public class LevelRequestHandler extends AddonRequestHandler { private Level addon; diff --git a/src/test/java/world/bentobox/level/LevelPresenterTest.java b/src/test/java/world/bentobox/level/LevelPresenterTest.java index 28679a1..51293e5 100644 --- a/src/test/java/world/bentobox/level/LevelPresenterTest.java +++ b/src/test/java/world/bentobox/level/LevelPresenterTest.java @@ -15,11 +15,11 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import world.bentobox.level.calculators.PlayerLevel; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.level.calculators.PlayerLevel; /** * @author tastybento