From 89f86a15f48f8abf5fb78afa700e258ccb528791 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 21:40:44 +0000 Subject: [PATCH 1/2] Initial plan From 4110ddefdf9d76c008982827e430c67a7fabd8f8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 21:47:21 +0000 Subject: [PATCH 2/2] fix: cap donated block counts to current block limits during level calculation When a block limit is lowered after players have already donated blocks, the donated count is now capped to the current limit in IslandLevelCalculator.tidyUp(). This ensures excess donated blocks no longer contribute points beyond the limit. Adds three new tests to IslandLevelCalculatorTidyUpTest covering: - donated count under limit (full count used) - donated count over limit (capped to current limit) - donated count with no limit (full count used) Agent-Logs-Url: https://github.com/BentoBoxWorld/Level/sessions/3c49eef0-18f1-4340-bdd3-2afd5ce36286 Co-authored-by: tastybento <4407265+tastybento@users.noreply.github.com> --- .../calculators/IslandLevelCalculator.java | 13 +++- .../IslandLevelCalculatorTidyUpTest.java | 69 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java index 53bb296..1256210 100644 --- a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java +++ b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java @@ -787,12 +787,19 @@ public void tidyUp() { // Add donated block points (permanent contributions that persist across recalculations). // Recalculate from the donated blocks map using current block config values so the // level always reflects the current configuration, even if block values changed since donation. + // Also apply the current block limit: if the limit was lowered after donation, only count + // up to the current limit (donated blocks over the limit are silently ignored). Map donatedBlocksMap = addon.getManager().getDonatedBlocks(island); long donatedPoints = donatedBlocksMap.entrySet().stream() .mapToLong(entry -> { - Integer value = addon.getBlockConfig().getValue(island.getWorld(), - entry.getKey().toLowerCase(java.util.Locale.ENGLISH)); - return (long) Objects.requireNonNullElse(value, 0) * entry.getValue(); + String key = entry.getKey().toLowerCase(java.util.Locale.ENGLISH); + Integer value = addon.getBlockConfig().getValue(island.getWorld(), key); + int count = entry.getValue(); + Integer limit = addon.getBlockConfig().getLimit(key); + if (limit != null) { + count = Math.min(count, limit); + } + return (long) Objects.requireNonNullElse(value, 0) * count; }) .sum(); results.rawBlockCount.addAndGet(donatedPoints); diff --git a/src/test/java/world/bentobox/level/calculators/IslandLevelCalculatorTidyUpTest.java b/src/test/java/world/bentobox/level/calculators/IslandLevelCalculatorTidyUpTest.java index 545989b..9dfcf81 100644 --- a/src/test/java/world/bentobox/level/calculators/IslandLevelCalculatorTidyUpTest.java +++ b/src/test/java/world/bentobox/level/calculators/IslandLevelCalculatorTidyUpTest.java @@ -7,6 +7,7 @@ import static org.mockito.Mockito.when; import java.util.Collections; +import java.util.Map; import java.util.concurrent.CompletableFuture; import org.bukkit.Location; @@ -183,4 +184,72 @@ void belowStart_sqrtFormula() { long remaining = r.getPointsToNextLevel(); assertEquals(true, remaining > 0, "pointsToNextLevel should be positive, got " + remaining); } + + @Test + @DisplayName("Donated blocks under limit: full donation count is used") + void donatedBlocksUnderLimit() { + // Player donated 500 iron blocks; limit is 1000; block value is 3. + // Expected donated points = 500 * 3 = 1500. + when(manager.getDonatedBlocks(any(Island.class))).thenReturn(Map.of("IRON_BLOCK", 500)); + when(blockConfig.getValue(any(), any(String.class))).thenAnswer(inv -> { + String key = inv.getArgument(1); + return "iron_block".equals(key) ? 3 : 0; + }); + when(blockConfig.getLimit(any(String.class))).thenAnswer(inv -> { + String key = inv.getArgument(0); + return "iron_block".equals(key) ? 1000 : null; + }); + + IslandLevelCalculator calc = newCalculator(); + Results r = calc.getResults(); + r.rawBlockCount.set(INITIAL_COUNT); + calc.tidyUp(); + + assertEquals(1500L, r.getDonatedPoints(), "donated points should equal 500 * 3 = 1500"); + } + + @Test + @DisplayName("Donated blocks exceed limit: only blocks up to the current limit count") + void donatedBlocksExceedLimit() { + // Player donated 1000 iron blocks; limit was later changed to 500; block value is 3. + // Expected donated points = 500 * 3 = 1500 (NOT 1000 * 3 = 3000). + when(manager.getDonatedBlocks(any(Island.class))).thenReturn(Map.of("IRON_BLOCK", 1000)); + when(blockConfig.getValue(any(), any(String.class))).thenAnswer(inv -> { + String key = inv.getArgument(1); + return "iron_block".equals(key) ? 3 : 0; + }); + when(blockConfig.getLimit(any(String.class))).thenAnswer(inv -> { + String key = inv.getArgument(0); + return "iron_block".equals(key) ? 500 : null; + }); + + IslandLevelCalculator calc = newCalculator(); + Results r = calc.getResults(); + r.rawBlockCount.set(INITIAL_COUNT); + calc.tidyUp(); + + assertEquals(1500L, r.getDonatedPoints(), + "donated points should be capped at 500 * 3 = 1500, not 1000 * 3 = 3000"); + } + + @Test + @DisplayName("Donated blocks with no limit: full count is used") + void donatedBlocksNoLimit() { + // Player donated 1000 iron blocks; no limit configured; block value is 3. + // Expected donated points = 1000 * 3 = 3000. + when(manager.getDonatedBlocks(any(Island.class))).thenReturn(Map.of("IRON_BLOCK", 1000)); + when(blockConfig.getValue(any(), any(String.class))).thenAnswer(inv -> { + String key = inv.getArgument(1); + return "iron_block".equals(key) ? 3 : 0; + }); + when(blockConfig.getLimit(any(String.class))).thenReturn(null); + + IslandLevelCalculator calc = newCalculator(); + Results r = calc.getResults(); + r.rawBlockCount.set(INITIAL_COUNT); + calc.tidyUp(); + + assertEquals(3000L, r.getDonatedPoints(), + "donated points should equal 1000 * 3 = 3000 when no limit is configured"); + } }