PlayerHandler.java 9.9 KB

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