From 20a22ae21c568c660a7584b6fb20c1dba805b92f Mon Sep 17 00:00:00 2001 From: byPixelTV Date: Tue, 27 Oct 2026 19:34:18 +0100 Subject: [PATCH 1/2] fix: some things were read of the wrong thread on folia --- .../sk89q/worldguard/bukkit/cause/Cause.java | 17 +++++++++++++++++ .../listener/EventAbstractionListener.java | 2 +- .../bukkit/listener/RegionFlagsListener.java | 18 +++++++----------- .../listener/RegionProtectionListener.java | 6 +++--- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java index 643dc3e4b..1b8109036 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java @@ -27,6 +27,7 @@ import com.sk89q.worldguard.bukkit.util.Entities; import io.papermc.lib.PaperLib; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; import org.bukkit.entity.AnimalTamer; @@ -187,6 +188,22 @@ public Block getFirstBlock() { return null; } + /** + * Get the first causing block's current material when its region is owned + * by the current thread. A cause may refer to a block in another region + * or world, for example the origin of a falling block. + * + * @return the material, or null if the block cannot be read here + */ + @Nullable + public Material getFirstBlockType() { + Block block = getFirstBlock(); + if (block == null || (WorldGuardPlugin.inst().isFolia() && !Bukkit.isOwnedByCurrentRegion(block.getLocation()))) { + return null; + } + return block.getType(); + } + /** * Find the first type matching one in the given array. * diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index 0b274f505..267e7ea37 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -246,7 +246,7 @@ public void onBlockPlace(BlockPlaceEvent event) { BlockState previousState = event.getBlockReplacedState(); // Some blocks, like tall grass and fire, get replaced - if (previousState.getType() != Material.AIR && previousState.getType() != event.getBlockReplacedState().getType()) { + if (previousState.getType() != Material.AIR && previousState.getType() != event.getBlockPlaced().getType()) { Events.fireToCancel(event, new BreakBlockEvent(event, create(event.getPlayer()), previousState.getLocation(), previousState.getType())); } diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java index 957579cea..decc316c7 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java @@ -33,8 +33,8 @@ import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.regions.RegionQuery; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -62,11 +62,9 @@ public void onPlaceBlock(final PlaceBlockEvent event) { RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); - Block block; - if ((block = event.getCause().getFirstBlock()) != null) { - if (Materials.isPistonBlock(block.getType())) { - event.filter(testState(query, Flags.PISTONS), false); - } + Material causeType = event.getCause().getFirstBlockType(); + if (causeType != null && Materials.isPistonBlock(causeType)) { + event.filter(testState(query, Flags.PISTONS), false); } if (event.getCause().find(EntityType.SNOW_GOLEM) != null) { @@ -85,11 +83,9 @@ public void onBreakBlock(final BreakBlockEvent event) { WorldConfiguration config = getWorldConfig(event.getWorld()); RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); - Block block; - if ((block = event.getCause().getFirstBlock()) != null) { - if (Materials.isPistonBlock(block.getType())) { - event.filter(testState(query, Flags.PISTONS), false); - } + Material causeType = event.getCause().getFirstBlockType(); + if (causeType != null && Materials.isPistonBlock(causeType)) { + event.filter(testState(query, Flags.PISTONS), false); } if (event.getCause().find(EntityType.CREEPER) != null) { // Creeper diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java index 5351d08f0..f69908469 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java @@ -177,9 +177,9 @@ public void onPlaceBlock(final PlaceBlockEvent event) { /* Flint and steel, fire charge, etc. */ if (Materials.isFire(type)) { - Block block = event.getCause().getFirstBlock(); - boolean fire = block != null && Materials.isFire(block.getType()); - boolean lava = block != null && Materials.isLava(block.getType()); + Material causeType = event.getCause().getFirstBlockType(); + boolean fire = causeType != null && Materials.isFire(causeType); + boolean lava = causeType != null && Materials.isLava(causeType); List flags = new ArrayList<>(); flags.add(Flags.BLOCK_PLACE); flags.add(Flags.LIGHTER); From 6cff74a0b7dca4a82d374292e1264a872f8011a6 Mon Sep 17 00:00:00 2001 From: byPixelTV Date: Sun, 1 Nov 2026 10:23:00 +0100 Subject: [PATCH 2/2] fix: move Folia ownership check into getFirstBlock --- .../sk89q/worldguard/bukkit/cause/Cause.java | 25 ++++++------------- .../bukkit/listener/RegionFlagsListener.java | 10 ++++---- .../listener/RegionProtectionListener.java | 3 ++- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java index 1b8109036..e09126d01 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/cause/Cause.java @@ -27,7 +27,6 @@ import com.sk89q.worldguard.bukkit.util.Entities; import io.papermc.lib.PaperLib; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; import org.bukkit.entity.AnimalTamer; @@ -177,33 +176,23 @@ public Entity getFirstNonPlayerEntity() { return null; } + /** + * Get the first causing block if it is accessible from the current thread. + * On Folia, the block may belong to a different region or world. + * + * @return the block, or null if there is no block or its region is not owned by the current thread + */ @Nullable public Block getFirstBlock() { for (Object object : causes) { if (object instanceof Block b) { - return b; + return WorldGuardPlugin.inst().isFolia() && !Bukkit.isOwnedByCurrentRegion(b.getLocation()) ? null : b; } } return null; } - /** - * Get the first causing block's current material when its region is owned - * by the current thread. A cause may refer to a block in another region - * or world, for example the origin of a falling block. - * - * @return the material, or null if the block cannot be read here - */ - @Nullable - public Material getFirstBlockType() { - Block block = getFirstBlock(); - if (block == null || (WorldGuardPlugin.inst().isFolia() && !Bukkit.isOwnedByCurrentRegion(block.getLocation()))) { - return null; - } - return block.getType(); - } - /** * Find the first type matching one in the given array. * diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java index decc316c7..8ef095821 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionFlagsListener.java @@ -33,8 +33,8 @@ import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.regions.RegionQuery; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -62,8 +62,8 @@ public void onPlaceBlock(final PlaceBlockEvent event) { RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); - Material causeType = event.getCause().getFirstBlockType(); - if (causeType != null && Materials.isPistonBlock(causeType)) { + Block causeBlock = event.getCause().getFirstBlock(); + if (causeBlock != null && Materials.isPistonBlock(causeBlock.getType())) { event.filter(testState(query, Flags.PISTONS), false); } @@ -83,8 +83,8 @@ public void onBreakBlock(final BreakBlockEvent event) { WorldConfiguration config = getWorldConfig(event.getWorld()); RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); - Material causeType = event.getCause().getFirstBlockType(); - if (causeType != null && Materials.isPistonBlock(causeType)) { + Block causeBlock = event.getCause().getFirstBlock(); + if (causeBlock != null && Materials.isPistonBlock(causeBlock.getType())) { event.filter(testState(query, Flags.PISTONS), false); } diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java index f69908469..265b553d8 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/RegionProtectionListener.java @@ -177,7 +177,8 @@ public void onPlaceBlock(final PlaceBlockEvent event) { /* Flint and steel, fire charge, etc. */ if (Materials.isFire(type)) { - Material causeType = event.getCause().getFirstBlockType(); + Block causeBlock = event.getCause().getFirstBlock(); + Material causeType = causeBlock != null ? causeBlock.getType() : null; boolean fire = causeType != null && Materials.isFire(causeType); boolean lava = causeType != null && Materials.isLava(causeType); List flags = new ArrayList<>();