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..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. @@ -1693,16 +1694,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..53da2b0 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); @@ -858,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));