From 1a6d7e98b7cad4d6038a015476e903d711bf9a78 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 16 Nov 2026 20:32:25 -0800 Subject: [PATCH 1/2] Start a new exhaustion period when the cooldown ends A cooldown shorter than the exhaustion period did nothing: when it expired the block count was still at the limit, so the generator went straight back on cooldown until the period ended. Now an expired cooldown starts a fresh period with the count reset to 0. A cooldown of 0 keeps its previous meaning (wait until the period ends) by holding the generator until the period end, which also makes the generator-exhausted message show the correct minutes remaining. Co-Authored-By: Claude Opus 5.5 --- .../config/Settings.java | 2 + .../managers/StoneGeneratorManager.java | 13 ++++-- .../managers/StoneGeneratorManagerTest.java | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/magiccobblestonegenerator/config/Settings.java b/src/main/java/world/bentobox/magiccobblestonegenerator/config/Settings.java index 34831d7..5891e1c 100644 --- a/src/main/java/world/bentobox/magiccobblestonegenerator/config/Settings.java +++ b/src/main/java/world/bentobox/magiccobblestonegenerator/config/Settings.java @@ -614,6 +614,8 @@ public enum GuiAction @ConfigComment("") @ConfigComment("This indicates the length, in minutes, that a generator will remain on a cooldown, once it") @ConfigComment("reaches its exhaustion limit. While on cooldown, the generator will not generate any new blocks.") + @ConfigComment("When the cooldown ends, a new period starts with the block count reset to 0.") + @ConfigComment("0 means no extra cooldown: the generator stops until the current period ends.") @ConfigEntry(path = "exhaustion.cooldown") private long generatorExhaustionCooldown = 1440; diff --git a/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java b/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java index 7fe34d2..9f06867 100644 --- a/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java +++ b/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java @@ -1693,16 +1693,21 @@ public boolean canGenerateBlock(@NotNull Island island, @NotNull GeneratorTierOb return false; } - if (exhaustionData.getPeriodStart() == 0 || now - exhaustionData.getPeriodStart() >= periodMillis) { - // Start a new exhaustion period. + if (exhaustionData.getPeriodStart() == 0 || now - exhaustionData.getPeriodStart() >= periodMillis + || exhaustionData.getCooldownUntil() > 0) { + // Start a new exhaustion period: first use, the previous period ended, or a cooldown just expired. + // Without the last condition, a cooldown shorter than the period would end with the count still at + // the limit and the generator would immediately go back on cooldown until the period ended. exhaustionData.setPeriodStart(now); exhaustionData.setGeneratedCount(0); exhaustionData.setCooldownUntil(0); } if (exhaustionData.getGeneratedCount() >= limit) { - // Limit reached. Put the generator tier on a cooldown. - exhaustionData.setCooldownUntil(now + cooldownMillis); + // Limit reached. Put the generator tier on a cooldown. A cooldown of 0 means "no extra cooldown": + // the generator waits until the current period ends. + exhaustionData.setCooldownUntil(cooldownMillis > 0 ? now + cooldownMillis + : exhaustionData.getPeriodStart() + periodMillis); this.notifyExhaustion(island, generatorTier, exhaustionData, now); return false; } diff --git a/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java b/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java index 267e9b1..fb39f3d 100644 --- a/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java +++ b/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java @@ -40,6 +40,7 @@ import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon; import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorBundleObject; import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorDataObject; +import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorExhaustionData; import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject; import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject.GeneratorType; import world.bentobox.magiccobblestonegenerator.events.GeneratorBuyEvent; @@ -724,6 +725,48 @@ void testCanGenerateBlockExhausted() { assertFalse(sgm.canGenerateBlock(island, generatorTier)); } + @Test + void testCanGenerateBlockCooldownShorterThanPeriodStartsNewPeriod() { + s.setGeneratorExhaustionLimit(1); + s.setGeneratorExhaustionPeriod(60); + s.setGeneratorExhaustionCooldown(10); + assertTrue(sgm.canGenerateBlock(island, generatorTier)); + assertFalse(sgm.canGenerateBlock(island, generatorTier)); + + // Simulate the 10-minute cooldown expiring while the 60-minute period is still running. + GeneratorExhaustionData exhaustion = + sgm.getGeneratorData(island).getExhaustionData().get(generatorTier.getUniqueId()); + long now = System.currentTimeMillis(); + exhaustion.setPeriodStart(now - 11 * 60_000L); + exhaustion.setCooldownUntil(now - 60_000L); + + // Generator must work again, with a fresh count. + assertTrue(sgm.canGenerateBlock(island, generatorTier)); + assertEquals(1, exhaustion.getGeneratedCount()); + assertEquals(0, exhaustion.getCooldownUntil()); + } + + @Test + void testCanGenerateBlockZeroCooldownWaitsForPeriodEnd() { + s.setGeneratorExhaustionLimit(1); + s.setGeneratorExhaustionPeriod(60); + s.setGeneratorExhaustionCooldown(0); + assertTrue(sgm.canGenerateBlock(island, generatorTier)); + assertFalse(sgm.canGenerateBlock(island, generatorTier)); + + GeneratorExhaustionData exhaustion = + sgm.getGeneratorData(island).getExhaustionData().get(generatorTier.getUniqueId()); + // Blocked until the period ends, not released immediately. + assertEquals(exhaustion.getPeriodStart() + 60 * 60_000L, exhaustion.getCooldownUntil()); + assertFalse(sgm.canGenerateBlock(island, generatorTier)); + + // Once the period is over, generation resumes. + long now = System.currentTimeMillis(); + exhaustion.setPeriodStart(now - 61 * 60_000L); + exhaustion.setCooldownUntil(now - 60_000L); + assertTrue(sgm.canGenerateBlock(island, generatorTier)); + } + @Test void testCanGenerateBlockTierOverridesLimit() { s.setGeneratorExhaustionLimit(1000); From 75cf2d3675f43a5446aead41841775cae5b7b695 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 16 Nov 2026 20:47:47 -0800 Subject: [PATCH 2/2] Fix NPE in canPurchaseGenerator on spawn or unowned islands The number formatter used owner.getLocale(), but owner is null on spawn islands and islands without an owner. Format with the purchasing user's locale instead: they are the one who receives the messages. Fixes Sonar java:S2259. Co-Authored-By: Claude Opus 5.5 --- .../managers/StoneGeneratorManager.java | 3 ++- .../managers/StoneGeneratorManagerTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java b/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java index 9f06867..7fde9cc 100644 --- a/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java +++ b/src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManager.java @@ -1322,7 +1322,8 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island, @NotNull GeneratorDataObject generatorData, @NotNull GeneratorTierObject generatorTier) { final User owner = island.isSpawn() || island.getOwner() == null ? null : User.getInstance(island.getOwner()); - NumberFormat numberFormat = NumberFormat.getNumberInstance(owner.getLocale()); + // Numbers are shown to the purchasing user, so format them in their locale. The owner may be null. + NumberFormat numberFormat = NumberFormat.getNumberInstance(user.getLocale()); if (generatorData.getPurchasedTiers().contains(generatorTier.getUniqueId())) { // Generator is not unlocked. Return false. diff --git a/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java b/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java index fb39f3d..53da2b0 100644 --- a/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java +++ b/src/test/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorManagerTest.java @@ -901,6 +901,19 @@ void testCanPurchaseGenerator() { assertFalse(sgm.canPurchaseGenerator(user, island, generatorData, generatorTier)); } + @Test + void testCanPurchaseGeneratorOnSpawnIslandDoesNotThrow() { + // Spawn islands (and unowned islands) have no owner; this used to throw a NullPointerException. + when(island.isSpawn()).thenReturn(true); + assertDoesNotThrow(() -> sgm.canPurchaseGenerator(user, island, generatorData, generatorTier)); + } + + @Test + void testCanPurchaseGeneratorNoOwnerDoesNotThrow() { + when(island.getOwner()).thenReturn(null); + assertDoesNotThrow(() -> sgm.canPurchaseGenerator(user, island, generatorData, generatorTier)); + } + @Test void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObject() { assertDoesNotThrow(() -> sgm.purchaseGenerator(user, island, generatorData, generatorTier));