diff --git a/pom.xml b/pom.xml index 16055c7..cebdfea 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ -LOCAL - 2.19.3 + 2.20.0 BentoBoxWorld_Level bentobox-world https://sonarcloud.io diff --git a/src/main/java/world/bentobox/level/Level.java b/src/main/java/world/bentobox/level/Level.java index d1b1d36..0ba0c1b 100644 --- a/src/main/java/world/bentobox/level/Level.java +++ b/src/main/java/world/bentobox/level/Level.java @@ -339,12 +339,13 @@ public long getIslandLevel(World world, @Nullable UUID targetPlayer) { } /** - * Sets the player's level to a value - * - * @param world - world - * @param targetPlayer - target player - * @param level - level - */ + * Sets the player's level to a value. This will only last until the player reruns the level command + * + * @param world - world + * @param targetPlayer - target player + * @param level - level + * @deprecated This is a useless method. + */ public void setIslandLevel(World world, UUID targetPlayer, long level) { getManager().setIslandLevel(world, targetPlayer, level); } @@ -355,19 +356,31 @@ public void setIslandLevel(World world, UUID targetPlayer, long level) { * @param island - island * @param level - initial calculated island level */ - public void setInitialIslandLevel(@NonNull Island island, long level) { - getManager().setInitialIslandLevel(island, level); - } + /* TODO + public void setInitialIslandLevel(@NonNull Island island, long level) { + getManager().setInitialIslandLevel(island, level); + } + + /** + * Get the initial island level + * + * @param island - island + * @return level or 0 by default + */ + /* TODO + public long getInitialIslandLevel(@NonNull Island island) { + return getManager().getInitialLevel(island); + }*/ - /** - * Get the initial island level - * - * @param island - island - * @return level or 0 by default - */ - public long getInitialIslandLevel(@NonNull Island island) { - return getManager().getInitialLevel(island); - } + /** + * Get the initial island count + * + * @param island - island + * @return count or 0 by default + */ + public long getInitialIslandCount(@NonNull Island island) { + return getManager().getInitialCount(island); + } /** * Calculates a user's island @@ -392,21 +405,22 @@ public void calculateIslandLevel(World world, @Nullable User user, @NonNull UUID * @return LevelsData object or null if not found. Only island levels are set! * @deprecated Do not use this anymore. Use {@link #getIslandLevel(World, UUID)} */ - @Deprecated(since = "2.3.0", forRemoval = true) - public LevelsData getLevelsData(UUID targetPlayer) { - LevelsData ld = new LevelsData(targetPlayer); - getPlugin().getAddonsManager().getGameModeAddons().stream() - .filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName())).forEach(gm -> { - if (getSettings().isZeroNewIslandLevels()) { - Island island = getIslands().getIsland(gm.getOverWorld(), targetPlayer); - if (island != null) { - ld.setInitialLevel(gm.getOverWorld(), this.getInitialIslandLevel(island)); - } - } - ld.setLevel(gm.getOverWorld(), this.getIslandLevel(gm.getOverWorld(), targetPlayer)); - }); - return ld; - } + /* + @Deprecated(since = "2.3.0", forRemoval = true) + public LevelsData getLevelsData(UUID targetPlayer) { + LevelsData ld = new LevelsData(targetPlayer); + getPlugin().getAddonsManager().getGameModeAddons().stream() + .filter(gm -> !settings.getGameModes().contains(gm.getDescription().getName())).forEach(gm -> { + if (getSettings().isZeroNewIslandLevels()) { + Island island = getIslands().getIsland(gm.getOverWorld(), targetPlayer); + if (island != null) { + ld.setInitialLevel(gm.getOverWorld(), this.getInitialIslandLevel(island)); + } + } + ld.setLevel(gm.getOverWorld(), this.getIslandLevel(gm.getOverWorld(), targetPlayer)); + }); + return ld; + }*/ /** * @return the registeredGameModes diff --git a/src/main/java/world/bentobox/level/LevelsManager.java b/src/main/java/world/bentobox/level/LevelsManager.java index 61cb122..ac9c46b 100644 --- a/src/main/java/world/bentobox/level/LevelsManager.java +++ b/src/main/java/world/bentobox/level/LevelsManager.java @@ -1,7 +1,9 @@ package world.bentobox.level; +import java.io.IOException; import java.math.BigInteger; import java.text.DecimalFormat; +import java.text.ParseException; import java.time.Instant; import java.util.AbstractMap; import java.util.Collections; @@ -26,6 +28,7 @@ import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.level.calculators.EquationEvaluator; import world.bentobox.level.calculators.Results; import world.bentobox.level.events.IslandLevelCalculatedEvent; import world.bentobox.level.events.IslandPreLevelEvent; @@ -130,7 +133,7 @@ private boolean fireIslandLevelCalcEvent(UUID targetPlayer, Island island, Resul return true; // Set the values if they were altered results.setLevel((Long) ilce.getKeyValues().getOrDefault("level", results.getLevel())); - results.setInitialLevel((Long) ilce.getKeyValues().getOrDefault("initialLevel", results.getInitialLevel())); + results.setInitialCount((Long) ilce.getKeyValues().getOrDefault("initialCount", results.getInitialCount())); results.setDeathHandicap((int) ilce.getKeyValues().getOrDefault("deathHandicap", results.getDeathHandicap())); results.setPointsToNextLevel( (Long) ilce.getKeyValues().getOrDefault("pointsToNextLevel", results.getPointsToNextLevel())); @@ -168,13 +171,65 @@ public String formatLevel(@Nullable Long lvl) { } /** - * Get the initial level of the island. Used to zero island levels + * Get the initial count of the island. Used to zero island levels * * @param island - island - * @return initial level of island + * @return initial count of island */ - public long getInitialLevel(Island island) { - return getLevelsData(island).getInitialLevel(); + @SuppressWarnings("deprecation") + public long getInitialCount(Island island) { + Long initialLevel = getLevelsData(island).getInitialLevel(); // Backward compatibility check. For all new islands, this should be null. + Long initialCount = getLevelsData(island).getInitialCount(); + if (initialLevel != null) { + // Initial level exists so convert it + if (initialCount == null) { // If initialCount is not null, then this is an edge case and initialCount will be used and initialLevel discarded + // Convert from level to count + initialCount = 0L; + try { + initialCount = getNumBlocks(initialLevel); + } catch (Exception e) { + addon.logError("Could not convert legacy initial level to count, so it will be set to 0. Error is: " + + e.getLocalizedMessage()); + initialCount = 0L; + } + } + // Null out the old initial level and save + getLevelsData(island).setInitialLevel(null); + // Save + this.setInitialIslandCount(island, initialCount); + } + // If initialCount doesn't exist, set it to 0L + if (initialCount == null) { + initialCount = 0L; + getLevelsData(island).setInitialCount(0L); + } + return initialCount; + } + + /** + * Runs the level calculation using the current formula until the level matches the initial value, or fails. + * @param initialLevel - the old initial level + * @return block count to obtain this level now + * @throws ParseException if the formula for level calc is bugged + * @throws IOException if the number of blocks cannot be found for this level + */ + private long getNumBlocks(final long initialLevel) throws ParseException, IOException { + String calcString = addon.getSettings().getLevelCalc(); + int result = -1; + long calculatedLevel = 0; + String withCost = calcString.replace("level_cost", String.valueOf(this.addon.getSettings().getLevelCost())); + long time = System.currentTimeMillis() + 10 * 1000; // 10 seconds + do { + result++; + if (System.currentTimeMillis() > time) { + throw new IOException("Timeout: Blocks cannot be found to create this initial level"); + } + // Paste in the values to the formula + String withValues = withCost.replace("blocks", String.valueOf(result)); + // Try and evaluate it + calculatedLevel = (long) EquationEvaluator.eval(withValues); + } while (calculatedLevel != initialLevel); + return result; } /** @@ -422,15 +477,13 @@ public void removeEntry(World world, String uuid) { } /** - * Set an initial island level + * Set an initial island count * - * @param island - the island to set. Must have a non-null world - * @param lv - initial island level + * @param island - the island to set. + * @param lv - initial island count */ - public void setInitialIslandLevel(@NonNull Island island, long lv) { - if (island.getWorld() == null) - return; - levelsCache.computeIfAbsent(island.getUniqueId(), IslandLevels::new).setInitialLevel(lv); + public void setInitialIslandCount(@NonNull Island island, long lv) { + levelsCache.computeIfAbsent(island.getUniqueId(), IslandLevels::new).setInitialCount(lv); handler.saveObjectAsync(levelsCache.get(island.getUniqueId())); } @@ -448,12 +501,7 @@ public void setIslandLevel(@NonNull World world, @NonNull UUID targetPlayer, lon if (island != null) { String id = island.getUniqueId(); IslandLevels il = levelsCache.computeIfAbsent(id, IslandLevels::new); - // Remove the initial level - if (addon.getSettings().isZeroNewIslandLevels()) { - il.setLevel(lv - il.getInitialLevel()); - } else { - il.setLevel(lv); - } + il.setLevel(lv); handler.saveObjectAsync(levelsCache.get(id)); // Update TopTen addToTopTen(island, levelsCache.get(id).getLevel()); diff --git a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java index 9188594..da71e57 100644 --- a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java +++ b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java @@ -93,7 +93,8 @@ public IslandLevelCalculator(Level addon, Island island, CompletableFuture(); // Get the initial island level - results.initialLevel.set(addon.getInitialIslandLevel(island)); + // TODO: results.initialLevel.set(addon.getInitialIslandLevel(island)); + results.setInitialCount(addon.getInitialIslandCount(island)); // Set up the worlds worlds.put(Environment.NORMAL, Util.getWorld(island.getWorld())); // Nether @@ -117,19 +118,22 @@ public IslandLevelCalculator(Level addon, Island island, CompletableFuture getReport() { reportLines.add("Formula to calculate island level: " + addon.getSettings().getLevelCalc()); reportLines.add("Level cost = " + addon.getSettings().getLevelCost()); reportLines.add("Deaths handicap = " + results.deathHandicap.get()); + /* if (addon.getSettings().isZeroNewIslandLevels()) { reportLines.add("Initial island level = " + (0L - addon.getManager().getInitialLevel(island))); + }*/ + if (addon.getSettings().isZeroNewIslandLevels()) { + reportLines.add("Initial island count = " + (0L - addon.getManager().getInitialCount(island))); } reportLines.add("Previous level = " + addon.getManager().getIslandLevel(island.getWorld(), island.getOwner())); reportLines.add("New level = " + results.getLevel()); diff --git a/src/main/java/world/bentobox/level/calculators/Results.java b/src/main/java/world/bentobox/level/calculators/Results.java index b744046..6dbd2a5 100644 --- a/src/main/java/world/bentobox/level/calculators/Results.java +++ b/src/main/java/world/bentobox/level/calculators/Results.java @@ -7,6 +7,9 @@ import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; +/** + * Where results are stored + */ public class Results { public enum Result { /** @@ -23,9 +26,21 @@ public enum Result { TIMEOUT } List report; + /** + * MaterialData count anything above sea level + */ final Multiset mdCount = HashMultiset.create(); + /** + * Underwater count + */ final Multiset uwCount = HashMultiset.create(); + /** + * Not-in-config count - blocks not listed in the scoring config file + */ final Multiset ncCount = HashMultiset.create(); + /** + * Blocks not counted because they exceeded limits + */ final Multiset ofCount = HashMultiset.create(); // AtomicLong and AtomicInteger must be used because they are changed by multiple concurrent threads AtomicLong rawBlockCount = new AtomicLong(0); @@ -33,7 +48,11 @@ public enum Result { AtomicLong level = new AtomicLong(0); AtomicInteger deathHandicap = new AtomicInteger(0); AtomicLong pointsToNextLevel = new AtomicLong(0); - AtomicLong initialLevel = new AtomicLong(0); + //AtomicLong initialLevel = new AtomicLong(0); + AtomicLong initialCount = new AtomicLong(0); + /** + * Total points before any death penalties + */ AtomicLong totalPoints = new AtomicLong(0); final Result state; @@ -107,24 +126,26 @@ public void setTotalPoints(long points) { totalPoints.set(points); } + /* public long getInitialLevel() { return initialLevel.get(); } - + public void setInitialLevel(long initialLevel) { this.initialLevel.set(initialLevel); } - + */ /* (non-Javadoc) * @see java.lang.Object#toString() */ + /* @Override public String toString() { return "Results [report=" + report + ", mdCount=" + mdCount + ", uwCount=" + uwCount + ", ncCount=" + ncCount + ", ofCount=" + ofCount + ", rawBlockCount=" + rawBlockCount + ", underWaterBlockCount=" + underWaterBlockCount + ", level=" + level + ", deathHandicap=" + deathHandicap + ", pointsToNextLevel=" + pointsToNextLevel + ", totalPoints=" + totalPoints + ", initialLevel=" + initialLevel + "]"; - } + }*/ /** * @return the mdCount */ @@ -144,4 +165,18 @@ public Result getState() { return state; } + /** + * @return the initialCount + */ + public long getInitialCount() { + return initialCount.get(); + } + + /** + * @param long1 the initialCount to set + */ + public void setInitialCount(Long count) { + this.initialCount.set(count); + } + } diff --git a/src/main/java/world/bentobox/level/commands/AdminSetInitialLevelCommand.java b/src/main/java/world/bentobox/level/commands/AdminSetInitialLevelCommand.java index 05134d8..7caaa64 100644 --- a/src/main/java/world/bentobox/level/commands/AdminSetInitialLevelCommand.java +++ b/src/main/java/world/bentobox/level/commands/AdminSetInitialLevelCommand.java @@ -46,7 +46,7 @@ public Optional> tabComplete(User user, String alias, List @Override public boolean execute(User user, String label, List args) { - long initialLevel = addon.getManager().getInitialLevel(island); + long initialLevel = addon.getManager().getInitialCount(island); long lv = 0; if (args.get(1).startsWith("+")) { String change = args.get(1).substring(1); @@ -57,7 +57,7 @@ public boolean execute(User user, String label, List args) { } else { lv = Long.parseLong(args.get(1)); } - addon.getManager().setInitialIslandLevel(island, lv); + addon.getManager().setInitialIslandCount(island, lv); // TODO Enable level setting user.sendMessage("admin.level.sethandicap.changed", TextVariables.NUMBER, String.valueOf(initialLevel), "[new_number]", String.valueOf(lv)); return true; diff --git a/src/main/java/world/bentobox/level/events/IslandLevelCalculatedEvent.java b/src/main/java/world/bentobox/level/events/IslandLevelCalculatedEvent.java index b59c6c5..ffc402b 100644 --- a/src/main/java/world/bentobox/level/events/IslandLevelCalculatedEvent.java +++ b/src/main/java/world/bentobox/level/events/IslandLevelCalculatedEvent.java @@ -59,11 +59,11 @@ public int getDeathHandicap() { } /** - * Get the island's initial level. It may be zero if it was never calculated. - * @return initial level of island as calculated when the island was created. + * Get the island's initial count. It may be zero if it was never calculated. + * @return initial count of island as calculated when the island was created. */ - public long getInitialLevel() { - return results.getInitialLevel(); + public long getInitiaCount() { + return results.getInitialCount(); } /** diff --git a/src/main/java/world/bentobox/level/listeners/IslandActivitiesListeners.java b/src/main/java/world/bentobox/level/listeners/IslandActivitiesListeners.java index 8e6166d..f139d70 100644 --- a/src/main/java/world/bentobox/level/listeners/IslandActivitiesListeners.java +++ b/src/main/java/world/bentobox/level/listeners/IslandActivitiesListeners.java @@ -34,90 +34,90 @@ public class IslandActivitiesListeners implements Listener { * @param addon - addon */ public IslandActivitiesListeners(Level addon) { - this.addon = addon; + this.addon = addon; } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onNewIsland(IslandCreatedEvent e) { - if (addon.getSettings().isZeroNewIslandLevels()) { - // Wait a few seconds before performing the zero - Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> zeroIsland(e.getIsland()), 150L); - } + if (addon.getSettings().isZeroNewIslandLevels()) { + // Wait a few seconds before performing the zero + Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> zeroIsland(e.getIsland()), 150L); + } } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onNewIsland(IslandResettedEvent e) { - if (addon.getSettings().isZeroNewIslandLevels()) { - zeroIsland(e.getIsland()); - } + if (addon.getSettings().isZeroNewIslandLevels()) { + zeroIsland(e.getIsland()); + } } private void zeroIsland(final Island island) { - // Clear the island setting - if (island.getOwner() != null && island.getWorld() != null) { - addon.getPipeliner().zeroIsland(island) - .thenAccept(results -> addon.getManager().setInitialIslandLevel(island, results.getLevel())); - } + // Clear the island setting + if (island.getOwner() != null && island.getWorld() != null) { + addon.getPipeliner().zeroIsland(island) + .thenAccept(results -> addon.getManager().setInitialIslandCount(island, results.getTotalPoints())); + } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onIslandDelete(IslandPreclearEvent e) { - remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); + remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onIslandDeleted(IslandDeleteEvent e) { - // Remove island - addon.getManager().deleteIsland(e.getIsland().getUniqueId()); + // Remove island + addon.getManager().deleteIsland(e.getIsland().getUniqueId()); } private void remove(World world, String uuid) { - if (uuid != null && world != null) { - addon.getManager().removeEntry(world, uuid); - } + if (uuid != null && world != null) { + addon.getManager().removeEntry(world, uuid); + } } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onNewIslandOwner(TeamSetownerEvent e) { - // Remove island from the top ten and level - remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); + // Remove island from the top ten and level + remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onIsland(TeamJoinedEvent e) { - // TODO: anything to do here? - // Remove player from the top ten and level - // remove(e.getIsland().getWorld(), e.getPlayerUUID()); + // TODO: anything to do here? + // Remove player from the top ten and level + // remove(e.getIsland().getWorld(), e.getPlayerUUID()); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onIsland(IslandUnregisteredEvent e) { - // Remove island from the top ten - remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); + // Remove island from the top ten + remove(e.getIsland().getWorld(), e.getIsland().getUniqueId()); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onIsland(IslandRegisteredEvent e) { - // TODO: anything to do here? - // Remove player from the top ten - // remove(e.getIsland().getWorld(), e.getPlayerUUID()); + // TODO: anything to do here? + // Remove player from the top ten + // remove(e.getIsland().getWorld(), e.getPlayerUUID()); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onIsland(TeamLeaveEvent e) { - // TODO: anything to do here? - // Remove player from the top ten and level - // remove(e.getIsland().getWorld(), e.getPlayerUUID()); + // TODO: anything to do here? + // Remove player from the top ten and level + // remove(e.getIsland().getWorld(), e.getPlayerUUID()); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onIsland(TeamKickEvent e) { - //// TODO: anything to do here? - // Remove player from the top ten and level - // remove(e.getIsland().getWorld(), e.getPlayerUUID()); + //// TODO: anything to do here? + // Remove player from the top ten and level + // remove(e.getIsland().getWorld(), e.getPlayerUUID()); } } diff --git a/src/main/java/world/bentobox/level/objects/IslandLevels.java b/src/main/java/world/bentobox/level/objects/IslandLevels.java index ffebd79..3666ea6 100644 --- a/src/main/java/world/bentobox/level/objects/IslandLevels.java +++ b/src/main/java/world/bentobox/level/objects/IslandLevels.java @@ -39,7 +39,12 @@ public class IslandLevels implements DataObject { * Initial level */ @Expose - private long initialLevel; + private Long initialLevel; + /** + * Initial count + */ + @Expose + private Long initialCount; /** * Points to next level */ @@ -116,17 +121,20 @@ public void setLevel(long level) { /** * @return the initialLevel */ + /* public long getInitialLevel() { + // TODO: Add Backwards compatibility return initialLevel; } - + /** * @param initialLevel the initialLevel to set */ + /* public void setInitialLevel(long initialLevel) { this.initialLevel = initialLevel; } - + */ /** * @return the pointsToNextLevel */ @@ -229,5 +237,34 @@ public void setMdCount(Map mdCount) { this.mdCount = mdCount; } + /** + * @return the initialCount + */ + public Long getInitialCount() { + return initialCount; + } + + /** + * @param initialCount the initialCount to set + */ + public void setInitialCount(Long initialCount) { + this.initialCount = initialCount; + } + + /** + * @return the initialLevel + * @deprecated only used for backwards compatibility. Use {@link #getInitialCount()} instead + */ + public Long getInitialLevel() { + return initialLevel; + } + + /** + * @param initialLevel the initialLevel to set + * @deprecated only used for backwards compatil + */ + public void setInitialLevel(Long initialLevel) { + this.initialLevel = initialLevel; + } } diff --git a/src/test/java/world/bentobox/level/LevelsManagerTest.java b/src/test/java/world/bentobox/level/LevelsManagerTest.java index b443123..a790604 100644 --- a/src/test/java/world/bentobox/level/LevelsManagerTest.java +++ b/src/test/java/world/bentobox/level/LevelsManagerTest.java @@ -115,20 +115,20 @@ public class LevelsManagerTest { @SuppressWarnings("unchecked") @BeforeClass public static void beforeClass() { - // This has to be done beforeClass otherwise the tests will interfere with each - // other - handler = mock(AbstractDatabaseHandler.class); - // Database - PowerMockito.mockStatic(DatabaseSetup.class); - DatabaseSetup dbSetup = mock(DatabaseSetup.class); - when(DatabaseSetup.getDatabase()).thenReturn(dbSetup); - when(dbSetup.getHandler(any())).thenReturn(handler); + // This has to be done beforeClass otherwise the tests will interfere with each + // other + handler = mock(AbstractDatabaseHandler.class); + // Database + PowerMockito.mockStatic(DatabaseSetup.class); + DatabaseSetup dbSetup = mock(DatabaseSetup.class); + when(DatabaseSetup.getDatabase()).thenReturn(dbSetup); + when(dbSetup.getHandler(any())).thenReturn(handler); } /** * @throws java.lang.Exception */ - @SuppressWarnings("unchecked") + @SuppressWarnings("deprecation") @Before public void setUp() throws Exception { when(addon.getPlugin()).thenReturn(plugin); @@ -209,15 +209,18 @@ public void setUp() throws Exception { List islands = new ArrayList<>(); for (long i = -5; i < 5; i ++) { IslandLevels il = new IslandLevels(UUID.randomUUID().toString()); - il.setInitialLevel(3); + il.setInitialCount(null); il.setLevel(i); il.setPointsToNextLevel(3); + il.setInitialLevel(26145L); // Legacy islands.add(il); } // Supply no island levels first (for migrate), then islands when(handler.loadObjects()).thenReturn(islands); when(handler.objectExists(anyString())).thenReturn(true); when(levelsData.getLevel()).thenReturn(-5L, -4L, -3L, -2L, -1L, 0L, 1L, 2L, 3L, 4L, 5L, 45678L); + when(levelsData.getInitialLevel()).thenReturn(26145L, -4L, -3L, -2L, -1L, 0L, 1L, 2L, 3L, 4L, 5L, 45678L); + when(levelsData.getInitialCount()).thenReturn(null); when(levelsData.getUniqueId()).thenReturn(uuid.toString()); when(handler.loadObject(anyString())).thenReturn(levelsData ); @@ -238,15 +241,15 @@ public void setUp() throws Exception { */ @After public void tearDown() throws Exception { - deleteAll(new File("database")); - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + deleteAll(new File("database")); + User.clearUsers(); + Mockito.framework().clearInlineMocks(); } private static void deleteAll(File file) throws IOException { - if (file.exists()) { - Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); - } + if (file.exists()) { + Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } } /** @@ -255,36 +258,36 @@ private static void deleteAll(File file) throws IOException { */ @Test public void testCalculateLevel() { - Results results = new Results(); - results.setLevel(10000); - results.setInitialLevel(3); - lm.calculateLevel(uuid, island); - // Complete the pipelined completable future - cf.complete(results); - - assertEquals(10000L, lm.getLevelsData(island).getLevel()); - // Map tt = lm.getTopTen(world, 10); - // assertEquals(1, tt.size()); - // assertTrue(tt.get(uuid) == 10000); - assertEquals(10000L, lm.getIslandMaxLevel(world, uuid)); - - results.setLevel(5000); - lm.calculateLevel(uuid, island); - // Complete the pipelined completable future - cf.complete(results); - assertEquals(5000L, lm.getLevelsData(island).getLevel()); - // Still should be 10000 - assertEquals(10000L, lm.getIslandMaxLevel(world, uuid)); + Results results = new Results(); + results.setLevel(10000); + results.setInitialCount(300L); + lm.calculateLevel(uuid, island); + // Complete the pipelined completable future + cf.complete(results); + + assertEquals(10000L, lm.getLevelsData(island).getLevel()); + // Map tt = lm.getTopTen(world, 10); + // assertEquals(1, tt.size()); + // assertTrue(tt.get(uuid) == 10000); + assertEquals(10000L, lm.getIslandMaxLevel(world, uuid)); + + results.setLevel(5000); + lm.calculateLevel(uuid, island); + // Complete the pipelined completable future + cf.complete(results); + assertEquals(5000L, lm.getLevelsData(island).getLevel()); + // Still should be 10000 + assertEquals(10000L, lm.getIslandMaxLevel(world, uuid)); } /** * Test method for - * {@link world.bentobox.level.LevelsManager#getInitialLevel(world.bentobox.bentobox.database.objects.Island)}. + * {@link world.bentobox.level.LevelsManager#getInitialCount(world.bentobox.bentobox.database.objects.Island)}. */ @Test - public void testGetInitialLevel() { - assertEquals(0, lm.getInitialLevel(island)); + public void testGetInitialCount() { + assertEquals(2614500L, lm.getInitialCount(island)); } /** @@ -293,7 +296,7 @@ public void testGetInitialLevel() { */ @Test public void testGetIslandLevel() { - assertEquals(-5, lm.getIslandLevel(world, uuid)); + assertEquals(-5, lm.getIslandLevel(world, uuid)); } /** @@ -302,10 +305,10 @@ public void testGetIslandLevel() { */ @Test public void testGetPointsToNextString() { - // No island player - assertEquals("", lm.getPointsToNextString(world, UUID.randomUUID())); - // Player has island - assertEquals("0", lm.getPointsToNextString(world, uuid)); + // No island player + assertEquals("", lm.getPointsToNextString(world, UUID.randomUUID())); + // Player has island + assertEquals("0", lm.getPointsToNextString(world, uuid)); } /** @@ -314,7 +317,7 @@ public void testGetPointsToNextString() { */ @Test public void testGetIslandLevelString() { - assertEquals("-5", lm.getIslandLevelString(world, uuid)); + assertEquals("-5", lm.getIslandLevelString(world, uuid)); } /** @@ -323,7 +326,7 @@ public void testGetIslandLevelString() { */ @Test public void testGetLevelsData() { - assertEquals(levelsData, lm.getLevelsData(island)); + assertEquals(levelsData, lm.getLevelsData(island)); } @@ -332,13 +335,13 @@ public void testGetLevelsData() { */ @Test public void testFormatLevel() { - assertEquals("123456789", lm.formatLevel(123456789L)); - settings.setShorthand(true); - assertEquals("123.5M", lm.formatLevel(123456789L)); - assertEquals("1.2k", lm.formatLevel(1234L)); - assertEquals("123.5G", lm.formatLevel(123456789352L)); - assertEquals("1.2T", lm.formatLevel(1234567893524L)); - assertEquals("12345.7T", lm.formatLevel(12345678345345349L)); + assertEquals("123456789", lm.formatLevel(123456789L)); + settings.setShorthand(true); + assertEquals("123.5M", lm.formatLevel(123456789L)); + assertEquals("1.2k", lm.formatLevel(1234L)); + assertEquals("123.5G", lm.formatLevel(123456789352L)); + assertEquals("1.2T", lm.formatLevel(1234567893524L)); + assertEquals("12345.7T", lm.formatLevel(12345678345345349L)); } @@ -348,8 +351,8 @@ public void testFormatLevel() { */ @Test public void testGetTopTenEmpty() { - Map tt = lm.getTopTen(world, Level.TEN); - assertTrue(tt.isEmpty()); + Map tt = lm.getTopTen(world, Level.TEN); + assertTrue(tt.isEmpty()); } /** @@ -358,11 +361,11 @@ public void testGetTopTenEmpty() { */ @Test public void testGetTopTen() { - testLoadTopTens(); - Map tt = lm.getTopTen(world, Level.TEN); - assertFalse(tt.isEmpty()); - assertEquals(1, tt.size()); - assertEquals(1, lm.getTopTen(world, 1).size()); + testLoadTopTens(); + Map tt = lm.getTopTen(world, Level.TEN); + assertFalse(tt.isEmpty()); + assertEquals(1, tt.size()); + assertEquals(1, lm.getTopTen(world, 1).size()); } /** @@ -371,11 +374,11 @@ public void testGetTopTen() { */ @Test public void testGetWeightedTopTen() { - testLoadTopTens(); - Map tt = lm.getWeightedTopTen(world, Level.TEN); - assertFalse(tt.isEmpty()); - assertEquals(1, tt.size()); - assertEquals(1, lm.getTopTen(world, 1).size()); + testLoadTopTens(); + Map tt = lm.getWeightedTopTen(world, Level.TEN); + assertFalse(tt.isEmpty()); + assertEquals(1, tt.size()); + assertEquals(1, lm.getTopTen(world, 1).size()); } /** @@ -384,7 +387,7 @@ public void testGetWeightedTopTen() { */ @Test public void testHasTopTenPerm() { - assertTrue(lm.hasTopTenPerm(world, uuid)); + assertTrue(lm.hasTopTenPerm(world, uuid)); } /** @@ -392,14 +395,14 @@ public void testHasTopTenPerm() { */ @Test public void testLoadTopTens() { - ArgumentCaptor task = ArgumentCaptor.forClass(Runnable.class); - lm.loadTopTens(); - PowerMockito.verifyStatic(Bukkit.class); // 1 - Bukkit.getScheduler(); - verify(scheduler).runTaskAsynchronously(eq(plugin), task.capture()); // Capture the task in the scheduler - task.getValue().run(); // run it - verify(addon).log("Generating rankings"); - verify(addon).log("Generated rankings for bskyblock-world"); + ArgumentCaptor task = ArgumentCaptor.forClass(Runnable.class); + lm.loadTopTens(); + PowerMockito.verifyStatic(Bukkit.class); // 1 + Bukkit.getScheduler(); + verify(scheduler).runTaskAsynchronously(eq(plugin), task.capture()); // Capture the task in the scheduler + task.getValue().run(); // run it + verify(addon).log("Generating rankings"); + verify(addon).log("Generated rankings for bskyblock-world"); } @@ -409,12 +412,12 @@ public void testLoadTopTens() { */ @Test public void testRemoveEntry() { - testLoadTopTens(); - Map tt = lm.getTopTen(world, Level.TEN); - assertTrue(tt.containsKey(uuid.toString())); - lm.removeEntry(world, uuid.toString()); - tt = lm.getTopTen(world, Level.TEN); - assertFalse(tt.containsKey(uuid.toString())); + testLoadTopTens(); + Map tt = lm.getTopTen(world, Level.TEN); + assertTrue(tt.containsKey(uuid.toString())); + lm.removeEntry(world, uuid.toString()); + tt = lm.getTopTen(world, Level.TEN); + assertFalse(tt.containsKey(uuid.toString())); } /** @@ -423,8 +426,8 @@ public void testRemoveEntry() { */ @Test public void testSetInitialIslandLevel() { - lm.setInitialIslandLevel(island, Level.TEN); - assertEquals(Level.TEN, lm.getInitialLevel(island)); + lm.setInitialIslandCount(island, Level.TEN); + assertEquals(Level.TEN, lm.getInitialCount(island)); } /** @@ -433,8 +436,8 @@ public void testSetInitialIslandLevel() { */ @Test public void testSetIslandLevel() { - lm.setIslandLevel(world, uuid, 1234); - assertEquals(1234, lm.getIslandLevel(world, uuid)); + lm.setIslandLevel(world, uuid, 1234); + assertEquals(1234, lm.getIslandLevel(world, uuid)); } @@ -444,20 +447,20 @@ public void testSetIslandLevel() { */ @Test public void testGetRank() { - lm.createAndCleanRankings(world); - Map ttl = lm.getTopTenLists(); - Map tt = ttl.get(world).getTopTen(); - for (long i = 100; i < 150; i++) { - tt.put(UUID.randomUUID().toString(), i); - } - // Put island as lowest rank - tt.put(uuid.toString(), 10L); - assertEquals(51, lm.getRank(world, uuid)); - // Put island as highest rank - tt.put(uuid.toString(), 1000L); - assertEquals(1, lm.getRank(world, uuid)); - // Unknown UUID - lowest rank + 1 - assertEquals(52, lm.getRank(world, UUID.randomUUID())); + lm.createAndCleanRankings(world); + Map ttl = lm.getTopTenLists(); + Map tt = ttl.get(world).getTopTen(); + for (long i = 100; i < 150; i++) { + tt.put(UUID.randomUUID().toString(), i); + } + // Put island as lowest rank + tt.put(uuid.toString(), 10L); + assertEquals(51, lm.getRank(world, uuid)); + // Put island as highest rank + tt.put(uuid.toString(), 1000L); + assertEquals(1, lm.getRank(world, uuid)); + // Unknown UUID - lowest rank + 1 + assertEquals(52, lm.getRank(world, UUID.randomUUID())); } }