AdminCreateItem.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.admincommandhandlers;
  16. import java.util.StringTokenizer;
  17. import com.l2jserver.gameserver.datatables.ItemTable;
  18. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  19. import com.l2jserver.gameserver.model.L2World;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.model.item.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. "admin_create_coin",
  36. "admin_give_item_target",
  37. "admin_give_item_to_all"
  38. };
  39. @Override
  40. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  41. {
  42. if (command.equals("admin_itemcreate"))
  43. {
  44. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  45. }
  46. else if (command.startsWith("admin_create_item"))
  47. {
  48. try
  49. {
  50. String val = command.substring(17);
  51. StringTokenizer st = new StringTokenizer(val);
  52. if (st.countTokens() == 2)
  53. {
  54. String id = st.nextToken();
  55. int idval = Integer.parseInt(id);
  56. String num = st.nextToken();
  57. long numval = Long.parseLong(num);
  58. createItem(activeChar, activeChar, idval, numval);
  59. }
  60. else if (st.countTokens() == 1)
  61. {
  62. String id = st.nextToken();
  63. int idval = Integer.parseInt(id);
  64. createItem(activeChar, activeChar, idval, 1);
  65. }
  66. }
  67. catch (StringIndexOutOfBoundsException e)
  68. {
  69. activeChar.sendMessage("Usage: //create_item <itemId> [amount]");
  70. }
  71. catch (NumberFormatException nfe)
  72. {
  73. activeChar.sendMessage("Specify a valid number.");
  74. }
  75. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  76. }
  77. else if (command.startsWith("admin_create_coin"))
  78. {
  79. try
  80. {
  81. String val = command.substring(17);
  82. StringTokenizer st = new StringTokenizer(val);
  83. if (st.countTokens() == 2)
  84. {
  85. String name = st.nextToken();
  86. int idval = getCoinId(name);
  87. if(idval > 0)
  88. {
  89. String num = st.nextToken();
  90. long numval = Long.parseLong(num);
  91. createItem(activeChar, activeChar, idval, numval);
  92. }
  93. }
  94. else if (st.countTokens() == 1)
  95. {
  96. String name = st.nextToken();
  97. int idval = getCoinId(name);
  98. createItem(activeChar, activeChar, idval, 1);
  99. }
  100. }
  101. catch (StringIndexOutOfBoundsException e)
  102. {
  103. activeChar.sendMessage("Usage: //create_coin <name> [amount]");
  104. }
  105. catch (NumberFormatException nfe)
  106. {
  107. activeChar.sendMessage("Specify a valid number.");
  108. }
  109. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  110. }
  111. else if (command.startsWith("admin_give_item_target"))
  112. {
  113. try
  114. {
  115. L2PcInstance target;
  116. if (activeChar.getTarget() instanceof L2PcInstance)
  117. target = (L2PcInstance) activeChar.getTarget();
  118. else
  119. {
  120. activeChar.sendMessage("Invalid target.");
  121. return false;
  122. }
  123. String val = command.substring(22);
  124. StringTokenizer st = new StringTokenizer(val);
  125. if (st.countTokens() == 2)
  126. {
  127. String id = st.nextToken();
  128. int idval = Integer.parseInt(id);
  129. String num = st.nextToken();
  130. long numval = Long.parseLong(num);
  131. createItem(activeChar, target, idval, numval);
  132. }
  133. else if (st.countTokens() == 1)
  134. {
  135. String id = st.nextToken();
  136. int idval = Integer.parseInt(id);
  137. createItem(activeChar, target, idval, 1);
  138. }
  139. }
  140. catch (StringIndexOutOfBoundsException e)
  141. {
  142. activeChar.sendMessage("Usage: //give_item_target <itemId> [amount]");
  143. }
  144. catch (NumberFormatException nfe)
  145. {
  146. activeChar.sendMessage("Specify a valid number.");
  147. }
  148. AdminHelpPage.showHelpPage(activeChar, "itemcreation.htm");
  149. }
  150. else if (command.startsWith("admin_give_item_to_all"))
  151. {
  152. String val = command.substring(22);
  153. StringTokenizer st = new StringTokenizer(val);
  154. int idval = 0;
  155. long numval = 0;
  156. if (st.countTokens() == 2)
  157. {
  158. String id = st.nextToken();
  159. idval = Integer.parseInt(id);
  160. String num = st.nextToken();
  161. numval = Long.parseLong(num);
  162. }
  163. else if (st.countTokens() == 1)
  164. {
  165. String id = st.nextToken();
  166. idval = Integer.parseInt(id);
  167. numval = 1;
  168. }
  169. int counter = 0;
  170. L2Item template = ItemTable.getInstance().getTemplate(idval);
  171. if (template == null)
  172. {
  173. activeChar.sendMessage("This item doesn't exist.");
  174. return false;
  175. }
  176. if (numval > 10 && !template.isStackable())
  177. {
  178. activeChar.sendMessage("This item does not stack - Creation aborted.");
  179. return false;
  180. }
  181. for (L2PcInstance onlinePlayer : L2World.getInstance().getAllPlayersArray())
  182. {
  183. if (activeChar != onlinePlayer && onlinePlayer.isOnline() && (onlinePlayer.getClient() != null && !onlinePlayer.getClient().isDetached()))
  184. {
  185. onlinePlayer.getInventory().addItem("Admin", idval, numval, onlinePlayer, activeChar);
  186. onlinePlayer.sendMessage("Admin spawned "+numval+" "+template.getName()+" in your inventory.");
  187. counter++;
  188. }
  189. }
  190. activeChar.sendMessage(counter +" players rewarded with " + template.getName());
  191. }
  192. return true;
  193. }
  194. @Override
  195. public String[] getAdminCommandList()
  196. {
  197. return ADMIN_COMMANDS;
  198. }
  199. private void createItem(L2PcInstance activeChar, L2PcInstance target, int id, long num)
  200. {
  201. L2Item template = ItemTable.getInstance().getTemplate(id);
  202. if (template == null)
  203. {
  204. activeChar.sendMessage("This item doesn't exist.");
  205. return;
  206. }
  207. if (num > 10 && !template.isStackable())
  208. {
  209. activeChar.sendMessage("This item does not stack - Creation aborted.");
  210. return;
  211. }
  212. target.getInventory().addItem("Admin", id, num, activeChar, null);
  213. if (activeChar != target)
  214. target.sendMessage("Admin spawned " + num + " "+template.getName()+" in your inventory.");
  215. activeChar.sendMessage("You have spawned " + num + " "+template.getName()+"(" + id + ") in "+target.getName()+" inventory.");
  216. }
  217. private int getCoinId(String name)
  218. {
  219. int id;
  220. if (name.equalsIgnoreCase("adena"))
  221. id = 57;
  222. else if (name.equalsIgnoreCase("ancientadena"))
  223. id = 5575;
  224. else if (name.equalsIgnoreCase("festivaladena"))
  225. id = 6673;
  226. else if (name.equalsIgnoreCase("blueeva"))
  227. id = 4355;
  228. else if (name.equalsIgnoreCase("goldeinhasad"))
  229. id = 4356;
  230. else if (name.equalsIgnoreCase("silvershilen"))
  231. id = 4357;
  232. else if (name.equalsIgnoreCase("bloodypaagrio"))
  233. id = 4358;
  234. else if (name.equalsIgnoreCase("fantasyislecoin"))
  235. id = 13067;
  236. else id = 0;
  237. return id;
  238. }
  239. }