Skip to content

Commit ca636d2

Browse files
authored
Fix NPE when non-player entities create new nether portals (#1995)
* Fix NPE when non-player entities create new nether portals * Fix flag checking for portal create event * Use failed load region set * Clean up
1 parent bddfbdd commit ca636d2

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
import com.sk89q.worldguard.WorldGuard;
2626
import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;
2727
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
28+
import com.sk89q.worldguard.bukkit.cause.Cause;
2829
import com.sk89q.worldguard.bukkit.util.Entities;
30+
import com.sk89q.worldguard.bukkit.util.InteropUtils;
2931
import com.sk89q.worldguard.config.ConfigurationManager;
3032
import com.sk89q.worldguard.config.WorldConfiguration;
3133
import com.sk89q.worldguard.protection.ApplicableRegionSet;
34+
import com.sk89q.worldguard.protection.FailedLoadRegionSet;
3235
import com.sk89q.worldguard.protection.association.RegionAssociable;
3336
import com.sk89q.worldguard.protection.flags.Flags;
3437
import com.sk89q.worldguard.protection.flags.StateFlag;
38+
import com.sk89q.worldguard.protection.flags.StateFlag.State;
3539
import com.sk89q.worldguard.protection.managers.RegionManager;
3640
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
3741
import com.sk89q.worldguard.protection.regions.RegionQuery;
@@ -653,35 +657,46 @@ public void onCreatureSpawn(CreatureSpawnEvent event) {
653657
public void onCreatePortal(PortalCreateEvent event) {
654658
WorldConfiguration wcfg = getWorldConfig(event.getWorld());
655659

656-
if (wcfg.regionNetherPortalProtection
660+
if (wcfg.useRegions && wcfg.regionNetherPortalProtection
657661
&& event.getReason() == PortalCreateEvent.CreateReason.NETHER_PAIR
658662
&& !event.getBlocks().isEmpty()) {
659663
final com.sk89q.worldedit.world.World world = BukkitAdapter.adapt(event.getWorld());
660-
final RegionManager regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer()
661-
.get(world);
662-
if (regionManager == null) return;
663-
LocalPlayer associable = null;
664-
if (event.getEntity() instanceof Player player) {
665-
associable = getPlugin().wrapPlayer(player);
666-
if (WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(associable, world)) {
664+
final Cause cause = Cause.create(event.getEntity());
665+
LocalPlayer localPlayer = null;
666+
if (cause.getRootCause() instanceof Player player) {
667+
if (wcfg.fakePlayerBuildOverride && InteropUtils.isFakePlayer(player)) {
668+
return;
669+
}
670+
localPlayer = getPlugin().wrapPlayer(player);
671+
if (WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(localPlayer, world)) {
667672
return;
668673
}
669674
}
670-
BlockVector3 min = null;
671-
BlockVector3 max = null;
672-
for (BlockState block : event.getBlocks()) {
673-
BlockVector3 loc = BlockVector3.at(block.getX(), block.getY(), block.getZ());
674-
min = min == null ? loc : loc.getMinimum(min);
675-
max = max == null ? loc : loc.getMaximum(max);
676-
}
677-
ProtectedCuboidRegion target = new ProtectedCuboidRegion("__portal_check", true, min, max);
678-
final ApplicableRegionSet regions = regionManager.getApplicableRegions(target);
679-
if (!regions.testState(associable, Flags.BUILD, Flags.BLOCK_PLACE)) {
680-
if (associable != null) {
675+
final RegionManager regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer()
676+
.get(world);
677+
ApplicableRegionSet regions;
678+
if (regionManager == null) {
679+
regions = FailedLoadRegionSet.getInstance();
680+
} else {
681+
BlockVector3 min = null;
682+
BlockVector3 max = null;
683+
for (BlockState block : event.getBlocks()) {
684+
BlockVector3 loc = BlockVector3.at(block.getX(), block.getY(), block.getZ());
685+
min = min == null ? loc : loc.getMinimum(min);
686+
max = max == null ? loc : loc.getMaximum(max);
687+
}
688+
ProtectedCuboidRegion target = new ProtectedCuboidRegion("__portal_check", true, min, max);
689+
regions = regionManager.getApplicableRegions(target);
690+
}
691+
final RegionAssociable associable = createRegionAssociable(cause);
692+
final State buildState = StateFlag.denyToNone(regions.queryState(associable, Flags.BUILD));
693+
if (!StateFlag.test(buildState, regions.queryState(associable, Flags.BLOCK_BREAK))
694+
|| !StateFlag.test(buildState, regions.queryState(associable, Flags.BLOCK_PLACE))) {
695+
if (localPlayer != null && !cause.isIndirect()) {
681696
// NB there is no way to cancel the teleport without PTA (since PlayerPortal doesn't have block info)
682697
// removing PTA was a mistake
683-
String message = regions.queryValue(associable, Flags.DENY_MESSAGE);
684-
RegionProtectionListener.formatAndSendDenyMessage("create portals", associable, message);
698+
String message = regions.queryValue(localPlayer, Flags.DENY_MESSAGE);
699+
RegionProtectionListener.formatAndSendDenyMessage("create portals", localPlayer, message);
685700
}
686701
event.setCancelled(true);
687702
}

0 commit comments

Comments
 (0)