TvTEvent.java 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.model.entity;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.datatables.DoorTable;
  19. import net.sf.l2j.gameserver.datatables.ItemTable;
  20. import net.sf.l2j.gameserver.datatables.NpcTable;
  21. import net.sf.l2j.gameserver.datatables.SpawnTable;
  22. import net.sf.l2j.gameserver.model.L2Character;
  23. import net.sf.l2j.gameserver.model.L2Spawn;
  24. import net.sf.l2j.gameserver.model.L2Summon;
  25. import net.sf.l2j.gameserver.model.PcInventory;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  30. import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  31. import net.sf.l2j.gameserver.network.SystemMessageId;
  32. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  33. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  34. import net.sf.l2j.gameserver.network.serverpackets.MagicSkillUse;
  35. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  36. import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
  37. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  38. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  39. import net.sf.l2j.util.Rnd;
  40. /**
  41. * @author FBIagent
  42. */
  43. public class TvTEvent
  44. {
  45. enum EventState
  46. {
  47. INACTIVE,
  48. INACTIVATING,
  49. PARTICIPATING,
  50. STARTING,
  51. STARTED,
  52. REWARDING
  53. }
  54. protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
  55. /** The teams of the TvTEvent<br> */
  56. private static TvTEventTeam[] _teams = new TvTEventTeam[2];
  57. /** The state of the TvTEvent<br> */
  58. private static EventState _state = EventState.INACTIVE;
  59. /** The spawn of the participation npc<br> */
  60. private static L2Spawn _npcSpawn = null;
  61. /** the npc instance of the participation npc<br> */
  62. private static L2NpcInstance _lastNpcSpawn = null;
  63. /**
  64. * No instance of this class!<br>
  65. */
  66. private TvTEvent()
  67. {
  68. }
  69. /**
  70. * Teams initializing<br>
  71. */
  72. public static void init()
  73. {
  74. _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
  75. _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
  76. }
  77. /**
  78. * Starts the participation of the TvTEvent<br>
  79. * 1. Get L2NpcTemplate by Config.TVT_EVENT_PARTICIPATION_NPC_ID<br>
  80. * 2. Try to spawn a new npc of it<br><br>
  81. *
  82. * @return boolean: true if success, otherwise false<br>
  83. */
  84. public static boolean startParticipation()
  85. {
  86. L2NpcTemplate tmpl = NpcTable.getInstance().getTemplate(Config.TVT_EVENT_PARTICIPATION_NPC_ID);
  87. if (tmpl == null)
  88. {
  89. _log.warning("TvTEventEngine[TvTEvent.startParticipation()]: L2NpcTemplate is a NullPointer -> Invalid npc id in configs?");
  90. return false;
  91. }
  92. try
  93. {
  94. _npcSpawn = new L2Spawn(tmpl);
  95. _npcSpawn.setLocx(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0]);
  96. _npcSpawn.setLocy(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1]);
  97. _npcSpawn.setLocz(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  98. _npcSpawn.setAmount(1);
  99. _npcSpawn.setHeading(0);
  100. _npcSpawn.setRespawnDelay(1);
  101. // later no need to delete spawn from db, we don't store it (false)
  102. SpawnTable.getInstance().addNewSpawn(_npcSpawn, false);
  103. _npcSpawn.init();
  104. _lastNpcSpawn = _npcSpawn.getLastSpawn();
  105. _lastNpcSpawn.setCurrentHp(_lastNpcSpawn.getMaxHp());
  106. _lastNpcSpawn.setTitle("TvT Event Participation");
  107. _lastNpcSpawn.isAggressive();
  108. _lastNpcSpawn.decayMe();
  109. _lastNpcSpawn.spawnMe(_npcSpawn.getLastSpawn().getX(), _npcSpawn.getLastSpawn().getY(), _npcSpawn.getLastSpawn().getZ());
  110. _lastNpcSpawn.broadcastPacket(new MagicSkillUse(_lastNpcSpawn, _lastNpcSpawn, 1034, 1, 1, 1));
  111. }
  112. catch (Exception e)
  113. {
  114. _log.warning("TvTEventEngine[TvTEvent.startParticipation()]: exception: " + e);
  115. return false;
  116. }
  117. setState(EventState.PARTICIPATING);
  118. return true;
  119. }
  120. /**
  121. * Starts the TvTEvent fight<br>
  122. * 1. Set state EventState.STARTING<br>
  123. * 2. Close doors specified in configs<br>
  124. * 3. Abort if not enought participants(return false)<br>
  125. * 4. Set state EventState.STARTED<br>
  126. * 5. Teleport all participants to team spot<br><br>
  127. *
  128. * @return boolean: true if success, otherwise false<br>
  129. */
  130. public static boolean startFight()
  131. {
  132. // Set state to STARTING
  133. setState(EventState.STARTING);
  134. // Check for enought participants
  135. if (_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS || _teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS)
  136. {
  137. // Set state INACTIVE
  138. setState(EventState.INACTIVE);
  139. // Cleanup of teams
  140. _teams[0].cleanMe();
  141. _teams[1].cleanMe();
  142. // Unspawn the event NPC
  143. unSpawnNpc();
  144. return false;
  145. }
  146. // Opens all doors specified in configs for tvt
  147. openDoors();
  148. // Closes all doors specified in configs for tvt
  149. closeDoors();
  150. // Set state STARTED
  151. setState(EventState.STARTED);
  152. // Iterate over all teams
  153. for (TvTEventTeam team : _teams)
  154. {
  155. // Iterate over all participated player instances in this team
  156. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  157. {
  158. if (playerInstance != null)
  159. {
  160. // Teleporter implements Runnable and starts itself
  161. new TvTEventTeleporter(playerInstance, team.getCoordinates(), false, false);
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * Calculates the TvTEvent reward<br>
  169. * 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>
  170. * 2. Wait till teams are not at a tie anymore<br>
  171. * 3. Set state EvcentState.REWARDING<br>
  172. * 4. Reward team with more points<br>
  173. * 5. Show win html to wining team participants<br><br>
  174. *
  175. * @return String: winning team name<br>
  176. */
  177. public static String calculateRewards()
  178. {
  179. if (_teams[0].getPoints() == _teams[1].getPoints())
  180. {
  181. // Check if one of the teams have no more players left
  182. if (_teams[0].getParticipatedPlayerCount() == 0 || _teams[1].getParticipatedPlayerCount() == 0)
  183. {
  184. // set state to rewarding
  185. setState(EventState.REWARDING);
  186. // return here, the fight can't be completed
  187. return "TvT Event: Event has ended. No team won due to inactivity!";
  188. }
  189. // Both teams have equals points
  190. sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
  191. if (Config.TVT_REWARD_TEAM_TIE)
  192. {
  193. rewardTeamOne();
  194. rewardTeamTwo();
  195. return "TvT Event: Event has ended with both teams tying.";
  196. }
  197. else
  198. return "TvT Event: Event has ended with both teams tying.";
  199. }
  200. // Set state REWARDING so nobody can point anymore
  201. setState(EventState.REWARDING);
  202. // Get team which has more points
  203. TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
  204. if (team == _teams[0])
  205. rewardTeamOne();
  206. else
  207. rewardTeamTwo();
  208. return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
  209. }
  210. private static void rewardTeamOne()
  211. {
  212. TvTEventTeam team = _teams[0];
  213. // Iterate over all participated player instances of the winning team
  214. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  215. {
  216. // Check for nullpointer
  217. if (playerInstance == null)
  218. {
  219. continue;
  220. }
  221. SystemMessage systemMessage = null;
  222. // Iterate over all tvt event rewards
  223. for (int[] reward : Config.TVT_EVENT_REWARDS)
  224. {
  225. PcInventory inv = playerInstance.getInventory();
  226. // Check for stackable item, non stackabe items need to be added one by one
  227. if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  228. {
  229. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  230. if (reward[1] > 1)
  231. {
  232. systemMessage = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  233. systemMessage.addItemName(reward[0]);
  234. systemMessage.addNumber(reward[1]);
  235. }
  236. else
  237. {
  238. systemMessage = new SystemMessage(SystemMessageId.EARNED_ITEM);
  239. systemMessage.addItemName(reward[0]);
  240. }
  241. playerInstance.sendPacket(systemMessage);
  242. }
  243. else
  244. {
  245. for (int i = 0; i < reward[1]; ++i)
  246. {
  247. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  248. systemMessage = new SystemMessage(SystemMessageId.EARNED_ITEM);
  249. systemMessage.addItemName(reward[0]);
  250. playerInstance.sendPacket(systemMessage);
  251. }
  252. }
  253. }
  254. StatusUpdate statusUpdate = new StatusUpdate(playerInstance.getObjectId());
  255. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  256. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  257. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>Your team won the event. Look in your inventory, there should be your reward.</body></html>");
  258. playerInstance.sendPacket(statusUpdate);
  259. playerInstance.sendPacket(npcHtmlMessage);
  260. }
  261. }
  262. private static void rewardTeamTwo()
  263. {
  264. TvTEventTeam team = _teams[1];
  265. // Iterate over all participated player instances of the winning team
  266. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  267. {
  268. // Check for nullpointer
  269. if (playerInstance == null)
  270. {
  271. continue;
  272. }
  273. SystemMessage systemMessage = null;
  274. // Iterate over all tvt event rewards
  275. for (int[] reward : Config.TVT_EVENT_REWARDS)
  276. {
  277. PcInventory inv = playerInstance.getInventory();
  278. // Check for stackable item, non stackabe items need to be added one by one
  279. if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  280. {
  281. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  282. if (reward[1] > 1)
  283. {
  284. systemMessage = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  285. systemMessage.addItemName(reward[0]);
  286. systemMessage.addNumber(reward[1]);
  287. }
  288. else
  289. {
  290. systemMessage = new SystemMessage(SystemMessageId.EARNED_ITEM);
  291. systemMessage.addItemName(reward[0]);
  292. }
  293. playerInstance.sendPacket(systemMessage);
  294. }
  295. else
  296. {
  297. for (int i = 0; i < reward[1]; ++i)
  298. {
  299. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  300. systemMessage = new SystemMessage(SystemMessageId.EARNED_ITEM);
  301. systemMessage.addItemName(reward[0]);
  302. playerInstance.sendPacket(systemMessage);
  303. }
  304. }
  305. }
  306. StatusUpdate statusUpdate = new StatusUpdate(playerInstance.getObjectId());
  307. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  308. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  309. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>Your team won the event. Look in your inventory, there should be your reward.</body></html>");
  310. playerInstance.sendPacket(statusUpdate);
  311. playerInstance.sendPacket(npcHtmlMessage);
  312. }
  313. }
  314. /**
  315. * Stops the TvTEvent fight<br>
  316. * 1. Set state EventState.INACTIVATING<br>
  317. * 2. Remove tvt npc from world<br>
  318. * 3. Open doors specified in configs<br>
  319. * 4. Teleport all participants back to participation npc location<br>
  320. * 5. Teams cleaning<br>
  321. * 6. Set state EventState.INACTIVE<br>
  322. */
  323. public static void stopFight()
  324. {
  325. // Set state INACTIVATING
  326. setState(EventState.INACTIVATING);
  327. //Unspawn event npc
  328. unSpawnNpc();
  329. // Opens all doors specified in configs for tvt
  330. openDoors();
  331. // Closes all doors specified in Configs for tvt
  332. closeDoors();
  333. // Iterate over all teams
  334. for (TvTEventTeam team : _teams)
  335. {
  336. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  337. {
  338. // Check for nullpointer
  339. if (playerInstance != null)
  340. {
  341. new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false, false);
  342. }
  343. }
  344. }
  345. // Cleanup of teams
  346. _teams[0].cleanMe();
  347. _teams[1].cleanMe();
  348. // Set state INACTIVE
  349. setState(EventState.INACTIVE);
  350. }
  351. /**
  352. * Adds a player to a TvTEvent team<br>
  353. * 1. Calculate the id of the team in which the player should be added<br>
  354. * 2. Add the player to the calculated team<br><br>
  355. *
  356. * @param playerInstance as L2PcInstance<br>
  357. * @return boolean: true if success, otherwise false<br>
  358. */
  359. public static synchronized boolean addParticipant(L2PcInstance playerInstance)
  360. {
  361. // Check for nullpoitner
  362. if (playerInstance == null)
  363. {
  364. return false;
  365. }
  366. byte teamId = 0;
  367. // Check to which team the player should be added
  368. if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
  369. {
  370. teamId = (byte) (Rnd.get(2));
  371. }
  372. else
  373. {
  374. teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
  375. }
  376. return _teams[teamId].addPlayer(playerInstance);
  377. }
  378. /**
  379. * Removes a TvTEvent player from it's team<br>
  380. * 1. Get team id of the player<br>
  381. * 2. Remove player from it's team<br><br>
  382. *
  383. * @param playerName as String<br>
  384. * @return boolean: true if success, otherwise false<br>
  385. */
  386. public static boolean removeParticipant(int playerObjectId)
  387. {
  388. // Get the teamId of the player
  389. byte teamId = getParticipantTeamId(playerObjectId);
  390. // Check if the player is participant
  391. if (teamId != -1)
  392. {
  393. // Remove the player from team
  394. _teams[teamId].removePlayer(playerObjectId);
  395. return true;
  396. }
  397. return false;
  398. }
  399. /**
  400. * Send a SystemMessage to all participated players<br>
  401. * 1. Send the message to all players of team number one<br>
  402. * 2. Send the message to all players of team number two<br><br>
  403. *
  404. * @param message as String<br>
  405. */
  406. public static void sysMsgToAllParticipants(String message)
  407. {
  408. for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
  409. {
  410. if (playerInstance != null)
  411. {
  412. playerInstance.sendMessage(message);
  413. }
  414. }
  415. for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
  416. {
  417. if (playerInstance != null)
  418. {
  419. playerInstance.sendMessage(message);
  420. }
  421. }
  422. }
  423. /**
  424. * Close doors specified in configs
  425. */
  426. private static void closeDoors()
  427. {
  428. for (int doorId : Config.TVT_DOORS_IDS_TO_CLOSE)
  429. {
  430. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  431. if (doorInstance != null)
  432. {
  433. doorInstance.closeMe();
  434. }
  435. }
  436. }
  437. /**
  438. * Open doors specified in configs
  439. */
  440. private static void openDoors()
  441. {
  442. for (int doorId : Config.TVT_DOORS_IDS_TO_OPEN)
  443. {
  444. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  445. if (doorInstance != null)
  446. {
  447. doorInstance.openMe();
  448. }
  449. }
  450. }
  451. /**
  452. * UnSpawns the TvTEvent npc
  453. */
  454. private static void unSpawnNpc()
  455. {
  456. // Delete the npc
  457. _lastNpcSpawn.deleteMe();
  458. // Stop respawningof the npc
  459. _npcSpawn.stopRespawn();
  460. _npcSpawn = null;
  461. _lastNpcSpawn = null;
  462. }
  463. /**
  464. * Called when a player logs in<br><br>
  465. *
  466. * @param playerInstance as L2PcInstance<br>
  467. */
  468. public static void onLogin(L2PcInstance playerInstance)
  469. {
  470. if (playerInstance == null || (!isStarting() && !isStarted()))
  471. {
  472. return;
  473. }
  474. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  475. if (teamId == -1)
  476. {
  477. return;
  478. }
  479. _teams[teamId].addPlayer(playerInstance);
  480. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true, false);
  481. }
  482. /**
  483. * Called when a player logs out<br><br>
  484. *
  485. * @param playerInstance as L2PcInstance<br>
  486. */
  487. public static void onLogout(L2PcInstance playerInstance)
  488. {
  489. if (playerInstance != null && (isStarting() || isStarted()))
  490. {
  491. removeParticipant(playerInstance.getObjectId());
  492. }
  493. }
  494. /**
  495. * Called on every bypass by npc of type L2TvTEventNpc<br>
  496. * Needs synchronization cause of the max player check<br><br>
  497. *
  498. * @param command as String<br>
  499. * @param playerInstance as L2PcInstance<br>
  500. */
  501. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  502. {
  503. if (playerInstance == null || !isParticipating())
  504. {
  505. return;
  506. }
  507. if (command.equals("tvt_event_participation"))
  508. {
  509. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  510. int playerLevel = playerInstance.getLevel();
  511. if (playerInstance.isCursedWeaponEquipped())
  512. {
  513. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>Cursed weapon owners are not allowed to participate.</body></html>");
  514. }
  515. else if (playerInstance.getKarma() > 0)
  516. {
  517. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>Chaotic players are not allowed to participate.</body></html>");
  518. }
  519. else if (playerLevel < Config.TVT_EVENT_MIN_LVL || playerLevel > Config.TVT_EVENT_MAX_LVL)
  520. {
  521. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>Only players from level " + Config.TVT_EVENT_MIN_LVL + " to level " + Config.TVT_EVENT_MAX_LVL + " are allowed tro participate.</body></html>");
  522. }
  523. else if (_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS && _teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS)
  524. {
  525. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>The event is full! Only " + Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS + " players are allowed per team.</body></html>");
  526. }
  527. else if (addParticipant(playerInstance))
  528. {
  529. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>You are on the registration list now.</body></html>");
  530. }
  531. else
  532. {
  533. return;
  534. }
  535. playerInstance.sendPacket(npcHtmlMessage);
  536. }
  537. else if (command.equals("tvt_event_remove_participation"))
  538. {
  539. removeParticipant(playerInstance.getObjectId());
  540. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  541. npcHtmlMessage.setHtml("<html><head><title>TvT Event</title></head><body>You are not longer on the registration list.</body></html>");
  542. playerInstance.sendPacket(npcHtmlMessage);
  543. }
  544. }
  545. /**
  546. * Called on every onAction in L2PcIstance<br><br>
  547. *
  548. * @param playerName as String<br>
  549. * @param targetPlayerName as String<br>
  550. * @return boolean: true if player is allowed to target, otherwise false<br>
  551. */
  552. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  553. {
  554. if (playerInstance == null || !isStarted())
  555. {
  556. return true;
  557. }
  558. if (playerInstance.isGM())
  559. {
  560. return true;
  561. }
  562. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  563. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  564. if ((playerTeamId != -1 && targetedPlayerTeamId == -1) || (playerTeamId == -1 && targetedPlayerTeamId != -1))
  565. {
  566. return false;
  567. }
  568. if (playerTeamId != -1 && targetedPlayerTeamId != -1 && playerTeamId == targetedPlayerTeamId && playerInstance.getObjectId() != targetedPlayerObjectId && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  569. {
  570. return false;
  571. }
  572. return true;
  573. }
  574. /**
  575. * Called on every scroll use<br><br>
  576. *
  577. * @param playerName as String<br>
  578. * @return boolean: true if player is allowed to use scroll, otherwise false<br>
  579. */
  580. public static boolean onScrollUse(int playerObjectId)
  581. {
  582. if (!isStarted())
  583. return true;
  584. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  585. return false;
  586. return true;
  587. }
  588. /**
  589. * Called on every potion use<br><br>
  590. *
  591. * @param playerName as String<br>
  592. * @return boolean: true if player is allowed to use potions, otherwise false<br>
  593. */
  594. public static boolean onPotionUse(int playerObjectId)
  595. {
  596. if (!isStarted())
  597. return true;
  598. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  599. return false;
  600. return true;
  601. }
  602. /**
  603. * Called on every escape use(thanks to nbd)<br><br>
  604. *
  605. * @param playerName as String<br>
  606. * @return boolean: true if player is not in tvt event, otherwise false<br>
  607. */
  608. public static boolean onEscapeUse(int playerObjectId)
  609. {
  610. if (!isStarted())
  611. {
  612. return true;
  613. }
  614. if (isPlayerParticipant(playerObjectId))
  615. {
  616. return false;
  617. }
  618. return true;
  619. }
  620. /**
  621. * Called on every summon item use<br><br>
  622. *
  623. * @param playerName as String<br>
  624. * @return boolean: true if player is allowed to summon by item, otherwise false<br>
  625. */
  626. public static boolean onItemSummon(int playerObjectId)
  627. {
  628. if (!isStarted())
  629. {
  630. return true;
  631. }
  632. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  633. {
  634. return false;
  635. }
  636. return true;
  637. }
  638. /**
  639. * Is called when a player is killed<br><br>
  640. *
  641. * @param killerCharacter as L2Character<br>
  642. * @param killedPlayerInstance as L2PcInstance<br>
  643. */
  644. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  645. {
  646. if (killedPlayerInstance == null || !isStarted())
  647. {
  648. return;
  649. }
  650. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  651. if (killedTeamId == -1)
  652. {
  653. return;
  654. }
  655. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false, false);
  656. if (killerCharacter == null)
  657. {
  658. return;
  659. }
  660. L2PcInstance killerPlayerInstance = null;
  661. if (killerCharacter instanceof L2PetInstance || killerCharacter instanceof L2SummonInstance)
  662. {
  663. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  664. if (killerPlayerInstance == null)
  665. {
  666. return;
  667. }
  668. }
  669. else if (killerCharacter instanceof L2PcInstance)
  670. {
  671. killerPlayerInstance = (L2PcInstance) killerCharacter;
  672. }
  673. else
  674. {
  675. return;
  676. }
  677. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  678. if (killerTeamId != -1 && killedTeamId != -1 && killerTeamId != killedTeamId)
  679. {
  680. TvTEventTeam killerTeam = _teams[killerTeamId];
  681. killerTeam.increasePoints();
  682. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  683. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  684. {
  685. if (playerInstance != null)
  686. {
  687. playerInstance.sendPacket(cs);
  688. }
  689. }
  690. }
  691. }
  692. /**
  693. * Sets the TvTEvent state<br><br>
  694. *
  695. * @param state as EventState<br>
  696. */
  697. private static void setState(EventState state)
  698. {
  699. synchronized (_state)
  700. {
  701. _state = state;
  702. }
  703. }
  704. /**
  705. * Is TvTEvent inactive?<br><br>
  706. *
  707. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  708. */
  709. public static boolean isInactive()
  710. {
  711. boolean isInactive;
  712. synchronized (_state)
  713. {
  714. isInactive = _state == EventState.INACTIVE;
  715. }
  716. return isInactive;
  717. }
  718. /**
  719. * Is TvTEvent in inactivating?<br><br>
  720. *
  721. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  722. */
  723. public static boolean isInactivating()
  724. {
  725. boolean isInactivating;
  726. synchronized (_state)
  727. {
  728. isInactivating = _state == EventState.INACTIVATING;
  729. }
  730. return isInactivating;
  731. }
  732. /**
  733. * Is TvTEvent in participation?<br><br>
  734. *
  735. * @return boolean: true if event is in participation progress, otherwise false<br>
  736. */
  737. public static boolean isParticipating()
  738. {
  739. boolean isParticipating;
  740. synchronized (_state)
  741. {
  742. isParticipating = _state == EventState.PARTICIPATING;
  743. }
  744. return isParticipating;
  745. }
  746. /**
  747. * Is TvTEvent starting?<br><br>
  748. *
  749. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  750. */
  751. public static boolean isStarting()
  752. {
  753. boolean isStarting;
  754. synchronized (_state)
  755. {
  756. isStarting = _state == EventState.STARTING;
  757. }
  758. return isStarting;
  759. }
  760. /**
  761. * Is TvTEvent started?<br><br>
  762. *
  763. * @return boolean: true if event is started, otherwise false<br>
  764. */
  765. public static boolean isStarted()
  766. {
  767. boolean isStarted;
  768. synchronized (_state)
  769. {
  770. isStarted = _state == EventState.STARTED;
  771. }
  772. return isStarted;
  773. }
  774. /**
  775. * Is TvTEvent rewadrding?<br><br>
  776. *
  777. * @return boolean: true if event is currently rewarding, otherwise false<br>
  778. */
  779. public static boolean isRewarding()
  780. {
  781. boolean isRewarding;
  782. synchronized (_state)
  783. {
  784. isRewarding = _state == EventState.REWARDING;
  785. }
  786. return isRewarding;
  787. }
  788. /**
  789. * Returns the team id of a player, if player is not participant it returns -1<br><br>
  790. *
  791. * @param playerName as String<br>
  792. * @return byte: team name of the given playerName, if not in event -1<br>
  793. */
  794. public static byte getParticipantTeamId(int playerObjectId)
  795. {
  796. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  797. }
  798. /**
  799. * Returns the team of a player, if player is not participant it returns null <br><br>
  800. *
  801. * @param player objectId as Integer<br>
  802. * @return TvTEventTeam: team of the given playerObjectId, if not in event null <br>
  803. */
  804. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  805. {
  806. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  807. }
  808. /**
  809. * Returns the enemy team of a player, if player is not participant it returns null <br><br>
  810. *
  811. * @param player objectId as Integer<br>
  812. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null <br>
  813. */
  814. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  815. {
  816. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  817. }
  818. /**
  819. * Returns the team coordinates in which the player is in, if player is not in a team return null<br><br>
  820. *
  821. * @param playerName as String<br>
  822. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  823. */
  824. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  825. {
  826. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  827. }
  828. /**
  829. * Is given player participant of the event?<br><br>
  830. *
  831. * @param playerName as String<br>
  832. * @return boolean: true if player is participant, ohterwise false<br>
  833. */
  834. public static boolean isPlayerParticipant(int playerObjectId)
  835. {
  836. if (!isParticipating() && !isStarting() && !isStarted())
  837. {
  838. return false;
  839. }
  840. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  841. }
  842. /**
  843. * Returns participated player count<br><br>
  844. *
  845. * @return int: amount of players registered in the event<br>
  846. */
  847. public static int getParticipatedPlayersCount()
  848. {
  849. if (!isParticipating() && !isStarting() && !isStarted())
  850. {
  851. return 0;
  852. }
  853. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  854. }
  855. /**
  856. * Returns teams names<br><br>
  857. *
  858. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  859. */
  860. public static String[] getTeamNames()
  861. {
  862. return new String[]
  863. {
  864. _teams[0].getName(), _teams[1].getName()
  865. };
  866. }
  867. /**
  868. * Returns player count of both teams<br><br>
  869. *
  870. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  871. */
  872. public static int[] getTeamsPlayerCounts()
  873. {
  874. return new int[]
  875. {
  876. _teams[0].getParticipatedPlayerCount(), _teams[1].getParticipatedPlayerCount()
  877. };
  878. }
  879. /**
  880. * Returns points count of both teams
  881. *
  882. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  883. */
  884. public static int[] getTeamsPoints()
  885. {
  886. return new int[]
  887. {
  888. _teams[0].getPoints(), _teams[1].getPoints()
  889. };
  890. }
  891. }