diff --git a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/WorldGuardUtils.java b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/WorldGuardUtils.java index 6fc471e..bec2aa1 100644 --- a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/WorldGuardUtils.java +++ b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/WorldGuardUtils.java @@ -2,11 +2,16 @@ package net.goldtreeservers.worldguardextraflags.wg; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.protection.FlagValueCalculator; @@ -87,4 +92,37 @@ public class WorldGuardUtils return new FlagValueCalculator(checkForRegions, global); } + + @SuppressWarnings("unchecked") + public static boolean hasNoTeleportLoop(Plugin plugin, Player player, Object location) + { + MetadataValue result = player.getMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META).stream() + .filter((p) -> p.getOwningPlugin().equals(plugin)) + .findFirst() + .orElse(null); + + if (result == null) + { + result = new FixedMetadataValue(plugin, new HashSet<>()); + + player.setMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META, result); + + new BukkitRunnable() + { + @Override + public void run() + { + player.removeMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META, plugin); + } + }.runTask(plugin); + } + + Set set = (Set)result.value(); + if (set.add(location)) + { + return true; + } + + return false; + } } diff --git a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnEntryFlagHandler.java b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnEntryFlagHandler.java index aeb7232..45d5f4a 100644 --- a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnEntryFlagHandler.java +++ b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnEntryFlagHandler.java @@ -4,7 +4,6 @@ import java.util.Set; import org.bukkit.Location; import org.bukkit.entity.Player; -import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.plugin.Plugin; import com.sk89q.worldguard.protection.ApplicableRegionSet; @@ -46,17 +45,10 @@ public class TeleportOnEntryFlagHandler extends HandlerWrapper @Override public boolean onCrossBoundary(Player player, Location from, Location to, ApplicableRegionSet toSet, Set entered, Set exited, MoveType moveType) { - if (!player.hasMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META)) + Object location = WorldGuardUtils.queryValueUnchecked(player, to.getWorld(), entered, Flags.TELEPORT_ON_ENTRY); + if (location != null && WorldGuardUtils.hasNoTeleportLoop(this.getPlugin(), player, location)) { - Object location = WorldGuardUtils.queryValueUnchecked(player, to.getWorld(), entered, Flags.TELEPORT_ON_ENTRY); - if (location != null) - { - player.setMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META, new FixedMetadataValue(this.getPlugin(), true)); - - player.teleport(WorldEditUtils.toLocation(location)); - - return false; - } + player.teleport(WorldEditUtils.toLocation(location)); } return true; diff --git a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnExitFlagHandler.java b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnExitFlagHandler.java index eada8d9..c428ccc 100644 --- a/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnExitFlagHandler.java +++ b/WG/src/main/java/net/goldtreeservers/worldguardextraflags/wg/handlers/TeleportOnExitFlagHandler.java @@ -4,7 +4,6 @@ import java.util.Set; import org.bukkit.Location; import org.bukkit.entity.Player; -import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.plugin.Plugin; import com.sk89q.worldguard.protection.ApplicableRegionSet; @@ -46,17 +45,10 @@ public class TeleportOnExitFlagHandler extends HandlerWrapper @Override public boolean onCrossBoundary(Player player, Location from, Location to, ApplicableRegionSet toSet, Set entered, Set exited, MoveType moveType) { - if (!player.hasMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META)) + Object location = WorldGuardUtils.queryValueUnchecked(player, to.getWorld(), exited, Flags.TELEPORT_ON_EXIT); + if (location != null && WorldGuardUtils.hasNoTeleportLoop(this.getPlugin(), player, location)) { - Object location = WorldGuardUtils.queryValueUnchecked(player, to.getWorld(), exited, Flags.TELEPORT_ON_EXIT); - if (location != null) - { - player.setMetadata(WorldGuardUtils.PREVENT_TELEPORT_LOOP_META, new FixedMetadataValue(this.getPlugin(), true)); - - player.teleport(WorldEditUtils.toLocation(location)); - - return false; - } + player.teleport(WorldEditUtils.toLocation(location)); } return true;