diff --git a/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java b/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java index 44bb9bd..c860833 100644 --- a/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java +++ b/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java @@ -8,13 +8,16 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.RegionContainer; import com.sk89q.worldguard.protection.regions.RegionQuery; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerTeleportEvent; import org.jetbrains.annotations.NotNull; import java.util.HashMap; @@ -66,10 +69,10 @@ public class WorldGuardListener implements Listener { /** * Loads clear regions for a world - * - * @param world

The world to load regions for

+ * + * @param world

The world to load regions for

* @param worldsSection

The configuration section listing all worlds and regions

- * @param worldId

The user-specified identifier for the currently processed world

+ * @param worldId

The user-specified identifier for the currently processed world

*/ private void loadRegions(@NotNull World world, @NotNull ConfigurationSection worldsSection, @NotNull String worldId) { // Get a region manager for the world @@ -99,17 +102,46 @@ public class WorldGuardListener implements Listener { if (event.getTo() == null || event.getFrom().getWorld() == null) { return; } - World playerWorld = event.getFrom().getWorld(); - ApplicableRegionSet setFrom = query.getApplicableRegions(BukkitAdapter.adapt(event.getFrom())); - ApplicableRegionSet setTo = query.getApplicableRegions(BukkitAdapter.adapt(event.getTo())); + clearIfClearRegionChange(event.getFrom(), event.getTo(), event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onTeleport(@NotNull PlayerTeleportEvent event) { + Location fromLocation = event.getFrom(); + Location toLocation = event.getTo(); + + if (toLocation == null || fromLocation.equals(toLocation)) { + return; + } + clearIfClearRegionChange(fromLocation, toLocation, event.getPlayer()); + } + + /** + * Clears the given player's inventory if they are leaving or entering a clear region + * + * @param fromLocation

The location the player moved or teleported from

+ * @param toLocation

The location the player moved or teleported to

+ * @param player

The player that's moving

+ */ + private void clearIfClearRegionChange(@NotNull Location fromLocation, @NotNull Location toLocation, + @NotNull Player player) { + World playerWorld = fromLocation.getWorld(); + if (playerWorld == null) { + ClearOnWorldGuard.logger().log(Level.WARNING, "Unable to check region change, as location " + + fromLocation + " has no world."); + return; + } + + ApplicableRegionSet setFrom = query.getApplicableRegions(BukkitAdapter.adapt(fromLocation)); + ApplicableRegionSet setTo = query.getApplicableRegions(BukkitAdapter.adapt(toLocation)); Set fromRegions = getOccupiedClearRegions(playerWorld, setFrom.getRegions()); Set toRegions = getOccupiedClearRegions(playerWorld, setTo.getRegions()); // If the player is in one or more clear regions, clear unless the clear regions are the same if ((!fromRegions.isEmpty() || !toRegions.isEmpty()) && !fromRegions.equals(toRegions)) { - event.getPlayer().getInventory().clear(); + player.getInventory().clear(); } }