diff --git a/src/main/java/net/knarcraft/clearonworldguard/listener/PlayerListener.java b/src/main/java/net/knarcraft/clearonworldguard/listener/PlayerListener.java index 0bce4db..459b2b6 100644 --- a/src/main/java/net/knarcraft/clearonworldguard/listener/PlayerListener.java +++ b/src/main/java/net/knarcraft/clearonworldguard/listener/PlayerListener.java @@ -65,7 +65,11 @@ public class PlayerListener extends WorldGuardListener { return; } Player player = event.getPlayer(); - updateClearRegions(player, from, to); + if (updateClearRegions(player, from, to)) { + /* This must be done by overwriting the destination, instead of cancelling, to prevent the user from + momentarily entering the game-mode of the clear region. */ + event.setTo(from); + } } @EventHandler(priority = EventPriority.MONITOR) @@ -80,7 +84,7 @@ public class PlayerListener extends WorldGuardListener { PlayerRegionTracker.replaceRegions(event.getPlayer(), getRegions(event.getPlayer().getLocation())); } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onTeleport(@NotNull PlayerTeleportEvent event) { Location fromLocation = event.getFrom(); Location toLocation = event.getTo(); @@ -90,7 +94,12 @@ public class PlayerListener extends WorldGuardListener { } ClearOnWorldGuard.logDebugMessage("Detected teleporting player " + event.getPlayer()); - updateClearRegions(event.getPlayer(), fromLocation, toLocation); + if (updateClearRegions(event.getPlayer(), fromLocation, toLocation)) { + event.setCancelled(true); + /* If this is missing, and the region puts players in creative mode, WorldGuard puts players in creative + mode until they move, even though the teleportation is cancelled. */ + Bukkit.getPluginManager().callEvent(new PlayerTeleportEvent(event.getPlayer(), toLocation, fromLocation)); + } } /** @@ -99,10 +108,14 @@ public class PlayerListener extends WorldGuardListener { * @param player

The player that moved

* @param from

The location the player moved from

* @param to

The location the player moved to

+ * @return

True if whatever called this should be cancelled

*/ - private void updateClearRegions(@NotNull Player player, @NotNull Location from, @NotNull Location to) { + private boolean updateClearRegions(@NotNull Player player, @NotNull Location from, @NotNull Location to) { Set regionsEntered = getRegionsEntered(from, to); if (!regionsEntered.isEmpty()) { + if (!player.getInventory().isEmpty()) { + return true; + } Bukkit.getPluginManager().callEvent(new EnterClearRegionEvent(player, regionsEntered)); } @@ -112,6 +125,7 @@ public class PlayerListener extends WorldGuardListener { if (!regionsLeft.isEmpty()) { Bukkit.getPluginManager().callEvent(new ExitClearRegionEvent(player, regionsLeft)); } + return false; } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)