PlayerHandler.java 11 KB

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