MercTicketManager.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 com.l2jserver.gameserver.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.datatables.NpcTable;
  24. import com.l2jserver.gameserver.idfactory.IdFactory;
  25. import com.l2jserver.gameserver.model.AutoChatHandler;
  26. import com.l2jserver.gameserver.model.L2ItemInstance;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
  30. import com.l2jserver.gameserver.model.entity.Castle;
  31. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  32. import javolution.util.FastList;
  33. /**
  34. * @author yellowperil & Fulminus
  35. * This class is similar to the SiegeGuardManager, except it handles
  36. * the loading of the mercenary tickets that are dropped on castle floors
  37. * by the castle lords.
  38. * These tickets (aka badges) need to be readded after each server reboot
  39. * except when the server crashed in the middle of an ongoig siege.
  40. * In addition, this class keeps track of the added tickets, in order to
  41. * properly limit the number of mercenaries in each castle and the
  42. * number of mercenaries from each mercenary type.
  43. * Finally, we provide auxilary functions to identify the castle in
  44. * which each item (and its corresponding NPC) belong to, in order to
  45. * help avoid mixing them up.
  46. *
  47. */
  48. public class MercTicketManager
  49. {
  50. protected static final Logger _log = Logger.getLogger(MercTicketManager.class.getName());
  51. public static final MercTicketManager getInstance()
  52. {
  53. return SingletonHolder._instance;
  54. }
  55. // =========================================================
  56. // =========================================================
  57. // Data Field
  58. private List<L2ItemInstance> _droppedTickets; // to keep track of items on the ground
  59. //TODO move all these values into siege.properties
  60. // max tickets per merc type = 10 + (castleid * 2)?
  61. // max ticker per castle = 40 + (castleid * 20)?
  62. private static final int[] MAX_MERC_PER_TYPE = {
  63. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, // Gludio
  64. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, // Dion
  65. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, // Giran
  66. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, // Oren
  67. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // Aden
  68. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // Innadril
  69. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // Goddard
  70. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // Rune
  71. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 // Schuttgart
  72. };
  73. //TODO: not retail like: clan lvl 5 - 30 ticks max, lvl 7+ - 50 max
  74. private static final int[] MERCS_MAX_PER_CASTLE = {
  75. 100, // Gludio
  76. 150, // Dion
  77. 200, // Giran
  78. 300, // Oren
  79. 400, // Aden
  80. 400, // Innadril
  81. 400, // Goddard
  82. 400, // Rune
  83. 400 // Schuttgart
  84. };
  85. private static final int[] ITEM_IDS = {
  86. 3960, 3961, 3962, 3963, 3964, 3965, 3966, 3967, 3968, 3969, 6115, 6116, 6117, 6118, 6119, 6120, 6121, 6122, 6123, 6124, 6038, 6039, 6040, 6041, 6042, 6043, 6044, 6045, 6046, 6047, 6175, 6176, 6177, 6178, 6179, 6180, 6181, 6182, 6183, 6184, 6235, 6236, 6237, 6238, 6239, 6240, 6241, 6242, 6243, 6244, 6295, 6296, // Gludio
  87. 3973, 3974, 3975, 3976, 3977, 3978, 3979, 3980, 3981, 3982, 6125, 6126, 6127, 6128, 6129, 6130, 6131, 6132, 6133, 6134, 6051, 6052, 6053, 6054, 6055, 6056, 6057, 6058, 6059, 6060, 6185, 6186, 6187, 6188, 6189, 6190, 6191, 6192, 6193, 6194, 6245, 6246, 6247, 6248, 6249, 6250, 6251, 6252, 6253, 6254, 6297, 6298, // Dion
  88. 3986, 3987, 3988, 3989, 3990, 3991, 3992, 3993, 3994, 3995, 6135, 6136, 6137, 6138, 6139, 6140, 6141, 6142, 6143, 6144, 6064, 6065, 6066, 6067, 6068, 6069, 6070, 6071, 6072, 6073, 6195, 6196, 6197, 6198, 6199, 6200, 6201, 6202, 6203, 6204, 6255, 6256, 6257, 6258, 6259, 6260, 6261, 6262, 6263, 6264, 6299, 6300, // Giran
  89. 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008, 6145, 6146, 6147, 6148, 6149, 6150, 6151, 6152, 6153, 6154, 6077, 6078, 6079, 6080, 6081, 6082, 6083, 6084, 6085, 6086, 6205, 6206, 6207, 6208, 6209, 6210, 6211, 6212, 6213, 6214, 6265, 6266, 6267, 6268, 6269, 6270, 6271, 6272, 6273, 6274, 6301, 6302, // Oren
  90. 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019, 4020, 4021, 6155, 6156, 6157, 6158, 6159, 6160, 6161, 6162, 6163, 6164, 6090, 6091, 6092, 6093, 6094, 6095, 6096, 6097, 6098, 6099, 6215, 6216, 6217, 6218, 6219, 6220, 6221, 6222, 6223, 6224, 6275, 6276, 6277, 6278, 6279, 6280, 6281, 6282, 6283, 6284, 6303, 6304, // Aden
  91. 5205, 5206, 5207, 5208, 5209, 5210, 5211, 5212, 5213, 5214, 6165, 6166, 6167, 6168, 6169, 6170, 6171, 6172, 6173, 6174, 6105, 6106, 6107, 6108, 6109, 6110, 6111, 6112, 6113, 6114, 6225, 6226, 6227, 6228, 6229, 6230, 6231, 6232, 6233, 6234, 6285, 6286, 6287, 6288, 6289, 6290, 6291, 6292, 6293, 6294, 6305, 6306, // Innadril
  92. 6779, 6780, 6781, 6782, 6783, 6784, 6785, 6786, 6787, 6788, 6802, 6803, 6804, 6805, 6806, 6807, 6808, 6809, 6810, 6811, 6792, 6793, 6794, 6795, 6796, 6797, 6798, 6799, 6800, 6801, 6812, 6813, 6814, 6815, 6816, 6817, 6818, 6819, 6820, 6821, 6822, 6823, 6824, 6825, 6826, 6827, 6828, 6829, 6830, 6831, 6832, 6833, // Goddard
  93. 7973, 7974, 7975, 7976, 7977, 7978, 7979, 7980, 7981, 7982, 7998, 7999, 8000, 8001, 8002, 8003, 8004, 8005, 8006, 8007, 7988, 7989, 7990, 7991, 7992, 7993, 7994, 7995, 7996, 7997, 8008, 8009, 8010, 8011, 8012, 8013, 8014, 8015, 8016, 8017, 8018, 8019, 8020, 8021, 8022, 8023, 8024, 8025, 8026, 8027, 8028, 8029, // Rune
  94. 7918, 7919, 7920, 7921, 7922, 7923, 7924, 7925, 7926, 7927, 7941, 7942, 7943, 7944, 7945, 7946, 7947, 7948, 7949, 7950, 7931, 7932, 7933, 7934, 7935, 7936, 7937, 7938, 7939, 7940, 7951, 7952, 7953, 7954, 7955, 7956, 7957, 7958, 7959, 7960, 7961, 7962, 7963, 7964, 7965, 7966, 7967, 7968, 7969, 7970, 7971, 7972 // Schuttgart
  95. };
  96. private static final int[] NPC_IDS = {35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Gludio
  97. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Dion
  98. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Giran
  99. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Oren
  100. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Aden
  101. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Innadril
  102. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Goddard
  103. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Rune
  104. 35010, 35011, 35012, 35013, 35014, 35015, 35016, 35017, 35018, 35019, 35020, 35021, 35022, 35023, 35024, 35025, 35026, 35027, 35028, 35029, 35030,35031,35032,35033,35034,35035,35036,35037,35038,35039, 35040, 35041, 35042, 35043, 35044, 35045, 35046, 35047, 35048, 35049, 35050, 35051, 35052, 35053, 35054, 35055, 35056, 35057, 35058, 35059, 35060, 35061, // Schuttgart
  105. };
  106. private static final int GUARDIAN_TYPES_COUNT = 52;
  107. // =========================================================
  108. // Constructor
  109. private MercTicketManager()
  110. {
  111. _log.info("Initializing MercTicketManager");
  112. load();
  113. }
  114. // =========================================================
  115. // Method - Public
  116. // returns the castleId for the passed ticket item id
  117. public int getTicketCastleId(int itemId)
  118. {
  119. for (int i = 0; i < 9; i++) //CastleID`s from 1 to 9 minus;
  120. {
  121. for (int i2 = 0; i2 < 50; i2 += 10)
  122. //Simplified if statement;
  123. if ((itemId >= ITEM_IDS[i2 + i * GUARDIAN_TYPES_COUNT] && itemId <= ITEM_IDS[i2 + 9 + i * GUARDIAN_TYPES_COUNT]))
  124. return i + 1;
  125. if (itemId >= ITEM_IDS[50] && itemId <= ITEM_IDS[51])
  126. return i + 1;
  127. }
  128. return -1;
  129. }
  130. public void reload()
  131. {
  132. getDroppedTickets().clear();
  133. load();
  134. }
  135. // =========================================================
  136. // Method - Private
  137. private final void load()
  138. {
  139. Connection con = null;
  140. // load merc tickets into the world
  141. try
  142. {
  143. PreparedStatement statement;
  144. ResultSet rs;
  145. con = L2DatabaseFactory.getInstance().getConnection();
  146. statement = con.prepareStatement("SELECT * FROM castle_siege_guards Where isHired = 1");
  147. rs = statement.executeQuery();
  148. int npcId;
  149. int itemId;
  150. int x, y, z;
  151. int mercPlaced[] = new int[20];
  152. // start index to begin the search for the itemId corresponding to this NPC
  153. // this will help with:
  154. // a) skip unnecessary iterations in the search loop
  155. // b) avoid finding the wrong itemId whenever tickets of different spawn the same npc!
  156. int startindex = 0;
  157. while (rs.next())
  158. {
  159. npcId = rs.getInt("npcId");
  160. x = rs.getInt("x");
  161. y = rs.getInt("y");
  162. z = rs.getInt("z");
  163. Castle castle = CastleManager.getInstance().getCastle(x, y, z);
  164. if (castle != null)
  165. {
  166. if (mercPlaced[castle.getCastleId() - 1] >= MERCS_MAX_PER_CASTLE[castle.getCastleId() - 1])
  167. continue;
  168. startindex = GUARDIAN_TYPES_COUNT * (castle.getCastleId() - 1);
  169. mercPlaced[castle.getCastleId() - 1] += 1;
  170. }
  171. // find the FIRST ticket itemId with spawns the saved NPC in the saved location
  172. for (int i = startindex; i < startindex + GUARDIAN_TYPES_COUNT; i++)
  173. if (NPC_IDS[i] == npcId) // Find the index of the item used
  174. {
  175. // only handle tickets if a siege is not ongoing in this npc's castle
  176. if ((castle != null) && !(castle.getSiege().getIsInProgress()))
  177. {
  178. itemId = ITEM_IDS[i];
  179. // create the ticket in the gameworld
  180. L2ItemInstance dropticket = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  181. dropticket.setLocation(L2ItemInstance.ItemLocation.VOID);
  182. dropticket.dropMe(null, x, y, z);
  183. dropticket.setDropTime(0); //avoids it from beeing removed by the auto item destroyer
  184. L2World.getInstance().storeObject(dropticket);
  185. getDroppedTickets().add(dropticket);
  186. }
  187. break;
  188. }
  189. }
  190. statement.close();
  191. _log.info("Loaded: " + getDroppedTickets().size() + " Mercenary Tickets");
  192. }
  193. catch (Exception e)
  194. {
  195. _log.warning("Exception: loadMercenaryData(): " + e.getMessage());
  196. e.printStackTrace();
  197. }
  198. finally
  199. {
  200. try
  201. {
  202. con.close();
  203. }
  204. catch (Exception e)
  205. {
  206. }
  207. }
  208. }
  209. // =========================================================
  210. // Property - Public
  211. /**
  212. * Checks if the passed item has reached the limit of number of dropped
  213. * tickets that this SPECIFIC item may have in its castle
  214. */
  215. public boolean isAtTypeLimit(int itemId)
  216. {
  217. int limit = -1;
  218. // find the max value for this item
  219. for (int i = 0; i < ITEM_IDS.length; i++)
  220. if (ITEM_IDS[i] == itemId) // Find the index of the item used
  221. {
  222. limit = MAX_MERC_PER_TYPE[i];
  223. break;
  224. }
  225. if (limit <= 0)
  226. return true;
  227. int count = 0;
  228. for (L2ItemInstance ticket : getDroppedTickets())
  229. {
  230. if (ticket != null && ticket.getItemId() == itemId)
  231. count++;
  232. }
  233. if (count >= limit)
  234. return true;
  235. return false;
  236. }
  237. /**
  238. * Checks if the passed item belongs to a castle which has reached its limit
  239. * of number of dropped tickets.
  240. */
  241. public boolean isAtCasleLimit(int itemId)
  242. {
  243. int castleId = getTicketCastleId(itemId);
  244. if (castleId <= 0)
  245. return true;
  246. int limit = MERCS_MAX_PER_CASTLE[castleId - 1];
  247. if (limit <= 0)
  248. return true;
  249. int count = 0;
  250. for (L2ItemInstance ticket : getDroppedTickets())
  251. {
  252. if ((ticket != null) && (getTicketCastleId(ticket.getItemId()) == castleId))
  253. count++;
  254. }
  255. if (count >= limit)
  256. return true;
  257. return false;
  258. }
  259. public int getMaxAllowedMerc(int castleId)
  260. {
  261. return MERCS_MAX_PER_CASTLE[castleId - 1];
  262. }
  263. public boolean isTooCloseToAnotherTicket(int x, int y, int z)
  264. {
  265. for (L2ItemInstance item : getDroppedTickets())
  266. {
  267. double dx = x - item.getX();
  268. double dy = y - item.getY();
  269. double dz = z - item.getZ();
  270. if ((dx * dx + dy * dy + dz * dz) < 25 * 25)
  271. return true;
  272. }
  273. return false;
  274. }
  275. /**
  276. * addTicket actions
  277. * 1) find the npc that needs to be saved in the mercenary spawns, given this item
  278. * 2) Use the passed character's location info to add the spawn
  279. * 3) create a copy of the item to drop in the world
  280. * returns the id of the mercenary npc that was added to the spawn
  281. * returns -1 if this fails.
  282. */
  283. public int addTicket(int itemId, L2PcInstance activeChar, String[] messages)
  284. {
  285. int x = activeChar.getX();
  286. int y = activeChar.getY();
  287. int z = activeChar.getZ();
  288. int heading = activeChar.getHeading();
  289. Castle castle = CastleManager.getInstance().getCastle(activeChar);
  290. if (castle == null) //this should never happen at this point
  291. return -1;
  292. for (int i = 0; i < ITEM_IDS.length; i++)
  293. {
  294. if (ITEM_IDS[i] == itemId) // Find the index of the item used
  295. {
  296. spawnMercenary(NPC_IDS[i], x, y, z, 3000, messages, 0);
  297. // Hire merc for this caslte. NpcId is at the same index as the item used.
  298. castle.getSiege().getSiegeGuardManager().hireMerc(x, y, z, heading, NPC_IDS[i]);
  299. // create the ticket in the gameworld
  300. L2ItemInstance dropticket = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  301. dropticket.setLocation(L2ItemInstance.ItemLocation.VOID);
  302. dropticket.dropMe(null, x, y, z);
  303. dropticket.setDropTime(0); //avoids it from beeing removed by the auto item destroyer
  304. L2World.getInstance().storeObject(dropticket); //add to the world
  305. // and keep track of this ticket in the list
  306. _droppedTickets.add(dropticket);
  307. return NPC_IDS[i];
  308. }
  309. }
  310. return -1;
  311. }
  312. private void spawnMercenary(int npcId, int x, int y, int z, int despawnDelay, String[] messages, int chatDelay)
  313. {
  314. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  315. if (template != null)
  316. {
  317. final L2DefenderInstance npc = new L2DefenderInstance(IdFactory.getInstance().getNextId(), template);
  318. npc.setCurrentHpMp(npc.getMaxHp(), npc.getMaxMp());
  319. npc.setDecayed(false);
  320. npc.spawnMe(x, y, (z + 20));
  321. if (messages != null && messages.length > 0)
  322. AutoChatHandler.getInstance().registerChat(npc, messages, chatDelay);
  323. if (despawnDelay > 0)
  324. {
  325. ThreadPoolManager.getInstance().scheduleGeneral(new Runnable() {
  326. public void run()
  327. {
  328. npc.deleteMe();
  329. }
  330. }, despawnDelay);
  331. }
  332. }
  333. }
  334. /**
  335. * Delete all tickets from a castle;
  336. * remove the items from the world and remove references to them from this class
  337. */
  338. public void deleteTickets(int castleId)
  339. {
  340. List<L2ItemInstance> toDelete = new FastList<L2ItemInstance>();
  341. for (L2ItemInstance item : getDroppedTickets())
  342. {
  343. if ((item != null) && (getTicketCastleId(item.getItemId()) == castleId))
  344. {
  345. item.decayMe();
  346. L2World.getInstance().removeObject(item);
  347. // remove from the list
  348. toDelete.add(item);
  349. }
  350. }
  351. getDroppedTickets().removeAll(toDelete);
  352. }
  353. /**
  354. * remove a single ticket and its associated spawn from the world
  355. * (used when the castle lord picks up a ticket, for example)
  356. */
  357. public void removeTicket(L2ItemInstance item)
  358. {
  359. int itemId = item.getItemId();
  360. int npcId = -1;
  361. // find the FIRST ticket itemId with spawns the saved NPC in the saved location
  362. for (int i = 0; i < ITEM_IDS.length; i++)
  363. if (ITEM_IDS[i] == itemId) // Find the index of the item used
  364. {
  365. npcId = NPC_IDS[i];
  366. break;
  367. }
  368. // find the castle where this item is
  369. Castle castle = CastleManager.getInstance().getCastleById(getTicketCastleId(itemId));
  370. if (npcId > 0 && castle != null)
  371. {
  372. (new SiegeGuardManager(castle)).removeMerc(npcId, item.getX(), item.getY(), item.getZ());
  373. }
  374. getDroppedTickets().remove(item);
  375. }
  376. public int[] getItemIds()
  377. {
  378. return ITEM_IDS;
  379. }
  380. public final List<L2ItemInstance> getDroppedTickets()
  381. {
  382. if (_droppedTickets == null)
  383. synchronized (this)
  384. {
  385. if (_droppedTickets == null)
  386. _droppedTickets = new FastList<L2ItemInstance>();
  387. }
  388. return _droppedTickets;
  389. }
  390. @SuppressWarnings("synthetic-access")
  391. private static class SingletonHolder
  392. {
  393. protected static final MercTicketManager _instance = new MercTicketManager();
  394. }
  395. }