AutoChatHandler.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. private AutoChatHandler()
  51. {
  52. _registeredChats = new FastMap<Integer, AutoChatInstance>();
  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<Integer, AutoChatInstance>();
  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 int npcId
  129. * @param String[] chatTexts
  130. * @param int 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 L2Npc npcInst
  145. * @param String[] chatTexts
  146. * @param int 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 int npcId
  172. * @return boolean 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 AutoChatInstance chatInst
  183. * @return boolean removedSuccessfully
  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 int id
  200. * @param boolean 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 boolean 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. */
  230. public void npcSpawned(L2Npc npc)
  231. {
  232. synchronized (_registeredChats)
  233. {
  234. if (npc == null)
  235. return;
  236. int npcId = npc.getNpcId();
  237. if (_registeredChats.containsKey(npcId))
  238. {
  239. AutoChatInstance chatInst = _registeredChats.get(npcId);
  240. if (chatInst != null && chatInst.isGlobal())
  241. chatInst.addChatDefinition(npc);
  242. }
  243. }
  244. }
  245. /**
  246. * Auto Chat Instance
  247. * <BR><BR>
  248. * Manages the auto chat instances for a specific registered NPC ID.
  249. *
  250. * @author Tempy
  251. */
  252. public class AutoChatInstance
  253. {
  254. protected int _npcId;
  255. private long _defaultDelay = DEFAULT_CHAT_DELAY;
  256. private String[] _defaultTexts;
  257. private boolean _defaultRandom = false;
  258. private boolean _globalChat = false;
  259. private boolean _isActive;
  260. private Map<Integer, AutoChatDefinition> _chatDefinitions = new FastMap<Integer, AutoChatDefinition>();
  261. protected ScheduledFuture<?> _chatTask;
  262. protected AutoChatInstance(int npcId, String[] chatTexts, long chatDelay, boolean isGlobal)
  263. {
  264. _defaultTexts = chatTexts;
  265. _npcId = npcId;
  266. _defaultDelay = chatDelay;
  267. _globalChat = isGlobal;
  268. if (Config.DEBUG)
  269. _log.info("AutoChatHandler: Registered auto chat for NPC ID " + _npcId + " (Global Chat = " + _globalChat + ").");
  270. setActive(true);
  271. }
  272. protected AutoChatDefinition getChatDefinition(int objectId)
  273. {
  274. return _chatDefinitions.get(objectId);
  275. }
  276. protected AutoChatDefinition[] getChatDefinitions()
  277. {
  278. return _chatDefinitions.values().toArray(new AutoChatDefinition[_chatDefinitions.values().size()]);
  279. }
  280. /**
  281. * Defines an auto chat for an instance matching this auto chat instance's registered NPC ID,
  282. * and launches the scheduled chat task.
  283. * <BR>
  284. * Returns the object ID for the NPC instance, with which to refer
  285. * to the created chat definition.
  286. * <BR>
  287. * <B>Note</B>: Uses pre-defined default values for texts and chat delays from the chat instance.
  288. *
  289. * @param L2Npc npcInst
  290. * @return int objectId
  291. */
  292. public int addChatDefinition(L2Npc npcInst)
  293. {
  294. return addChatDefinition(npcInst, null, 0);
  295. }
  296. /**
  297. * Defines an auto chat for an instance matching this auto chat instance's registered NPC ID,
  298. * and launches the scheduled chat task.
  299. * <BR>
  300. * Returns the object ID for the NPC instance, with which to refer
  301. * to the created chat definition.
  302. *
  303. * @param L2Npc npcInst
  304. * @param String[] chatTexts
  305. * @param int chatDelay
  306. * @return int objectId
  307. */
  308. public int addChatDefinition(L2Npc npcInst, String[] chatTexts, long chatDelay)
  309. {
  310. int objectId = npcInst.getObjectId();
  311. AutoChatDefinition chatDef = new AutoChatDefinition(this, npcInst, chatTexts, chatDelay);
  312. if (npcInst instanceof L2DefenderInstance)
  313. chatDef.setRandomChat(true);
  314. _chatDefinitions.put(objectId, chatDef);
  315. return objectId;
  316. }
  317. /**
  318. * Removes a chat definition specified by the given object ID.
  319. *
  320. * @param int objectId
  321. * @return boolean removedSuccessfully
  322. */
  323. public boolean removeChatDefinition(int objectId)
  324. {
  325. if (!_chatDefinitions.containsKey(objectId))
  326. return false;
  327. AutoChatDefinition chatDefinition = _chatDefinitions.get(objectId);
  328. chatDefinition.setActive(false);
  329. _chatDefinitions.remove(objectId);
  330. return true;
  331. }
  332. /**
  333. * Tests if this auto chat instance is active.
  334. *
  335. * @return boolean isActive
  336. */
  337. public boolean isActive()
  338. {
  339. return _isActive;
  340. }
  341. /**
  342. * Tests if this auto chat instance applies to
  343. * ALL currently spawned instances of the registered NPC ID.
  344. *
  345. * @return boolean isGlobal
  346. */
  347. public boolean isGlobal()
  348. {
  349. return _globalChat;
  350. }
  351. /**
  352. * Tests if random order is the DEFAULT for new chat definitions.
  353. *
  354. * @return boolean isRandom
  355. */
  356. public boolean isDefaultRandom()
  357. {
  358. return _defaultRandom;
  359. }
  360. /**
  361. * Tests if the auto chat definition given by its object ID is set to be random.
  362. *
  363. * @return boolean isRandom
  364. */
  365. public boolean isRandomChat(int objectId)
  366. {
  367. if (!_chatDefinitions.containsKey(objectId))
  368. return false;
  369. return _chatDefinitions.get(objectId).isRandomChat();
  370. }
  371. /**
  372. * Returns the ID of the NPC type managed by this auto chat instance.
  373. *
  374. * @return int npcId
  375. */
  376. public int getNPCId()
  377. {
  378. return _npcId;
  379. }
  380. /**
  381. * Returns the number of auto chat definitions stored for this instance.
  382. *
  383. * @return int definitionCount
  384. */
  385. public int getDefinitionCount()
  386. {
  387. return _chatDefinitions.size();
  388. }
  389. /**
  390. * Returns a list of all NPC instances handled by this auto chat instance.
  391. *
  392. * @return L2NpcInstance[] npcInsts
  393. */
  394. public L2Npc[] getNPCInstanceList()
  395. {
  396. List<L2Npc> npcInsts = new FastList<L2Npc>();
  397. for (AutoChatDefinition chatDefinition : _chatDefinitions.values())
  398. npcInsts.add(chatDefinition._npcInstance);
  399. return npcInsts.toArray(new L2Npc[npcInsts.size()]);
  400. }
  401. /**
  402. * A series of methods used to get and set default values for new chat definitions.
  403. */
  404. public long getDefaultDelay()
  405. {
  406. return _defaultDelay;
  407. }
  408. public String[] getDefaultTexts()
  409. {
  410. return _defaultTexts;
  411. }
  412. public void setDefaultChatDelay(long delayValue)
  413. {
  414. _defaultDelay = delayValue;
  415. }
  416. public void setDefaultChatTexts(String[] textsValue)
  417. {
  418. _defaultTexts = textsValue;
  419. }
  420. public void setDefaultRandom(boolean randValue)
  421. {
  422. _defaultRandom = randValue;
  423. }
  424. /**
  425. * Sets a specific chat delay for the specified auto chat definition given by its object ID.
  426. *
  427. * @param int objectId
  428. * @param long delayValue
  429. */
  430. public void setChatDelay(int objectId, long delayValue)
  431. {
  432. AutoChatDefinition chatDef = getChatDefinition(objectId);
  433. if (chatDef != null)
  434. chatDef.setChatDelay(delayValue);
  435. }
  436. /**
  437. * Sets a specific set of chat texts for the specified auto chat definition given by its object ID.
  438. *
  439. * @param int objectId
  440. * @param String[] textsValue
  441. */
  442. public void setChatTexts(int objectId, String[] textsValue)
  443. {
  444. AutoChatDefinition chatDef = getChatDefinition(objectId);
  445. if (chatDef != null)
  446. chatDef.setChatTexts(textsValue);
  447. }
  448. /**
  449. * Sets specifically to use random chat order for the auto chat definition given by its object ID.
  450. *
  451. * @param int objectId
  452. * @param boolean randValue
  453. */
  454. public void setRandomChat(int objectId, boolean randValue)
  455. {
  456. AutoChatDefinition chatDef = getChatDefinition(objectId);
  457. if (chatDef != null)
  458. chatDef.setRandomChat(randValue);
  459. }
  460. /**
  461. * Sets the activity of ALL auto chat definitions handled by this chat instance.
  462. *
  463. * @param boolean isActive
  464. */
  465. public void setActive(boolean activeValue)
  466. {
  467. if (_isActive == activeValue)
  468. return;
  469. _isActive = activeValue;
  470. if (!isGlobal())
  471. {
  472. for (AutoChatDefinition chatDefinition : _chatDefinitions.values())
  473. chatDefinition.setActive(activeValue);
  474. return;
  475. }
  476. if (isActive())
  477. {
  478. AutoChatRunner acr = new AutoChatRunner(_npcId, -1);
  479. _chatTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(acr, _defaultDelay, _defaultDelay);
  480. }
  481. else
  482. {
  483. _chatTask.cancel(false);
  484. }
  485. }
  486. /**
  487. * Auto Chat Definition
  488. * <BR><BR>
  489. * Stores information about specific chat data for an instance of the NPC ID
  490. * specified by the containing auto chat instance.
  491. * <BR>
  492. * Each NPC instance of this type should be stored in a subsequent AutoChatDefinition class.
  493. *
  494. * @author Tempy
  495. */
  496. private class AutoChatDefinition
  497. {
  498. protected int _chatIndex = 0;
  499. protected L2Npc _npcInstance;
  500. protected AutoChatInstance _chatInstance;
  501. private long _chatDelay = 0;
  502. private String[] _chatTexts = null;
  503. private boolean _isActiveDefinition;
  504. private boolean _randomChat;
  505. protected AutoChatDefinition(AutoChatInstance chatInst, L2Npc npcInst, String[] chatTexts, long chatDelay)
  506. {
  507. _npcInstance = npcInst;
  508. _chatInstance = chatInst;
  509. _randomChat = chatInst.isDefaultRandom();
  510. _chatDelay = chatDelay;
  511. _chatTexts = chatTexts;
  512. if (Config.DEBUG)
  513. _log.info("AutoChatHandler: Chat definition added for NPC ID " + _npcInstance.getNpcId() + " (Object ID = "
  514. + _npcInstance.getObjectId() + ").");
  515. // If global chat isn't enabled for the parent instance,
  516. // then handle the chat task locally.
  517. if (!chatInst.isGlobal())
  518. setActive(true);
  519. }
  520. protected String[] getChatTexts()
  521. {
  522. if (_chatTexts != null)
  523. return _chatTexts;
  524. return _chatInstance.getDefaultTexts();
  525. }
  526. private long getChatDelay()
  527. {
  528. if (_chatDelay > 0)
  529. return _chatDelay;
  530. return _chatInstance.getDefaultDelay();
  531. }
  532. private boolean isActive()
  533. {
  534. return _isActiveDefinition;
  535. }
  536. boolean isRandomChat()
  537. {
  538. return _randomChat;
  539. }
  540. void setRandomChat(boolean randValue)
  541. {
  542. _randomChat = randValue;
  543. }
  544. void setChatDelay(long delayValue)
  545. {
  546. _chatDelay = delayValue;
  547. }
  548. void setChatTexts(String[] textsValue)
  549. {
  550. _chatTexts = textsValue;
  551. }
  552. void setActive(boolean activeValue)
  553. {
  554. if (isActive() == activeValue)
  555. return;
  556. if (activeValue)
  557. {
  558. AutoChatRunner acr = new AutoChatRunner(_npcId, _npcInstance.getObjectId());
  559. if (getChatDelay() == 0)
  560. // Schedule it set to 5Ms, isn't error, if use 0 sometine
  561. // chatDefinition return null in AutoChatRunner
  562. _chatTask = ThreadPoolManager.getInstance().scheduleGeneral(acr, 5);
  563. else
  564. _chatTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(acr, getChatDelay(), getChatDelay());
  565. }
  566. else
  567. {
  568. _chatTask.cancel(false);
  569. }
  570. _isActiveDefinition = activeValue;
  571. }
  572. }
  573. /**
  574. * Auto Chat Runner
  575. * <BR><BR>
  576. * Represents the auto chat scheduled task for each chat instance.
  577. *
  578. * @author Tempy
  579. */
  580. private class AutoChatRunner implements Runnable
  581. {
  582. private int _runnerNpcId;
  583. private int _objectId;
  584. protected AutoChatRunner(int pNpcId, int pObjectId)
  585. {
  586. _runnerNpcId = pNpcId;
  587. _objectId = pObjectId;
  588. }
  589. public synchronized void run()
  590. {
  591. AutoChatInstance chatInst = _registeredChats.get(_runnerNpcId);
  592. AutoChatDefinition[] chatDefinitions;
  593. if (chatInst.isGlobal())
  594. {
  595. chatDefinitions = chatInst.getChatDefinitions();
  596. }
  597. else
  598. {
  599. AutoChatDefinition chatDef = chatInst.getChatDefinition(_objectId);
  600. if (chatDef == null)
  601. {
  602. _log.warning("AutoChatHandler: Auto chat definition is NULL for NPC ID " + _npcId + ".");
  603. return;
  604. }
  605. chatDefinitions = new AutoChatDefinition[] { chatDef };
  606. }
  607. if (Config.DEBUG)
  608. _log.info("AutoChatHandler: Running auto chat for " + chatDefinitions.length + " instances of NPC ID " + _npcId + "."
  609. + " (Global Chat = " + chatInst.isGlobal() + ")");
  610. for (AutoChatDefinition chatDef : chatDefinitions)
  611. {
  612. try
  613. {
  614. L2Npc chatNpc = chatDef._npcInstance;
  615. List<L2PcInstance> nearbyPlayers = new FastList<L2PcInstance>();
  616. List<L2PcInstance> nearbyGMs = new FastList<L2PcInstance>();
  617. for (L2Character player : chatNpc.getKnownList().getKnownCharactersInRadius(1500))
  618. {
  619. if (!(player instanceof L2PcInstance))
  620. continue;
  621. if (((L2PcInstance) player).isGM())
  622. nearbyGMs.add((L2PcInstance) player);
  623. else
  624. nearbyPlayers.add((L2PcInstance) player);
  625. }
  626. int maxIndex = chatDef.getChatTexts().length;
  627. int lastIndex = Rnd.nextInt(maxIndex);
  628. String creatureName = chatNpc.getName();
  629. String text;
  630. if (!chatDef.isRandomChat())
  631. {
  632. lastIndex = chatDef._chatIndex + 1;
  633. if (lastIndex == maxIndex)
  634. lastIndex = 0;
  635. chatDef._chatIndex = lastIndex;
  636. }
  637. text = chatDef.getChatTexts()[lastIndex];
  638. if (text == null)
  639. return;
  640. if (!nearbyPlayers.isEmpty())
  641. {
  642. int randomPlayerIndex = Rnd.nextInt(nearbyPlayers.size());
  643. L2PcInstance randomPlayer = nearbyPlayers.get(randomPlayerIndex);
  644. final int winningCabal = SevenSigns.getInstance().getCabalHighestScore();
  645. int losingCabal = SevenSigns.CABAL_NULL;
  646. if (winningCabal == SevenSigns.CABAL_DAWN)
  647. losingCabal = SevenSigns.CABAL_DUSK;
  648. else if (winningCabal == SevenSigns.CABAL_DUSK)
  649. losingCabal = SevenSigns.CABAL_DAWN;
  650. if (text.indexOf("%player_random%") > -1)
  651. text = text.replaceAll("%player_random%", randomPlayer.getName());
  652. if (text.indexOf("%player_cabal_winner%") > -1)
  653. {
  654. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  655. {
  656. if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer.getObjectId()) == winningCabal)
  657. {
  658. text = text.replaceAll("%player_cabal_winner%", nearbyPlayer.getName());
  659. break;
  660. }
  661. }
  662. }
  663. if (text.indexOf("%player_cabal_loser%") > -1)
  664. {
  665. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  666. {
  667. if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer.getObjectId()) == losingCabal)
  668. {
  669. text = text.replaceAll("%player_cabal_loser%", nearbyPlayer.getName());
  670. break;
  671. }
  672. }
  673. }
  674. }
  675. if (text == null)
  676. return;
  677. if (!text.contains("%player_"))
  678. {
  679. CreatureSay cs = new CreatureSay(chatNpc.getObjectId(), Say2.ALL, creatureName, text);
  680. for (L2PcInstance nearbyPlayer : nearbyPlayers)
  681. nearbyPlayer.sendPacket(cs);
  682. for (L2PcInstance nearbyGM : nearbyGMs)
  683. nearbyGM.sendPacket(cs);
  684. }
  685. if (Config.DEBUG)
  686. _log.fine("AutoChatHandler: Chat propogation for object ID " + chatNpc.getObjectId() + " (" + creatureName
  687. + ") with text '" + text + "' sent to " + nearbyPlayers.size() + " nearby players.");
  688. }
  689. catch (Exception e)
  690. {
  691. _log.log(Level.WARNING, "Exception on AutoChatRunner.run(): " + e.getMessage(), e);
  692. return;
  693. }
  694. }
  695. }
  696. }
  697. }
  698. @SuppressWarnings("synthetic-access")
  699. private static class SingletonHolder
  700. {
  701. protected static final AutoChatHandler _instance = new AutoChatHandler();
  702. }
  703. }