Code: Select all
package services; import java.util.TreeMap;import java.util.Vector; import javolution.util.FastMap;import l2d.ext.scripts.Functions;import l2d.ext.scripts.ScriptFile;import l2d.game.model.L2Object;import l2d.game.model.L2Player;import l2d.game.model.L2World;import l2d.game.model.TradeItem;import l2d.game.model.instances.L2NpcInstance;import l2d.game.network.serverpackets.ExShowTrace;import l2d.game.tables.ItemTable;import l2d.game.templates.L2Item;import l2d.util.Location;import l2d.util.Util; public class ItemBroker extends Functions implements ScriptFile{ public L2Object self; public L2NpcInstance npc; private static FastMap<Integer, NpcInfo> _npcInfos = new FastMap<Integer, NpcInfo>(); public class NpcInfo { public long lastUpdate; public TreeMap<String, Item> bestSellItems; public TreeMap<String, Item> bestBuyItems; } public class Item { public int itemId; public int type; public int price; public int count; public String name; public String merchantName; public Vector<Location> path; } public String DialogAppend_32320(Integer val) { return getHtmlAppends(val); } public String DialogAppend_32321(Integer val) { return getHtmlAppends(val); } public String DialogAppend_32322(Integer val) { return getHtmlAppends(val); } public String getHtmlAppends(Integer val) { if(val != 0) return ""; L2Player player = (L2Player) self; String lang = player.getVar("lang@"); String append = ""; append += "<br>"; if(lang.equalsIgnoreCase("en")) { append += "<font color=\"LEVEL\">Search for dealers:</font><br1>"; append += "[scripts_services.ItemBroker:listForSell 1 10|The list of goods for sale.]<br1>"; append += "[scripts_services.ItemBroker:listToBuy 1 10|The list of goods to buy.]<br1>"; } else { append += "<font color=\"LEVEL\">Поиск торговцев:</font><br1>"; append += "[scripts_services.ItemBroker:listForSell 1 10|Список продаваемых товаров.]<br1>"; append += "[scripts_services.ItemBroker:listToBuy 1 10|Список покупаемых товаров.]<br1>"; } return append; } public void listForSell(String[] var) { if(self == null || npc == null) return; L2Player player = (L2Player) self; if(var.length != 2) { show("Некорректные данные", player); return; } int min; int max; try { min = Integer.valueOf(var[0]); max = Integer.valueOf(var[1]); } catch(Exception e) { show("Некорректные данные", player); return; } updateInfo(player, npc); NpcInfo info = _npcInfos.get(npc.getObjectId()); String out = "<table width=100%>"; int i = 0; for(Item item : info.bestSellItems.values()) { i++; if(i < min || i > max) continue; L2Item temp = ItemTable.getInstance().getTemplate(item.itemId); String icon = "<img src=icon." + temp.getIcon() + " width=32 height=32>"; out += "<tr><td>" + icon + "</td><td><table width=100%><tr><td>[scripts_services.ItemBroker:path " + npc.getObjectId() + " " + L2Player.STORE_PRIVATE_SELL + " " + item.itemId + "|<font color=\"LEVEL\">" + item.name + "</font>]</td></tr><tr><td>price: " + Util.formatAdena(item.price) + ", count: " + Util.formatAdena(item.count) + "</td></tr></table></td></tr>"; } out += "</table><br> "; int pages = Math.max(1, info.bestSellItems.size() / 10 + 1); if(pages > 1) for(int j = 1; j <= pages; j++) if(min == (j - 1) * 10 + 1) out += j + " "; else out += "[scripts_services.ItemBroker:listForSell " + ((j - 1) * 10 + 1) + " " + (j * 10) + "|" + j + "] "; show(out, player); } public void listToBuy(String[] var) { if(self == null || npc == null) return; L2Player player = (L2Player) self; if(var.length != 2) { show("Некорректные данные", player); return; } int min; int max; try { min = Integer.valueOf(var[0]); max = Integer.valueOf(var[1]); } catch(Exception e) { show("Некорректные данные", player); return; } updateInfo(player, npc); NpcInfo info = _npcInfos.get(npc.getObjectId()); String out = "<table width=100%>"; int i = 0; for(Item item : info.bestBuyItems.values()) { i++; if(i < min || i > max) continue; L2Item temp = ItemTable.getInstance().getTemplate(item.itemId); String icon = "<img src=icon." + temp.getIcon() + " width=32 height=32>"; out += "<tr><td>" + icon + "</td><td><table width=100%><tr><td>[scripts_services.ItemBroker:path " + npc.getObjectId() + " " + L2Player.STORE_PRIVATE_BUY + " " + item.itemId + "|<font color=\"LEVEL\">" + item.name + "</font>]</td></tr><tr><td>price: " + Util.formatAdena(item.price) + ", count: " + Util.formatAdena(item.count) + "</td></tr></table></td></tr>"; } out += "</table><br> "; int pages = Math.max(1, info.bestBuyItems.size() / 10 + 1); if(pages > 1) for(int j = 1; j <= pages; j++) if(min == (j - 1) * 10 + 1) out += j + " "; else out += "[scripts_services.ItemBroker:listToBuy " + ((j - 1) * 10 + 1) + " " + (j * 10) + "|" + j + "] "; show(out, player); } public void path(String[] var) { L2Player player = (L2Player) self; if(var.length != 3) { show("Некорректные данные", player); return; } int objId; int type; int itemId; try { objId = Integer.valueOf(var[0]); type = Integer.valueOf(var[1]); itemId = Integer.valueOf(var[2]); } catch(Exception e) { show("Некорректные данные", player); return; } L2Item temp = ItemTable.getInstance().getTemplate(itemId); if(temp == null) { show("Неизвестная ошибка", player); return; } String itemName = temp.getName(); NpcInfo info = _npcInfos.get(objId); if(info == null) { show("Неизвестная ошибка", player); return; } Item item = null; if(type == L2Player.STORE_PRIVATE_SELL) item = info.bestSellItems.get(itemName); else if(type == L2Player.STORE_PRIVATE_BUY) item = info.bestBuyItems.get(itemName); if(item == null) { show("Неизвестная ошибка", player); return; } player.sendPacket(Points2Trace(player, item.path, 50, 60000)); } public void updateInfo(L2Player player, L2NpcInstance npc) { NpcInfo info = _npcInfos.get(npc.getObjectId()); if(info == null || info.lastUpdate < System.currentTimeMillis() - 300000) { info = new NpcInfo(); info.lastUpdate = System.currentTimeMillis(); info.bestBuyItems = new TreeMap<String, Item>(); info.bestSellItems = new TreeMap<String, Item>(); for(L2Player pl : L2World.getAroundPlayers(npc, 3000, 400)) if(pl != null && (pl.getPrivateStoreType() == L2Player.STORE_PRIVATE_SELL || pl.getPrivateStoreType() == L2Player.STORE_PRIVATE_BUY)) { String merchantName = pl.getName(); switch(pl.getPrivateStoreType()) { case L2Player.STORE_PRIVATE_SELL: for(TradeItem item : pl.getSellList()) { L2Item temp = ItemTable.getInstance().getTemplate(item.getItemId()); Item oldItem = info.bestSellItems.get(temp.getName()); if(oldItem == null || oldItem.price > item.getOwnersPrice()) { Item newItem = new Item(); newItem.itemId = item.getItemId(); newItem.type = pl.getPrivateStoreType(); newItem.price = item.getOwnersPrice(); newItem.count = item.getCount(); newItem.name = temp.getName(); newItem.merchantName = merchantName; info.bestSellItems.put(newItem.name, newItem); } } break; case L2Player.STORE_PRIVATE_BUY: for(TradeItem item : pl.getBuyList()) { L2Item temp = ItemTable.getInstance().getTemplate(item.getItemId()); Item oldItem = info.bestBuyItems.get(temp.getName()); if(oldItem == null || oldItem.price < item.getOwnersPrice()) { Item newItem = new Item(); newItem.itemId = item.getItemId(); newItem.type = pl.getPrivateStoreType(); newItem.price = item.getOwnersPrice(); newItem.count = item.getCount(); newItem.name = temp.getName(); newItem.merchantName = merchantName; info.bestBuyItems.put(newItem.name, newItem); } } break; } } _npcInfos.put(npc.getObjectId(), info); } } public static ExShowTrace Points2Trace(L2Player player, Vector<Location> points, int step, int time) { ExShowTrace result = new ExShowTrace(); Location _prev = null; int i = 0; for(Location p : points) { i++; if(player.isGM()) player.sendMessage("(" + p.x + ", " + p.y + ", " + p.z + ")"); if(_prev != null) result.addLine(_prev.x, _prev.y, _prev.z, p.x, p.y, p.z, step, time); _prev = p; } return result; } public void onLoad() { System.out.println("Loaded Service: Item Broker"); } public void onReload() {} public void onShutdown() {}}
sory for my bad english