/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package net.sf.l2j.gameserver.handler.admincommandhandlers;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import net.sf.l2j.Config;
import net.sf.l2j.gameserver.NpcTable;
import net.sf.l2j.gameserver.SpawnTable;
import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
import net.sf.l2j.gameserver.model.L2NpcInstance;
import net.sf.l2j.gameserver.model.L2Object;
import net.sf.l2j.gameserver.model.L2PcInstance;
import net.sf.l2j.gameserver.model.L2Spawn;
import net.sf.l2j.gameserver.model.L2World;
import net.sf.l2j.gameserver.serverpackets.ActionFailed;
import net.sf.l2j.gameserver.serverpackets.NpcHtmlMessage;
import net.sf.l2j.gameserver.serverpackets.SystemMessage;
import net.sf.l2j.gameserver.templates.L2NpcTemplate;
/**
* This class handles following admin commands:
* - show_moves
* - show_teleport
* - teleport_to_character
* - move_to
* - teleport_character
*
* @version $Revision: 1.3.2.6.2.4 $ $Date: 2005/04/11 10:06:06 $
*/
public class AdminTeleport implements IAdminCommandHandler
{
private static final Logger _log = Logger.getLogger(AdminTeleport.class.getName());
private static String[] _adminCommands = {
"admin_teleport_to_character",
"admin_teleport_character_to",
"admin_teleport_character",
"admin_show_moves_other",
"admin_show_teleport",
"admin_teleportto",
"admin_show_moves",
"admin_recall_npc",
"admin_move_to",
"admin_recall",
"admin_failed",
"teleportto",
"recall"
};
private static final int REQUIRED_LEVEL = Config.GM_TELEPORT;
public boolean useAdminCommand(String command, L2PcInstance activeChar) {
if (!(checkLevel(activeChar.getAccessLevel()) && activeChar.isGM())) return false;
if (command.equals("admin_show_moves"))
{
AdminHelpPage.showHelpPage(activeChar, "teleports.htm");
}
if (command.equals("admin_show_moves_other"))
{
AdminHelpPage.showHelpPage(activeChar, "tele/other.html");
}
else if (command.equals("admin_show_teleport"))
{
showTeleportCharWindow(activeChar);
}
else if (command.equals("admin_recall_npc"))
{
recallNPC(activeChar);
}
else if (command.equals("admin_teleport_to_character"))
{
teleportToCharacter(activeChar, activeChar.getTarget());
}
else if (command.startsWith("admin_move_to"))
{
try
{
String val = command.substring(14);
teleportTo(activeChar, val);
}
catch (StringIndexOutOfBoundsException e)
{ //Case of empty co-ordinates
SystemMessage sm = new SystemMessage(614);
sm.addString("Wrong or no Co-ordinates given.");
activeChar.sendPacket(sm);
}
}
else if (command.startsWith("admin_teleport_character"))
{
try
{
String val = command.substring(25);
teleportCharacter(activeChar, val);
}
catch (StringIndexOutOfBoundsException e)
{
//Case of empty co-ordinates
SystemMessage sm = new SystemMessage(614);
sm.addString("Wrong or no Co-ordinates given.");
activeChar.sendPacket(sm);
showTeleportCharWindow(activeChar); //back to character teleport
}
}
else if (command.startsWith("admin_teleport_character_to "))
{
String[] data = command.split(" ");
if(data.length==5)
{
String playerName=data[1];
int x=Integer.parseInt(data[2]);
int y=Integer.parseInt(data[3]);
int z=Integer.parseInt(data[4]);
L2PcInstance player = L2World.getInstance().getPlayer(playerName);
if(player!=null)
{
teleportCharacter(player,x,y,z);
}
}
}
else if (command.startsWith("admin_teleportto "))
{
String targetName = command.substring(17);
L2PcInstance player = L2World.getInstance().getPlayer(targetName);
teleportToCharacter(activeChar, player);
}
else if (command.startsWith("admin_recall "))
{
String targetName = command.substring(13);
L2PcInstance player = L2World.getInstance().getPlayer(targetName);
teleportCharacter(player, activeChar.getX(), activeChar.getY(), activeChar.getZ());
}
else if (command.startsWith("admin_failed"))
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Trying ActionFailed...");
activeChar.sendPacket(sm);
activeChar.sendPacket(new ActionFailed());
}
return true;
}
public String[] getAdminCommandList()
{
return _adminCommands;
}
private boolean checkLevel(int level)
{
return (level >= REQUIRED_LEVEL);
}
private void teleportTo(L2PcInstance activeChar, String Cords)
{
StringTokenizer st = new StringTokenizer(Cords);
String x1 = st.nextToken();
int x = Integer.parseInt(x1);
String y1 = st.nextToken();
int y = Integer.parseInt(y1);
String z1 = st.nextToken();
int z = Integer.parseInt(z1);
activeChar.teleToLocation(x, y, z);
SystemMessage sm = new SystemMessage(614);
sm.addString("You have been teleported to " + Cords);
activeChar.sendPacket(sm);
}
private void showTeleportCharWindow(L2PcInstance activeChar)
{
L2Object target = activeChar.getTarget();
L2PcInstance player = null;
if (target instanceof L2PcInstance)
{
player = (L2PcInstance)target;
}
else
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Incorrect target.");
activeChar.sendPacket(sm);
return;
}
NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
StringBuffer replyMSG = new StringBuffer("<html><title>Teleport Character</title>");
replyMSG.append("<body>");
replyMSG.append("The character you will teleport is " + player.getName() + ".");
replyMSG.append("<br>");
replyMSG.append("Co-ordinate x");
replyMSG.append("<edit var=\"char_cord_x\" width=110>");
replyMSG.append("Co-ordinate y");
replyMSG.append("<edit var=\"char_cord_y\" width=110>");
replyMSG.append("Co-ordinate z");
replyMSG.append("<edit var=\"char_cord_z\" width=110>");
replyMSG.append("<button value=\"Teleport\" action=\"bypass -h admin_teleport_character $char_cord_x $char_cord_y $char_cord_z\" width=60 height=15 back=\"sek.cbui94\" fore=\"sek.cbui92\">");
replyMSG.append("<button value=\"Teleport near you\" action=\"bypass -h admin_teleport_character " + activeChar.getX() + " " + activeChar.getY() + " " + activeChar.getZ() + "\" width=115 height=15 back=\"sek.cbui94\" fore=\"sek.cbui92\">");
replyMSG.append("<center><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15 back=\"sek.cbui94\" fore=\"sek.cbui92\"></center>");
replyMSG.append("</body></html>");
adminReply.setHtml(replyMSG.toString());
activeChar.sendPacket(adminReply);
}
private void teleportCharacter(L2PcInstance activeChar , String Cords)
{
L2Object target = activeChar.getTarget();
L2PcInstance player = null;
if (target instanceof L2PcInstance)
{
player = (L2PcInstance)target;
}
else
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Incorrect target.");
activeChar.sendPacket(sm);
return;
}
if (player.getObjectId() == activeChar.getObjectId())
{
SystemMessage sm = new SystemMessage(614);
sm.addString("You cannot teleport your character.");
player.sendPacket(sm);
}
else
{
StringTokenizer st = new StringTokenizer(Cords);
String x1 = st.nextToken();
int x = Integer.parseInt(x1);
String y1 = st.nextToken();
int y = Integer.parseInt(y1);
String z1 = st.nextToken();
int z = Integer.parseInt(z1);
teleportCharacter(player, x,y,z);
}
}
/**
* @param player
* @param x
* @param y
* @param z
*/
private void teleportCharacter(L2PcInstance player, int x, int y, int z)
{
if (player != null) {
//Common character information
SystemMessage sm = new SystemMessage(614);
sm.addString("Admin is teleporting you.");
player.sendPacket(sm);
player.teleToLocation(x, y, z);
}
}
private void teleportToCharacter(L2PcInstance activeChar, L2Object target)
{
L2PcInstance player = null;
if (target != null && target instanceof L2PcInstance)
{
player = (L2PcInstance)target;
}
else
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Incorrect target.");
activeChar.sendPacket(sm);
return;
}
if (player.getObjectId() == activeChar.getObjectId())
{
SystemMessage sm = new SystemMessage(614);
sm.addString("You cannot self teleport.");
activeChar.sendPacket(sm);
}
else
{
int x = player.getX();
int y = player.getY();
int z = player.getZ();
activeChar.teleToLocation(x, y, z);
SystemMessage sm = new SystemMessage(614);
sm.addString("You have teleported to character " + player.getName() + ".");
activeChar.sendPacket(sm);
}
}
private void recallNPC(L2PcInstance activeChar)
{
L2Object obj = activeChar.getTarget();
if ((obj != null) && (obj instanceof L2NpcInstance))
{
L2NpcInstance target = (L2NpcInstance) obj;
L2Spawn spawn = target.getSpawn();
int monsterTemplate = target.getTemplate().npcId;
L2NpcTemplate template1 = NpcTable.getInstance().getTemplate(monsterTemplate);
if (template1 == null)
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Incorrect monster template.");
activeChar.sendPacket(sm);
return;
}
int respawnTime = spawn.getRespawnDelay();
target.deleteMe();
spawn.stopRespawn();
SpawnTable.getInstance().deleteSpawn(spawn, true);
try
{
//L2MonsterInstance mob = new L2MonsterInstance(monsterTemplate, template1);
spawn = new L2Spawn(template1);
spawn.setLocx(activeChar.getX());
spawn.setLocy(activeChar.getY());
spawn.setLocz(activeChar.getZ());
spawn.setAmount(1);
spawn.setHeading(activeChar.getHeading());
spawn.setRespawnDelay(respawnTime);
SpawnTable.getInstance().addNewSpawn(spawn, true);
spawn.init();
SystemMessage sm = new SystemMessage(614);
sm.addString("Created " + template1.name + " on " + target.getObjectId() + ".");
_log.fine("Spawn at X="+spawn.getLocx()+" Y="+spawn.getLocy()+" Z="+spawn.getLocz());
activeChar.sendPacket(sm);
_log.warning("GM: Admin id " + activeChar.getObjectId() + " moved NPC" + target.getObjectId());
}
catch (Exception e)
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Target is not in game.");
activeChar.sendPacket(sm);
}
}
else
{
SystemMessage sm = new SystemMessage(614);
sm.addString("Incorrect target.");
activeChar.sendPacket(sm);
}
}
}