TvTEvent.java 38 KB

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