TvTEvent.java 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /*
  2. * Copyright (C) 2004-2013 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.NpcTable;
  33. import com.l2jserver.gameserver.datatables.SkillTable;
  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.L2Skill;
  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 = NpcTable.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. // XXX: 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. // XXX: 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.addItemNumber(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. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  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. /**
  533. * Close doors specified in configs
  534. * @param doors
  535. */
  536. private static void closeDoors(List<Integer> doors)
  537. {
  538. for (int doorId : doors)
  539. {
  540. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  541. if (doorInstance != null)
  542. {
  543. doorInstance.closeMe();
  544. }
  545. }
  546. }
  547. /**
  548. * Open doors specified in configs
  549. * @param doors
  550. */
  551. private static void openDoors(List<Integer> doors)
  552. {
  553. for (int doorId : doors)
  554. {
  555. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  556. if (doorInstance != null)
  557. {
  558. doorInstance.openMe();
  559. }
  560. }
  561. }
  562. /**
  563. * UnSpawns the TvTEvent npc
  564. */
  565. private static void unSpawnNpc()
  566. {
  567. // Delete the npc
  568. _lastNpcSpawn.deleteMe();
  569. SpawnTable.getInstance().deleteSpawn(_lastNpcSpawn.getSpawn(), false);
  570. // Stop respawning of the npc
  571. _npcSpawn.stopRespawn();
  572. _npcSpawn = null;
  573. _lastNpcSpawn = null;
  574. }
  575. /**
  576. * Called when a player logs in<br>
  577. * <br>
  578. * @param playerInstance as L2PcInstance<br>
  579. */
  580. public static void onLogin(L2PcInstance playerInstance)
  581. {
  582. if ((playerInstance == null) || (!isStarting() && !isStarted()))
  583. {
  584. return;
  585. }
  586. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  587. if (teamId == -1)
  588. {
  589. return;
  590. }
  591. _teams[teamId].addPlayer(playerInstance);
  592. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  593. }
  594. /**
  595. * Called when a player logs out<br>
  596. * <br>
  597. * @param playerInstance as L2PcInstance<br>
  598. */
  599. public static void onLogout(L2PcInstance playerInstance)
  600. {
  601. if ((playerInstance != null) && (isStarting() || isStarted() || isParticipating()))
  602. {
  603. if (removeParticipant(playerInstance.getObjectId()))
  604. {
  605. 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]);
  606. }
  607. }
  608. }
  609. /**
  610. * Called on every bypass by npc of type L2TvTEventNpc<br>
  611. * Needs synchronization cause of the max player check<br>
  612. * <br>
  613. * @param command as String<br>
  614. * @param playerInstance as L2PcInstance<br>
  615. */
  616. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  617. {
  618. if ((playerInstance == null) || !isParticipating())
  619. {
  620. return;
  621. }
  622. final String htmContent;
  623. if (command.equals("tvt_event_participation"))
  624. {
  625. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  626. int playerLevel = playerInstance.getLevel();
  627. if (playerInstance.isCursedWeaponEquipped())
  628. {
  629. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "CursedWeaponEquipped.htm");
  630. if (htmContent != null)
  631. {
  632. npcHtmlMessage.setHtml(htmContent);
  633. }
  634. }
  635. else if (OlympiadManager.getInstance().isRegistered(playerInstance))
  636. {
  637. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Olympiad.htm");
  638. if (htmContent != null)
  639. {
  640. npcHtmlMessage.setHtml(htmContent);
  641. }
  642. }
  643. else if (playerInstance.getKarma() > 0)
  644. {
  645. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Karma.htm");
  646. if (htmContent != null)
  647. {
  648. npcHtmlMessage.setHtml(htmContent);
  649. }
  650. }
  651. else if ((playerLevel < Config.TVT_EVENT_MIN_LVL) || (playerLevel > Config.TVT_EVENT_MAX_LVL))
  652. {
  653. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Level.htm");
  654. if (htmContent != null)
  655. {
  656. npcHtmlMessage.setHtml(htmContent);
  657. npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
  658. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
  659. }
  660. }
  661. else if ((_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS) && (_teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS))
  662. {
  663. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "TeamsFull.htm");
  664. if (htmContent != null)
  665. {
  666. npcHtmlMessage.setHtml(htmContent);
  667. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
  668. }
  669. }
  670. else if ((Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0) && !AntiFeedManager.getInstance().tryAddPlayer(AntiFeedManager.TVT_ID, playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP))
  671. {
  672. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "IPRestriction.htm");
  673. if (htmContent != null)
  674. {
  675. npcHtmlMessage.setHtml(htmContent);
  676. npcHtmlMessage.replace("%max%", String.valueOf(AntiFeedManager.getInstance().getLimit(playerInstance, Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP)));
  677. }
  678. }
  679. else if (needParticipationFee() && !hasParticipationFee(playerInstance))
  680. {
  681. htmContent = HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "ParticipationFee.htm");
  682. if (htmContent != null)
  683. {
  684. npcHtmlMessage.setHtml(htmContent);
  685. npcHtmlMessage.replace("%fee%", getParticipationFee());
  686. }
  687. }
  688. else if (addParticipant(playerInstance))
  689. {
  690. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Registered.htm"));
  691. }
  692. else
  693. {
  694. return;
  695. }
  696. playerInstance.sendPacket(npcHtmlMessage);
  697. }
  698. else if (command.equals("tvt_event_remove_participation"))
  699. {
  700. removeParticipant(playerInstance.getObjectId());
  701. if (Config.TVT_EVENT_MAX_PARTICIPANTS_PER_IP > 0)
  702. {
  703. AntiFeedManager.getInstance().removePlayer(AntiFeedManager.TVT_ID, playerInstance);
  704. }
  705. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  706. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(playerInstance.getHtmlPrefix(), htmlPath + "Unregistered.htm"));
  707. playerInstance.sendPacket(npcHtmlMessage);
  708. }
  709. }
  710. /**
  711. * Called on every onAction in L2PcIstance<br>
  712. * <br>
  713. * @param playerInstance
  714. * @param targetedPlayerObjectId
  715. * @return boolean: true if player is allowed to target, otherwise false
  716. */
  717. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  718. {
  719. if ((playerInstance == null) || !isStarted())
  720. {
  721. return true;
  722. }
  723. if (playerInstance.isGM())
  724. {
  725. return true;
  726. }
  727. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  728. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  729. if (((playerTeamId != -1) && (targetedPlayerTeamId == -1)) || ((playerTeamId == -1) && (targetedPlayerTeamId != -1)))
  730. {
  731. return false;
  732. }
  733. if ((playerTeamId != -1) && (targetedPlayerTeamId != -1) && (playerTeamId == targetedPlayerTeamId) && (playerInstance.getObjectId() != targetedPlayerObjectId) && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  734. {
  735. return false;
  736. }
  737. return true;
  738. }
  739. /**
  740. * Called on every scroll use<br>
  741. * <br>
  742. * @param playerObjectId
  743. * @return boolean: true if player is allowed to use scroll, otherwise false
  744. */
  745. public static boolean onScrollUse(int playerObjectId)
  746. {
  747. if (!isStarted())
  748. {
  749. return true;
  750. }
  751. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  752. {
  753. return false;
  754. }
  755. return true;
  756. }
  757. /**
  758. * Called on every potion use
  759. * @param playerObjectId
  760. * @return boolean: true if player is allowed to use potions, otherwise false
  761. */
  762. public static boolean onPotionUse(int playerObjectId)
  763. {
  764. if (!isStarted())
  765. {
  766. return true;
  767. }
  768. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  769. {
  770. return false;
  771. }
  772. return true;
  773. }
  774. /**
  775. * Called on every escape use(thanks to nbd)
  776. * @param playerObjectId
  777. * @return boolean: true if player is not in tvt event, otherwise false
  778. */
  779. public static boolean onEscapeUse(int playerObjectId)
  780. {
  781. if (!isStarted())
  782. {
  783. return true;
  784. }
  785. if (isPlayerParticipant(playerObjectId))
  786. {
  787. return false;
  788. }
  789. return true;
  790. }
  791. /**
  792. * Called on every summon item use
  793. * @param playerObjectId
  794. * @return boolean: true if player is allowed to summon by item, otherwise false
  795. */
  796. public static boolean onItemSummon(int playerObjectId)
  797. {
  798. if (!isStarted())
  799. {
  800. return true;
  801. }
  802. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  803. {
  804. return false;
  805. }
  806. return true;
  807. }
  808. /**
  809. * Is called when a player is killed<br>
  810. * <br>
  811. * @param killerCharacter as L2Character<br>
  812. * @param killedPlayerInstance as L2PcInstance<br>
  813. */
  814. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  815. {
  816. if ((killedPlayerInstance == null) || !isStarted())
  817. {
  818. return;
  819. }
  820. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  821. if (killedTeamId == -1)
  822. {
  823. return;
  824. }
  825. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  826. if (killerCharacter == null)
  827. {
  828. return;
  829. }
  830. L2PcInstance killerPlayerInstance = null;
  831. if ((killerCharacter instanceof L2PetInstance) || (killerCharacter instanceof L2ServitorInstance))
  832. {
  833. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  834. if (killerPlayerInstance == null)
  835. {
  836. return;
  837. }
  838. }
  839. else if (killerCharacter instanceof L2PcInstance)
  840. {
  841. killerPlayerInstance = (L2PcInstance) killerCharacter;
  842. }
  843. else
  844. {
  845. return;
  846. }
  847. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  848. if ((killerTeamId != -1) && (killedTeamId != -1) && (killerTeamId != killedTeamId))
  849. {
  850. TvTEventTeam killerTeam = _teams[killerTeamId];
  851. killerTeam.increasePoints();
  852. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  853. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  854. {
  855. if (playerInstance != null)
  856. {
  857. playerInstance.sendPacket(cs);
  858. }
  859. }
  860. fireTvtKillListeners(killerPlayerInstance, killedPlayerInstance, killerTeam);
  861. }
  862. }
  863. /**
  864. * Called on Appearing packet received (player finished teleporting)
  865. * @param playerInstance
  866. */
  867. public static void onTeleported(L2PcInstance playerInstance)
  868. {
  869. if (!isStarted() || (playerInstance == null) || !isPlayerParticipant(playerInstance.getObjectId()))
  870. {
  871. return;
  872. }
  873. if (playerInstance.isMageClass())
  874. {
  875. if ((Config.TVT_EVENT_MAGE_BUFFS != null) && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  876. {
  877. for (Entry<Integer, Integer> e : Config.TVT_EVENT_MAGE_BUFFS.entrySet())
  878. {
  879. L2Skill skill = SkillTable.getInstance().getInfo(e.getKey(), e.getValue());
  880. if (skill != null)
  881. {
  882. skill.getEffects(playerInstance, playerInstance);
  883. }
  884. }
  885. }
  886. }
  887. else
  888. {
  889. if ((Config.TVT_EVENT_FIGHTER_BUFFS != null) && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  890. {
  891. for (Entry<Integer, Integer> e : Config.TVT_EVENT_FIGHTER_BUFFS.entrySet())
  892. {
  893. L2Skill skill = SkillTable.getInstance().getInfo(e.getKey(), e.getValue());
  894. if (skill != null)
  895. {
  896. skill.getEffects(playerInstance, playerInstance);
  897. }
  898. }
  899. }
  900. }
  901. }
  902. /**
  903. * @param source
  904. * @param target
  905. * @param skill
  906. * @return true if player valid for skill
  907. */
  908. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
  909. {
  910. if (!isStarted())
  911. {
  912. return true;
  913. }
  914. // TvT is started
  915. final int sourcePlayerId = source.getObjectId();
  916. final int targetPlayerId = target.getObjectId();
  917. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  918. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  919. // both players not participating
  920. if (!isSourceParticipant && !isTargetParticipant)
  921. {
  922. return true;
  923. }
  924. // one player not participating
  925. if (!(isSourceParticipant && isTargetParticipant))
  926. {
  927. return false;
  928. }
  929. // players in the different teams ?
  930. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  931. {
  932. if (!skill.isBad())
  933. {
  934. return false;
  935. }
  936. }
  937. return true;
  938. }
  939. /**
  940. * Sets the TvTEvent state<br>
  941. * <br>
  942. * @param state as EventState<br>
  943. */
  944. private static void setState(EventState state)
  945. {
  946. synchronized (_state)
  947. {
  948. _state = state;
  949. }
  950. }
  951. /**
  952. * Is TvTEvent inactive?<br>
  953. * <br>
  954. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  955. */
  956. public static boolean isInactive()
  957. {
  958. boolean isInactive;
  959. synchronized (_state)
  960. {
  961. isInactive = _state == EventState.INACTIVE;
  962. }
  963. return isInactive;
  964. }
  965. /**
  966. * Is TvTEvent in inactivating?<br>
  967. * <br>
  968. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  969. */
  970. public static boolean isInactivating()
  971. {
  972. boolean isInactivating;
  973. synchronized (_state)
  974. {
  975. isInactivating = _state == EventState.INACTIVATING;
  976. }
  977. return isInactivating;
  978. }
  979. /**
  980. * Is TvTEvent in participation?<br>
  981. * <br>
  982. * @return boolean: true if event is in participation progress, otherwise false<br>
  983. */
  984. public static boolean isParticipating()
  985. {
  986. boolean isParticipating;
  987. synchronized (_state)
  988. {
  989. isParticipating = _state == EventState.PARTICIPATING;
  990. }
  991. return isParticipating;
  992. }
  993. /**
  994. * Is TvTEvent starting?<br>
  995. * <br>
  996. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  997. */
  998. public static boolean isStarting()
  999. {
  1000. boolean isStarting;
  1001. synchronized (_state)
  1002. {
  1003. isStarting = _state == EventState.STARTING;
  1004. }
  1005. return isStarting;
  1006. }
  1007. /**
  1008. * Is TvTEvent started?<br>
  1009. * <br>
  1010. * @return boolean: true if event is started, otherwise false<br>
  1011. */
  1012. public static boolean isStarted()
  1013. {
  1014. boolean isStarted;
  1015. synchronized (_state)
  1016. {
  1017. isStarted = _state == EventState.STARTED;
  1018. }
  1019. return isStarted;
  1020. }
  1021. /**
  1022. * Is TvTEvent rewarding?<br>
  1023. * <br>
  1024. * @return boolean: true if event is currently rewarding, otherwise false<br>
  1025. */
  1026. public static boolean isRewarding()
  1027. {
  1028. boolean isRewarding;
  1029. synchronized (_state)
  1030. {
  1031. isRewarding = _state == EventState.REWARDING;
  1032. }
  1033. return isRewarding;
  1034. }
  1035. /**
  1036. * Returns the team id of a player, if player is not participant it returns -1
  1037. * @param playerObjectId
  1038. * @return byte: team name of the given playerName, if not in event -1
  1039. */
  1040. public static byte getParticipantTeamId(int playerObjectId)
  1041. {
  1042. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  1043. }
  1044. /**
  1045. * Returns the team of a player, if player is not participant it returns null
  1046. * @param playerObjectId
  1047. * @return TvTEventTeam: team of the given playerObjectId, if not in event null
  1048. */
  1049. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  1050. {
  1051. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  1052. }
  1053. /**
  1054. * Returns the enemy team of a player, if player is not participant it returns null
  1055. * @param playerObjectId
  1056. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null
  1057. */
  1058. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  1059. {
  1060. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  1061. }
  1062. /**
  1063. * Returns the team coordinates in which the player is in, if player is not in a team return null
  1064. * @param playerObjectId
  1065. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2
  1066. */
  1067. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  1068. {
  1069. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  1070. }
  1071. /**
  1072. * Is given player participant of the event?
  1073. * @param playerObjectId
  1074. * @return boolean: true if player is participant, ohterwise false
  1075. */
  1076. public static boolean isPlayerParticipant(int playerObjectId)
  1077. {
  1078. if (!isParticipating() && !isStarting() && !isStarted())
  1079. {
  1080. return false;
  1081. }
  1082. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  1083. }
  1084. /**
  1085. * Returns participated player count<br>
  1086. * <br>
  1087. * @return int: amount of players registered in the event<br>
  1088. */
  1089. public static int getParticipatedPlayersCount()
  1090. {
  1091. if (!isParticipating() && !isStarting() && !isStarted())
  1092. {
  1093. return 0;
  1094. }
  1095. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1096. }
  1097. /**
  1098. * Returns teams names<br>
  1099. * <br>
  1100. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1101. */
  1102. public static String[] getTeamNames()
  1103. {
  1104. return new String[]
  1105. {
  1106. _teams[0].getName(),
  1107. _teams[1].getName()
  1108. };
  1109. }
  1110. /**
  1111. * Returns player count of both teams<br>
  1112. * <br>
  1113. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1114. */
  1115. public static int[] getTeamsPlayerCounts()
  1116. {
  1117. return new int[]
  1118. {
  1119. _teams[0].getParticipatedPlayerCount(),
  1120. _teams[1].getParticipatedPlayerCount()
  1121. };
  1122. }
  1123. /**
  1124. * Returns points count of both teams
  1125. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1126. */
  1127. public static int[] getTeamsPoints()
  1128. {
  1129. return new int[]
  1130. {
  1131. _teams[0].getPoints(),
  1132. _teams[1].getPoints()
  1133. };
  1134. }
  1135. public static int getTvTEventInstance()
  1136. {
  1137. return _TvTEventInstance;
  1138. }
  1139. // Listeners
  1140. /**
  1141. * Fires all the TvTListener.onKill() methods, if any
  1142. * @param killer
  1143. * @param victim
  1144. * @param killerTeam
  1145. */
  1146. private static void fireTvtKillListeners(L2PcInstance killer, L2PcInstance victim, TvTEventTeam killerTeam)
  1147. {
  1148. if (!tvtListeners.isEmpty() && (killer != null) && (victim != null) && (killerTeam != null))
  1149. {
  1150. TvtKillEvent event = new TvtKillEvent();
  1151. event.setKiller(killer);
  1152. event.setVictim(victim);
  1153. event.setKillerTeam(killerTeam);
  1154. for (TvTListener listener : tvtListeners)
  1155. {
  1156. listener.onKill(event);
  1157. }
  1158. }
  1159. }
  1160. /**
  1161. * Fires the appropriate TvtEventListeners, if any
  1162. * @param stage
  1163. */
  1164. private static void fireTvtEventListeners(EventStage stage)
  1165. {
  1166. if (!tvtListeners.isEmpty())
  1167. {
  1168. switch (stage)
  1169. {
  1170. case REGISTRATION_BEGIN:
  1171. {
  1172. for (TvTListener listener : tvtListeners)
  1173. {
  1174. listener.onRegistrationStart();
  1175. }
  1176. break;
  1177. }
  1178. case START:
  1179. {
  1180. for (TvTListener listener : tvtListeners)
  1181. {
  1182. listener.onBegin();
  1183. }
  1184. break;
  1185. }
  1186. case END:
  1187. {
  1188. for (TvTListener listener : tvtListeners)
  1189. {
  1190. listener.onEnd();
  1191. }
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * Adds a TvT listener
  1199. * @param listener
  1200. */
  1201. public static void addTvTListener(TvTListener listener)
  1202. {
  1203. if (!tvtListeners.contains(listener))
  1204. {
  1205. tvtListeners.add(listener);
  1206. }
  1207. }
  1208. /**
  1209. * Removes a TvT listener
  1210. * @param listener
  1211. */
  1212. public static void removeTvtListener(TvTListener listener)
  1213. {
  1214. tvtListeners.remove(listener);
  1215. }
  1216. }