Guest User

Untitled

a guest
Feb 17th, 2016
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.58 KB | None | 0 0
  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 custom.events;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.logging.Logger;
  22.  
  23. import net.sf.l2j.Config;
  24. import net.sf.l2j.gameserver.cache.HtmCache;
  25. import net.sf.l2j.gameserver.datatables.DoorTable;
  26. import net.sf.l2j.gameserver.datatables.ItemTable;
  27. import net.sf.l2j.gameserver.datatables.SkillTable;
  28. import net.sf.l2j.gameserver.model.L2Skill;
  29. import net.sf.l2j.gameserver.model.actor.L2Character;
  30. import net.sf.l2j.gameserver.model.actor.L2Summon;
  31. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  32. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  33. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  36. import net.sf.l2j.gameserver.model.itemcontainer.PcInventory;
  37. import net.sf.l2j.gameserver.model.olympiad.OlympiadManager;
  38. import net.sf.l2j.gameserver.network.SystemMessageId;
  39. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  40. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  41. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  42. import net.sf.l2j.gameserver.network.serverpackets.StatusUpdate;
  43. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  44. import net.sf.l2j.util.Rnd;
  45. import net.sf.l2j.util.StringUtil;
  46.  
  47. /**
  48. * @author FBIagent
  49. */
  50. public class TvTEvent
  51. {
  52. enum EventState
  53. {
  54. INACTIVE,
  55. INACTIVATING,
  56. PARTICIPATING,
  57. STARTING,
  58. STARTED,
  59. REWARDING
  60. }
  61.  
  62. protected static final Logger _log = Logger.getLogger(TvTEvent.class.getName());
  63. /** html path **/
  64. private static final String htmlPath = "data/html/mods/TvTEvent/";
  65. /** The teams of the TvTEvent<br> */
  66. private static TvTEventTeam[] _teams = new TvTEventTeam[2];
  67. /** The state of the TvTEvent<br> */
  68. private static EventState _state = EventState.INACTIVE;
  69. /** the npc instance of the participation npc<br> */
  70. private static L2NpcInstance _lastNpcSpawn = null;
  71. /** Instance id<br> */
  72. private static int _TvTEventInstance = 0;
  73. /** Listeds Ips<br> */
  74. public static ArrayList<String> _listedIps;
  75.  
  76. /**
  77. * No instance of this class!<br>
  78. */
  79. private TvTEvent()
  80. {
  81. }
  82.  
  83. /**
  84. * Teams initializing<br>
  85. */
  86. @SuppressWarnings("unused")
  87. public static void init()
  88. {
  89. _teams[0] = new TvTEventTeam(Config.TVT_EVENT_TEAM_1_NAME, Config.TVT_EVENT_TEAM_1_COORDINATES);
  90. _teams[1] = new TvTEventTeam(Config.TVT_EVENT_TEAM_2_NAME, Config.TVT_EVENT_TEAM_2_COORDINATES);
  91.  
  92. if (Config.TVT_RESTORE_PLAYER_POS)
  93. TvTEventTeleporter.initializeRestoreMap();
  94.  
  95. if (Config.TVT_DISALLOW_DUALBOX)
  96. _listedIps = new ArrayList<String>(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS * 2);
  97. }
  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.  
  109. setState(EventState.PARTICIPATING);
  110. return true;
  111. }
  112.  
  113. private static int highestLevelPcInstanceOf(Map< Integer, L2PcInstance > players)
  114. {
  115. int maxLevel = Integer.MIN_VALUE, maxLevelId = -1;
  116. for (L2PcInstance player : players.values())
  117. {
  118. if (player.getLevel() >= maxLevel)
  119. {
  120. maxLevel = player.getLevel();
  121. maxLevelId = player.getObjectId();
  122. }
  123. }
  124. return maxLevelId;
  125. }
  126.  
  127. /**
  128. * Starts the TvTEvent fight<br>
  129. * 1. Set state EventState.STARTING<br>
  130. * 2. Close doors specified in configs<br>
  131. * 3. Abort if not enought participants(return false)<br>
  132. * 4. Set state EventState.STARTED<br>
  133. * 5. Teleport all participants to team spot<br><br>
  134. *
  135. * @return boolean: true if success, otherwise false<br>
  136. */
  137. public static boolean startFight()
  138. {
  139. // Set state to STARTING
  140. setState(EventState.STARTING);
  141.  
  142. // Randomize and balance team distribution
  143. @SuppressWarnings("unused")
  144. Map< Integer, L2PcInstance > allParticipants = new HashMap< Integer, L2PcInstance >();
  145. allParticipants.putAll(_teams[0].getParticipatedPlayers());
  146. allParticipants.putAll(_teams[1].getParticipatedPlayers());
  147. _teams[0].cleanMe();
  148. _teams[1].cleanMe();
  149. int balance[] = { 0, 0 }, priority = 0, highestLevelPlayerId;
  150. L2PcInstance highestLevelPlayer;
  151. // XXX: allParticipants should be sorted by level instead of using highestLevelPcInstanceOf for every fetch
  152. while (!allParticipants.isEmpty())
  153. {
  154. // Priority team gets one player
  155. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  156. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  157. allParticipants.remove(highestLevelPlayerId);
  158. _teams[priority].addPlayer(highestLevelPlayer);
  159. balance[priority] += highestLevelPlayer.getLevel();
  160. // Exiting if no more players
  161. if (allParticipants.isEmpty()) break;
  162. // The other team gets one player
  163. // XXX: Code not dry
  164. priority = 1-priority;
  165. highestLevelPlayerId = highestLevelPcInstanceOf(allParticipants);
  166. highestLevelPlayer = allParticipants.get(highestLevelPlayerId);
  167. allParticipants.remove(highestLevelPlayerId);
  168. _teams[priority].addPlayer(highestLevelPlayer);
  169. balance[priority] += highestLevelPlayer.getLevel();
  170. // Recalculating priority
  171. priority = balance[0] > balance[1] ? 1 : 0;
  172. }
  173.  
  174. // Check for enought participants
  175. if (_teams[0].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS || _teams[1].getParticipatedPlayerCount() < Config.TVT_EVENT_MIN_PLAYERS_IN_TEAMS)
  176. {
  177. // Set state INACTIVE
  178. setState(EventState.INACTIVE);
  179. // Cleanup of teams
  180. _teams[0].cleanMe();
  181. _teams[1].cleanMe();
  182. return false;
  183. }
  184.  
  185. // Opens all doors specified in configs for tvt
  186. openDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  187. // Closes all doors specified in configs for tvt
  188. closeDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  189. // Set state STARTED
  190. setState(EventState.STARTED);
  191.  
  192. // Iterate over all teams
  193. for (TvTEventTeam team : _teams)
  194. {
  195. // Iterate over all participated player instances in this team
  196. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  197. {
  198. @SuppressWarnings("unused")
  199. boolean _canreward = true;
  200.  
  201. if (playerInstance != null)
  202. {
  203. // Teleporter implements Runnable and starts itself
  204. new TvTEventTeleporter(playerInstance, team.getCoordinates(), false);
  205. }
  206. }
  207. }
  208.  
  209. return true;
  210. }
  211.  
  212. /**
  213. * Calculates the TvTEvent reward<br>
  214. * 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>
  215. * 2. Wait till teams are not at a tie anymore<br>
  216. * 3. Set state EvcentState.REWARDING<br>
  217. * 4. Reward team with more points<br>
  218. * 5. Show win html to wining team participants<br><br>
  219. *
  220. * @return String: winning team name<br>
  221. */
  222. public static String calculateRewards()
  223. {
  224. if (_teams[0].getPoints() == _teams[1].getPoints())
  225. {
  226. // Check if one of the teams have no more players left
  227. if (_teams[0].getParticipatedPlayerCount() == 0 || _teams[1].getParticipatedPlayerCount() == 0)
  228. {
  229. // set state to rewarding
  230. setState(EventState.REWARDING);
  231. // return here, the fight can't be completed
  232. return "Team vs Team Event: Event has ended. No team won due to inactivity!";
  233. }
  234.  
  235. // Both teams have equals points
  236. sysMsgToAllParticipants("TvT Event: Event has ended, both teams have tied.");
  237. if (Config.TVT_REWARD_TEAM_TIE)
  238. {
  239. rewardTeam(_teams[0]);
  240. rewardTeam(_teams[1]);
  241. return "Team vs Team Event: Event has ended with both teams tying.";
  242. }
  243. else
  244. return "Team vs Team Event: Event has ended with both teams tying.";
  245. }
  246.  
  247. // Set state REWARDING so nobody can point anymore
  248. setState(EventState.REWARDING);
  249.  
  250. // Get team which has more points
  251. TvTEventTeam team = _teams[_teams[0].getPoints() > _teams[1].getPoints() ? 0 : 1];
  252.  
  253. rewardTeam(team);
  254.  
  255. return "TvT Event: Event finish. Team " + team.getName() + " won with " + team.getPoints() + " kills.";
  256. }
  257.  
  258. private static void rewardTeam(TvTEventTeam team)
  259. {
  260. // Iterate over all participated player instances of the winning team
  261. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  262. {
  263. boolean _canreward = true;
  264.  
  265. // Check for nullpointer
  266. if (playerInstance == null)
  267. {
  268. continue;
  269. }
  270.  
  271. if (Config.TVT_REWARD_ONLY_KILLERS)
  272. {
  273. if (playerInstance.getTvTKills() <= 0)
  274. _canreward = false;
  275. }
  276.  
  277. SystemMessage systemMessage = null;
  278.  
  279. if (_canreward)
  280. {
  281. // Iterate over all tvt event rewards
  282. for (int[] reward : Config.TVT_EVENT_REWARDS)
  283. {
  284. PcInventory inv = playerInstance.getInventory();
  285.  
  286. // Check for stackable item, non stackabe items need to be added one by one
  287. if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  288. {
  289. inv.addItem("TvT Event", reward[0], reward[1], playerInstance, playerInstance);
  290.  
  291. if (reward[1] > 1)
  292. {
  293. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
  294. systemMessage.addItemName(reward[0]);
  295. systemMessage.addNumber(reward[1]);
  296. }
  297. else
  298. {
  299. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  300. systemMessage.addItemName(reward[0]);
  301. }
  302.  
  303. playerInstance.sendPacket(systemMessage);
  304. }
  305. else
  306. {
  307. for (int i = 0; i < reward[1]; ++i)
  308. {
  309. inv.addItem("TvT Event", reward[0], 1, playerInstance, playerInstance);
  310. systemMessage = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
  311. systemMessage.addItemName(reward[0]);
  312. playerInstance.sendPacket(systemMessage);
  313. }
  314. }
  315. }
  316.  
  317. StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
  318. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  319.  
  320. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  321. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+ "Reward.htm"));
  322. playerInstance.sendPacket(statusUpdate);
  323. playerInstance.sendPacket(npcHtmlMessage);
  324. }
  325. else
  326. {
  327. StatusUpdate statusUpdate = new StatusUpdate(playerInstance);
  328. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  329.  
  330. statusUpdate.addAttribute(StatusUpdate.CUR_LOAD, playerInstance.getCurrentLoad());
  331. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Reward0.htm"));
  332. playerInstance.sendPacket(statusUpdate);
  333. playerInstance.sendPacket(npcHtmlMessage);
  334. }
  335. }
  336. }
  337.  
  338. /**
  339. * Stops the TvTEvent fight<br>
  340. * 1. Set state EventState.INACTIVATING<br>
  341. * 2. Remove tvt npc from world<br>
  342. * 3. Open doors specified in configs<br>
  343. * 4. Teleport all participants back to participation npc location<br>
  344. * 5. Teams cleaning<br>
  345. * 6. Set state EventState.INACTIVE<br>
  346. */
  347. public static void stopFight()
  348. {
  349. // Set state INACTIVATING
  350. setState(EventState.INACTIVATING);
  351. // Opens all doors specified in configs for tvt
  352. openDoors(Config.TVT_DOORS_IDS_TO_CLOSE);
  353. // Closes all doors specified in Configs for tvt
  354. closeDoors(Config.TVT_DOORS_IDS_TO_OPEN);
  355.  
  356. // Iterate over all teams
  357. for (TvTEventTeam team : _teams)
  358. {
  359. for (L2PcInstance playerInstance : team.getParticipatedPlayers().values())
  360. {
  361. @SuppressWarnings("unused")
  362. boolean _canreward = true;
  363.  
  364. // Check for nullpointer
  365. if (playerInstance != null)
  366. {
  367. new TvTEventTeleporter(playerInstance, Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES, false);
  368. }
  369. }
  370. }
  371.  
  372. if (Config.TVT_RESTORE_PLAYER_POS)
  373. TvTEventTeleporter.clearRestoreMap();
  374.  
  375. if (Config.TVT_DISALLOW_DUALBOX)
  376. _listedIps.clear();
  377.  
  378. // Cleanup of teams
  379. _teams[0].cleanMe();
  380. _teams[1].cleanMe();
  381. // Set state INACTIVE
  382. setState(EventState.INACTIVE);
  383. }
  384.  
  385. /**
  386. * Adds a player to a TvTEvent team<br>
  387. * 1. Calculate the id of the team in which the player should be added<br>
  388. * 2. Add the player to the calculated team<br><br>
  389. *
  390. * @param playerInstance as L2PcInstance<br>
  391. * @return boolean: true if success, otherwise false<br>
  392. */
  393. public static synchronized boolean addParticipant(L2PcInstance playerInstance)
  394. {
  395. // Check for nullpoitner
  396. if (playerInstance == null)
  397. {
  398. return false;
  399. }
  400.  
  401. byte teamId = 0;
  402.  
  403. // Check to which team the player should be added
  404. if (_teams[0].getParticipatedPlayerCount() == _teams[1].getParticipatedPlayerCount())
  405. {
  406. teamId = (byte) (Rnd.get(2));
  407. }
  408. else
  409. {
  410. teamId = (byte) (_teams[0].getParticipatedPlayerCount() > _teams[1].getParticipatedPlayerCount() ? 1 : 0);
  411. }
  412.  
  413. if (Config.TVT_DISALLOW_DUALBOX)
  414. {
  415. String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
  416.  
  417. if (host != null && !_listedIps.contains(host))
  418. _listedIps.add(host);
  419. else
  420. return false;
  421. }
  422.  
  423. return _teams[teamId].addPlayer(playerInstance);
  424. }
  425.  
  426. /**
  427. * Removes a TvTEvent player from it's team<br>
  428. * 1. Get team id of the player<br>
  429. * 2. Remove player from it's team<br><br>
  430. * @param playerObjectId
  431. * @return boolean: true if success, otherwise false<br>
  432. */
  433. public static boolean removeParticipant(int playerObjectId)
  434. {
  435. // Get the teamId of the player
  436. byte teamId = getParticipantTeamId(playerObjectId);
  437.  
  438. // Check if the player is participant
  439. if (teamId != -1)
  440. {
  441. // Remove the player from team
  442. _teams[teamId].removePlayer(playerObjectId);
  443. return true;
  444. }
  445.  
  446. return false;
  447. }
  448.  
  449. public static boolean payParticipationFee(L2PcInstance playerInstance)
  450. {
  451. int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
  452. int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
  453. if (itemId == 0 || itemNum == 0)
  454. return true;
  455.  
  456. if (playerInstance.getInventory().getInventoryItemCount(itemId, -1) < itemNum)
  457. return false;
  458.  
  459. return playerInstance.destroyItemByItemId("TvT Participation Fee", itemId, itemNum, _lastNpcSpawn, true);
  460. }
  461.  
  462. public static String getParticipationFee()
  463. {
  464. int itemId = Config.TVT_EVENT_PARTICIPATION_FEE[0];
  465. int itemNum = Config.TVT_EVENT_PARTICIPATION_FEE[1];
  466.  
  467. if (itemId == 0 || itemNum == 0)
  468. return "-";
  469.  
  470. return StringUtil.concat(String.valueOf(itemNum), " ", ItemTable.getInstance().getTemplate(itemId).getName());
  471. }
  472.  
  473. /**
  474. * Send a SystemMessage to all participated players<br>
  475. * 1. Send the message to all players of team number one<br>
  476. * 2. Send the message to all players of team number two<br><br>
  477. *
  478. * @param message as String<br>
  479. */
  480. public static void sysMsgToAllParticipants(String message)
  481. {
  482. for (L2PcInstance playerInstance : _teams[0].getParticipatedPlayers().values())
  483. {
  484.  
  485. if (playerInstance != null)
  486. {
  487. playerInstance.sendMessage(message);
  488. }
  489. }
  490.  
  491. for (L2PcInstance playerInstance : _teams[1].getParticipatedPlayers().values())
  492. {
  493. if (playerInstance != null)
  494. {
  495. playerInstance.sendMessage(message);
  496. }
  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.  
  510. if (doorInstance != null)
  511. {
  512. doorInstance.closeMe();
  513. }
  514. }
  515. }
  516.  
  517. /**
  518. * Open doors specified in configs
  519. * @param doors
  520. */
  521. private static void openDoors(List<Integer> doors)
  522. {
  523. for (int doorId : doors)
  524. {
  525. L2DoorInstance doorInstance = DoorTable.getInstance().getDoor(doorId);
  526.  
  527. if (doorInstance != null)
  528. {
  529. doorInstance.openMe();
  530. }
  531. }
  532. }
  533.  
  534. /**
  535. * Called when a player logs in<br><br>
  536. *
  537. * @param playerInstance as L2PcInstance<br>
  538. */
  539. public static void onLogin(L2PcInstance playerInstance)
  540. {
  541. if (playerInstance == null || (!isStarting() && !isStarted()))
  542. {
  543. return;
  544. }
  545.  
  546. byte teamId = getParticipantTeamId(playerInstance.getObjectId());
  547.  
  548. if (teamId == -1)
  549. {
  550. return;
  551. }
  552.  
  553. _teams[teamId].addPlayer(playerInstance);
  554. new TvTEventTeleporter(playerInstance, _teams[teamId].getCoordinates(), true);
  555. }
  556.  
  557. /**
  558. * Called when a player logs out<br><br>
  559. *
  560. * @param playerInstance as L2PcInstance<br>
  561. */
  562. public static void onLogout(L2PcInstance playerInstance)
  563. {
  564. if (playerInstance != null && (isStarting() || isStarted() || isParticipating()))
  565. {
  566. if (removeParticipant(playerInstance.getObjectId()))
  567. playerInstance.setXYZInvisible(Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[0] + Rnd.get(101)-50,
  568. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[1] + Rnd.get(101)-50,
  569. Config.TVT_EVENT_PARTICIPATION_NPC_COORDINATES[2]);
  570. }
  571. }
  572.  
  573. /**
  574. * Called on every bypass by npc of type L2TvTEventNpc<br>
  575. * Needs synchronization cause of the max player check<br><br>
  576. *
  577. * @param command as String<br>
  578. * @param playerInstance as L2PcInstance<br>
  579. */
  580. public static synchronized void onBypass(String command, L2PcInstance playerInstance)
  581. {
  582. if (playerInstance == null || !isParticipating())
  583. return;
  584.  
  585. final String htmContent;
  586.  
  587. if (command.equals("tvt_event_participation"))
  588. {
  589. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  590. int playerLevel = playerInstance.getLevel();
  591.  
  592. if (Config.TVT_DISALLOW_DUALBOX && isDualBoxing(playerInstance))
  593. {
  594. htmContent = HtmCache.getInstance().getHtm(htmlPath+"DualBox.htm");
  595. if (htmContent != null)
  596. {
  597. npcHtmlMessage.setHtml(htmContent);
  598. npcHtmlMessage.replace("%pName%", playerInstance.getName());
  599. String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
  600. npcHtmlMessage.replace("%ip%", host);
  601. }
  602. }
  603. else if (playerInstance.isCursedWeaponEquipped())
  604. {
  605. htmContent = HtmCache.getInstance().getHtm(htmlPath+"CursedWeaponEquipped.htm");
  606. if (htmContent != null)
  607. {
  608. npcHtmlMessage.setHtml(htmContent);
  609. }
  610. }
  611. else if (OlympiadManager.getInstance().isRegistered(playerInstance))
  612. {
  613. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Olympiad.htm");
  614. if (htmContent != null)
  615. {
  616. npcHtmlMessage.setHtml(htmContent);
  617. }
  618. }
  619. /*else if(playerInstance.isAio())
  620. {
  621. playerInstance.sendMessage("AIO cannot register for TvT Event.");
  622. return;
  623. }*/
  624. else if (playerInstance.getKarma() > 0)
  625. {
  626. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Karma.htm");
  627. if (htmContent != null)
  628. {
  629. npcHtmlMessage.setHtml(htmContent);
  630. }
  631. }
  632. else if (playerLevel < Config.TVT_EVENT_MIN_LVL || playerLevel > Config.TVT_EVENT_MAX_LVL)
  633. {
  634. htmContent = HtmCache.getInstance().getHtm(htmlPath+"Level.htm");
  635. if (htmContent != null)
  636. {
  637. npcHtmlMessage.setHtml(htmContent);
  638. npcHtmlMessage.replace("%min%", String.valueOf(Config.TVT_EVENT_MIN_LVL));
  639. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_LVL));
  640. }
  641. }
  642. else if (_teams[0].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS && _teams[1].getParticipatedPlayerCount() == Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS)
  643. {
  644. htmContent = HtmCache.getInstance().getHtm(htmlPath+"TeamsFull.htm");
  645. if (htmContent != null)
  646. {
  647. npcHtmlMessage.setHtml(htmContent);
  648. npcHtmlMessage.replace("%max%", String.valueOf(Config.TVT_EVENT_MAX_PLAYERS_IN_TEAMS));
  649. }
  650. }
  651. else if (!payParticipationFee(playerInstance))
  652. {
  653. htmContent = HtmCache.getInstance().getHtm(htmlPath+"ParticipationFee.htm");
  654. if (htmContent != null)
  655. {
  656. npcHtmlMessage.setHtml(htmContent);
  657. npcHtmlMessage.replace("%fee%", getParticipationFee());
  658. }
  659. }
  660. else if (addParticipant(playerInstance))
  661. {
  662. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Registered.htm"));
  663. }
  664. else
  665. {
  666. return;
  667. }
  668. playerInstance.sendPacket(npcHtmlMessage);
  669. }
  670. else if (command.equals("tvt_event_remove_participation"))
  671. {
  672. removeParticipant(playerInstance.getObjectId());
  673.  
  674. if (Config.TVT_DISALLOW_DUALBOX)
  675. {
  676. String host = playerInstance.getClient().getConnection().getInetAddress().getHostAddress();
  677. if (_listedIps.contains(host))
  678. {
  679. _listedIps.remove(host);
  680. }
  681. }
  682.  
  683. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  684.  
  685. npcHtmlMessage.setHtml(HtmCache.getInstance().getHtm(htmlPath+"Unregistered.htm"));
  686. playerInstance.sendPacket(npcHtmlMessage);
  687. }
  688. }
  689.  
  690. /**
  691. * Called on every onAction in L2PcIstance<br><br>
  692. * @param playerInstance
  693. * @param targetedPlayerObjectId
  694. *
  695. * @return boolean: true if player is allowed to target, otherwise false<br>
  696. */
  697. public static boolean onAction(L2PcInstance playerInstance, int targetedPlayerObjectId)
  698. {
  699. if (playerInstance == null || !isStarted())
  700. {
  701. return true;
  702. }
  703.  
  704. if (playerInstance.isGM())
  705. {
  706. return true;
  707. }
  708.  
  709. byte playerTeamId = getParticipantTeamId(playerInstance.getObjectId());
  710. byte targetedPlayerTeamId = getParticipantTeamId(targetedPlayerObjectId);
  711.  
  712. if ((playerTeamId != -1 && targetedPlayerTeamId == -1) || (playerTeamId == -1 && targetedPlayerTeamId != -1))
  713. {
  714. return false;
  715. }
  716.  
  717. if (playerTeamId != -1 && targetedPlayerTeamId != -1 && playerTeamId == targetedPlayerTeamId && playerInstance.getObjectId() != targetedPlayerObjectId && !Config.TVT_EVENT_TARGET_TEAM_MEMBERS_ALLOWED)
  718. {
  719. return false;
  720. }
  721.  
  722. return true;
  723. }
  724.  
  725. /**
  726. * Called on every scroll use<br><br>
  727. * @param playerObjectId
  728. *
  729. * @return boolean: true if player is allowed to use scroll, otherwise false<br>
  730. */
  731. public static boolean onScrollUse(int playerObjectId)
  732. {
  733. if (!isStarted())
  734. return true;
  735.  
  736. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SCROLL_ALLOWED)
  737. return false;
  738.  
  739. return true;
  740. }
  741.  
  742. /**
  743. * Called on every potion use<br><br>
  744. * @param playerObjectId
  745. *
  746. * @return boolean: true if player is allowed to use potions, otherwise false<br>
  747. */
  748. public static boolean onPotionUse(int playerObjectId)
  749. {
  750. if (!isStarted())
  751. return true;
  752.  
  753. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_POTIONS_ALLOWED)
  754. return false;
  755.  
  756. return true;
  757. }
  758.  
  759. /**
  760. * Called on every escape use(thanks to nbd)<br><br>
  761. * @param playerObjectId
  762. *
  763. * @return boolean: true if player is not in tvt event, otherwise false<br>
  764. */
  765. public static boolean onEscapeUse(int playerObjectId)
  766. {
  767. if (!isStarted())
  768. {
  769. return true;
  770. }
  771.  
  772. if (isPlayerParticipant(playerObjectId))
  773. {
  774. return false;
  775. }
  776.  
  777. return true;
  778. }
  779.  
  780. /**
  781. * Called on every summon item use<br><br>
  782. * @param playerObjectId
  783. *
  784. * @return boolean: true if player is allowed to summon by item, otherwise false<br>
  785. */
  786. public static boolean onItemSummon(int playerObjectId)
  787. {
  788. if (!isStarted())
  789. {
  790. return true;
  791. }
  792.  
  793. if (isPlayerParticipant(playerObjectId) && !Config.TVT_EVENT_SUMMON_BY_ITEM_ALLOWED)
  794. {
  795. return false;
  796. }
  797.  
  798. return true;
  799. }
  800.  
  801. /**
  802. * Is called when a player is killed<br><br>
  803. *
  804. * @param killerCharacter as L2Character<br>
  805. * @param killedPlayerInstance as L2PcInstance<br>
  806. */
  807. public static void onKill(L2Character killerCharacter, L2PcInstance killedPlayerInstance)
  808. {
  809. if (killedPlayerInstance == null || !isStarted())
  810. {
  811. return;
  812. }
  813.  
  814. byte killedTeamId = getParticipantTeamId(killedPlayerInstance.getObjectId());
  815.  
  816. if (killedTeamId == -1)
  817. {
  818. return;
  819. }
  820.  
  821. new TvTEventTeleporter(killedPlayerInstance, _teams[killedTeamId].getCoordinates(), false);
  822.  
  823. if (killerCharacter == null)
  824. {
  825. return;
  826. }
  827.  
  828. L2PcInstance killerPlayerInstance = null;
  829.  
  830. if (killerCharacter instanceof L2PetInstance || killerCharacter instanceof L2SummonInstance)
  831. {
  832. killerPlayerInstance = ((L2Summon) killerCharacter).getOwner();
  833.  
  834. if (killerPlayerInstance == null)
  835. {
  836. return;
  837. }
  838. }
  839. else if (killerCharacter instanceof L2PcInstance)
  840. {
  841. killerPlayerInstance = (L2PcInstance) killerCharacter;
  842. }
  843. else
  844. {
  845. return;
  846. }
  847.  
  848. byte killerTeamId = getParticipantTeamId(killerPlayerInstance.getObjectId());
  849.  
  850. if (killerTeamId != -1 && killedTeamId != -1 && killerTeamId != killedTeamId)
  851. {
  852. TvTEventTeam killerTeam = _teams[killerTeamId];
  853.  
  854. killerTeam.increasePoints();
  855.  
  856. if (Config.TVT_REWARD_ONLY_KILLERS)
  857. killerPlayerInstance.increaseTvTKills();
  858.  
  859. CreatureSay cs = new CreatureSay(killerPlayerInstance.getObjectId(), Say2.TELL, killerPlayerInstance.getName(), "I have killed " + killedPlayerInstance.getName() + "!");
  860. killerPlayerInstance._originalKarmaTvT = killerPlayerInstance.getKarma();
  861. killerPlayerInstance._originalTitle = killerPlayerInstance.getTitle();
  862. killerPlayerInstance.setTitle("Kills: "+killerPlayerInstance.getTvTKills());
  863. killerPlayerInstance.setKarma(0);
  864. killerPlayerInstance.broadcastStatusUpdate();
  865. killerPlayerInstance.broadcastUserInfo();
  866. for (L2PcInstance playerInstance : _teams[killerTeamId].getParticipatedPlayers().values())
  867. {
  868. if (playerInstance != null)
  869. {
  870. playerInstance.sendPacket(cs);
  871. }
  872. }
  873. }
  874. }
  875.  
  876. /**
  877. * Called on Appearing packet received (player finished teleporting)<br><br>
  878. * @param playerInstance
  879. */
  880. public static void onTeleported(L2PcInstance playerInstance)
  881. {
  882. if (!isStarted() || playerInstance == null || !isPlayerParticipant(playerInstance.getObjectId()))
  883. return;
  884.  
  885. if (playerInstance.isMageClass())
  886. {
  887. if (Config.TVT_EVENT_MAGE_BUFFS != null && !Config.TVT_EVENT_MAGE_BUFFS.isEmpty())
  888. {
  889. for (int i : Config.TVT_EVENT_MAGE_BUFFS.keySet())
  890. {
  891. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_MAGE_BUFFS.get(i));
  892. if (skill != null)
  893. skill.getEffects(playerInstance, playerInstance);
  894. }
  895. }
  896. }
  897. else
  898. {
  899. if (Config.TVT_EVENT_FIGHTER_BUFFS != null && !Config.TVT_EVENT_FIGHTER_BUFFS.isEmpty())
  900. {
  901. for (int i : Config.TVT_EVENT_FIGHTER_BUFFS.keySet())
  902. {
  903. L2Skill skill = SkillTable.getInstance().getInfo(i, Config.TVT_EVENT_FIGHTER_BUFFS.get(i));
  904. if (skill != null)
  905. skill.getEffects(playerInstance, playerInstance);
  906. }
  907. }
  908. }
  909.  
  910. if (Config.TVT_EVENT_RESTORE_CPHPMP)
  911. {
  912. playerInstance.setCurrentCp(playerInstance.getMaxCp());
  913. playerInstance.setCurrentHpMp(playerInstance.getMaxHp(), playerInstance.getMaxMp());
  914. }
  915. }
  916.  
  917. /*
  918. * Return true if player valid for skill
  919. */
  920. public static final boolean checkForTvTSkill(L2PcInstance source, L2PcInstance target, L2Skill skill)
  921. {
  922. if (!isStarted())
  923. return true;
  924. // TvT is started
  925. final int sourcePlayerId = source.getObjectId();
  926. final int targetPlayerId = target.getObjectId();
  927. final boolean isSourceParticipant = isPlayerParticipant(sourcePlayerId);
  928. final boolean isTargetParticipant = isPlayerParticipant(targetPlayerId);
  929.  
  930. // both players not participating
  931. if (!isSourceParticipant && !isTargetParticipant)
  932. return true;
  933. // one player not participating
  934. if (!(isSourceParticipant && isTargetParticipant))
  935. return false;
  936. // players in the different teams ?
  937. if (getParticipantTeamId(sourcePlayerId) != getParticipantTeamId(targetPlayerId))
  938. {
  939. if (!skill.isOffensive())
  940. return false;
  941. }
  942. return true;
  943. }
  944.  
  945. /**
  946. * Sets the TvTEvent state<br><br>
  947. *
  948. * @param state as EventState<br>
  949. */
  950. private static void setState(EventState state)
  951. {
  952. synchronized (_state)
  953. {
  954. _state = state;
  955. }
  956. }
  957.  
  958. /**
  959. * Is TvTEvent inactive?<br><br>
  960. *
  961. * @return boolean: true if event is inactive(waiting for next event cycle), otherwise false<br>
  962. */
  963. public static boolean isInactive()
  964. {
  965. boolean isInactive;
  966.  
  967. synchronized (_state)
  968. {
  969. isInactive = _state == EventState.INACTIVE;
  970. }
  971.  
  972. return isInactive;
  973. }
  974.  
  975. /**
  976. * Is TvTEvent in inactivating?<br><br>
  977. *
  978. * @return boolean: true if event is in inactivating progress, otherwise false<br>
  979. */
  980. public static boolean isInactivating()
  981. {
  982. boolean isInactivating;
  983.  
  984. synchronized (_state)
  985. {
  986. isInactivating = _state == EventState.INACTIVATING;
  987. }
  988.  
  989. return isInactivating;
  990. }
  991.  
  992. /**
  993. * Is TvTEvent in participation?<br><br>
  994. *
  995. * @return boolean: true if event is in participation progress, otherwise false<br>
  996. */
  997. public static boolean isParticipating()
  998. {
  999. boolean isParticipating = false;
  1000.  
  1001. synchronized (_state)
  1002. {
  1003. isParticipating = _state == EventState.PARTICIPATING;
  1004. }
  1005.  
  1006. return isParticipating;
  1007. }
  1008.  
  1009. /**
  1010. * Is TvTEvent starting?<br><br>
  1011. *
  1012. * @return boolean: true if event is starting up(setting up fighting spot, teleport players etc.), otherwise false<br>
  1013. */
  1014. public static boolean isStarting()
  1015. {
  1016. boolean isStarting;
  1017.  
  1018. synchronized (_state)
  1019. {
  1020. isStarting = _state == EventState.STARTING;
  1021. }
  1022.  
  1023. return isStarting;
  1024. }
  1025.  
  1026. /**
  1027. * Is TvTEvent started?<br><br>
  1028. *
  1029. * @return boolean: true if event is started, otherwise false<br>
  1030. */
  1031. public static boolean isStarted()
  1032. {
  1033. boolean isStarted;
  1034.  
  1035. synchronized (_state)
  1036. {
  1037. isStarted = _state == EventState.STARTED;
  1038. }
  1039.  
  1040. return isStarted;
  1041. }
  1042.  
  1043. /**
  1044. * Is TvTEvent rewadrding?<br><br>
  1045. *
  1046. * @return boolean: true if event is currently rewarding, otherwise false<br>
  1047. */
  1048. public static boolean isRewarding()
  1049. {
  1050. boolean isRewarding;
  1051.  
  1052. synchronized (_state)
  1053. {
  1054. isRewarding = _state == EventState.REWARDING;
  1055. }
  1056.  
  1057. return isRewarding;
  1058. }
  1059.  
  1060. /**
  1061. * Returns the team id of a player, if player is not participant it returns -1<br><br>
  1062. * @param playerObjectId
  1063. *
  1064. * @return byte: team name of the given playerName, if not in event -1<br>
  1065. */
  1066. public static byte getParticipantTeamId(int playerObjectId)
  1067. {
  1068. return (byte) (_teams[0].containsPlayer(playerObjectId) ? 0 : (_teams[1].containsPlayer(playerObjectId) ? 1 : -1));
  1069. }
  1070.  
  1071. /**
  1072. * Returns the team of a player, if player is not participant it returns null <br><br>
  1073. * @param playerObjectId
  1074. *
  1075. * @return TvTEventTeam: team of the given playerObjectId, if not in event null <br>
  1076. */
  1077. public static TvTEventTeam getParticipantTeam(int playerObjectId)
  1078. {
  1079. return (_teams[0].containsPlayer(playerObjectId) ? _teams[0] : (_teams[1].containsPlayer(playerObjectId) ? _teams[1] : null));
  1080. }
  1081.  
  1082. /**
  1083. * Returns the enemy team of a player, if player is not participant it returns null <br><br>
  1084. * @param playerObjectId
  1085. *
  1086. * @return TvTEventTeam: enemy team of the given playerObjectId, if not in event null <br>
  1087. */
  1088. public static TvTEventTeam getParticipantEnemyTeam(int playerObjectId)
  1089. {
  1090. return (_teams[0].containsPlayer(playerObjectId) ? _teams[1] : (_teams[1].containsPlayer(playerObjectId) ? _teams[0] : null));
  1091. }
  1092.  
  1093. /**
  1094. * Returns the team coordinates in which the player is in, if player is not in a team return null<br><br>
  1095. * @param playerObjectId
  1096. *
  1097. * @return int[]: coordinates of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1098. */
  1099. public static int[] getParticipantTeamCoordinates(int playerObjectId)
  1100. {
  1101. return _teams[0].containsPlayer(playerObjectId) ? _teams[0].getCoordinates() : (_teams[1].containsPlayer(playerObjectId) ? _teams[1].getCoordinates() : null);
  1102. }
  1103.  
  1104. /**
  1105. * Is given player participant of the event?<br><br>
  1106. * @param playerObjectId
  1107. *
  1108. * @return boolean: true if player is participant, ohterwise false<br>
  1109. */
  1110. public static boolean isPlayerParticipant(int playerObjectId)
  1111. {
  1112. if (!isParticipating() && !isStarting() && !isStarted())
  1113. {
  1114. return false;
  1115. }
  1116.  
  1117. return _teams[0].containsPlayer(playerObjectId) || _teams[1].containsPlayer(playerObjectId);
  1118. }
  1119.  
  1120. /**
  1121. * Returns participated player count<br><br>
  1122. *
  1123. * @return int: amount of players registered in the event<br>
  1124. */
  1125. public static int getParticipatedPlayersCount()
  1126. {
  1127. if (!isParticipating() && !isStarting() && !isStarted())
  1128. {
  1129. return 0;
  1130. }
  1131.  
  1132. return _teams[0].getParticipatedPlayerCount() + _teams[1].getParticipatedPlayerCount();
  1133. }
  1134.  
  1135. /**
  1136. * Returns teams names<br><br>
  1137. *
  1138. * @return String[]: names of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1139. */
  1140. public static String[] getTeamNames()
  1141. {
  1142. return new String[]
  1143. {
  1144. _teams[0].getName(), _teams[1].getName()
  1145. };
  1146. }
  1147.  
  1148. /**
  1149. * Returns player count of both teams<br><br>
  1150. *
  1151. * @return int[]: player count of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1152. */
  1153. public static int[] getTeamsPlayerCounts()
  1154. {
  1155. return new int[]
  1156. {
  1157. _teams[0].getParticipatedPlayerCount(), _teams[1].getParticipatedPlayerCount()
  1158. };
  1159. }
  1160.  
  1161. /**
  1162. * Returns points count of both teams
  1163. *
  1164. * @return int[]: points of teams, 2 elements, index 0 for team 1 and index 1 for team 2<br>
  1165. */
  1166. public static int[] getTeamsPoints()
  1167. {
  1168. return new int[]
  1169. {
  1170. _teams[0].getPoints(), _teams[1].getPoints()
  1171. };
  1172. }
  1173.  
  1174. public static int getTvTEventInstance()
  1175. {
  1176. return _TvTEventInstance;
  1177. }
  1178.  
  1179. public static boolean isDualBoxing(L2PcInstance player)
  1180. {
  1181. if (player == null) return true;
  1182.  
  1183. String host = player.getClient().getConnection().getInetAddress().getHostAddress();
  1184.  
  1185. if (_listedIps.contains(host))
  1186. return true;
  1187.  
  1188. return false;
  1189. }
  1190.  
  1191.  
  1192. }
  1193. /*
  1194. * This program is free software: you can redistribute it and/or modify it under
  1195. * the terms of the GNU General Public License as published by the Free Software
  1196. * Foundation, either version 3 of the License, or (at your option) any later
  1197. * version.
  1198. *
  1199. * This program is distributed in the hope that it will be useful, but WITHOUT
  1200. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  1201. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  1202. * details.
  1203. *
  1204. * You should have received a copy of the GNU General Public License along with
  1205. * this program. If not, see <http://www.gnu.org/licenses/>.
  1206. */
  1207. package custom.events;
  1208.  
  1209. import java.util.HashMap;
  1210. import java.util.Map;
  1211.  
  1212. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  1213.  
  1214. /**
  1215. * @author FBIagent
  1216. */
  1217. public class TvTEventTeam
  1218. {
  1219. /** The name of the team<br> */
  1220. private String _name;
  1221. /** The team spot coordinated<br> */
  1222. private int[] _coordinates = new int[3];
  1223. /** The points of the team<br> */
  1224. private short _points;
  1225. /** Name and instance of all participated players in HashMap<br> */
  1226. @SuppressWarnings("unused")
  1227. private Map<Integer, L2PcInstance> _participatedPlayers = new HashMap<Integer, L2PcInstance>();
  1228.  
  1229. /**
  1230. * C'tor initialize the team<br><br>
  1231. *
  1232. * @param name as String<br>
  1233. * @param coordinates as int[]<br>
  1234. */
  1235. public TvTEventTeam(String name, int[] coordinates)
  1236. {
  1237. _name = name;
  1238. _coordinates = coordinates;
  1239. _points = 0;
  1240. }
  1241.  
  1242. /**
  1243. * Adds a player to the team<br><br>
  1244. *
  1245. * @param playerInstance as L2PcInstance<br>
  1246. * @return boolean: true if success, otherwise false<br>
  1247. */
  1248. public boolean addPlayer(L2PcInstance playerInstance)
  1249. {
  1250. if (playerInstance == null)
  1251. {
  1252. return false;
  1253. }
  1254.  
  1255. synchronized (_participatedPlayers)
  1256. {
  1257. _participatedPlayers.put(playerInstance.getObjectId(), playerInstance);
  1258. }
  1259.  
  1260. return true;
  1261. }
  1262.  
  1263. /**
  1264. * Removes a player from the team<br><br>
  1265. * @param playerObjectId
  1266. *
  1267. */
  1268. public void removePlayer(int playerObjectId)
  1269. {
  1270. synchronized (_participatedPlayers)
  1271. {
  1272. _participatedPlayers.remove(playerObjectId);
  1273. }
  1274. }
  1275.  
  1276. /**
  1277. * Increases the points of the team<br>
  1278. */
  1279. public void increasePoints()
  1280. {
  1281. ++_points;
  1282. }
  1283.  
  1284. /**
  1285. * Cleanup the team and make it ready for adding players again<br>
  1286. */
  1287. @SuppressWarnings("unused")
  1288. public void cleanMe()
  1289. {
  1290. _participatedPlayers.clear();
  1291. _participatedPlayers = new HashMap<Integer, L2PcInstance>();
  1292. _points = 0;
  1293. }
  1294.  
  1295. /**
  1296. * Is given player in this team?<br><br>
  1297. * @param playerObjectId
  1298. * @return boolean: true if player is in this team, otherwise false<br>
  1299. */
  1300. public boolean containsPlayer(int playerObjectId)
  1301. {
  1302. boolean containsPlayer;
  1303.  
  1304. synchronized (_participatedPlayers)
  1305. {
  1306. containsPlayer = _participatedPlayers.containsKey(playerObjectId);
  1307. }
  1308.  
  1309. return containsPlayer;
  1310. }
  1311.  
  1312. /**
  1313. * Returns the name of the team<br><br>
  1314. *
  1315. * @return String: name of the team<br>
  1316. */
  1317. public String getName()
  1318. {
  1319. return _name;
  1320. }
  1321.  
  1322. /**
  1323. * Returns the coordinates of the team spot<br><br>
  1324. *
  1325. * @return int[]: team coordinates<br>
  1326. */
  1327. public int[] getCoordinates()
  1328. {
  1329. return _coordinates;
  1330. }
  1331.  
  1332. /**
  1333. * Returns the points of the team<br><br>
  1334. *
  1335. * @return short: team points<br>
  1336. */
  1337. public short getPoints()
  1338. {
  1339. return _points;
  1340. }
  1341.  
  1342. /**
  1343. * Returns name and instance of all participated players in HashMap<br><br>
  1344. *
  1345. * @return Map<String, L2PcInstance>: map of players in this team<br>
  1346. */
  1347. public Map<Integer, L2PcInstance> getParticipatedPlayers()
  1348. {
  1349. Map<Integer, L2PcInstance> participatedPlayers = null;
  1350.  
  1351. synchronized (_participatedPlayers)
  1352. {
  1353. participatedPlayers = _participatedPlayers;
  1354. }
  1355.  
  1356. return participatedPlayers;
  1357. }
  1358.  
  1359. /**
  1360. * Returns player count of this team<br><br>
  1361. *
  1362. * @return int: number of players in team<br>
  1363. */
  1364. public int getParticipatedPlayerCount()
  1365. {
  1366. int participatedPlayerCount;
  1367.  
  1368. synchronized (_participatedPlayers)
  1369. {
  1370. participatedPlayerCount = _participatedPlayers.size();
  1371. }
  1372.  
  1373. return participatedPlayerCount;
  1374. }
  1375. }
  1376. /*
  1377. * This program is free software: you can redistribute it and/or modify it under
  1378. * the terms of the GNU General Public License as published by the Free Software
  1379. * Foundation, either version 3 of the License, or (at your option) any later
  1380. * version.
  1381. *
  1382. * This program is distributed in the hope that it will be useful, but WITHOUT
  1383. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  1384. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  1385. * details.
  1386. *
  1387. * You should have received a copy of the GNU General Public License along with
  1388. * this program. If not, see <http://www.gnu.org/licenses/>.
  1389. */
  1390. package custom.events;
  1391.  
  1392. import java.util.HashMap;
  1393. import java.util.logging.Logger;
  1394.  
  1395. import net.sf.l2j.Config;
  1396. import net.sf.l2j.gameserver.ThreadPoolManager;
  1397. import net.sf.l2j.gameserver.model.actor.L2Summon;
  1398. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  1399. import net.sf.l2j.gameserver.model.entity.Duel;
  1400. import net.sf.l2j.gameserver.model.entity.Duel.DuelState;
  1401. import net.sf.l2j.util.Rnd;
  1402.  
  1403.  
  1404. public class TvTEventTeleporter implements Runnable
  1405. {
  1406. /** The instance of the player to teleport */
  1407. private L2PcInstance _playerInstance = null;
  1408. /** Coordinates of the spot to teleport to */
  1409. private int[] _coordinates = new int[3];
  1410. /** Admin removed this player from event */
  1411. //private boolean _adminRemove = false;
  1412. /** Old Player Coordinates */
  1413. private static HashMap<Integer, Integer[]> _oldPlayerPos;
  1414. public static final Logger _log = Logger.getLogger(TvTEventTeleporter.class.getName());
  1415.  
  1416. /**
  1417. * Initialize the teleporter and start the delayed task<br><br>
  1418. *
  1419. * @param playerInstance as L2PcInstance<br>
  1420. * @param coordinates as int[]<br>
  1421. * @param fastSchedule
  1422. */
  1423. public TvTEventTeleporter(L2PcInstance playerInstance, int[] coordinates, boolean fastSchedule)
  1424. {
  1425. _playerInstance = playerInstance;
  1426. _coordinates = coordinates;
  1427. //_adminRemove = adminRemove;
  1428.  
  1429. long delay = (TvTEvent.isStarted() ? Config.TVT_EVENT_RESPAWN_TELEPORT_DELAY : Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY) * 1000;
  1430.  
  1431. ThreadPoolManager.getInstance().scheduleGeneral(this, fastSchedule ? 0 : delay);
  1432. }
  1433.  
  1434. /**
  1435. * The task method to teleport the player<br>
  1436. * 1. Unsummon pet if there is one<br>
  1437. * 2. Remove all effects<br>
  1438. * 3. Revive and full heal the player<br>
  1439. * 4. Teleport the player<br>
  1440. * 5. Broadcast status and user info<br><br>
  1441. */
  1442. @Override
  1443. public void run()
  1444. {
  1445. if (_playerInstance == null)
  1446. return;
  1447.  
  1448. L2Summon summon = _playerInstance.getPet();
  1449.  
  1450. if (summon != null)
  1451. summon.unSummon(_playerInstance);
  1452.  
  1453. if (Config.TVT_EVENT_EFFECTS_REMOVAL == 0
  1454. || (Config.TVT_EVENT_EFFECTS_REMOVAL == 1 || (_playerInstance.isInDuel() && _playerInstance.getDuelState() != DuelState.INTERRUPTED)))
  1455. _playerInstance.stopAllEffects();
  1456.  
  1457. if (_playerInstance.isInDuel())
  1458. _playerInstance.setDuelState(DuelState.INTERRUPTED);
  1459.  
  1460. _playerInstance.doRevive();
  1461.  
  1462. int objId = _playerInstance.getObjectId();
  1463.  
  1464. if (Config.TVT_RESTORE_PLAYER_POS && TvTEvent.isStarted())
  1465. {
  1466. final Integer[] oldCoords =
  1467. {
  1468. _playerInstance.getX(),
  1469. _playerInstance.getY(),
  1470. _playerInstance.getZ()
  1471. };
  1472.  
  1473. _oldPlayerPos.put(objId, oldCoords);
  1474. }
  1475.  
  1476. if (Config.TVT_RESTORE_PLAYER_POS && !TvTEvent.isStarted())
  1477. {
  1478. Integer[] coor = _oldPlayerPos.get(objId);
  1479.  
  1480. if (coor != null)
  1481. {
  1482. _playerInstance.teleToLocation(coor[0], coor[1], coor[2], 0);
  1483. }
  1484. else
  1485. {
  1486. _playerInstance.teleToLocation( _coordinates[ 0 ] + Rnd.get(101)-50, _coordinates[ 1 ] + Rnd.get(101)-50, _coordinates[ 2 ], 0 );
  1487. }
  1488. }
  1489. else
  1490. _playerInstance.teleToLocation( _coordinates[ 0 ] + Rnd.get(101)-50, _coordinates[ 1 ] + Rnd.get(101)-50, _coordinates[ 2 ], 0 );
  1491.  
  1492. if (TvTEvent.isStarted())
  1493. {
  1494. _playerInstance.getAppearance().getNameColor();
  1495. }
  1496. else
  1497. {
  1498. _playerInstance.getAppearance().getNameColor();
  1499. }
  1500.  
  1501. if (_oldPlayerPos.containsKey(Integer.valueOf(objId)))
  1502. {
  1503. _oldPlayerPos.remove(Integer.valueOf(objId));
  1504. }
  1505.  
  1506. _playerInstance.setCurrentCp(_playerInstance.getMaxCp());
  1507. _playerInstance.setCurrentHp(_playerInstance.getMaxHp());
  1508. _playerInstance.setCurrentMp(_playerInstance.getMaxMp());
  1509.  
  1510. _playerInstance.broadcastStatusUpdate();
  1511. _playerInstance.broadcastTitleInfo();
  1512. _playerInstance.broadcastUserInfo();
  1513. }
  1514.  
  1515. /**
  1516. * Initializes the map where the player position
  1517. * will be stored
  1518. */
  1519. @SuppressWarnings("unused")
  1520. public static void initializeRestoreMap()
  1521. {
  1522. if (_oldPlayerPos == null)
  1523. _oldPlayerPos = new HashMap<Integer, Integer[]>();
  1524. }
  1525.  
  1526. /**
  1527. * Clear all containing data for a new event
  1528. */
  1529. public static void clearRestoreMap()
  1530. {
  1531. _oldPlayerPos.clear();
  1532. }
  1533. }
  1534. /*
  1535. * Copyright (C) 2004-2013 L2J Server
  1536. *
  1537. * This file is part of L2J Server.
  1538. *
  1539. * L2J Server is free software: you can redistribute it and/or modify
  1540. * it under the terms of the GNU General Public License as published by
  1541. * the Free Software Foundation, either version 3 of the License, or
  1542. * (at your option) any later version.
  1543. *
  1544. * L2J Server is distributed in the hope that it will be useful,
  1545. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1546. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1547. * General Public License for more details.
  1548. *
  1549. * You should have received a copy of the GNU General Public License
  1550. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1551. */
  1552. package custom.events;
  1553.  
  1554. import java.util.Calendar;
  1555. import java.util.concurrent.ScheduledFuture;
  1556. import java.util.logging.Logger;
  1557.  
  1558. import net.sf.l2j.Config;
  1559. import net.sf.l2j.gameserver.Announcements;
  1560. import net.sf.l2j.gameserver.ThreadPoolManager;
  1561.  
  1562. /**
  1563. * @author FBIagent
  1564. */
  1565. public class TvTManager
  1566. {
  1567. protected static final Logger _log = Logger.getLogger(TvTManager.class.getName());
  1568.  
  1569. /** Task for event cycles<br> */
  1570. private TvTStartTask _task;
  1571.  
  1572. /**
  1573. * New instance only by getInstance()<br>
  1574. */
  1575. protected TvTManager()
  1576. {
  1577. if (Config.TVT_EVENT_ENABLED)
  1578. {
  1579. TvTEvent.init();
  1580.  
  1581. scheduleEventStart();
  1582. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Started.");
  1583. }
  1584. else
  1585. {
  1586. _log.info("TvTEventEngine[TvTManager.TvTManager()]: Engine is disabled.");
  1587. }
  1588. }
  1589.  
  1590. /**
  1591. * Initialize new/Returns the one and only instance<br>
  1592. * <br>
  1593. * @return TvTManager<br>
  1594. */
  1595. public static TvTManager getInstance()
  1596. {
  1597. return SingletonHolder._instance;
  1598. }
  1599.  
  1600. /**
  1601. * Starts TvTStartTask
  1602. */
  1603. public void scheduleEventStart()
  1604. {
  1605. try
  1606. {
  1607. Calendar currentTime = Calendar.getInstance();
  1608. Calendar nextStartTime = null;
  1609. Calendar testStartTime = null;
  1610. for (String timeOfDay : Config.TVT_EVENT_INTERVAL)
  1611. {
  1612. // Creating a Calendar object from the specified interval value
  1613. testStartTime = Calendar.getInstance();
  1614. testStartTime.setLenient(true);
  1615. String[] splitTimeOfDay = timeOfDay.split(":");
  1616. testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
  1617. testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
  1618. // If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
  1619. if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
  1620. {
  1621. testStartTime.add(Calendar.DAY_OF_MONTH, 1);
  1622. }
  1623. // Check for the test date to be the minimum (smallest in the specified list)
  1624. if ((nextStartTime == null) || (testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis()))
  1625. {
  1626. nextStartTime = testStartTime;
  1627. }
  1628. }
  1629. if (nextStartTime != null)
  1630. {
  1631. _task = new TvTStartTask(nextStartTime.getTimeInMillis());
  1632. ThreadPoolManager.getInstance().executeTask(_task);
  1633. }
  1634. }
  1635. catch (Exception e)
  1636. {
  1637. _log.warning("TvTEventEngine[TvTManager.scheduleEventStart()]: Error figuring out a start time. Check TvTEventInterval in config file.");
  1638. }
  1639. }
  1640.  
  1641. /**
  1642. * Method to start participation
  1643. */
  1644. public void startReg()
  1645. {
  1646. if (!TvTEvent.startParticipation())
  1647. {
  1648. Announcements.EventAnnounceToAll("TvT Event:","Team vs Team Event: Event was cancelled.");
  1649. _log.warning("TvTEventEngine[TvTManager.run()]: Error spawning event npc for participation.");
  1650.  
  1651. scheduleEventStart();
  1652. }
  1653. else
  1654. {
  1655. Announcements.EventAnnounceToAll("TvT Event: ","Team vs Team Event: Registration opened for " + Config.TVT_EVENT_PARTICIPATION_TIME + " minute(s).");
  1656. // schedule registration end
  1657. _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_PARTICIPATION_TIME));
  1658. ThreadPoolManager.getInstance().executeTask(_task);
  1659. Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: Type .events to enter panel!");
  1660. }
  1661. }
  1662.  
  1663. /**
  1664. * Method to start the fight
  1665. */
  1666. public void startEvent()
  1667. {
  1668. if (!TvTEvent.startFight())
  1669. {
  1670. Announcements.EventAnnounceToAll("TvT Event","Team vs Team Event: Event cancelled due to lack of Participation.");
  1671. _log.info("TvTEventEngine[TvTManager.run()]: Lack of registration, abort event.");
  1672. scheduleEventStart();
  1673. }
  1674. else
  1675. {
  1676. _task.setStartTime(System.currentTimeMillis() + (60000L * Config.TVT_EVENT_RUNNING_TIME));
  1677. ThreadPoolManager.getInstance().executeTask(_task);
  1678. }
  1679. }
  1680.  
  1681. /**
  1682. * Method to end the event and reward
  1683. */
  1684. public void endEvent()
  1685. {
  1686. Announcements.EventAnnounceToAll("",TvTEvent.calculateRewards());
  1687. TvTEvent.sysMsgToAllParticipants("Team vs Team Event: Teleporting back to the MainTown " + Config.TVT_EVENT_START_LEAVE_TELEPORT_DELAY + " second(s).");
  1688. TvTEvent.stopFight();
  1689.  
  1690. scheduleEventStart();
  1691. }
  1692.  
  1693. public void skipDelay()
  1694. {
  1695. if (_task.nextRun.cancel(false))
  1696. {
  1697. _task.setStartTime(System.currentTimeMillis());
  1698. ThreadPoolManager.getInstance().executeTask(_task);
  1699. }
  1700. }
  1701.  
  1702. /**
  1703. * Class for TvT cycles
  1704. */
  1705. class TvTStartTask implements Runnable
  1706. {
  1707. private long _startTime;
  1708. public ScheduledFuture<?> nextRun;
  1709.  
  1710. public TvTStartTask(long startTime)
  1711. {
  1712. _startTime = startTime;
  1713. }
  1714.  
  1715. public void setStartTime(long startTime)
  1716. {
  1717. _startTime = startTime;
  1718. }
  1719.  
  1720. @Override
  1721. public void run()
  1722. {
  1723. int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
  1724.  
  1725. if (delay > 0)
  1726. {
  1727. announce(delay);
  1728. }
  1729.  
  1730. int nextMsg = 0;
  1731. if (delay > 3600)
  1732. {
  1733. nextMsg = delay - 3600;
  1734. }
  1735. else if (delay > 1800)
  1736. {
  1737. nextMsg = delay - 1800;
  1738. }
  1739. else if (delay > 900)
  1740. {
  1741. nextMsg = delay - 900;
  1742. }
  1743. else if (delay > 600)
  1744. {
  1745. nextMsg = delay - 600;
  1746. }
  1747. else if (delay > 300)
  1748. {
  1749. nextMsg = delay - 300;
  1750. }
  1751. else if (delay > 60)
  1752. {
  1753. nextMsg = delay - 60;
  1754. }
  1755. else if (delay > 5)
  1756. {
  1757. nextMsg = delay - 5;
  1758. }
  1759. else if (delay > 0)
  1760. {
  1761. nextMsg = delay;
  1762. }
  1763. else
  1764. {
  1765. // start
  1766. if (TvTEvent.isInactive())
  1767. {
  1768. startReg();
  1769. }
  1770. else if(TvTEvent.isParticipating())
  1771. {
  1772. startEvent();
  1773. }
  1774. else
  1775. {
  1776. endEvent();
  1777. }
  1778. }
  1779.  
  1780. if (delay > 0)
  1781. {
  1782. nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
  1783. }
  1784. }
  1785.  
  1786. private void announce(long time)
  1787. {
  1788. if ((time >= 3600) && ((time % 3600) == 0))
  1789. {
  1790. if (TvTEvent.isParticipating())
  1791. {
  1792. Announcements.EventAnnounceToAll("TvT Event","Team vs Team Event: " + (time / 60 / 60) + " hour(s) until registration is closed!");
  1793. }
  1794. else if (TvTEvent.isStarted())
  1795. {
  1796. TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
  1797. }
  1798. }
  1799. else if (time >= 60)
  1800. {
  1801. if (TvTEvent.isParticipating())
  1802. {
  1803. Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: " + (time / 60) + " minute(s) until registration is closed!");
  1804. }
  1805. else if (TvTEvent.isStarted())
  1806. {
  1807. TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + (time / 60) + " minute(s) until the event is finished!");
  1808. }
  1809. }
  1810. else
  1811. {
  1812. if (TvTEvent.isParticipating())
  1813. {
  1814. Announcements.EventAnnounceToAll("Team vs Team Event","Team vs Team Event: " + time + " second(s) until registration is closed!");
  1815. }
  1816. else if (TvTEvent.isStarted())
  1817. {
  1818. TvTEvent.sysMsgToAllParticipants("Team vs Team Event: " + time + " second(s) until the event is finished!");
  1819. }
  1820. }
  1821. }
  1822. }
  1823.  
  1824. private static class SingletonHolder
  1825. {
  1826. protected static final TvTManager _instance = new TvTManager();
  1827. }
  1828. }
Advertisement
Add Comment
Please, Sign In to add comment