TvTEvent.java 29 KB

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