AutoChatHandler.java 22 KB

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