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;
@ -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();
} }
} }