PlayerHandler.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.telnethandlers;
  20. import java.io.PrintWriter;
  21. import java.net.Socket;
  22. import java.util.NoSuchElementException;
  23. import java.util.StringTokenizer;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import com.l2jserver.gameserver.data.sql.impl.CharNameTable;
  27. import com.l2jserver.gameserver.handler.ITelnetHandler;
  28. import com.l2jserver.gameserver.instancemanager.PunishmentManager;
  29. import com.l2jserver.gameserver.model.L2World;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  32. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  33. import com.l2jserver.gameserver.model.punishment.PunishmentAffect;
  34. import com.l2jserver.gameserver.model.punishment.PunishmentTask;
  35. import com.l2jserver.gameserver.model.punishment.PunishmentType;
  36. import com.l2jserver.gameserver.network.SystemMessageId;
  37. import com.l2jserver.gameserver.network.serverpackets.CharInfo;
  38. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  39. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  40. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  41. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  42. import com.l2jserver.gameserver.util.GMAudit;
  43. import com.l2jserver.gameserver.util.Util;
  44. /**
  45. * @author UnAfraid
  46. */
  47. public class PlayerHandler implements ITelnetHandler
  48. {
  49. private static final Logger LOGGER = LoggerFactory.getLogger(PlayerHandler.class);
  50. private final String[] _commands =
  51. {
  52. "kick",
  53. "give",
  54. "enchant",
  55. "jail",
  56. "unjail"
  57. };
  58. @Override
  59. public boolean useCommand(String command, PrintWriter _print, Socket _cSocket, int _uptime)
  60. {
  61. if (command.startsWith("kick"))
  62. {
  63. try
  64. {
  65. command = command.substring(5);
  66. L2PcInstance player = L2World.getInstance().getPlayer(command);
  67. if (player != null)
  68. {
  69. player.sendMessage("You are kicked by gm");
  70. player.logout();
  71. _print.println("Player kicked");
  72. }
  73. }
  74. catch (StringIndexOutOfBoundsException e)
  75. {
  76. _print.println("Please enter player name to kick");
  77. }
  78. }
  79. else if (command.startsWith("give"))
  80. {
  81. StringTokenizer st = new StringTokenizer(command.substring(5));
  82. try
  83. {
  84. L2PcInstance player = L2World.getInstance().getPlayer(st.nextToken());
  85. int itemId = Integer.parseInt(st.nextToken());
  86. int amount = Integer.parseInt(st.nextToken());
  87. if (player != null)
  88. {
  89. L2ItemInstance item = player.getInventory().addItem("Status-Give", itemId, amount, null, null);
  90. InventoryUpdate iu = new InventoryUpdate();
  91. iu.addItem(item);
  92. player.sendPacket(iu);
  93. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_PICKED_UP_S1_S2);
  94. sm.addItemName(itemId);
  95. sm.addLong(amount);
  96. player.sendPacket(sm);
  97. _print.println("ok");
  98. GMAudit.auditGMAction("Telnet Admin", "Give Item", player.getName(), "item: " + itemId + " amount: " + amount);
  99. }
  100. else
  101. {
  102. _print.println("Player not found");
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. }
  108. }
  109. else if (command.startsWith("enchant"))
  110. {
  111. StringTokenizer st = new StringTokenizer(command.substring(8), " ");
  112. int enchant = 0, itemType = 0;
  113. try
  114. {
  115. L2PcInstance player = L2World.getInstance().getPlayer(st.nextToken());
  116. itemType = Integer.parseInt(st.nextToken());
  117. enchant = Integer.parseInt(st.nextToken());
  118. switch (itemType)
  119. {
  120. case 1:
  121. itemType = Inventory.PAPERDOLL_HEAD;
  122. break;
  123. case 2:
  124. itemType = Inventory.PAPERDOLL_CHEST;
  125. break;
  126. case 3:
  127. itemType = Inventory.PAPERDOLL_GLOVES;
  128. break;
  129. case 4:
  130. itemType = Inventory.PAPERDOLL_FEET;
  131. break;
  132. case 5:
  133. itemType = Inventory.PAPERDOLL_LEGS;
  134. break;
  135. case 6:
  136. itemType = Inventory.PAPERDOLL_RHAND;
  137. break;
  138. case 7:
  139. itemType = Inventory.PAPERDOLL_LHAND;
  140. break;
  141. case 8:
  142. itemType = Inventory.PAPERDOLL_LEAR;
  143. break;
  144. case 9:
  145. itemType = Inventory.PAPERDOLL_REAR;
  146. break;
  147. case 10:
  148. itemType = Inventory.PAPERDOLL_LFINGER;
  149. break;
  150. case 11:
  151. itemType = Inventory.PAPERDOLL_RFINGER;
  152. break;
  153. case 12:
  154. itemType = Inventory.PAPERDOLL_NECK;
  155. break;
  156. case 13:
  157. itemType = Inventory.PAPERDOLL_UNDER;
  158. break;
  159. case 14:
  160. itemType = Inventory.PAPERDOLL_CLOAK;
  161. break;
  162. case 15:
  163. itemType = Inventory.PAPERDOLL_BELT;
  164. break;
  165. default:
  166. itemType = 0;
  167. }
  168. if (enchant > 65535)
  169. {
  170. enchant = 65535;
  171. }
  172. else if (enchant < 0)
  173. {
  174. enchant = 0;
  175. }
  176. boolean success = false;
  177. if ((player != null) && (itemType > 0))
  178. {
  179. success = setEnchant(player, enchant, itemType);
  180. if (success)
  181. {
  182. _print.println("Item enchanted successfully.");
  183. }
  184. }
  185. else if (!success)
  186. {
  187. _print.println("Item failed to enchant.");
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. }
  193. }
  194. else if (command.startsWith("jail"))
  195. {
  196. StringTokenizer st = new StringTokenizer(command.substring(5));
  197. try
  198. {
  199. String name = st.nextToken();
  200. int charId = CharNameTable.getInstance().getIdByName(name);
  201. int delay = 0;
  202. String reason = "";
  203. if (st.hasMoreTokens())
  204. {
  205. String token = st.nextToken();
  206. if (Util.isDigit(token))
  207. {
  208. delay = Integer.parseInt(token);
  209. }
  210. while (st.hasMoreTokens())
  211. {
  212. reason += st.nextToken() + " ";
  213. }
  214. if (!reason.isEmpty())
  215. {
  216. reason = reason.substring(0, reason.length() - 1);
  217. }
  218. }
  219. if (charId > 0)
  220. {
  221. long expirationTime = delay > 0 ? System.currentTimeMillis() + (delay * 60 * 1000) : -1;
  222. PunishmentManager.getInstance().startPunishment(new PunishmentTask(charId, PunishmentAffect.CHARACTER, PunishmentType.JAIL, expirationTime, reason, "Telnet Admin: " + _cSocket.getInetAddress().getHostAddress()));
  223. _print.println("Character " + name + " jailed for " + (delay > 0 ? delay + " minutes." : "ever!"));
  224. }
  225. else
  226. {
  227. _print.println("Character with name: " + name + " was not found!");
  228. }
  229. }
  230. catch (NoSuchElementException nsee)
  231. {
  232. _print.println("Specify a character name.");
  233. }
  234. catch (Exception e)
  235. {
  236. LOGGER.debug("Could not jail player via telnet!", e);
  237. }
  238. }
  239. else if (command.startsWith("unjail"))
  240. {
  241. StringTokenizer st = new StringTokenizer(command.substring(7));
  242. try
  243. {
  244. String name = st.nextToken();
  245. int charId = CharNameTable.getInstance().getIdByName(name);
  246. if (charId > 0)
  247. {
  248. PunishmentManager.getInstance().stopPunishment(charId, PunishmentAffect.CHARACTER, PunishmentType.JAIL);
  249. _print.println("Character " + name + " have been unjailed");
  250. }
  251. else
  252. {
  253. _print.println("Character with name: " + name + " was not found!");
  254. }
  255. }
  256. catch (NoSuchElementException nsee)
  257. {
  258. _print.println("Specify a character name.");
  259. }
  260. catch (Exception e)
  261. {
  262. LOGGER.debug("Could not unjail player via telnet!", e);
  263. }
  264. }
  265. return false;
  266. }
  267. private boolean setEnchant(L2PcInstance activeChar, int ench, int armorType)
  268. {
  269. // now we need to find the equipped weapon of the targeted character...
  270. int curEnchant = 0; // display purposes only
  271. L2ItemInstance itemInstance = null;
  272. // only attempt to enchant if there is a weapon equipped
  273. L2ItemInstance parmorInstance = activeChar.getInventory().getPaperdollItem(armorType);
  274. if ((parmorInstance != null) && (parmorInstance.getLocationSlot() == armorType))
  275. {
  276. itemInstance = parmorInstance;
  277. }
  278. else
  279. {
  280. // for bows/crossbows and double handed weapons
  281. parmorInstance = activeChar.getInventory().getPaperdollItem(Inventory.PAPERDOLL_RHAND);
  282. if ((parmorInstance != null) && (parmorInstance.getLocationSlot() == Inventory.PAPERDOLL_RHAND))
  283. {
  284. itemInstance = parmorInstance;
  285. }
  286. }
  287. if (itemInstance != null)
  288. {
  289. curEnchant = itemInstance.getEnchantLevel();
  290. // set enchant value
  291. activeChar.getInventory().unEquipItemInSlot(armorType);
  292. itemInstance.setEnchantLevel(ench);
  293. activeChar.getInventory().equipItem(itemInstance);
  294. // send packets
  295. InventoryUpdate iu = new InventoryUpdate();
  296. iu.addModifiedItem(itemInstance);
  297. activeChar.sendPacket(iu);
  298. activeChar.broadcastPacket(new CharInfo(activeChar));
  299. activeChar.sendPacket(new UserInfo(activeChar));
  300. activeChar.broadcastPacket(new ExBrExtraUserInfo(activeChar));
  301. // informations
  302. activeChar.sendMessage("Changed enchantment of " + activeChar.getName() + "'s " + itemInstance.getItem().getName() + " from " + curEnchant + " to " + ench + ".");
  303. activeChar.sendMessage("Admin has changed the enchantment of your " + itemInstance.getItem().getName() + " from " + curEnchant + " to " + ench + ".");
  304. // log
  305. GMAudit.auditGMAction("TelnetAdministrator", "enchant", activeChar.getName(), itemInstance.getItem().getName() + "(" + itemInstance.getObjectId() + ")" + " from " + curEnchant + " to " + ench);
  306. return true;
  307. }
  308. return false;
  309. }
  310. @Override
  311. public String[] getCommandList()
  312. {
  313. return _commands;
  314. }
  315. }