Made teleport-on flags teleport loop detection smarter

This commit is contained in:
isokissa3 2018-12-11 02:44:36 +02:00
parent 1addc326db
commit 99a96a3c77
3 changed files with 44 additions and 22 deletions

View file

@ -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<Object> set = (Set<Object>)result.value();
if (set.add(location))
{
return true;
}
return false;
}
}

View file

@ -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<ProtectedRegion> entered, Set<ProtectedRegion> 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;

View file

@ -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<ProtectedRegion> entered, Set<ProtectedRegion> 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;