Russian 49 Опубликовано 5 августа, 2013 Вот и вторая шара. Реализация баша в кб Config.java public static String NAME_SERVER; NAME_SERVER = properties.getProperty("CommunityNameServer", "Russian"); package services.community.custom; import org.mmocore.gameserver.Config; import org.mmocore.gameserver.data.htm.HtmCache; import org.mmocore.gameserver.handler.bbs.CommunityBoardManager; import org.mmocore.gameserver.handler.bbs.ICommunityBoardHandler; import org.mmocore.gameserver.model.Player; import org.mmocore.gameserver.model.instances.NpcInstance; import org.mmocore.gameserver.scripts.ScriptFile; import org.mmocore.gameserver.utils.Files; import org.mmocore.gameserver.serverpackets.ShowBoard; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import javax.xml.parsers.DocumentBuilderFactory; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; import java.util.List; /** * @author Russian * * Реализовано: * - вывод страниц с баша и навигация */ public class CommunityBash implements ScriptFile, ICommunityBoardHandler { private static final Logger _log = LoggerFactory.getLogger(CommunityBash.class); private static String xmlData = Config.DATAPACK_ROOT + "/data/bash.xml"; private static List<String> quotes = new ArrayList<String>(); @Override public String[] getBypassCommands() { return new String[]{ "_bbsbash", }; } @Override public void onBypassCommand(Player player, String bypass) { if (bypass.equals("_bbsbash")) { showBash(player); } } private void showBash(Player player) { if (player == null) { return; } String htm = HtmCache.getInstance().getNotNull("scripts/services/community/pages/bash.htm", player); StringBuilder sb = new StringBuilder(); int page = 1; int totalPages = quotes.size(); if(page > totalPages && page == 1) { // Пустышка, но нужно для правильной работы return; } if(page > totalPages || page < 1) { ShowBoard.separateAndSend("<html><body><br><br><center>Вы ввели неверный номер или страницы с таким номером не существует.</center><br><center><button value='На главную страницу' action='bypass -h _bbsbash 1' width=230 height=27 back='L2UI_CT1.Button_DF_Down' fore='L2UI_CT1.Button_DF'></center><br><br></body></html>", player); return; } //Отдельные переменные htm = htm.replace("<?content?>", sb.toString()); htm = htm.replace("<?Name_Server?>", Config.NAME_SERVER); //-------------------- htm = htm.replace("<?quote?>", quotes.get(page - 1)); // Цитата htm = htm.replace("<?page?>", String.valueOf(page)); // Страница htm = htm.replace("<?cpage?>", String.valueOf(page)); // Страница дубляж htm = htm.replace("<?total_pages?>", String.valueOf(totalPages)); // Текущая страница htm = htm.replace("<?back_page?>", String.valueOf(page - 1)); // Страница возврата htm = htm.replace("<?next_page?>", String.valueOf(page + 1)); // Следующая страница ShowBoard.separateAndSend(htm, player); } private int parseRSS() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); Document doc = null; try { doc = factory.newDocumentBuilder().parse(new File(xmlData)); } catch(Exception e) { e.printStackTrace(); } if(doc == null) return 0; quotes.clear(); int quotesCounter = 0; for(Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) if("rss".equalsIgnoreCase(n.getNodeName())) for(Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) if("channel".equalsIgnoreCase(d.getNodeName())) for(Node i = d.getFirstChild(); i != null; i = i.getNextSibling()) if("item".equalsIgnoreCase(i.getNodeName())) for(Node z = i.getFirstChild(); z != null; z = z.getNextSibling()) if("description".equalsIgnoreCase(z.getNodeName())) { //Убираем лишние обратные слэши и знаки $ quotes.add(z.getTextContent().replaceAll("\\\\", "").replaceAll("\\$", "")); quotesCounter++; } return quotesCounter; } public String getPage(String url_server, String url_document) { StringBuilder buf = new StringBuilder(); Socket s; try { try { s = new Socket(url_server, 80); } catch(Exception e) { return null; } s.setSoTimeout(30000); //Таймут 30 секунд BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "Cp1251")); PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8")); out.print("GET http://" + url_server + "/" + url_document + " HTTP/1.1\r\n" + // "User-Agent: MMoCore\r\n" + // "Host: " + url_server + "\r\n" + // "Accept: */*\r\n" + // "Connection: close\r\n" + // "\r\n"); out.flush(); boolean header = true; for(String line = in.readLine(); line != null; line = in.readLine()) { if(header && line.startsWith("<?xml ")) header = false; if(!header) buf.append(line).append("\r\n"); if(!header && line.startsWith("</rss>")) break; } s.close(); } catch(IOException e) { e.printStackTrace(); } return buf.toString(); } public void loadData() { // Скачиваем файл и сохраняем его на диске String data; try { data = getPage("bash.im", "rss/"); } catch(Exception E) { data = null; } if(data == null) { _log.info("CommunityBoard: Bash - RSS data download failed."); return; } data = data.replaceFirst("windows-1251", "utf-8"); Files.writeFile(xmlData, data); _log.info("CommunityBoard: Bash - RSS data download completed."); int parse = parseRSS(); if(parse == 0) { _log.warn("CommunityBoard: Bash - RSS data parse error."); return; } _log.info("CommunityBoard: Bash - RSS data parsed: loaded " + parse + " quotes."); } @Override public void onWriteCommand(Player player, String bypass, String arg1, String arg2, String arg3, String arg4, String arg5) {} @Override public void onLoad() { if (Config.COMMUNITYBOARD_ENABLED) { _log.info("Custom CommunityBoard by Russian: Bash loaded."); CommunityBoardManager.getInstance().registerHandler(this); loadData(); } } @Override public void onReload() { CommunityBoardManager.getInstance().removeHandler(this); } @Override public void onShutdown() {} } <html><body><center> <table><tr> <td><button value="Главная" action="bypass _bbspage:index" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> <td><button value="Магазин" action="bypass _bbspage:shop" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> <td><button value="Баффы" action="bypass _cbbsbuffer 0" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> <td><button value="Карьера" action="bypass _bbservices profession" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> <td><button value="Телепорт" action="bypass _cbbsteleportlist" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> <td><button value="Справка" action="bypass _bbspage:help" width=90 height=25 back="L2UI_ct1.button_df_down" fore="L2UI_ct1.button_df"></td> </tr></table> <img src="L2UI.SquareWhite" width=600 height=1><table width=600 bgcolor=A7A19A><tr><td height=20 width=600> <font color=LEVEL> <a action="bypass _bbspage:index"><?Name_Server?></a> -> Юмор</font> </td></tr></table> <img src="L2UI.SquareWhite" width=600 height=1> <table width=586 height=350> <tr> <td height=357></td> <br><br> <td height=350> <font color="LEVEL">Страница <?page?> из <?total_pages?></font> <br> <table border=0 width=340> <tr><td><?quote?></td></tr> </table> <br> <table border=0 width=240> <tr> <td widht=30> <a action="bypass _bbsbash <?back_page?>">Назад</a> </td> <td widht=160> [ <?cpage?> ] </td> <td widht=40> <a action="bypass _bbsbash <?next_page?>">Вперед</a> </td> </tr> </table> </td> </tr> </table> </center></body></html> 2 Поделиться сообщением Ссылка на сообщение Поделиться на другие сайты