Adds item clearing on teleportation

This commit is contained in:
Kristian Knarvik 2024-01-09 20:24:22 +01:00
parent d3a1ed2145
commit c9a2f082e2

View File

@ -8,13 +8,16 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer; import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery; import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.HashMap;
@ -66,10 +69,10 @@ public class WorldGuardListener implements Listener {
/** /**
* Loads clear regions for a world * Loads clear regions for a world
* *
* @param world <p>The world to load regions for</p> * @param world <p>The world to load regions for</p>
* @param worldsSection <p>The configuration section listing all worlds and regions</p> * @param worldsSection <p>The configuration section listing all worlds and regions</p>
* @param worldId <p>The user-specified identifier for the currently processed world</p> * @param worldId <p>The user-specified identifier for the currently processed world</p>
*/ */
private void loadRegions(@NotNull World world, @NotNull ConfigurationSection worldsSection, @NotNull String worldId) { private void loadRegions(@NotNull World world, @NotNull ConfigurationSection worldsSection, @NotNull String worldId) {
// Get a region manager for the world // Get a region manager for the world
@ -99,17 +102,46 @@ public class WorldGuardListener implements Listener {
if (event.getTo() == null || event.getFrom().getWorld() == null) { if (event.getTo() == null || event.getFrom().getWorld() == null) {
return; return;
} }
World playerWorld = event.getFrom().getWorld();
ApplicableRegionSet setFrom = query.getApplicableRegions(BukkitAdapter.adapt(event.getFrom())); clearIfClearRegionChange(event.getFrom(), event.getTo(), event.getPlayer());
ApplicableRegionSet setTo = query.getApplicableRegions(BukkitAdapter.adapt(event.getTo())); }
@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 <p>The location the player moved or teleported from</p>
* @param toLocation <p>The location the player moved or teleported to</p>
* @param player <p>The player that's moving</p>
*/
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<ProtectedRegion> fromRegions = getOccupiedClearRegions(playerWorld, setFrom.getRegions()); Set<ProtectedRegion> fromRegions = getOccupiedClearRegions(playerWorld, setFrom.getRegions());
Set<ProtectedRegion> toRegions = getOccupiedClearRegions(playerWorld, setTo.getRegions()); Set<ProtectedRegion> toRegions = getOccupiedClearRegions(playerWorld, setTo.getRegions());
// If the player is in one or more clear regions, clear unless the clear regions are the same // 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)) { if ((!fromRegions.isEmpty() || !toRegions.isEmpty()) && !fromRegions.equals(toRegions)) {
event.getPlayer().getInventory().clear(); player.getInventory().clear();
} }
} }