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