Use a Pair<Vector,String> to allow worlds to unload while they are stored in TP history

This commit is contained in:
Spencer 2018-01-01 23:38:18 -05:00 committed by Alexander Meech
parent f54b6606ab
commit 76eb530b5f
2 changed files with 20 additions and 14 deletions

View File

@ -3,8 +3,13 @@ package mineplex.mapparser.command.teleport;
import java.util.LinkedList;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import mineplex.core.common.Pair;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.mapparser.command.BaseCommand;
public class BackCommand extends BaseCommand
@ -21,7 +26,7 @@ public class BackCommand extends BaseCommand
@Override
public boolean execute(Player player, String alias, String[] args)
{
LinkedList<Location> teleportHistory = _teleportManager.getTeleportHistory(player);
LinkedList<Pair<Vector, String>> teleportHistory = _teleportManager.getTeleportHistory(player);
if (teleportHistory == null || teleportHistory.isEmpty())
{
@ -49,39 +54,38 @@ public class BackCommand extends BaseCommand
}
}
Location location = null;
Pair<Vector, String> locationPair = null;
if (steps == 1)
{
location = teleportHistory.removeFirst();
locationPair = teleportHistory.removeFirst();
}
else
{
for (int i = 0; i < steps; i++)
{
location = teleportHistory.remove(i);
locationPair = teleportHistory.remove(i);
}
}
if (location == null)
if (locationPair == null)
{
message(player, "Something went wrong. Couldn't teleport you back.");
return true;
}
World locationWorld =_teleportManager.getPlugin().getWorldFromName(locationPair.getRight());
Vector locationVec = locationPair.getLeft();
Location location = new Location(locationWorld, locationVec.getX(), locationVec.getY(), locationVec.getZ());
if (!_teleportManager.canTeleportTo(player, location))
{
message(player, "You don't have access to teleport back to that location.");
return true;
}
// If the world doesn't already exist, load it
if (location.getWorld() == null || (!location.getWorld().equals(player.getWorld()) && location.getWorld().getPlayers().size() == 0))
{
_teleportManager.getPlugin().getWorldFromName(location.getWorld().getName());
}
player.teleport(location);
message(player, "You undid your last " + ((steps == 1) ? "teleport." : F.count(steps) + C.mBody + " teleports."));
return true;
}
}

View File

@ -6,14 +6,16 @@ import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import mineplex.core.common.Pair;
import mineplex.mapparser.MapData;
import mineplex.mapparser.MapParser;
public class TeleportManager
{
private MapParser _plugin;
private Map<Player, LinkedList<Location>> _teleportHistory;
private Map<Player, LinkedList<Pair<Vector, String>>> _teleportHistory;
public TeleportManager(MapParser plugin)
{
@ -23,10 +25,10 @@ public class TeleportManager
private void addToHistory(Player player, Location location)
{
_teleportHistory.computeIfAbsent(player, key -> new LinkedList<>()).addFirst(location);
_teleportHistory.computeIfAbsent(player, key -> new LinkedList<>()).addFirst(Pair.create(new Vector(location.getX(), location.getY(), location.getZ()), location.getWorld().getName()));
}
public LinkedList<Location> getTeleportHistory(Player player)
public LinkedList<Pair<Vector, String>> getTeleportHistory(Player player)
{
return _teleportHistory.get(player);
}