Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
Loading