07-15-2014, 09:28 PM
Не проверял, но на внешний вид рабочий код.
Проверьте правильность импортов, т.к. исходники ацис не знаю какой ревы.
В папке data теперь должно быть 6 файлов анонсов: один общий, а остальные для каждой расы.
Как-то так
Код:
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.l2j.gameserver;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.base.Race;
import net.sf.l2j.gameserver.network.clientpackets.Say2;
import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
import net.sf.l2j.gameserver.util.Broadcast;
public class Announcements
{
private static Logger _log = Logger.getLogger(Announcements.class.getName());
private static Announcements _instance;
private final String[] categories = {"all", "human", "elf", "darkelf", "orc", "dwarf"};
private final Map<String, List<String>> _announcements = new HashMap<>();
private Announcements()
{
loadAnnouncements();
}
public static Announcements getInstance()
{
if (_instance == null)
_instance = new Announcements();
return _instance;
}
public void loadAnnouncements()
{
_announcements.clear();
for (String cat : categories)
{
File file = new File("./data/announcements-" + cat + ".txt");
if (file.exists())
readFromDisk(file, cat);
else
_log.config("The announcements file (normally located to 'data/announcements-" + cat + ".txt') doesn't exist.");
}
}
public void showAnnouncements(L2PcInstance activeChar)
{
String category;
if (activeChar.isHero())
category = getRace(activeChar.getRace());
else
category = "all";
List<String> announce = _announcements.get(category);
if (announce != null)
{
for (int i = 0; i < announce.size(); i++)
{
CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, activeChar.getName(), announce.get(i));
activeChar.sendPacket(cs);
}
}
}
private void readFromDisk(File file, String category)
{
try (LineNumberReader lnr = new LineNumberReader(new FileReader(file)))
{
int i = 0;
String line = null;
while ((line = lnr.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line, "\n\r");
if (st.hasMoreTokens())
{
String announcement = st.nextToken();
if (!_announcements.containsKey(category))
_announcements.put(category, new ArrayList<String>());
_announcements.get(category).add(announcement);
i++;
}
}
_log.config("Announcements: Loaded " + i + " announcements for " + category + " category.");
}
catch (IOException e1)
{
_log.log(Level.SEVERE, "Error reading announcements", e1);
}
}
private static String getRace(Race race)
{
switch (race)
{
case Human:
return "human";
case Elf:
return "elf";
case DarkElf:
return "darkelf";
case Orc:
return "orc";
case Dwarf:
return "dwarf";
default:
return "all";
}
}
public static void announceToAll(String text)
{
Broadcast.announceToOnlinePlayers(text);
}
public static void announceToAll(SystemMessage sm)
{
Broadcast.toAllOnlinePlayers(sm);
}
// Method for handling announcements from admin
public static void handleAnnounce(String command, int lengthToTrim)
{
try
{
// Announce string to everyone on server
announceToAll(command.substring(lengthToTrim));
}
catch (StringIndexOutOfBoundsException e)
{
}
}
}
В папке data теперь должно быть 6 файлов анонсов: один общий, а остальные для каждой расы.
Родился, живу и когда-нибудь умру.