Skip to content

Commit 9e3f0e5

Browse files
tastybentoclaude
andcommitted
Respect ignore-center-block in the no-place-event block helpers
Addresses the Copilot review on #280: checkBlockLimit and addBlockCount (used for the copper golem's chest, which appears without a place event) bypassed the ignore-center-block exclusion that process() applies, so a build at the island center could be limited or counted when a normal placement there would not be. The comparison also has to be by block coordinates: these helpers receive entity spawn locations with fractional coordinates and yaw/pitch, which never equal the block-aligned center Location. A shared predicate now covers both helpers and processKey. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015dbJyrv2uxHfTmiWkqGUJB
1 parent 4802d28 commit 9e3f0e5

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,22 @@ && isSamePlant(b.getRelative(BlockFace.DOWN).getType(), fixMaterial(blockData)))
472472
*
473473
* @return limit amount if at/over the limit (nothing was counted), or -1 on success
474474
*/
475+
/**
476+
* True when the config ignores the island's center block and {@code loc} is that
477+
* block. Compares block coordinates so entity locations (fractional coordinates,
478+
* yaw/pitch) are matched correctly.
479+
*/
480+
private boolean isIgnoredCenterBlock(Island island, Location loc) {
481+
if (!addon.getConfig().getBoolean("ignore-center-block", true)) {
482+
return false;
483+
}
484+
Location center = island.getCenter();
485+
return center != null && Objects.equals(center.getWorld(), loc.getWorld())
486+
&& center.getBlockX() == loc.getBlockX()
487+
&& center.getBlockY() == loc.getBlockY()
488+
&& center.getBlockZ() == loc.getBlockZ();
489+
}
490+
475491
public int processKey(World world, Location location, NamespacedKey key, boolean add) {
476492
if (!addon.inGameModeWorld(world)) {
477493
return -1;
@@ -483,8 +499,7 @@ public int processKey(World world, Location location, NamespacedKey key, boolean
483499
if (gameMode.isEmpty()) {
484500
return -1;
485501
}
486-
if (addon.getConfig().getBoolean("ignore-center-block", true)
487-
&& i.getCenter().equals(location)) {
502+
if (isIgnoredCenterBlock(i, location)) {
488503
return -1;
489504
}
490505
islandCountMap.putIfAbsent(id, new IslandBlockCount(id, gameMode));
@@ -551,7 +566,7 @@ public int checkBlockLimit(Location loc, NamespacedKey key) {
551566
return addon.getIslands().getIslandAt(loc).map(i -> {
552567
String id = i.getUniqueId();
553568
String gameMode = addon.getGameModeName(w);
554-
if (gameMode.isEmpty()) {
569+
if (gameMode.isEmpty() || isIgnoredCenterBlock(i, loc)) {
555570
return -1;
556571
}
557572
islandCountMap.putIfAbsent(id, new IslandBlockCount(id, gameMode));
@@ -573,7 +588,7 @@ public void addBlockCount(Location loc, NamespacedKey key) {
573588
addon.getIslands().getIslandAt(loc).ifPresent(i -> {
574589
String id = i.getUniqueId();
575590
String gameMode = addon.getGameModeName(w);
576-
if (gameMode.isEmpty()) {
591+
if (gameMode.isEmpty() || isIgnoredCenterBlock(i, loc)) {
577592
return;
578593
}
579594
Environment env = envOf(w);

src/test/java/world/bentobox/limits/listeners/BlockLimitsListenerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,35 @@ void testIndividualLimitStillAppliesInsideGroup() {
10971097
assertTrue(event.isCancelled());
10981098
}
10991099

1100+
// --- Center-block exclusion for the no-place-event helpers (#276 review) ---
1101+
1102+
@Test
1103+
void testCheckBlockLimitIgnoresCenterBlock() {
1104+
// At the limit everywhere else, but the location is the island center block
1105+
IslandBlockCount ibc = new IslandBlockCount("test-island-id", "BSkyBlock");
1106+
ibc.setBlockLimit(Environment.NORMAL, Material.CHEST.getKey(), 0);
1107+
listener.setIsland("test-island-id", ibc);
1108+
1109+
// Entity-style location inside the center block (fractional coordinates)
1110+
Location entityLoc = new Location(world, 0.5, 65.0, 0.3);
1111+
assertEquals(-1, listener.checkBlockLimit(entityLoc, Material.CHEST.getKey()));
1112+
// Away from the center the limit applies
1113+
assertEquals(0, listener.checkBlockLimit(blockLocation, Material.CHEST.getKey()));
1114+
}
1115+
1116+
@Test
1117+
void testAddBlockCountIgnoresCenterBlock() {
1118+
IslandBlockCount ibc = new IslandBlockCount("test-island-id", "BSkyBlock");
1119+
listener.setIsland("test-island-id", ibc);
1120+
1121+
Location entityLoc = new Location(world, 0.5, 65.0, 0.3);
1122+
listener.addBlockCount(entityLoc, Material.CHEST.getKey());
1123+
assertEquals(0, listener.getIsland("test-island-id").getBlockCount(Material.CHEST.getKey()));
1124+
1125+
listener.addBlockCount(blockLocation, Material.CHEST.getKey());
1126+
assertEquals(1, listener.getIsland("test-island-id").getBlockCount(Material.CHEST.getKey()));
1127+
}
1128+
11001129
// --- Custom block keys (#176) ---
11011130

11021131
@Test

0 commit comments

Comments
 (0)