MercTicket.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.itemhandlers;
  16. import net.sf.l2j.gameserver.handler.IItemHandler;
  17. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  18. import net.sf.l2j.gameserver.instancemanager.MercTicketManager;
  19. import net.sf.l2j.gameserver.model.L2ItemInstance;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  21. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  22. import net.sf.l2j.gameserver.model.entity.Castle;
  23. import net.sf.l2j.gameserver.network.SystemMessageId;
  24. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  25. public class MercTicket implements IItemHandler
  26. {
  27. private static final String[] MESSAGES =
  28. {
  29. "To arms!.",
  30. "I am ready to serve you my lord when the time comes.",
  31. "You summon me."
  32. };
  33. /**
  34. * handler for using mercenary tickets. Things to do:
  35. * 1) Check constraints:
  36. * 1.a) Tickets may only be used in a castle
  37. * 1.b) Only specific tickets may be used in each castle (different tickets for each castle)
  38. * 1.c) only the owner of that castle may use them
  39. * 1.d) tickets cannot be used during siege
  40. * 1.e) Check if max number of tickets has been reached
  41. * 1.f) Check if max number of tickets from this ticket's TYPE has been reached
  42. * 2) If allowed, call the MercTicketManager to add the item and spawn in the world
  43. * 3) Remove the item from the person's inventory
  44. */
  45. public void useItem(L2PlayableInstance playable, L2ItemInstance item)
  46. {
  47. int itemId = item.getItemId();
  48. L2PcInstance activeChar = (L2PcInstance) playable;
  49. Castle castle = CastleManager.getInstance().getCastle(activeChar);
  50. int castleId = -1;
  51. if (castle != null)
  52. castleId = castle.getCastleId();
  53. //add check that certain tickets can only be placed in certain castles
  54. if (MercTicketManager.getInstance().getTicketCastleId(itemId) != castleId)
  55. {
  56. switch (castleId)
  57. {
  58. case 1:
  59. activeChar.sendMessage("This Mercenary Ticket can only be used in Gludio.");
  60. return;
  61. case 2:
  62. activeChar.sendMessage("This Mercenary Ticket can only be used in Dion.");
  63. return;
  64. case 3:
  65. activeChar.sendMessage("This Mercenary Ticket can only be used in Giran.");
  66. return;
  67. case 4:
  68. activeChar.sendMessage("This Mercenary Ticket can only be used in Oren.");
  69. return;
  70. case 5:
  71. activeChar.sendMessage("This Mercenary Ticket can only be used in Aden.");
  72. return;
  73. case 6:
  74. activeChar.sendMessage("This Mercenary Ticket can only be used in Heine.");
  75. return;
  76. case 7:
  77. activeChar.sendMessage("This Mercenary Ticket can only be used in Goddard.");
  78. return;
  79. case 8:
  80. activeChar.sendMessage("This Mercenary Ticket can only be used in Rune.");
  81. return;
  82. case 9:
  83. activeChar.sendMessage("This Mercenary Ticket can only be used in Schuttgart.");
  84. return;
  85. // player is not in a castle
  86. default:
  87. activeChar.sendMessage("Mercenary Tickets can only be used in a castle.");
  88. return;
  89. }
  90. }
  91. if (!activeChar.isCastleLord(castleId))
  92. {
  93. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_DO_NOT_HAVE_AUTHORITY_TO_POSITION_MERCENARIES));
  94. return;
  95. }
  96. if (castle.getSiege().getIsInProgress())
  97. {
  98. activeChar.sendMessage("You cannot hire mercenary while siege is in progress!");
  99. return;
  100. }
  101. if (MercTicketManager.getInstance().isAtCasleLimit(item.getItemId()))
  102. {
  103. activeChar.sendMessage("You cannot hire any more mercenaries");
  104. return;
  105. }
  106. if (MercTicketManager.getInstance().isAtTypeLimit(item.getItemId()))
  107. {
  108. activeChar.sendMessage("You cannot hire any more mercenaries of this type. You may still hire other types of mercenaries");
  109. return;
  110. }
  111. if (MercTicketManager.getInstance().isTooCloseToAnotherTicket(activeChar.getX(), activeChar.getY(), activeChar.getZ()))
  112. {
  113. activeChar.sendPacket(new SystemMessage(SystemMessageId.POSITIONING_CANNOT_BE_DONE_BECAUSE_DISTANCE_BETWEEN_MERCENARIES_TOO_SHORT));
  114. return;
  115. }
  116. int npcId = MercTicketManager.getInstance().addTicket(item.getItemId(), activeChar, MESSAGES);
  117. activeChar.destroyItem("Consume", item.getObjectId(), 1, null, false); // Remove item from char's inventory
  118. activeChar.sendMessage("Hired mercenary (" + itemId + "," + npcId + ") at coords:" + activeChar.getX() + "," + activeChar.getY() + "," + activeChar.getZ() + " heading:" + activeChar.getHeading());
  119. }
  120. /**
  121. *
  122. * @see net.sf.l2j.gameserver.handler.IItemHandler#getItemIds()
  123. */
  124. public int[] getItemIds()
  125. {
  126. return MercTicketManager.getInstance().getItemIds();
  127. }
  128. }