TvTEvent.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.entity;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.gameserver.cache.HtmCache;
  30. import com.l2jserver.gameserver.datatables.DoorTable;
  31. import com.l2jserver.gameserver.datatables.ItemTable;
  32. import com.l2jserver.gameserver.datatables.NpcData;
  33. import com.l2jserver.gameserver.datatables.SkillData;
  34. import com.l2jserver.gameserver.datatables.SpawnTable;
  35. import com.l2jserver.gameserver.instancemanager.AntiFeedManager;
  36. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  37. import com.l2jserver.gameserver.model.L2Spawn;
  38. import com.l2jserver.gameserver.model.L2World;
  39. import com.l2jserver.gameserver.model.actor.L2Character;
  40. import com.l2jserver.gameserver.model.actor.L2Npc;
  41. import com.l2jserver.gameserver.model.actor.L2Summon;
  42. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  43. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  44. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  45. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  46. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  47. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  48. import com.l2jserver.gameserver.model.olympiad.OlympiadManager;
  49. import com.l2jserver.gameserver.model.skills.Skill;
  50. import com.l2jserver.gameserver.network.SystemMessageId;
  51. import com.l2jserver.gameserver.network.clientpackets.Say2;
  52. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  53. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  54. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  55. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  56. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  57. import com.l2jserver.gameserver.scripting.scriptengine.events.TvtKillEvent;
  58. import com.l2jserver.gameserver.scripting.scriptengine.impl.L2Script.EventStage;
  59. import com.l2jserver.gameserver.scripting.scriptengine.listeners.events.TvTListener;
  60. import com.l2jserver.util.Rnd;
  61. import com.l2jserver.util.StringUtil;
  62. /**
  63. * @author FBIagent
  64. */
  65. public class TvTEvent
  66. {
  67. enum EventState
  68. {
  69. INACTIVE,
  70. INACTIVATING,
  71. PARTICIPATING,
  72. STARTING,
  73. STARTED,
  74. REWARDING
  75. }
  76. protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
  77. /** html path **/
  78. private static final String htmlPath = "data/html/mods/TvTEvent/";
  79. /** The teams of the TvTEvent<br> */
  80. private static TvTEventTeam[] _teams = new TvTEventTeam[2];
  81. /** The state of the TvTEvent<br> */
  82. private static EventState _state = EventState.INACTIVE;
  83. /** The spawn of the participation npc<br> */
  84. private static L2Spawn _npcSpawn = null;
  85. /** the npc instance of the participation npc<br> */
  86. private static L2Npc _lastNpcSpawn = null;
  87. /** Instance id<br> */
  88. private static int _TvTEventInstance = 0;
  89. private static FastList<TvTListener> tvtListeners = new FastList<TvTListener>().shared();
  90. /**
  91. * No instance of this class!<br>
  92. */
  93. private TvTEvent()
  94. {
  95. }
  96. /**
  97. * Teams initializing<br>
  98. */
  99. public static void init()
  100. {
  101. AntiFeedManager.getInstance().registerEvent(AntiFeedManager.TVT_ID);
  102. _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
  103. _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
  104. }
  105. /**
  106. * Starts the participation of the TvTEvent<br>
  107. * 1. Get L2NpcTemplate by Config.TVT_EVENT_PARTICIPATION_NPC_ID<br>
  108. * 2. Try to spawn a new npc of it<br>
  109. * <br>
  110. * @return boolean: true if success, otherwise false<br>
  111. */
  112. public static boolean startParticipation()
  113. {
  114. L2NpcTemplate tmpl = NpcData.getInstance().getTemplate(Config.TVT_EVENT_PARTICIPATION_NPC_ID);
  115. if (tmpl == null)
  116. {
  117. _log.warning("TvTEventEngine[TvTEvent.startParticipation()]: L2NpcTemplate is a NullPointer -> Invalid npc id in configs?");
  118. return false;
  119. }
  120. try
  121. {
  122. _npcSpawn = new L2Spawn(tmpl);
  123. _npcSpawn.setX(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0]);
  124. _npcSpawn.setY(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1]);
  125. _npcSpawn.setZ(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  126. _npcSpawn.setAmount(1);
  127. _npcSpawn.setHeading(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[3]);
  128. _npcSpawn.setRespawnDelay(1);
  129. // later no need to delete spawn from db, we don't store it (false)
  130. SpawnTable.getInstance().addNewSpawn(_npcSpawn, false);
  131. _npcSpawn.init();
  132. _lastNpcSpawn = _npcSpawn.getLastSpawn();
  133. _lastNpcSpawn.setCurrentHp(_lastNpcSpawn.getMaxHp());
  134. _lastNpcSpawn.setTitle("TvT Event Participation");
  135. _lastNpcSpawn.isAggressive();
  136. _lastNpcSpawn.decayMe();
  137. _lastNpcSpawn.spawnMe(_npcSpawn.getLastSpawn().getX(), _npcSpawn.getLastSpawn().getY(), _npcSpawn.getLastSpawn().getZ());
  138. _lastNpcSpawn.broadcastPacket(new MagicSkillUse(_lastNpcSpawn, _lastNpcSpawn, 1034, 1, 1, 1));
  139. }
  140. catch (Exception e)
  141. {
  142. _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.startParticipation()]: exception: " + e.getMessage(), e);
  143. return false;
  144. }
  145. setState(EventState.PARTICIPATING);
  146. fireTvtEventListeners(EventStage.REGISTRATION_BEGIN);
  147. return true;
  148. }
  149. private static int highestLevelPcInstanceOf(Map<Integer, L2PcInstance> players)
  150. {
  151. int maxLevel = Integer.MIN_VALUE, maxLevelId = -1;
  152. for (L2PcInstance player : players.values())
  153. {
  154. if (player.getLevel() >= maxLevel)
  155. {
  156. maxLevel = player.getLevel();
  157. maxLevelId = player.getObjectId();
  158. }
  159. }
  160. return maxLevelId;
  161. }
  162. /**
  163. * Starts the TvTEvent fight<br>
  164. * 1. Set state EventState.STARTING<br>
  165. * 2. Close doors specified in configs<br>
  166. * 3. Abort if not enought participants(return false)<br>
  167. * 4. Set state EventState.STARTED<br>
  168. * 5. Teleport all participants to team spot<br>
  169. * <br>
  170. * @return boolean: true if success, otherwise false<br>
  171. */
  172. public static boolean startFight()
  173. {
  174. // Set state to STARTING
  175. setState(EventState.STARTING);
  176. // Randomize and balance team distribution
  177. Map<Integer, L2PcInstance> allParticipants = new FastMap<>();
  178. allParticipants.putAll(_teams[0].getParticipatedPlayers());
  179. allParticipants.putAll(_teams[1].getParticipatedPlayers());
  180. _teams[0].cleanMe();
  181. _teams[1].cleanMe();
  182. L2PcInstance player;
  183. Iterator<L2PcInstance> iter;
  184. if (needParticipationFee())
  185. {
  186. iter = allParticipants.values().iterator();
  187. while (iter.hasNext())
  188. {
  189. player = iter.next();
  190. if (!hasParticipationFee(player))
  191. {
  192. iter.remove();
  193. }
  194. }
  195. }
  196. int balance[] =
  197. {
  198. 0,
  199. 0
  200. }, priority = 0, highestLevelPlayerId;
  201. L2PcInstance highestLevelPlayer;
  202. // TODO: allParticipants should be sorted by level instead of using highestLevelPcInstanceOf for every fetch
  203. while (!allParticipants.isEmpty())
  204. {
  205. // Priority team gets one player
  206. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  207. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  208. allParticipants.remove(highestLevelPlayerId);
  209. _teams[priority].addPlayer(highestLevelPlayer);
  210. balance[priority] += highestLevelPlayer.getLevel();
  211. // Exiting if no more players
  212. if (allParticipants.isEmpty())
  213. {
  214. break;
  215. }
  216. // The other team gets one player
  217. // TODO: Code not dry
  218. priority = 1 - priority;
  219. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  220. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  221. allParticipants.remove(highestLevelPlayerId);
  222. _teams[priority].addPlayer(highestLevelPlayer);
  223. balance[priority] += highestLevelPlayer.getLevel();
  224. // Recalculating priority
  225. priority = balance[0] > balance[1] ? 1 : 0;
  226. }
  227. // Check for enought participants
  228. if ((_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS) || (_teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS))
  229. {
  230. // Set state INACTIVE
  231. setState(EventState.INACTIVE);
  232. // Cleanup of teams
  233. _teams[0].cleanMe();
  234. _teams[1].cleanMe();
  235. // Unspawn the event NPC
  236. unSpawnNpc();
  237. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  238. return false;
  239. }
  240. if (needParticipationFee())
  241. {
  242. iter = _teams[0].getParticipatedPlayers().values().iterator();
  243. while (iter.hasNext())
  244. {
  245. player = iter.next();
  246. if (!payParticipationFee(player))
  247. {
  248. iter.remove();
  249. }
  250. }
  251. iter = _teams[1].getParticipatedPlayers().values().iterator();
  252. while (iter.hasNext())
  253. {
  254. player = iter.next();
  255. if (!payParticipationFee(player))
  256. {
  257. iter.remove();
  258. }
  259. }
  260. }
  261. if (Config.TVT_EVENT_IN_INSTANCE)
  262. {
  263. try
  264. {
  265. _TvTEventInstance = InstanceManager.getInstance().createDynamicInstance(Config.TVT_EVENT_INSTANCE_FILE);
  266. InstanceManager.getInstance().getInstance(_TvTEventInstance).setAllowSummon(false);
  267. InstanceManager.getInstance().getInstance(_TvTEventInstance).setPvPInstance(true);
  268. InstanceManager.getInstance().getInstance(_TvTEventInstance).setEmptyDestroyTime((Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY * 1000) + 60000L);
  269. }
  270. catch (Exception e)
  271. {
  272. _TvTEventInstance = 0;
  273. _log.log(Level.WARNING, "TvTEventEngine[TvTEvent.createDynamicInstance]: exception: " + e.getMessage(), e);
  274. }
  275. }
  276. // Opens all doors specified in configs for tvt
  277. openDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  278. // Closes all doors specified in configs for tvt
  279. closeDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  280. // Set state STARTED
  281. setState(EventState.STARTED);
  282. // Iterate over all teams
  283. for (TvTEventTeam team : _teams)
  284. {
  285. // Iterate over all participated player instances in this team
  286. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  287. {
  288. if (playerInstance != null)
  289. {
  290. // Disable player revival.
  291. playerInstance.setCanRevive(false);
  292. // Teleporter implements Runnable and starts itself
  293. new TvTEventTeleporter(playerInstance, team.getCoordinates(), false, false);
  294. }
  295. }
  296. }
  297. fireTvtEventListeners(EventStage.START);
  298. return true;
  299. }
  300. /**
  301. * Calculates the TvTEvent reward<br>
  302. * 1. If both teams are at a tie(points equals), send it as system message to all participants, if one of the teams have 0 participants left online abort rewarding<br>
  303. * 2. Wait till teams are not at a tie anymore<br>
  304. * 3. Set state EvcentState.REWARDING<br>
  305. * 4. Reward team with more points<br>
  306. * 5. Show win html to wining team participants<br>
  307. * <br>
  308. * @return String: winning team name<br>
  309. */
  310. public static String calculateRewards()
  311. {
  312. if (_teams[0].getPoints() == _teams[1].getPoints())
  313. {
  314. // Check if one of the teams have no more players left
  315. if ((_teams[0].getParticipatedPlayerCount() == 0) || (_teams[1].getParticipatedPlayerCount() == 0))
  316. {
  317. // set state to rewarding
  318. setState(EventState.REWARDING);
  319. // return here, the fight can't be completed
  320. return "TvT Event: Event has ended. No team won due to inactivity!";
  321. }
  322. // Both teams have equals points
  323. sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
  324. if (Config.TVT_REWARD_TEAM_TIE)
  325. {
  326. rewardTeam(_teams[0]);
  327. rewardTeam(_teams[1]);
  328. return "TvT Event: Event has ended with both teams tying.";
  329. }
  330. return "TvT Event: Event has ended with both teams tying.";
  331. }
  332. // Set state REWARDING so nobody can point anymore
  333. setState(EventState.REWARDING);
  334. // Get team which has more points
  335. TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
  336. rewardTeam(team);
  337. fireTvtEventListeners(EventStage.END);
  338. return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
  339. }
  340. private static void rewardTeam(TvTEventTeam team)
  341. {
  342. // Iterate over all participated player instances of the winning team
  343. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  344. {
  345. // Check for nullpointer
  346. if (playerInstance == null)
  347. {
  348. continue;
  349. }
  350. SystemMessage systemMessage = null;
  351. // Iterate over all tvt event rewards
  352. for (int[] reward : Config.TVT_EVENT_REWARDS)
  353. {
  354. PcInventory inv = playerInstance.getInventory();
  355. // Check for stackable item, non stackabe items need to be added one by one
  356. if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  357. {
  358. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  359. if (reward[1] > 1)
  360. {
  361. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  362. systemMessage.addItemName(reward[0]);
  363. systemMessage.addLong(reward[1]);
  364. }
  365. else
  366. {
  367. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  368. systemMessage.addItemName(reward[0]);
  369. }
  370. playerInstance.sendPacket(systemMessage);
  371. }
  372. else
  373. {
  374. for (int i = 0; i < reward[1]; ++i)
  375. {
  376. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  377. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  378. systemMessage.addItemName(reward[0]);
  379. playerInstance.sendPacket(systemMessage);
  380. }
  381. }
  382. }
  383. StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
  384. final NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage();
  385. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  386. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Reward.htm"));
  387. playerInstance.sendPacket(statusUpdate);
  388. playerInstance.sendPacket(npcHtmlMessage);
  389. }
  390. }
  391. /**
  392. * Stops the TvTEvent fight<br>
  393. * 1. Set state EventState.INACTIVATING<br>
  394. * 2. Remove tvt npc from world<br>
  395. * 3. Open doors specified in configs<br>
  396. * 4. Teleport all participants back to participation npc location<br>
  397. * 5. Teams cleaning<br>
  398. * 6. Set state EventState.INACTIVE<br>
  399. */
  400. public static void stopFight()
  401. {
  402. // Set state INACTIVATING
  403. setState(EventState.INACTIVATING);
  404. // Unspawn event npc
  405. unSpawnNpc();
  406. // Opens all doors specified in configs for tvt
  407. openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  408. // Closes all doors specified in Configs for tvt
  409. closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  410. // Iterate over all teams
  411. for (TvTEventTeam team : _teams)
  412. {
  413. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  414. {
  415. // Check for nullpointer
  416. if (playerInstance != null)
  417. {
  418. // Enable player revival.
  419. playerInstance.setCanRevive(true);
  420. // Teleport back.
  421. new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false, false);
  422. }
  423. }
  424. }
  425. // Cleanup of teams
  426. _teams[0].cleanMe();
  427. _teams[1].cleanMe();
  428. // Set state INACTIVE
  429. setState(EventState.INACTIVE);
  430. AntiFeedManager.getInstance().clear(AntiFeedManager.TVT_ID);
  431. }
  432. /**
  433. * Adds a player to a TvTEvent team<br>
  434. * 1. Calculate the id of the team in which the player should be added<br>
  435. * 2. Add the player to the calculated team<br>
  436. * <br>
  437. * @param playerInstance as L2PcInstance<br>
  438. * @return boolean: true if success, otherwise false<br>
  439. */
  440. public static synchronized boolean addParticipant(L2PcInstance playerInstance)
  441. {
  442. // Check for nullpoitner
  443. if (playerInstance == null)
  444. {
  445. return false;
  446. }
  447. byte teamId = 0;
  448. // Check to which team the player should be added
  449. if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
  450. {
  451. teamId = (byte) (Rnd.get(2));
  452. }
  453. else
  454. {
  455. teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
  456. }
  457. playerInstance.addEventListener(new TvTEventListener(playerInstance));
  458. return _teams[teamId].addPlayer(playerInstance);
  459. }
  460. /**
  461. * Removes a TvTEvent player from it's team<br>
  462. * 1. Get team id of the player<br>
  463. * 2. Remove player from it's team<br>
  464. * <br>
  465. * @param playerObjectId
  466. * @return boolean: true if success, otherwise false
  467. */
  468. public static boolean removeParticipant(int playerObjectId)
  469. {
  470. // Get the teamId of the player
  471. byte teamId = getParticipantTeamId(playerObjectId);
  472. // Check if the player is participant
  473. if (teamId != -1)
  474. {
  475. // Remove the player from team
  476. _teams[teamId].removePlayer(playerObjectId);
  477. final L2PcInstance player = L2World.getInstance().getPlayer(playerObjectId);
  478. if (player != null)
  479. {
  480. player.removeEventListener(TvTEventListener.class);
  481. }
  482. return true;
  483. }
  484. return false;
  485. }
  486. public static boolean needParticipationFee()
  487. {
  488. return (Config.TVT_EVENT_PARTICIPATION_FEE[0] != 0) && (Config.TVT_EVENT_PARTICIPATION_FEE[1] != 0);
  489. }
  490. public static boolean hasParticipationFee(L2PcInstance playerInstance)
  491. {
  492. return playerInstance.getInventory().getInventoryItemCount(Config.TVT_EVENT_PARTICIPATION_FEE[0], -1) >= Config.TVT_EVENT_PARTICIPATION_FEE[1];
  493. }
  494. public static boolean payParticipationFee(L2PcInstance playerInstance)
  495. {
  496. return playerInstance.destroyItemByItemId("TvT Participation Fee", Config.TVT_EVENT_PARTICIPATION_FEE[0], Config.TVT_EVENT_PARTICIPATION_FEE[1], _lastNpcSpawn, true);
  497. }
  498. public static String getParticipationFee()
  499. {
  500. int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
  501. int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
  502. if ((itemId == 0) || (itemNum == 0))
  503. {
  504. return "-";
  505. }
  506. return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
  507. }
  508. /**
  509. * Send a SystemMessage to all participated players<br>
  510. * 1. Send the message to all players of team number one<br>
  511. * 2. Send the message to all players of team number two<br>
  512. * <br>
  513. * @param message as String<br>
  514. */
  515. public static void sysMsgToAllParticipants(String message)
  516. {
  517. for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
  518. {
  519. if (playerInstance != null)
  520. {
  521. playerInstance.sendMessage(message);
  522. }
  523. }
  524. for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
  525. {
  526. if (playerInstance != null)
  527. {
  528. playerInstance.sendMessage(message);
  529. }
  530. }
  531. }
  532. private static L2DoorInstance getDoor(int doorId)
  533. {
  534. L2DoorInstance door = null;
  535. if (_TvTEventInstance <= 0)
  536. {
  537. door = DoorTable.getInstance().getDoor(doorId);
  538. }
  539. else
  540. {
  541. final Instance inst = InstanceManager.getInstance().getInstance(_TvTEventInstance);
  542. if (inst != null)
  543. {
  544. door = inst.getDoor(doorId);
  545. }
  546. }
  547. return door;
  548. }
  549. /**
  550. * Close doors specified in configs
  551. * @param doors
  552. */
  553. private static void closeDoors(List<Integer> doors)
  554. {
  555. for (int doorId : doors)
  556. {
  557. final L2DoorInstance doorInstance = getDoor(doorId);
  558. if (doorInstance != null)
  559. {
  560. doorInstance.closeMe();
  561. }
  562. }
  563. }
  564. /**
  565. * Open doors specified in configs
  566. * @param doors
  567. */
  568. private static void openDoors(List<Integer> doors)
  569. {
  570. for (int doorId : doors)
  571. {
  572. final L2DoorInstance doorInstance = getDoor(doorId);
  573. if (doorInstance != null)
  574. {
  575. doorInstance.openMe();
  576. }
  577. }
  578. }
  579. /**
  580. * UnSpawns the TvTEvent npc
  581. */
  582. private static void unSpawnNpc()
  583. {
  584. // Delete the npc
  585. _lastNpcSpawn.deleteMe();
  586. SpawnTable.getInstance().deleteSpawn(_lastNpcSpawn.getSpawn(), false);
  587. // Stop respawning of the npc
  588. _npcSpawn.stopRespawn();
  589. _npcSpawn = null;
  590. _lastNpcSpawn = null;
  591. }
  592. /**
  593. * Called when a player logs in<br>
  594. * <br>
  595. * @param playerInstance as L2PcInstance<br>
  596. */
  597. public static void onLogin(L2PcInstance playerInstance)
  598. {
  599. if ((playerInstance == null) || (!isStarting() && !isStarted()))
  600. {
  601. return;
  602. }
  603. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  604. if (teamId == -1)
  605. {
  606. return;
  607. }
  608. _teams[teamId].addPlayer(playerInstance);
  609. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  610. }
  611. /**
  612. * Called when a player logs out<br>
  613. * <br>
  614. * @param playerInstance as L2PcInstance<br>
  615. */
  616. public static void onLogout(L2PcInstance playerInstance)
  617. {
  618. if ((playerInstance != null) && (isStarting() || isStarted() || isParticipating()))
  619. {
  620. if (removeParticipant(playerInstance.getObjectId()))
  621. {
  622. playerInstance.setXYZInvisible((Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)) - 50, (Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)) - 50, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  623. }
  624. }
  625. }
  626. /**
  627. * Called on every bypass by npc of type L2TvTEventNpc<br>
  628. * Needs synchronization cause of the max player check<br>
  629. * <br>
  630. * @param command as String<br>
  631. * @param playerInstance as L2PcInstance<br>
  632. */
  633. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  634. {
  635. if ((playerInstance == null) || !isParticipating())
  636. {
  637. return;
  638. }
  639. final String htmContent;
  640. if (command.equals("tvt_event_participation"))
  641. {
  642. final NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage();
  643. int playerLevel = playerInstance.getLevel();
  644. if (playerInstance.isCursedWeaponEquipped())
  645. {
  646. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "CursedWeaponEquipped.htm");
  647. if (htmContent != null)
  648. {
  649. npcHtmlMessage.setHtml(htmContent);
  650. }
  651. }
  652. else if (OlympiadManager.getInstance().isRegistered(playerInstance))
  653. {
  654. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Olympiad.htm");
  655. if (htmContent != null)
  656. {
  657. npcHtmlMessage.setHtml(htmContent);
  658. }
  659. }
  660. else if (playerInstance.getKarma() > 0)
  661. {
  662. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Karma.htm");
  663. if (htmContent != null)
  664. {
  665. npcHtmlMessage.setHtml(htmContent);
  666. }
  667. }
  668. else if ((playerLevel < Config.TVT_EVENT_MIN_LVL) || (playerLevel > Config.TVT_EVENT_MAX_LVL))
  669. {
  670. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Level.htm");
  671. if (htmContent != null)
  672. {
  673. npcHtmlMessage.setHtml(htmContent);
  674. npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
  675. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
  676. }
  677. }
  678. else if ((_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS) && (_teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS))
  679. {
  680. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "TeamsFull.htm");
  681. if (htmContent != null)
  682. {
  683. npcHtmlMessage.setHtml(htmContent);
  684. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
  685. }
  686. }
  687. else if ((Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0) && !AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.TVT_ID, playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP))
  688. {
  689. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "IPRestriction.htm");
  690. if (htmContent != null)
  691. {
  692. npcHtmlMessage.setHtml(htmContent);
  693. npcHtmlMessage.replace("%max%", String.valueOf(AntiFeedManager.getInstance().getLimit(playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP)));
  694. }
  695. }
  696. else if (needParticipationFee() && !hasParticipationFee(playerInstance))
  697. {
  698. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "ParticipationFee.htm");
  699. if (htmContent != null)
  700. {
  701. npcHtmlMessage.setHtml(htmContent);
  702. npcHtmlMessage.replace("%fee%", getParticipationFee());
  703. }
  704. }
  705. else if (addParticipant(playerInstance))
  706. {
  707. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Registered.htm"));
  708. }
  709. else
  710. {
  711. return;
  712. }
  713. playerInstance.sendPacket(npcHtmlMessage);
  714. }
  715. else if (command.equals("tvt_event_remove_participation"))
  716. {
  717. removeParticipant(playerInstance.getObjectId());
  718. if (Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0)
  719. {
  720. AntiFeedManager.getInstance().removePlayer(AntiFeedManager.TVT_ID, playerInstance);
  721. }
  722. final NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage();
  723. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Unregistered.htm"));
  724. playerInstance.sendPacket(npcHtmlMessage);
  725. }
  726. }
  727. /**
  728. * Called on every onAction in L2PcIstance<br>
  729. * <br>
  730. * @param playerInstance
  731. * @param targetedPlayerObjectId
  732. * @return boolean: true if player is allowed to target, otherwise false
  733. */
  734. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  735. {
  736. if ((playerInstance == null) || !isStarted())
  737. {
  738. return true;
  739. }
  740. if (playerInstance.isGM())
  741. {
  742. return true;
  743. }
  744. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  745. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  746. if (((playerTeamId != -1) && (targetedPlayerTeamId == -1)) || ((playerTeamId == -1) && (targetedPlayerTeamId != -1)))
  747. {
  748. return false;
  749. }
  750. if ((playerTeamId != -1) && (targetedPlayerTeamId != -1) && (playerTeamId == targetedPlayerTeamId) && (playerInstance.getObjectId() != targetedPlayerObjectId) && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  751. {
  752. return false;
  753. }
  754. return true;
  755. }
  756. /**
  757. * Called on every scroll use<br>
  758. * <br>
  759. * @param playerObjectId
  760. * @return boolean: true if player is allowed to use scroll, otherwise false
  761. */
  762. public static boolean onScrollUse(int playerObjectId)
  763. {
  764. if (!isStarted())
  765. {
  766. return true;
  767. }
  768. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  769. {
  770. return false;
  771. }
  772. return true;
  773. }
  774. /**
  775. * Called on every potion use
  776. * @param playerObjectId
  777. * @return boolean: true if player is allowed to use potions, otherwise false
  778. */
  779. public static boolean onPotionUse(int playerObjectId)
  780. {
  781. if (!isStarted())
  782. {
  783. return true;
  784. }
  785. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  786. {
  787. return false;
  788. }
  789. return true;
  790. }
  791. /**
  792. * Called on every escape use(thanks to nbd)
  793. * @param playerObjectId
  794. * @return boolean: true if player is not in tvt event, otherwise false
  795. */
  796. public static boolean onEscapeUse(int playerObjectId)
  797. {
  798. if (!isStarted())
  799. {
  800. return true;
  801. }
  802. if (isPlayerParticipant(playerObjectId))
  803. {
  804. return false;
  805. }
  806. return true;
  807. }
  808. /**
  809. * Called on every summon item use
  810. * @param playerObjectId
  811. * @return boolean: true if player is allowed to summon by item, otherwise false
  812. */
  813. public static boolean onItemSummon(int playerObjectId)
  814. {
  815. if (!isStarted())
  816. {
  817. return true;
  818. }
  819. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  820. {
  821. return false;
  822. }
  823. return true;
  824. }
  825. /**
  826. * Is called when a player is killed<br>
  827. * <br>
  828. * @param killerCharacter as L2Character<br>
  829. * @param killedPlayerInstance as L2PcInstance<br>
  830. */
  831. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  832. {
  833. if ((killedPlayerInstance == null) || !isStarted())
  834. {
  835. return;
  836. }
  837. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  838. if (killedTeamId == -1)
  839. {
  840. return;
  841. }
  842. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  843. if (killerCharacter == null)
  844. {
  845. return;
  846. }
  847. L2PcInstance killerPlayerInstance = null;
  848. if ((killerCharacter instanceof L2PetInstance) || (killerCharacter instanceof L2ServitorInstance))
  849. {
  850. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  851. if (killerPlayerInstance == null)
  852. {
  853. return;
  854. }
  855. }
  856. else if (killerCharacter instanceof L2PcInstance)
  857. {
  858. killerPlayerInstance = (L2PcInstance) killerCharacter;
  859. }
  860. else
  861. {
  862. return;
  863. }
  864. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  865. if ((killerTeamId != -1) && (killedTeamId != -1) && (killerTeamId != killedTeamId))
  866. {
  867. TvTEventTeam killerTeam = _teams[killerTeamId];
  868. killerTeam.increasePoints();
  869. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  870. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  871. {
  872. if (playerInstance != null)
  873. {
  874. playerInstance.sendPacket(cs);
  875. }
  876. }
  877. fireTvtKillListeners(killerPlayerInstance, killedPlayerInstance, killerTeam);
  878. }
  879. }
  880. /**
  881. * Called on Appearing packet received (player finished teleporting)
  882. * @param playerInstance
  883. */
  884. public static void onTeleported(L2PcInstance playerInstance)
  885. {
  886. if (!isStarted() || (playerInstance == null) || !isPlayerParticipant(playerInstance.getObjectId()))
  887. {
  888. return;
  889. }
  890. if (playerInstance.isMageClass())
  891. {
  892. if ((Config.TVT_EVENT_MAGE_BUFFS != null) && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  893. {
  894. for (Entry<Integer, Integer> e : Config.TVT_EVENT_MAGE_BUFFS.entrySet())
  895. {
  896. Skill skill = SkillData.getInstance().getSkill(e.getKey(), e.getValue());
  897. if (skill != null)
  898. {
  899. skill.applyEffects(playerInstance, playerInstance);
  900. }
  901. }
  902. }
  903. }
  904. else
  905. {
  906. if ((Config.TVT_EVENT_FIGHTER_BUFFS != null) && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  907. {
  908. for (Entry<Integer, Integer> e : Config.TVT_EVENT_FIGHTER_BUFFS.entrySet())
  909. {
  910. Skill skill = SkillData.getInstance().getSkill(e.getKey(), e.getValue());
  911. if (skill != null)
  912. {
  913. skill.applyEffects(playerInstance, playerInstance);
  914. }
  915. }
  916. }
  917. }
  918. }
  919. /**
  920. * @param source
  921. * @param target
  922. * @param skill
  923. * @return true if player valid for skill
  924. */
  925. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, Skill skill)
  926. {
  927. if (!isStarted())
  928. {
  929. return true;
  930. }
  931. // TvT is started
  932. final int sourcePlayerId = source.getObjectId();
  933. final int targetPlayerId = target.getObjectId();
  934. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  935. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  936. // both players not participating
  937. if (!isSourceParticipant && !isTargetParticipant)
  938. {
  939. return true;
  940. }
  941. // one player not participating
  942. if (!(isSourceParticipant && isTargetParticipant))
  943. {
  944. return false;
  945. }
  946. // players in the different teams ?
  947. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  948. {
  949. if (!skill.isBad())
  950. {
  951. return false;
  952. }
  953. }
  954. return true;
  955. }
  956. /**
  957. * Sets the TvTEvent state<br>
  958. * <br>
  959. * @param state as EventState<br>
  960. */
  961. private static void setState(EventState state)
  962. {
  963. synchronized (_state)
  964. {
  965. _state = state;
  966. }
  967. }
  968. /**
  969. * Is TvTEvent inactive?<br>
  970. * <br>
  971. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  972. */
  973. public static boolean isInactive()
  974. {
  975. boolean isInactive;
  976. synchronized (_state)
  977. {
  978. isInactive = _state == EventState.INACTIVE;
  979. }
  980. return isInactive;
  981. }
  982. /**
  983. * Is TvTEvent in inactivating?<br>
  984. * <br>
  985. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  986. */
  987. public static boolean isInactivating()
  988. {
  989. boolean isInactivating;
  990. synchronized (_state)
  991. {
  992. isInactivating = _state == EventState.INACTIVATING;
  993. }
  994. return isInactivating;
  995. }
  996. /**
  997. * Is TvTEvent in participation?<br>
  998. * <br>
  999. * @return boolean: true if event is in participation progress, otherwise false<br>
  1000. */
  1001. public static boolean isParticipating()
  1002. {
  1003. boolean isParticipating;
  1004. synchronized (_state)
  1005. {
  1006. isParticipating = _state == EventState.PARTICIPATING;
  1007. }
  1008. return isParticipating;
  1009. }
  1010. /**
  1011. * Is TvTEvent starting?<br>
  1012. * <br>
  1013. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  1014. */
  1015. public static boolean isStarting()
  1016. {
  1017. boolean isStarting;
  1018. synchronized (_state)
  1019. {
  1020. isStarting = _state == EventState.STARTING;
  1021. }
  1022. return isStarting;
  1023. }
  1024. /**
  1025. * Is TvTEvent started?<br>
  1026. * <br>
  1027. * @return boolean: true if event is started, otherwise false<br>
  1028. */
  1029. public static boolean isStarted()
  1030. {
  1031. boolean isStarted;
  1032. synchronized (_state)
  1033. {
  1034. isStarted = _state == EventState.STARTED;
  1035. }
  1036. return isStarted;
  1037. }
  1038. /**
  1039. * Is TvTEvent rewarding?<br>
  1040. * <br>
  1041. * @return boolean: true if event is currently rewarding, otherwise false<br>
  1042. */
  1043. public static boolean isRewarding()
  1044. {
  1045. boolean isRewarding;
  1046. synchronized (_state)
  1047. {
  1048. isRewarding = _state == EventState.REWARDING;
  1049. }
  1050. return isRewarding;
  1051. }
  1052. /**
  1053. * Returns the team id of a player, if player is not participant it returns -1
  1054. * @param playerObjectId
  1055. * @return byte: team name of the given playerName, if not in event -1
  1056. */
  1057. public static byte getParticipantTeamId(int playerObjectId)
  1058. {
  1059. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  1060. }
  1061. /**
  1062. * Returns the team of a player, if player is not participant it returns null
  1063. * @param playerObjectId
  1064. * @return TvTEventTeam: team of the given playerObjectId, if not in event null
  1065. */
  1066. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  1067. {
  1068. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  1069. }
  1070. /**
  1071. * Returns the enemy team of a player, if player is not participant it returns null
  1072. * @param playerObjectId
  1073. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null
  1074. */
  1075. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  1076. {
  1077. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  1078. }
  1079. /**
  1080. * Returns the team coordinates in which the player is in, if player is not in a team return null
  1081. * @param playerObjectId
  1082. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2
  1083. */
  1084. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  1085. {
  1086. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  1087. }
  1088. /**
  1089. * Is given player participant of the event?
  1090. * @param playerObjectId
  1091. * @return boolean: true if player is participant, ohterwise false
  1092. */
  1093. public static boolean isPlayerParticipant(int playerObjectId)
  1094. {
  1095. if (!isParticipating() && !isStarting() && !isStarted())
  1096. {
  1097. return false;
  1098. }
  1099. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  1100. }
  1101. /**
  1102. * Returns participated player count<br>
  1103. * <br>
  1104. * @return int: amount of players registered in the event<br>
  1105. */
  1106. public static int getParticipatedPlayersCount()
  1107. {
  1108. if (!isParticipating() && !isStarting() && !isStarted())
  1109. {
  1110. return 0;
  1111. }
  1112. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1113. }
  1114. /**
  1115. * Returns teams names<br>
  1116. * <br>
  1117. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1118. */
  1119. public static String[] getTeamNames()
  1120. {
  1121. return new String[]
  1122. {
  1123. _teams[0].getName(),
  1124. _teams[1].getName()
  1125. };
  1126. }
  1127. /**
  1128. * Returns player count of both teams<br>
  1129. * <br>
  1130. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1131. */
  1132. public static int[] getTeamsPlayerCounts()
  1133. {
  1134. return new int[]
  1135. {
  1136. _teams[0].getParticipatedPlayerCount(),
  1137. _teams[1].getParticipatedPlayerCount()
  1138. };
  1139. }
  1140. /**
  1141. * Returns points count of both teams
  1142. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1143. */
  1144. public static int[] getTeamsPoints()
  1145. {
  1146. return new int[]
  1147. {
  1148. _teams[0].getPoints(),
  1149. _teams[1].getPoints()
  1150. };
  1151. }
  1152. public static int getTvTEventInstance()
  1153. {
  1154. return _TvTEventInstance;
  1155. }
  1156. // Listeners
  1157. /**
  1158. * Fires all the TvTListener.onKill() methods, if any
  1159. * @param killer
  1160. * @param victim
  1161. * @param killerTeam
  1162. */
  1163. private static void fireTvtKillListeners(L2PcInstance killer, L2PcInstance victim, TvTEventTeam killerTeam)
  1164. {
  1165. if (!tvtListeners.isEmpty() && (killer != null) && (victim != null) && (killerTeam != null))
  1166. {
  1167. TvtKillEvent event = new TvtKillEvent();
  1168. event.setKiller(killer);
  1169. event.setVictim(victim);
  1170. event.setKillerTeam(killerTeam);
  1171. for (TvTListener listener : tvtListeners)
  1172. {
  1173. listener.onKill(event);
  1174. }
  1175. }
  1176. }
  1177. /**
  1178. * Fires the appropriate TvtEventListeners, if any
  1179. * @param stage
  1180. */
  1181. private static void fireTvtEventListeners(EventStage stage)
  1182. {
  1183. if (!tvtListeners.isEmpty())
  1184. {
  1185. switch (stage)
  1186. {
  1187. case REGISTRATION_BEGIN:
  1188. {
  1189. for (TvTListener listener : tvtListeners)
  1190. {
  1191. listener.onRegistrationStart();
  1192. }
  1193. break;
  1194. }
  1195. case START:
  1196. {
  1197. for (TvTListener listener : tvtListeners)
  1198. {
  1199. listener.onBegin();
  1200. }
  1201. break;
  1202. }
  1203. case END:
  1204. {
  1205. for (TvTListener listener : tvtListeners)
  1206. {
  1207. listener.onEnd();
  1208. }
  1209. break;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. /**
  1215. * Adds a TvT listener
  1216. * @param listener
  1217. */
  1218. public static void addTvTListener(TvTListener listener)
  1219. {
  1220. if (!tvtListeners.contains(listener))
  1221. {
  1222. tvtListeners.add(listener);
  1223. }
  1224. }
  1225. /**
  1226. * Removes a TvT listener
  1227. * @param listener
  1228. */
  1229. public static void removeTvtListener(TvTListener listener)
  1230. {
  1231. tvtListeners.remove(listener);
  1232. }
  1233. }