AutoChatHandler.java 22 KB

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