AdminCreateItem.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Collection;
  17. import java.util.StringTokenizer;
  18. import com.l2jserver.gameserver.datatables.ItemTable;
  19. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  20. import com.l2jserver.gameserver.model.L2World;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.templates.item.L2Item;
  23. /**
  24. * This class handles following admin commands:
  25. * - itemcreate = show menu
  26. * - create_item <id> [num] = creates num items with respective id, if num is not specified, assumes 1.
  27. *
  28. * @version $Revision: 1.2.2.2.2.3 $ $Date: 2005/04/11 10:06:06 $
  29. */
  30. public class AdminCreateItem implements IAdminCommandHandler
  31. {
  32. private static final String[] ADMIN_COMMANDS =
  33. {
  34. "admin_itemcreate",
  35. "admin_create_item",
  36. "admin_create_coin",
  37. "admin_give_item_target",
  38. "admin_give_item_to_all"
  39. };
  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. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  182. {
  183. for (L2PcInstance onlinePlayer : pls)
  184. {
  185. if (activeChar != onlinePlayer && onlinePlayer.isOnline() && (onlinePlayer.getClient() != null && !onlinePlayer.getClient().isDetached()))
  186. {
  187. onlinePlayer.getInventory().addItem("Admin", idval, numval, onlinePlayer, activeChar);
  188. onlinePlayer.sendMessage("Admin spawned "+numval+" "+template.getName()+" in your inventory.");
  189. counter++;
  190. }
  191. }
  192. }
  193. activeChar.sendMessage(counter +" players rewarded with " + template.getName());
  194. }
  195. return true;
  196. }
  197. public String[] getAdminCommandList()
  198. {
  199. return ADMIN_COMMANDS;
  200. }
  201. private void createItem(L2PcInstance activeChar, L2PcInstance target, int id, long num)
  202. {
  203. L2Item template = ItemTable.getInstance().getTemplate(id);
  204. if (template == null)
  205. {
  206. activeChar.sendMessage("This item doesn't exist.");
  207. return;
  208. }
  209. if (num > 10 && !template.isStackable())
  210. {
  211. activeChar.sendMessage("This item does not stack - Creation aborted.");
  212. return;
  213. }
  214. target.getInventory().addItem("Admin", id, num, activeChar, null);
  215. if (activeChar != target)
  216. target.sendMessage("Admin spawned " + num + " "+template.getName()+" in your inventory.");
  217. activeChar.sendMessage("You have spawned " + num + " "+template.getName()+"(" + id + ") in "+target.getName()+" inventory.");
  218. }
  219. private int getCoinId(String name)
  220. {
  221. int id;
  222. if (name.equalsIgnoreCase("adena"))
  223. id = 57;
  224. else if (name.equalsIgnoreCase("ancientadena"))
  225. id = 5575;
  226. else if (name.equalsIgnoreCase("festivaladena"))
  227. id = 6673;
  228. else if (name.equalsIgnoreCase("blueeva"))
  229. id = 4355;
  230. else if (name.equalsIgnoreCase("goldeinhasad"))
  231. id = 4356;
  232. else if (name.equalsIgnoreCase("silvershilen"))
  233. id = 4357;
  234. else if (name.equalsIgnoreCase("bloodypaagrio"))
  235. id = 4358;
  236. else if (name.equalsIgnoreCase("fantasyislecoin"))
  237. id = 13067;
  238. else id = 0;
  239. return id;
  240. }
  241. }