AutoChatHandler.java 21 KB

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