PlayerHandler.java 11 KB

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