Recipes.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.items.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. @Override
  30. public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  31. {
  32. if (!(playable instanceof L2PcInstance))
  33. {
  34. return false;
  35. }
  36. final L2PcInstance activeChar = playable.getActingPlayer();
  37. if (activeChar.isInCraftMode())
  38. {
  39. activeChar.sendPacket(SystemMessageId.CANT_ALTER_RECIPEBOOK_WHILE_CRAFTING);
  40. return false;
  41. }
  42. final L2RecipeList rp = RecipeController.getInstance().getRecipeByItemId(item.getItemId());
  43. if (rp == null)
  44. {
  45. return false;
  46. }
  47. if (activeChar.hasRecipeList(rp.getId()))
  48. {
  49. activeChar.sendPacket(SystemMessageId.RECIPE_ALREADY_REGISTERED);
  50. return false;
  51. }
  52. boolean canCraft = false;
  53. boolean recipeLevel = false;
  54. boolean recipeLimit = false;
  55. if (rp.isDwarvenRecipe() && (canCraft = activeChar.hasDwarvenCraft()))
  56. {
  57. if (recipeLevel = (rp.getLevel() > activeChar.getDwarvenCraft()))
  58. {
  59. recipeLimit = activeChar.getDwarvenRecipeBook().length >= activeChar.getDwarfRecipeLimit();
  60. }
  61. }
  62. else if (canCraft = activeChar.hasCommonCraft())
  63. {
  64. if (recipeLevel = (rp.getLevel() > activeChar.getCommonCraft()))
  65. {
  66. recipeLimit = activeChar.getCommonRecipeBook().length >= activeChar.getCommonRecipeLimit();
  67. }
  68. }
  69. if (!canCraft)
  70. {
  71. activeChar.sendPacket(SystemMessageId.CANT_REGISTER_NO_ABILITY_TO_CRAFT);
  72. return false;
  73. }
  74. if (recipeLevel)
  75. {
  76. activeChar.sendPacket(SystemMessageId.CREATE_LVL_TOO_LOW_TO_REGISTER);
  77. return false;
  78. }
  79. if (recipeLimit)
  80. {
  81. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.UP_TO_S1_RECIPES_CAN_REGISTER);
  82. sm.addNumber(rp.isDwarvenRecipe() ? activeChar.getDwarfRecipeLimit() : activeChar.getCommonRecipeLimit());
  83. activeChar.sendPacket(sm);
  84. return false;
  85. }
  86. if (rp.isDwarvenRecipe())
  87. {
  88. activeChar.registerDwarvenRecipeList(rp, true);
  89. }
  90. else
  91. {
  92. activeChar.registerCommonRecipeList(rp, true);
  93. }
  94. activeChar.destroyItem("Consume", item.getObjectId(), 1, null, false);
  95. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_ADDED);
  96. sm.addItemName(item);
  97. activeChar.sendPacket(sm);
  98. return true;
  99. }
  100. }