for (AutoChatDefinition chatDef : chatDefinitions)
{
try
{
L2NpcInstance chatNpc = chatDef._npcInstance;
List<L2PcInstance> nearbyPlayers = new FastList<L2PcInstance>();
List<L2PcInstance> nearbyGMs = new FastList<L2PcInstance>();
for (L2Character player : chatNpc.getKnownList().getKnownCharactersInRadius(1500))
{
if (!(player instanceof L2PcInstance)) continue;
if (((L2PcInstance) player).isGM()) nearbyGMs.add((L2PcInstance) player);
else nearbyPlayers.add((L2PcInstance) player);
}
int maxIndex = chatDef.getChatTexts().length;
int lastIndex = Rnd.nextInt(maxIndex);
String creatureName = chatNpc.getName();
String text;
if (!chatDef.isRandomChat())
{
lastIndex = chatDef._chatIndex;
lastIndex++;
if (lastIndex == maxIndex) lastIndex = 0;
chatDef._chatIndex = lastIndex;
}
text = chatDef.getChatTexts()[lastIndex];
if (text == null) return;
if (!nearbyPlayers.isEmpty())
{
int randomPlayerIndex = Rnd.nextInt(nearbyPlayers.size());
L2PcInstance randomPlayer = nearbyPlayers.get(randomPlayerIndex);
final int winningCabal = SevenSigns.getInstance().getCabalHighestScore();
int losingCabal = SevenSigns.CABAL_NULL;
if (winningCabal == SevenSigns.CABAL_DAWN) losingCabal = SevenSigns.CABAL_DUSK;
else if (winningCabal == SevenSigns.CABAL_DUSK)
losingCabal = SevenSigns.CABAL_DAWN;
if (text.indexOf("%player_random%") > -1)
text = text.replaceAll("%player_random%", randomPlayer.getName());
if (text.indexOf("%player_cabal_winner%") > -1)
{
for (L2PcInstance nearbyPlayer : nearbyPlayers)
{
if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer) == winningCabal)
{
text = text.replaceAll("%player_cabal_winner%",
nearbyPlayer.getName());
break;
}
}
}
if (text.indexOf("%player_cabal_loser%") > -1)
{
for (L2PcInstance nearbyPlayer : nearbyPlayers)
{
if (SevenSigns.getInstance().getPlayerCabal(nearbyPlayer) == losingCabal)
{
text = text.replaceAll("%player_cabal_loser%",
nearbyPlayer.getName());
break;
}
}
}
}
if (text == null) return;
if (text.contains("%player_cabal_loser%")
|| text.contains("%player_cabal_winner%")
|| text.contains("%player_random%")) return;
CreatureSay cs = new CreatureSay(chatNpc.getObjectId(), 0, creatureName, text);
for (L2PcInstance nearbyPlayer : nearbyPlayers)
nearbyPlayer.sendPacket(cs);
for (L2PcInstance nearbyGM : nearbyGMs)
nearbyGM.sendPacket(cs);
if (Config.DEBUG)
_log.fine("AutoChatHandler: Chat propogation for object ID "
+ chatNpc.getObjectId() + " (" + creatureName + ") with text '" + text
+ "' sent to " + nearbyPlayers.size() + " nearby players.");
}
catch (Exception e)
{
e.printStackTrace();