Recipes.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.itemhandlers;
  16. import com.l2jserver.gameserver.RecipeController;
  17. import com.l2jserver.gameserver.handler.IItemHandler;
  18. import com.l2jserver.gameserver.model.L2RecipeList;
  19. import com.l2jserver.gameserver.model.actor.L2Playable;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  22. import com.l2jserver.gameserver.network.SystemMessageId;
  23. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  24. /**
  25. * @author Zoey76
  26. */
  27. public class Recipes implements IItemHandler
  28. {
  29. public void useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  30. {
  31. if (!(playable instanceof L2PcInstance))
  32. {
  33. return;
  34. }
  35. final L2PcInstance activeChar = playable.getActingPlayer();
  36. if (activeChar.isInCraftMode())
  37. {
  38. activeChar.sendPacket(SystemMessageId.CANT_ALTER_RECIPEBOOK_WHILE_CRAFTING);
  39. return;
  40. }
  41. final L2RecipeList rp = RecipeController.getInstance().getRecipeByItemId(item.getItemId());
  42. if (rp == null)
  43. {
  44. return;
  45. }
  46. if (activeChar.hasRecipeList(rp.getId()))
  47. {
  48. activeChar.sendPacket(SystemMessageId.RECIPE_ALREADY_REGISTERED);
  49. return;
  50. }
  51. boolean canCraft = false;
  52. boolean recipeLevel = false;
  53. boolean recipeLimit = false;
  54. if (rp.isDwarvenRecipe() && (canCraft = activeChar.hasDwarvenCraft()))
  55. {
  56. if (recipeLevel = (rp.getLevel() > activeChar.getDwarvenCraft()))
  57. {
  58. recipeLimit = activeChar.getDwarvenRecipeBook().length >= activeChar.getDwarfRecipeLimit();
  59. }
  60. }
  61. else if (canCraft = activeChar.hasCommonCraft())
  62. {
  63. if (recipeLevel = (rp.getLevel() > activeChar.getCommonCraft()))
  64. {
  65. recipeLimit = activeChar.getCommonRecipeBook().length >= activeChar.getCommonRecipeLimit();
  66. }
  67. }
  68. if (!canCraft)
  69. {
  70. activeChar.sendPacket(SystemMessageId.CANT_REGISTER_NO_ABILITY_TO_CRAFT);
  71. return;
  72. }
  73. if (recipeLevel)
  74. {
  75. activeChar.sendPacket(SystemMessageId.CREATE_LVL_TOO_LOW_TO_REGISTER);
  76. return;
  77. }
  78. if (recipeLimit)
  79. {
  80. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.UP_TO_S1_RECIPES_CAN_REGISTER);
  81. sm.addNumber(rp.isDwarvenRecipe() ? activeChar.getDwarfRecipeLimit() : activeChar.getCommonRecipeLimit());
  82. activeChar.sendPacket(sm);
  83. return;
  84. }
  85. if (rp.isDwarvenRecipe())
  86. {
  87. activeChar.registerDwarvenRecipeList(rp, true);
  88. }
  89. else
  90. {
  91. activeChar.registerCommonRecipeList(rp, true);
  92. }
  93. activeChar.destroyItem("Consume", item.getObjectId(), 1, null, false);
  94. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_ADDED);
  95. sm.addItemName(item);
  96. activeChar.sendPacket(sm);
  97. }
  98. }