Prevents clear region entry for players with items in their inventory

This commit is contained in:
Kristian Knarvik 2024-01-12 01:22:04 +01:00
parent cd83c0404b
commit 69a667dd95

View File

@ -65,7 +65,11 @@ public class PlayerListener extends WorldGuardListener {
return; return;
} }
Player player = event.getPlayer(); 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) @EventHandler(priority = EventPriority.MONITOR)
@ -80,7 +84,7 @@ public class PlayerListener extends WorldGuardListener {
PlayerRegionTracker.replaceRegions(event.getPlayer(), getRegions(event.getPlayer().getLocation())); 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) { public void onTeleport(@NotNull PlayerTeleportEvent event) {
Location fromLocation = event.getFrom(); Location fromLocation = event.getFrom();
Location toLocation = event.getTo(); Location toLocation = event.getTo();
@ -90,7 +94,12 @@ public class PlayerListener extends WorldGuardListener {
} }
ClearOnWorldGuard.logDebugMessage("Detected teleporting player " + event.getPlayer()); 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 <p>The player that moved</p> * @param player <p>The player that moved</p>
* @param from <p>The location the player moved from</p> * @param from <p>The location the player moved from</p>
* @param to <p>The location the player moved to</p> * @param to <p>The location the player moved to</p>
* @return <p>True if whatever called this should be cancelled</p>
*/ */
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<ProtectedRegion> regionsEntered = getRegionsEntered(from, to); Set<ProtectedRegion> regionsEntered = getRegionsEntered(from, to);
if (!regionsEntered.isEmpty()) { if (!regionsEntered.isEmpty()) {
if (!player.getInventory().isEmpty()) {
return true;
}
Bukkit.getPluginManager().callEvent(new EnterClearRegionEvent(player, regionsEntered)); Bukkit.getPluginManager().callEvent(new EnterClearRegionEvent(player, regionsEntered));
} }
@ -112,6 +125,7 @@ public class PlayerListener extends WorldGuardListener {
if (!regionsLeft.isEmpty()) { if (!regionsLeft.isEmpty()) {
Bukkit.getPluginManager().callEvent(new ExitClearRegionEvent(player, regionsLeft)); Bukkit.getPluginManager().callEvent(new ExitClearRegionEvent(player, regionsLeft));
} }
return false;
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)