AutoChatHandler.java 27 KB

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