AutoChatHandler.java 22 KB

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