TeleportManager, TeleportCommand and BackCommand (replaces EventModule's /tp)

This commit is contained in:
Spencer 2017-12-30 16:40:46 -05:00 committed by Alexander Meech
parent bda5813c9c
commit 99f3dd1805
4 changed files with 188 additions and 37 deletions

View File

@ -0,0 +1,82 @@
package mineplex.mapparser.command.teleport;
import java.util.LinkedList;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import mineplex.mapparser.MapParser;
import mineplex.mapparser.command.BaseCommand;
public class BackCommand extends BaseCommand
{
private TeleportManager _teleportManager;
public BackCommand(MapParser plugin, TeleportManager teleportManager)
{
super(plugin, "back", "tpback");
_teleportManager = teleportManager;
}
@Override
public boolean execute(Player player, String alias, String[] args)
{
LinkedList<Location> teleportHistory = _teleportManager.getTeleportHistory(player);
if (teleportHistory == null || teleportHistory.isEmpty())
{
message(player, "You don't have any teleport history.");
return true;
}
int steps = 1;
if (args.length > 0)
{
try
{
steps = Integer.parseInt(args[0]);
if (steps < 1)
{
throw new NumberFormatException("Steps must be >0");
}
}
catch (NumberFormatException ex)
{
message(player, "Please enter a valid number of steps to teleport back.");
return true;
}
}
Location location = null;
if (steps == 1)
{
location = teleportHistory.removeFirst();
}
else
{
for (int i = 0; i < steps; i++)
{
location = teleportHistory.remove(i);
}
}
if (location == null)
{
message(player, "Something went wrong. Couldn't teleport you back.");
return true;
}
if (_teleportManager.canTeleportTo(player, location))
{
message(player, "You don't have access to teleport back to that location.");
return true;
}
player.teleport(location);
return true;
}
}

View File

@ -0,0 +1,44 @@
package mineplex.mapparser.command.teleport;
import org.bukkit.entity.Player;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayerBase;
import mineplex.mapparser.MapData;
import mineplex.mapparser.MapParser;
import mineplex.mapparser.command.BaseCommand;
public class TeleportCommand extends BaseCommand
{
private TeleportManager _teleportManager;
public TeleportCommand(MapParser plugin, TeleportManager teleportManager)
{
super(plugin, "tp", "teleport");
_teleportManager = teleportManager;
}
@Override
public boolean execute(Player player, String alias, String[] args)
{
Player target = UtilPlayerBase.searchOnline(player, args[0], true);
if (target == null)
{
// Player has already been informed
return true;
}
if (!_teleportManager.canTeleportTo(player, target.getLocation()))
{
message(player, "That map is currently locked, and you don't have access to enter it.");
return true;
}
message(player, "You teleported to " + F.name(target.getName()) + C.mBody + ".");
_teleportManager.teleportPlayer(player, target);
return true;
}
}

View File

@ -0,0 +1,62 @@
package mineplex.mapparser.command.teleport;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import mineplex.mapparser.MapData;
import mineplex.mapparser.MapParser;
public class TeleportManager
{
private MapParser _plugin;
private Map<Player, LinkedList<Location>> _teleportHistory;
public TeleportManager(MapParser plugin)
{
_plugin = plugin;
_teleportHistory = new HashMap<>();
}
private void addToHistory(Player player, Location location)
{
_teleportHistory.computeIfAbsent(player, key -> new LinkedList<>()).addFirst(location);
}
public LinkedList<Location> getTeleportHistory(Player player)
{
return _teleportHistory.get(player);
}
public void teleportPlayer(Player player, Location destination)
{
addToHistory(player, player.getLocation().clone());
player.teleport(destination);
}
public void teleportPlayer(Player player, Player destination)
{
teleportPlayer(player, destination.getLocation());
}
public boolean canTeleportTo(Player player, Location target)
{
if (target.getWorld().getName().equals("world") || target.getWorld().getName().equals("world_lobby"))
{
return true;
}
MapData data = getPlugin().getData(target.getWorld().getName());
return data.CanJoin(player);
}
public MapParser getPlugin()
{
return _plugin;
}
}

View File

@ -191,43 +191,6 @@ public class EventModule extends Module
} }
} }
@EventHandler(priority = EventPriority.LOWEST)
public void TeleportCommand(PlayerCommandPreprocessEvent event)
{
if (!event.getMessage().toLowerCase().startsWith("/tp"))
{
return;
}
Player player = event.getPlayer();
String[] tokens = event.getMessage().split(" ");
if (tokens.length != 2)
{
return;
}
event.setCancelled(true);
Player target = UtilPlayerBase.searchOnline(player, tokens[1], true);
if (target != null)
{
if (!target.getWorld().getName().equals("world"))
{
MapData data = getPlugin().getData(target.getWorld().getName());
if(!data.CanJoin(player))
{
player.sendMessage(C.cRed + "That world is currently locked, and you don't have access to it.");
return;
}
}
UtilPlayerBase.message(player, F.main("Game", "You teleported to " + F.name(target.getName()) + "."));
player.teleport(target);
}
}
//################################################################################################# //#################################################################################################
//# # //# #
//# Simple methods # //# Simple methods #