2
0

AdminCreateItem.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 net.sf.l2j.gameserver.handler.admincommandhandlers;
  16. import java.util.StringTokenizer;
  17. import net.sf.l2j.gameserver.datatables.ItemTable;
  18. import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. import net.sf.l2j.gameserver.network.serverpackets.ItemList;
  21. import net.sf.l2j.gameserver.templates.L2Item;
  22. /**
  23. * This class handles following admin commands:
  24. * - itemcreate = show menu
  25. * - create_item <id> [num] = creates num items with respective id, if num is not specified, assumes 1.
  26. *
  27. * @version $Revision: 1.2.2.2.2.3 $ $Date: 2005/04/11 10:06:06 $
  28. */
  29. public class AdminCreateItem implements IAdminCommandHandler
  30. {
  31. private static final String[] ADMIN_COMMANDS =
  32. {
  33. "admin_itemcreate",
  34. "admin_create_item"
  35. };
  36. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  37. {
  38. if (command.equals("admin_itemcreate"))
  39. {
  40. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  41. }
  42. else if (command.startsWith("admin_create_item"))
  43. {
  44. try
  45. {
  46. String val = command.substring(17);
  47. StringTokenizer st = new StringTokenizer(val);
  48. if (st.countTokens() == 2)
  49. {
  50. String id = st.nextToken();
  51. int idval = Integer.parseInt(id);
  52. String num = st.nextToken();
  53. int numval = Integer.parseInt(num);
  54. createItem(activeChar, idval, numval);
  55. }
  56. else if (st.countTokens() == 1)
  57. {
  58. String id = st.nextToken();
  59. int idval = Integer.parseInt(id);
  60. createItem(activeChar, idval, 1);
  61. }
  62. }
  63. catch (StringIndexOutOfBoundsException e)
  64. {
  65. activeChar.sendMessage("Usage: //itemcreate <itemId> [amount]");
  66. }
  67. catch (NumberFormatException nfe)
  68. {
  69. activeChar.sendMessage("Specify a valid number.");
  70. }
  71. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  72. }
  73. return true;
  74. }
  75. public String[] getAdminCommandList()
  76. {
  77. return ADMIN_COMMANDS;
  78. }
  79. private void createItem(L2PcInstance activeChar, int id, int num)
  80. {
  81. L2Item template = ItemTable.getInstance().getTemplate(id);
  82. if (template == null)
  83. {
  84. activeChar.sendMessage("This item doesn't exist.");
  85. return;
  86. }
  87. if (num > 20)
  88. {
  89. if (!template.isStackable())
  90. {
  91. activeChar.sendMessage("This item does not stack - Creation aborted.");
  92. return;
  93. }
  94. }
  95. activeChar.getInventory().addItem("Admin", id, num, activeChar, null);
  96. ItemList il = new ItemList(activeChar, true);
  97. activeChar.sendPacket(il);
  98. activeChar.sendMessage("You have spawned " + num + " item(s) number " + id + " in your inventory.");
  99. }
  100. }