AutoChatHandler.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.concurrent.ScheduledFuture;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.SevenSigns;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.instance.L2DefenderInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.network.clientpackets.Say2;
  35. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  36. import com.l2jserver.util.Rnd;
  37. /**
  38. * Auto Chat Handler
  39. *
  40. * Allows NPCs to automatically send messages to nearby players
  41. * at a set time interval.
  42. *
  43. * @author Tempy
  44. */
  45. public class AutoChatHandler implements SpawnListener
  46. {
  47. protected static final Logger _log = Logger.getLogger(AutoChatHandler.class.getName());
  48. private static final int DEFAULT_CHAT_DELAY = 60000; // 60 secs by default
  49. protected Map<Integer, AutoChatInstance> _registeredChats;
  50. protected AutoChatHandler()
  51. {
  52. _registeredChats = new FastMap<>();
  53. restoreChatData();
  54. L2Spawn.addSpawnListener(this);
  55. }
  56. private void restoreChatData()
  57. {
  58. int numLoaded = 0;
  59. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  60. {
  61. PreparedStatement statement = con.prepareStatement("SELECT * FROM auto_chat ORDER BY groupId ASC");
  62. ResultSet rs = statement.executeQuery();
  63. PreparedStatement statement2 = con.prepareStatement("SELECT * FROM auto_chat_text WHERE groupId=?");
  64. while (rs.next())
  65. {
  66. numLoaded++;
  67. statement2.setInt(1, rs.getInt("groupId"));
  68. try (ResultSet rs2 = statement2.executeQuery())
  69. {
  70. statement2.clearParameters();
  71. rs2.last();
  72. String[] chatTexts = new String[rs2.getRow()];
  73. int i = 0;
  74. rs2.beforeFirst();
  75. while (rs2.next())
  76. {
  77. chatTexts[i++] = rs2.getString("chatText");
  78. }
  79. registerGlobalChat(rs.getInt("npcId"), chatTexts, rs.getLong("chatDelay"));
  80. }
  81. }
  82. statement2.close();
  83. rs.close();
  84. statement.close();
  85. if (Config.DEBUG)
  86. _log.info("AutoChatHandler: Loaded " + numLoaded + " chat group(s) from the database.");
  87. }
  88. catch (Exception e)
  89. {
  90. _log.log(Level.WARNING, "AutoSpawnHandler: Could not restore chat data: " + e.getMessage(), e);
  91. }
  92. }
  93. public void reload()
  94. {
  95. // unregister all registered spawns
  96. for (AutoChatInstance aci : _registeredChats.values())
  97. {
  98. if (aci != null)
  99. {
  100. // clear timer
  101. if (aci._chatTask != null)
  102. aci._chatTask.cancel(true);
  103. this.removeChat(aci);
  104. }
  105. }
  106. // create clean list
  107. _registeredChats = new FastMap<>();
  108. // load
  109. restoreChatData();
  110. }
  111. public static AutoChatHandler getInstance()
  112. {
  113. return SingletonHolder._instance;
  114. }
  115. public int size()
  116. {
  117. return _registeredChats.size();
  118. }
  119. /**
  120. * Registers a globally active auto chat for ALL instances of the given NPC ID.
  121. * <BR>
  122. * Returns the associated auto chat instance.
  123. *
  124. * @param npcId
  125. * @param chatTexts
  126. * @param chatDelay (-1 = default delay)
  127. * @return AutoChatInstance chatInst
  128. */
  129. public AutoChatInstance registerGlobalChat(int npcId, String[] chatTexts, long chatDelay)
  130. {
  131. return registerChat(npcId, null, chatTexts, chatDelay);
  132. }
  133. /**
  134. * Registers a NON globally-active auto chat for the given NPC instance, and adds to the currently
  135. * assigned chat instance for this NPC ID, otherwise creates a new instance if
  136. * a previous one is not found.
  137. * <BR>
  138. * Returns the associated auto chat instance.
  139. *
  140. * @param npcInst
  141. * @param chatTexts
  142. * @param chatDelay (-1 = default delay)
  143. * @return AutoChatInstance chatInst
  144. */
  145. public AutoChatInstance registerChat(L2Npc npcInst, String[] chatTexts, long chatDelay)
  146. {
  147. return registerChat(npcInst.getNpcId(), npcInst, chatTexts, chatDelay);
  148. }
  149. private final AutoChatInstance registerChat(int npcId, L2Npc npcInst, String[] chatTexts, long chatDelay)
  150. {
  151. AutoChatInstance chatInst = null;
  152. if (chatDelay < 0)
  153. chatDelay = DEFAULT_CHAT_DELAY + Rnd.nextInt(DEFAULT_CHAT_DELAY);
  154. if (_registeredChats.containsKey(npcId))
  155. chatInst = _registeredChats.get(npcId);
  156. else
  157. chatInst = new AutoChatInstance(npcId, chatTexts, chatDelay, (npcInst == null));
  158. if (npcInst != null)
  159. chatInst.addChatDefinition(npcInst);
  160. _registeredChats.put(npcId, chatInst);
  161. return chatInst;
  162. }
  163. /**
  164. * Removes and cancels ALL auto chat definition for the given NPC ID,
  165. * and removes its chat instance if it exists.
  166. *
  167. * @param npcId
  168. * @return removedSuccessfully
  169. */
  170. public boolean removeChat(int npcId)
  171. {
  172. AutoChatInstance chatInst = _registeredChats.get(npcId);
  173. return removeChat(chatInst);
  174. }
  175. /**
  176. * Removes and cancels ALL auto chats for the given chat instance.
  177. *
  178. * @param chatInst
  179. * @return {@code true} if the chat is removed successfully.
  180. */
  181. public boolean removeChat(AutoChatInstance chatInst)
  182. {
  183. if (chatInst == null)
  184. return false;
  185. _registeredChats.remove(chatInst.getNPCId());
  186. chatInst.setActive(false);
  187. if (Config.DEBUG)
  188. _log.info("AutoChatHandler: Removed auto chat for NPC ID " + chatInst.getNPCId());
  189. return true;
  190. }
  191. /**
  192. * Returns the associated auto chat instance either by the given NPC ID
  193. * or object ID.
  194. *
  195. * @param id
  196. * @param byObjectId
  197. * @return AutoChatInstance chatInst
  198. */
  199. public AutoChatInstance getAutoChatInstance(int id, boolean byObjectId)
  200. {
  201. if (!byObjectId)
  202. return _registeredChats.get(id);
  203. for (AutoChatInstance chatInst : _registeredChats.values())
  204. if (chatInst.getChatDefinition(id) != null)
  205. return chatInst;
  206. return null;
  207. }
  208. /**
  209. * Sets the active state of all auto chat instances to that specified,
  210. * and cancels the scheduled chat task if necessary.
  211. *
  212. * @param isActive
  213. */
  214. public void setAutoChatActive(boolean isActive)
  215. {
  216. for (AutoChatInstance chatInst : _registeredChats.values())
  217. chatInst.setActive(isActive);
  218. }
  219. /**
  220. * Used in conjunction with a SpawnListener, this method is called every time
  221. * an NPC is spawned in the world.
  222. * <BR><BR>
  223. * If an auto chat instance is set to be "global", all instances matching the registered
  224. * NPC ID will be added to that chat instance.
  225. * @param npc
  226. */
  227. @Override
  228. public void npcSpawned(L2Npc npc)
  229. {
  230. synchronized (_registeredChats)
  231. {
  232. if (npc == null)
  233. return;
  234. int npcId = npc.getNpcId();
  235. if (_registeredChats.containsKey(npcId))
  236. {
  237. AutoChatInstance chatInst = _registeredChats.get(npcId);
  238. if (chatInst != null && chatInst.isGlobal())
  239. chatInst.addChatDefinition(npc);
  240. }
  241. }
  242. }
  243. /**
  244. * Auto Chat Instance
  245. * <BR><BR>
  246. * Manages the auto chat instances for a specific registered NPC ID.
  247. *
  248. * @author Tempy
  249. */
  250. public class AutoChatInstance
  251. {
  252. protected int _npcId;
  253. private long _defaultDelay = DEFAULT_CHAT_DELAY;
  254. private String[] _defaultTexts;
  255. private boolean _defaultRandom = false;
  256. private boolean _globalChat = false;
  257. private boolean _isActive;
  258. private Map<Integer, AutoChatDefinition> _chatDefinitions = new FastMap<>();
  259. protected ScheduledFuture<?> _chatTask;
  260. protected AutoChatInstance(int npcId, String[] chatTexts, long chatDelay, boolean isGlobal)
  261. {
  262. _defaultTexts = chatTexts;
  263. _npcId = npcId;
  264. _defaultDelay = chatDelay;
  265. _globalChat = isGlobal;
  266. if (Config.DEBUG)
  267. _log.info("AutoChatHandler: Registered auto chat for NPC ID " + _npcId + " (Global Chat = " + _globalChat + ").");
  268. setActive(true);
  269. }
  270. protected AutoChatDefinition getChatDefinition(int objectId)
  271. {
  272. return _chatDefinitions.get(objectId);
  273. }
  274. protected AutoChatDefinition[] getChatDefinitions()
  275. {
  276. return _chatDefinitions.values().toArray(new AutoChatDefinition[_chatDefinitions.values().size()]);
  277. }
  278. /**
  279. * Defines an auto chat for an instance matching this auto chat instance's registered NPC ID,
  280. * and launches the scheduled chat task.
  281. * <BR>
  282. * Returns the object ID for the NPC instance, with which to refer
  283. * to the created chat definition.
  284. * <BR>
  285. * <B>Note</B>: Uses pre-defined default values for texts and chat delays from the chat instance.
  286. * @param npcInst
  287. * @return
  288. */
  289. public int addChatDefinition(L2Npc npcInst)
  290. {
  291. return addChatDefinition(npcInst, null, 0);
  292. }
  293. /**
  294. * Defines an auto chat for an instance matching this auto chat instance's registered NPC ID,
  295. * and launches the scheduled chat task.
  296. * <BR>
  297. * Returns the object ID for the NPC instance, with which to refer
  298. * to the created chat definition.
  299. *
  300. * @param npcInst
  301. * @param chatTexts
  302. * @param chatDelay
  303. * @return objectId
  304. */
  305. public int addChatDefinition(L2Npc npcInst, String[] chatTexts, long chatDelay)
  306. {
  307. int objectId = npcInst.getObjectId();
  308. AutoChatDefinition chatDef = new AutoChatDefinition(this, npcInst, chatTexts, chatDelay);
  309. if (npcInst instanceof L2DefenderInstance)
  310. chatDef.setRandomChat(true);
  311. _chatDefinitions.put(objectId, chatDef);
  312. return objectId;
  313. }
  314. /**
  315. * Removes a chat definition specified by the given object ID.
  316. *
  317. * @param objectId
  318. * @return {@code true} if the chat is removed successfully.
  319. */
  320. public boolean removeChatDefinition(int objectId)
  321. {
  322. if (!_chatDefinitions.containsKey(objectId))
  323. return false;
  324. AutoChatDefinition chatDefinition = _chatDefinitions.get(objectId);
  325. chatDefinition.setActive(false);
  326. _chatDefinitions.remove(objectId);
  327. return true;
  328. }
  329. /**
  330. * Tests if this auto chat instance is active.
  331. *
  332. * @return boolean isActive
  333. */
  334. public boolean isActive()
  335. {
  336. return _isActive;
  337. }
  338. /**
  339. * Tests if this auto chat instance applies to
  340. * ALL currently spawned instances of the registered NPC ID.
  341. *
  342. * @return boolean isGlobal
  343. */
  344. public boolean isGlobal()
  345. {
  346. return _globalChat;
  347. }
  348. /**
  349. * Tests if random order is the DEFAULT for new chat definitions.
  350. *
  351. * @return boolean isRandom
  352. */
  353. public boolean isDefaultRandom()
  354. {
  355. return _defaultRandom;
  356. }
  357. /**
  358. * Tests if the auto chat definition given by its object ID is set to be random.
  359. * @param objectId
  360. * @return boolean isRandom
  361. */
  362. public boolean isRandomChat(int objectId)
  363. {
  364. if (!_chatDefinitions.containsKey(objectId))
  365. return false;
  366. return _chatDefinitions.get(objectId).isRandomChat();
  367. }
  368. /**
  369. * Returns the ID of the NPC type managed by this auto chat instance.
  370. *
  371. * @return int npcId
  372. */
  373. public int getNPCId()
  374. {
  375. return _npcId;
  376. }
  377. /**
  378. * Returns the number of auto chat definitions stored for this instance.
  379. *
  380. * @return int definitionCount
  381. */
  382. public int getDefinitionCount()
  383. {
  384. return _chatDefinitions.size();
  385. }
  386. /**
  387. * Returns a list of all NPC instances handled by this auto chat instance.
  388. *
  389. * @return L2NpcInstance[] npcInsts
  390. */
  391. public L2Npc[] getNPCInstanceList()
  392. {
  393. List<L2Npc> npcInsts = new FastList<>();
  394. for (AutoChatDefinition chatDefinition : _chatDefinitions.values())
  395. npcInsts.add(chatDefinition._npcInstance);
  396. return npcInsts.toArray(new L2Npc[npcInsts.size()]);
  397. }
  398. /**
  399. * A series of methods used to get and set default values for new chat definitions.
  400. * @return
  401. */
  402. public long getDefaultDelay()
  403. {
  404. return _defaultDelay;
  405. }
  406. public String[] getDefaultTexts()
  407. {
  408. return _defaultTexts;
  409. }
  410. public void setDefaultChatDelay(long delayValue)
  411. {
  412. _defaultDelay = delayValue;
  413. }
  414. public void setDefaultChatTexts(String[] textsValue)
  415. {
  416. _defaultTexts = textsValue;
  417. }
  418. public void setDefaultRandom(boolean randValue)
  419. {
  420. _defaultRandom = randValue;
  421. }
  422. /**
  423. * Sets a specific chat delay for the specified auto chat definition given by its object ID.
  424. *
  425. * @param objectId
  426. * @param delayValue
  427. */
  428. public void setChatDelay(int objectId, long delayValue)
  429. {
  430. AutoChatDefinition chatDef = getChatDefinition(objectId);
  431. if (chatDef != null)
  432. chatDef.setChatDelay(delayValue);
  433. }
  434. /**
  435. * Sets a specific set of chat texts for the specified auto chat definition given by its object ID.
  436. *
  437. * @param objectId
  438. * @param textsValue
  439. */
  440. public void setChatTexts(int objectId, String[] textsValue)
  441. {
  442. AutoChatDefinition chatDef = getChatDefinition(objectId);
  443. if (chatDef != null)
  444. chatDef.setChatTexts(textsValue);
  445. }
  446. /**
  447. * Sets specifically to use random chat order for the auto chat definition given by its object ID.
  448. *
  449. * @param objectId
  450. * @param randValue
  451. */
  452. public void setRandomChat(int objectId, boolean randValue)
  453. {
  454. AutoChatDefinition chatDef = getChatDefinition(objectId);
  455. if (chatDef != null)
  456. chatDef.setRandomChat(randValue);
  457. }
  458. /**
  459. * Sets the activity of ALL auto chat definitions handled by this chat instance.
  460. *
  461. * @param activeValue
  462. */
  463. public void setActive(boolean activeValue)
  464. {
  465. if (_isActive == activeValue)
  466. return;
  467. _isActive = activeValue;
  468. if (!isGlobal())
  469. {
  470. for (AutoChatDefinition chatDefinition : _chatDefinitions.values())
  471. chatDefinition.setActive(activeValue);
  472. return;
  473. }
  474. if (isActive())
  475. {
  476. AutoChatRunner acr = new AutoChatRunner(_npcId, -1);
  477. _chatTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(acr, _defaultDelay, _defaultDelay);
  478. }
  479. else
  480. {
  481. _chatTask.cancel(false);
  482. }
  483. }
  484. /**
  485. * Auto Chat Definition
  486. * <BR><BR>
  487. * Stores information about specific chat data for an instance of the NPC ID
  488. * specified by the containing auto chat instance.
  489. * <BR>
  490. * Each NPC instance of this type should be stored in a subsequent AutoChatDefinition class.
  491. *
  492. * @author Tempy
  493. */
  494. private class AutoChatDefinition
  495. {
  496. protected int _chatIndex = 0;
  497. protected L2Npc _npcInstance;
  498. protected AutoChatInstance _chatInstance;
  499. private long _chatDelay = 0;
  500. private String[] _chatTexts = null;
  501. private boolean _isActiveDefinition;
  502. private boolean _randomChat;
  503. protected AutoChatDefinition(AutoChatInstance chatInst, L2Npc npcInst, String[] chatTexts, long chatDelay)
  504. {
  505. _npcInstance = npcInst;
  506. _chatInstance = chatInst;
  507. _randomChat = chatInst.isDefaultRandom();
  508. _chatDelay = chatDelay;
  509. _chatTexts = chatTexts;
  510. if (Config.DEBUG)
  511. _log.info("AutoChatHandler: Chat definition added for NPC ID " + _npcInstance.getNpcId() + " (Object ID = "
  512. + _npcInstance.getObjectId() + ").");
  513. // If global chat isn't enabled for the parent instance,
  514. // then handle the chat task locally.
  515. if (!chatInst.isGlobal())
  516. setActive(true);
  517. }
  518. protected String[] getChatTexts()
  519. {
  520. if (_chatTexts != null)
  521. return _chatTexts;
  522. return _chatInstance.getDefaultTexts();
  523. }
  524. private long getChatDelay()
  525. {
  526. if (_chatDelay > 0)
  527. return _chatDelay;
  528. return _chatInstance.getDefaultDelay();
  529. }
  530. private boolean isActive()
  531. {
  532. return _isActiveDefinition;
  533. }
  534. boolean isRandomChat()
  535. {
  536. return _randomChat;
  537. }
  538. void setRandomChat(boolean randValue)
  539. {
  540. _randomChat = randValue;
  541. }
  542. void setChatDelay(long delayValue)
  543. {
  544. _chatDelay = delayValue;
  545. }
  546. void setChatTexts(String[] textsValue)
  547. {
  548. _chatTexts = textsValue;
  549. }
  550. void setActive(boolean activeValue)
  551. {
  552. if (isActive() == activeValue)
  553. return;
  554. if (activeValue)
  555. {
  556. AutoChatRunner acr = new AutoChatRunner(_npcId, _npcInstance.getObjectId());
  557. if (getChatDelay() == 0)
  558. // Schedule it set to 5Ms, isn't error, if use 0 sometime
  559. // chatDefinition return null in AutoChatRunner
  560. _chatTask = ThreadPoolManager.getInstance().scheduleGeneral(acr, 5);
  561. else
  562. _chatTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(acr, getChatDelay(), getChatDelay());
  563. }
  564. else
  565. {
  566. _chatTask.cancel(false);
  567. }
  568. _isActiveDefinition = activeValue;
  569. }
  570. }
  571. /**
  572. * Auto Chat Runner
  573. * <BR><BR>
  574. * Represents the auto chat scheduled task for each chat instance.
  575. *
  576. * @author Tempy
  577. */
  578. private class AutoChatRunner implements Runnable
  579. {
  580. private int _runnerNpcId;
  581. private int _objectId;
  582. protected AutoChatRunner(int pNpcId, int pObjectId)
  583. {
  584. _runnerNpcId = pNpcId;
  585. _objectId = pObjectId;
  586. }
  587. @Override
  588. public synchronized void run()
  589. {
  590. AutoChatInstance chatInst = _registeredChats.get(_runnerNpcId);
  591. AutoChatDefinition[] chatDefinitions;
  592. if (chatInst.isGlobal())
  593. {
  594. chatDefinitions = chatInst.getChatDefinitions();
  595. }
  596. else
  597. {
  598. AutoChatDefinition chatDef = chatInst.getChatDefinition(_objectId);
  599. if (chatDef == null)
  600. {
  601. _log.warning("AutoChatHandler: Auto chat definition is NULL for NPC ID " + _npcId + ".");
  602. return;
  603. }
  604. chatDefinitions = new AutoChatDefinition[] { chatDef };
  605. }
  606. if (Config.DEBUG)
  607. _log.info("AutoChatHandler: Running auto chat for " + chatDefinitions.length + " instances of NPC ID " + _npcId + "."
  608. + " (Global Chat = " + chatInst.isGlobal() + ")");
  609. for (AutoChatDefinition chatDef : chatDefinitions)
  610. {
  611. try
  612. {
  613. L2Npc chatNpc = chatDef._npcInstance;
  614. List<L2PcInstance> nearbyPlayers = new FastList<>();
  615. List<L2PcInstance> nearbyGMs = new FastList<>();
  616. for (L2Character player : chatNpc.getKnownList().getKnownCharactersInRadius(1500))
  617. {
  618. if (!(player instanceof L2PcInstance))
  619. continue;
  620. if (((L2PcInstance) player).isGM())
  621. nearbyGMs.add((L2PcInstance) player);
  622. else
  623. nearbyPlayers.add((L2PcInstance) player);
  624. }
  625. int maxIndex = chatDef.getChatTexts().length;
  626. int lastIndex = Rnd.nextInt(maxIndex);
  627. String creatureName = chatNpc.getName();
  628. String text;
  629. if (!chatDef.isRandomChat())
  630. {
  631. lastIndex = chatDef._chatIndex + 1;
  632. if (lastIndex == maxIndex)
  633. lastIndex = 0;
  634. chatDef._chatIndex = lastIndex;
  635. }
  636. text = chatDef.getChatTexts()[lastIndex];
  637. if (text == null)
  638. return;
  639. if (!nearbyPlayers.isEmpty())
  640. {
  641. int randomPlayerIndex = Rnd.nextInt(nearbyPlayers.size());
  642. L2PcInstance randomPlayer = nearbyPlayers.get(randomPlayerIndex);
  643. final int winningCabal = SevenSigns.getInstance().getCabalHighestScore();
  644. int losingCabal = SevenSigns.CABAL_NULL;
  645. if (winningCabal == SevenSigns.CABAL_DAWN)
  646. losingCabal = SevenSigns.CABAL_DUSK;
  647. else if (winningCabal == SevenSigns.CABAL_DUSK)
  648. losingCabal = SevenSigns.CABAL_DAWN;
  649. if (text.indexOf("%player_random%") > -1)
  650. text = text.replaceAll("%player_random%", randomPlayer.getName());
  651. if (text.indexOf("%player_cabal_winner%") > -1)
  652. {
  653. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  654. {
  655. if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer.getObjectId()) == winningCabal)
  656. {
  657. text = text.replaceAll("%player_cabal_winner%", nearbyPlayer.getName());
  658. break;
  659. }
  660. }
  661. }
  662. if (text.indexOf("%player_cabal_loser%") > -1)
  663. {
  664. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  665. {
  666. if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer.getObjectId()) == losingCabal)
  667. {
  668. text = text.replaceAll("%player_cabal_loser%", nearbyPlayer.getName());
  669. break;
  670. }
  671. }
  672. }
  673. }
  674. if (text == null)
  675. return;
  676. if (!text.contains("%player_"))
  677. {
  678. CreatureSay cs = new CreatureSay(chatNpc.getObjectId(), Say2.ALL, creatureName, text);
  679. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  680. nearbyPlayer.sendPacket(cs);
  681. for (L2PcInstance nearbyGM : nearbyGMs)
  682. nearbyGM.sendPacket(cs);
  683. }
  684. if (Config.DEBUG)
  685. _log.fine("AutoChatHandler: Chat propogation for object ID " + chatNpc.getObjectId() + " (" + creatureName
  686. + ") with text '" + text + "' sent to " + nearbyPlayers.size() + " nearby players.");
  687. }
  688. catch (Exception e)
  689. {
  690. _log.log(Level.WARNING, "Exception on AutoChatRunner.run(): " + e.getMessage(), e);
  691. return;
  692. }
  693. }
  694. }
  695. }
  696. }
  697. private static class SingletonHolder
  698. {
  699. protected static final AutoChatHandler _instance = new AutoChatHandler();
  700. }
  701. }