|
35 | 35 | import org.bukkit.block.data.type.TechnicalPiston; |
36 | 36 | import org.bukkit.configuration.file.FileConfiguration; |
37 | 37 | import org.bukkit.configuration.file.YamlConfiguration; |
| 38 | +import org.bukkit.entity.EntityType; |
38 | 39 | import org.bukkit.entity.Player; |
39 | 40 | import org.bukkit.ExplosionResult; |
40 | 41 | import org.bukkit.entity.Entity; |
@@ -1308,4 +1309,79 @@ void testSaveSavesChangedIslands() { |
1308 | 1309 | // saveObjectAsync is called once by setIsland and once by save |
1309 | 1310 | verify(dbMock, atLeastOnce()).saveObjectAsync(any()); |
1310 | 1311 | } |
| 1312 | + |
| 1313 | + // --- Entity count persistence tests --- |
| 1314 | + |
| 1315 | + @Test |
| 1316 | + void testIncrementEntityCreatesRecordAndCounts() { |
| 1317 | + when(island.getGameMode()).thenReturn("BSkyBlock"); |
| 1318 | + |
| 1319 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1320 | + |
| 1321 | + IslandBlockCount ibc = listener.getIsland("test-island-id"); |
| 1322 | + assertNotNull(ibc); |
| 1323 | + assertEquals(1, ibc.getEntityCount(Environment.NORMAL, EntityType.CHICKEN)); |
| 1324 | + } |
| 1325 | + |
| 1326 | + @Test |
| 1327 | + void testDecrementEntityLowersCount() { |
| 1328 | + when(island.getGameMode()).thenReturn("BSkyBlock"); |
| 1329 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1330 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1331 | + |
| 1332 | + listener.decrementEntity("test-island-id", Environment.NORMAL, EntityType.CHICKEN); |
| 1333 | + |
| 1334 | + assertEquals(1, listener.getIsland("test-island-id").getEntityCount(Environment.NORMAL, EntityType.CHICKEN)); |
| 1335 | + } |
| 1336 | + |
| 1337 | + @Test |
| 1338 | + void testDecrementEntityWithoutRecordIsNoOp() { |
| 1339 | + // No record exists and repeated no-op decrements must not count toward the batch save |
| 1340 | + for (int i = 0; i < 20; i++) { |
| 1341 | + listener.decrementEntity("unknown-island", Environment.NORMAL, EntityType.CHICKEN); |
| 1342 | + } |
| 1343 | + |
| 1344 | + assertNull(listener.getIsland("unknown-island")); |
| 1345 | + Database<?> dbMock = mockedDb.constructed().get(0); |
| 1346 | + verify(dbMock, never()).saveObjectAsync(any()); |
| 1347 | + } |
| 1348 | + |
| 1349 | + @Test |
| 1350 | + void testEntityChangesBatchSaveAfterThreshold() { |
| 1351 | + when(island.getGameMode()).thenReturn("BSkyBlock"); |
| 1352 | + // CHANGE_LIMIT = 9, so the 10th change triggers a save — entity changes must |
| 1353 | + // join the same batch-save cycle as block changes (they used to be persisted |
| 1354 | + // only on addon disable, losing counts on a crash) |
| 1355 | + for (int i = 0; i < 10; i++) { |
| 1356 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1357 | + } |
| 1358 | + |
| 1359 | + Database<?> dbMock = mockedDb.constructed().get(0); |
| 1360 | + verify(dbMock, atLeastOnce()).saveObjectAsync(any()); |
| 1361 | + } |
| 1362 | + |
| 1363 | + @Test |
| 1364 | + void testEntityChangesNoSaveBeforeThreshold() { |
| 1365 | + when(island.getGameMode()).thenReturn("BSkyBlock"); |
| 1366 | + for (int i = 0; i < 9; i++) { |
| 1367 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1368 | + } |
| 1369 | + |
| 1370 | + Database<?> dbMock = mockedDb.constructed().get(0); |
| 1371 | + verify(dbMock, never()).saveObjectAsync(any()); |
| 1372 | + } |
| 1373 | + |
| 1374 | + @Test |
| 1375 | + void testMixedEntityIncrementAndDecrementCountTowardBatchSave() { |
| 1376 | + when(island.getGameMode()).thenReturn("BSkyBlock"); |
| 1377 | + for (int i = 0; i < 5; i++) { |
| 1378 | + listener.incrementEntity(island, Environment.NORMAL, EntityType.CHICKEN); |
| 1379 | + } |
| 1380 | + for (int i = 0; i < 5; i++) { |
| 1381 | + listener.decrementEntity("test-island-id", Environment.NORMAL, EntityType.CHICKEN); |
| 1382 | + } |
| 1383 | + |
| 1384 | + Database<?> dbMock = mockedDb.constructed().get(0); |
| 1385 | + verify(dbMock, atLeastOnce()).saveObjectAsync(any()); |
| 1386 | + } |
1311 | 1387 | } |
0 commit comments