AutoChatHandler.java 21 KB

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