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);