...gameserver.model.actor.instance/AuctionMInstance.java
/*
* 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 org.l2jmobius.gameserver.model.actor.instance;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.data.sql.AuctionTable;
import org.l2jmobius.gameserver.data.xml.ItemData;
import org.l2jmobius.gameserver.instancemanager.IdManager;
import org.l2jmobius.gameserver.model.MarketItem;
import org.l2jmobius.gameserver.model.World;
import org.l2jmobius.gameserver.model.actor.Npc;
import org.l2jmobius.gameserver.model.actor.Player;
import org.l2jmobius.gameserver.model.actor.templates.NpcTemplate;
import org.l2jmobius.gameserver.model.item.instance.Item;
import org.l2jmobius.gameserver.network.serverpackets.InventoryUpdate;
import org.l2jmobius.gameserver.network.serverpackets.NpcHtmlMessage;
/**
* @author Anarchy adapted by Dramaa
*/
public class AuctionMInstance extends Npc
{
public AuctionMInstance(NpcTemplate template)
{
super(template);
}
[member=79]override[/member]
public void onBypassFeedback(Player player, String command)
{
if (command.startsWith("auction"))
{
try
{
String[] data = command.substring(8).trim().split(" ");
int page = Integer.parseInt(data[0]);
String search = ((data.length > 1) && !data[1].equals("-")) ? data[1] : "";
showAuction(player, page, search);
}
catch (Exception e)
{
showChatWindow(player);
player.sendMessage("Invalid input. Please try again.");
return;
}
}
else if (command.startsWith("buy"))
{
int auctionId = Integer.parseInt(command.substring(4));
MarketItem item = AuctionTable.getInstance().getItem(auctionId);
if (item == null)
{
showChatWindow(player);
player.sendMessage("Invalid choice. Please try again.");
return;
}
// Check if player has enough cost items
if ((player.getInventory().getItemByItemId(item.getCostId()) == null) || (player.getInventory().getItemByItemId(item.getCostId()).getCount() < item.getCostCount()))
{
showChatWindow(player);
player.sendMessage("Incorrect item count.");
return;
}
// Destroy cost items from player inventory
player.destroyItemByItemId("auction", item.getCostId(), (int) item.getCostCount(), null, true);
Player owner = World.getInstance().getPlayer(item.getOwnerId());
if ((owner != null) && owner.isOnline())
{
owner.addItem("auction", item.getCostId(), (int) item.getCostCount(), null, true);
owner.sendMessage("You have sold an item in the Auction Shop.");
}
else
{
addItemToOffline(item.getOwnerId(), item.getCostId(), (int) item.getCostCount());
}
Item i = player.addItem("auction", item.getItemId(), item.getCount(), null, true);
i.setEnchantLevel(item.getEnchant());
player.sendPacket(new InventoryUpdate());
player.sendMessage("You have purchased an item from the Auction Shop.");
AuctionTable.getInstance().deleteItem(item);
showChatWindow(player);
}
else if (command.startsWith("addpanel"))
{
int page = Integer.parseInt(command.substring(9));
showAddPanel(player, page);
}
else if (command.startsWith("additem"))
{
int itemId = Integer.parseInt(command.substring(8));
if (player.getInventory().getItemByObjectId(itemId) == null)
{
showChatWindow(player);
player.sendMessage("Invalid item. Please try again.");
return;
}
showAddPanel2(player, itemId);
}
else if (command.startsWith("addit2"))
{
try
{
String[] data = command.substring(7).split(" ");
int itemId = Integer.parseInt(data[0]);
String costitemtype = data[1];
int costCount = Integer.parseInt(data[2]);
int itemAmount = Integer.parseInt(data[3]);
if (player.getInventory().getItemByObjectId(itemId) == null)
{
showChatWindow(player);
player.sendMessage("Invalid item. Please try again.");
return;
}
if (player.getInventory().getItemByObjectId(itemId).getCount() < itemAmount)
{
showChatWindow(player);
player.sendMessage("Invalid item. Please try again.");
return;
}
if (!player.getInventory().getItemByObjectId(itemId).isTradeable())
{
showChatWindow(player);
player.sendMessage("Invalid item. Please try again.");
return;
}
int costId;
switch (costitemtype.toLowerCase())
{
case "adena":
costId = 57;
break;
case "goldencoin":
costId = 4356;
break;
case "festivaladena":
costId = 6673;
break;
default:
try
{
costId = Integer.parseInt(costitemtype);
}
catch (NumberFormatException e)
{
showChatWindow(player);
player.sendMessage("Invalid cost type. Use item name or ID.");
return;
}
break;
}
AuctionTable.getInstance().addItem(new MarketItem(AuctionTable.getInstance().getNextAuctionId(), player.getObjectId(), player.getInventory().getItemByObjectId(itemId).getId(), itemAmount, player.getInventory().getItemByObjectId(itemId).getEnchantLevel(), costId, costCount));
player.destroyItem("auction", itemId, itemAmount, this, true);
player.sendPacket(new InventoryUpdate());
player.sendMessage("You have added an item for sale in the Auction Shop.");
showChatWindow(player);
}
catch (Exception e)
{
showChatWindow(player);
player.sendMessage("Invalid input. Please try again.");
return;
}
}
else if (command.startsWith("myitems"))
{
int page = Integer.parseInt(command.substring(8));
showMyItems(player, page);
}
else if (command.startsWith("remove"))
{
int auctionId = Integer.parseInt(command.substring(7));
MarketItem item = AuctionTable.getInstance().getItem(auctionId);
if (item == null)
{
showChatWindow(player);
player.sendMessage("Invalid choice. Please try again.");
return;
}
AuctionTable.getInstance().deleteItem(item);
Item i = player.addItem("auction", item.getItemId(), item.getCount(), this, true);
i.setEnchantLevel(item.getEnchant());
player.sendPacket(new InventoryUpdate());
player.sendMessage("You have removed an item from the Auction Shop.");
showChatWindow(player);
}
else
{
super.onBypassFeedback(player, command);
}
}
private void showMyItems(Player player, int page)
{
HashMap<Integer, ArrayList<MarketItem>> items = new HashMap<>();
int curr = 1;
int counter = 0;
ArrayList<MarketItem> temp = new ArrayList<>();
for (MarketItem item : AuctionTable.getInstance().getItems())
{
if (item.getOwnerId() == player.getObjectId())
{
temp.add(item);
counter++;
if (counter == 10)
{
items.put(curr, temp);
temp = new ArrayList<>();
curr++;
counter = 0;
}
}
}
items.put(curr, temp);
if (!items.containsKey(page))
{
showChatWindow(player);
player.sendMessage("Invalid page. Please try again.");
return;
}
String html = "";
html += "<html><title>Auction Shop</title><body><center><br1>";
html += "<table width=310 bgcolor=000000 border=1>";
html += "<tr><td>Item</td><td>Cost</td><td></td></tr>";
for (MarketItem item : items.get(page))
{
html += "<tr>";
html += "<td><img src=\"" + ItemData.getInstance().getTemplate(item.getItemId()).getIcon() + "\" width=32 height=32 align=center></td>";
html += "<td>Item: " + (item.getEnchant() > 0 ? "+" + item.getEnchant() + " " + ItemData.getInstance().getTemplate(item.getItemId()).getName() + " - " + item.getCount() : ItemData.getInstance().getTemplate(item.getItemId()).getName() + " - " + item.getCount());
html += "<br1>Cost: " + item.getCostCount() + " " + ItemData.getInstance().getTemplate(item.getCostId()).getName();
html += "</td>";
html += "<td fixwidth=71><button value=\"Remove\" action=\"bypass -h npc_" + getObjectId() + "_remove " + item.getAuctionId() + "\" width=70 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">";
html += "</td></tr>";
}
html += "</table><br><br>";
html += "Page: " + page;
html += "<br1>";
if (items.keySet().size() > 1)
{
if (page > 1)
{
html += "<a action=\"bypass -h npc_" + getObjectId() + "_myitems " + (page - 1) + "\"><- Prev</a>";
}
if (items.keySet().size() > page)
{
html += "<a action=\"bypass -h npc_" + getObjectId() + "_myitems " + (page + 1) + "\">Next -></a>";
}
}
html += "</center></body></html>";
NpcHtmlMessage htm = new NpcHtmlMessage(getObjectId());
htm.setHtml(html);
player.sendPacket(htm);
}
private void showAddPanel2(Player player, int itemId)
{
Item item = player.getInventory().getItemByObjectId(itemId);
String html = "";
html += "<html><title>Auction Shop</title><body><center><br1>";
html += "<img src=\"" + item.getTemplate().getIcon() + "\" width=32 height=32 align=center>";
html += "Item: " + (item.getEnchantLevel() > 0 ? "+" + item.getEnchantLevel() + " " + item.getName() : item.getName());
if (item.isStackable())
{
html += "<br>Set amount of items to sell:";
html += "<edit var=amm type=number width=120 height=17>";
}
html += "<br>Select price:";
html += "<br><combobox width=120 height=17 var=ebox list=Adena;GoldenCoin;FestivalAdena;>";
html += "<br><edit var=count type=number width=120 height=17>";
html += "<br><button value=\"Add item\" action=\"bypass -h npc_" + getObjectId() + "_addit2 " + itemId + " $ebox $count " + (item.isStackable() ? "$amm" : "1") + "\" width=70 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">";
html += "</center></body></html>";
NpcHtmlMessage htm = new NpcHtmlMessage(getObjectId());
htm.setHtml(html);
player.sendPacket(htm);
}
private void showAddPanel(Player player, int page)
{
HashMap<Integer, ArrayList<Item>> items = new HashMap<>();
int curr = 1;
int counter = 0;
ArrayList<Item> temp = new ArrayList<>();
for (Item item : player.getInventory().getItems())
{
if ((item.getId() != 57) && item.isTradeable())
{
temp.add(item);
counter++;
if (counter == 10)
{
items.put(curr, temp);
temp = new ArrayList<>();
curr++;
counter = 0;
}
}
}
items.put(curr, temp);
if (!items.containsKey(page))
{
showChatWindow(player);
player.sendMessage("Invalid page. Please try again.");
return;
}
String html = "";
html += "<html><title>Auction Shop</title><body><center><br1>";
html += "Select item:";
html += "<br><table width=310 bgcolor=000000 border=1>";
for (Item item : items.get(page))
{
html += "<tr>";
html += "<td>";
html += "<img src=\"" + item.getTemplate().getIcon() + "\" width=32 height=32 align=center></td>";
html += "<td>" + (item.getEnchantLevel() > 0 ? "+" + item.getEnchantLevel() + " " + item.getName() : item.getName());
html += "</td>";
html += "<td><button value=\"Select\" action=\"bypass -h npc_" + getObjectId() + "_additem " + item.getObjectId() + "\" width=70 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">";
html += "</td>";
html += "</tr>";
}
html += "</table><br><br>";
html += "Page: " + page;
html += "<br1>";
if (items.keySet().size() > 1)
{
if (page > 1)
{
html += "<a action=\"bypass -h npc_" + getObjectId() + "_addpanel " + (page - 1) + "\"><- Prev</a>";
}
if (items.keySet().size() > page)
{
html += "<a action=\"bypass -h npc_" + getObjectId() + "_addpanel " + (page + 1) + "\">Next -></a>";
}
}
html += "</center></body></html>";
NpcHtmlMessage htm = new NpcHtmlMessage(getObjectId());
htm.setHtml(html);
player.sendPacket(htm);
}
private static void addItemToOffline(int playerId, int itemId, int count)
{
Connection con = null;
try
{
con = DatabaseFactory.getConnection();
PreparedStatement stm = con.prepareStatement("SELECT count FROM items WHERE owner_id=? AND item_id=?");
stm.setInt(1, playerId);
stm.setInt(2, itemId);
ResultSet rset = stm.executeQuery();
if (rset.next())
{
stm = con.prepareStatement("UPDATE items SET count=? WHERE owner_id=? AND item_id=?");
stm.setInt(1, rset.getInt("count") + count);
stm.setInt(2, playerId);
stm.setInt(3, itemId);
stm.execute();
}
else
{
stm = con.prepareStatement("INSERT INTO items VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
stm.setInt(1, playerId);
stm.setInt(2, IdManager.getInstance().getNextId());
stm.setInt(3, itemId);
stm.setInt(4, count);
stm.setInt(5, 0);
stm.setString(6, "INVENTORY");
stm.setInt(7, 0);
stm.setInt(8, 0);
stm.setInt(9, 0);
stm.setInt(10, 0);
stm.setInt(11, -1);
stm.setInt(12, 0);
stm.execute();
}
rset.close();
stm.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (con != null)
{
con.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
private void showAuction(Player player, int page, String search)
{
boolean src = !search.equals("*null*");
HashMap<Integer, ArrayList<MarketItem>> items = new HashMap<>();
int curr = 1;
int counter = 0;
ArrayList<MarketItem> temp = new ArrayList<>();
for (MarketItem item : AuctionTable.getInstance().getItems())
{
if (item.getOwnerId() == player.getObjectId())
{
continue; // Don't show player's own items in general auction listing
}
// If search string is not empty, filter by item name containing search (case insensitive)
if (src)
{
String itemName = ItemData.getInstance().getTemplate(item.getItemId()).getName().toLowerCase();
if (!itemName.contains(search.toLowerCase()))
{
continue;
}
}
temp.add(item);
counter++;
if (counter == 10)
{
items.put(curr, temp);
temp = new ArrayList<>();
curr++;
counter = 0;
}
}
if (!temp.isEmpty())
{
items.put(curr, temp);
}
if (!items.containsKey(page))
{
showChatWindow(player);
player.sendMessage("Invalid page. Please try again.");
return;
}
StringBuilder html = new StringBuilder();
html.append("<html><title>Auction Shop</title><body><center><br1>");
html.append("<table width=310 bgcolor=000000 border=1>");
html.append("<tr><td>Item</td><td>Cost</td><td></td></tr>");
for (MarketItem item : items.get(page))
{
String icon = ItemData.getInstance().getTemplate(item.getItemId()).getIcon();
String name = ItemData.getInstance().getTemplate(item.getItemId()).getName();
String costName = ItemData.getInstance().getTemplate(item.getCostId()).getName();
html.append("<tr>");
html.append("<td><img src=\"").append(icon).append("\" width=32 height=32 align=center></td>");
html.append("<td>Item: ").append(item.getEnchant() > 0 ? "+" + item.getEnchant() + " " + name : name).append(" - ").append(item.getCount());
html.append("<br1>Cost: ").append(item.getCostCount()).append(" ").append(costName).append("</td>");
html.append("<td fixwidth=71><button value=\"Buy\" action=\"bypass -h npc_").append(getObjectId()).append("_buy ").append(item.getAuctionId()).append("\" width=70 height=21 back=\"L2UI.DefaultButton_click\" fore=\"L2UI.DefaultButton\">");
html.append("</td></tr>");
}
html.append("</table><br><br>");
html.append("Page: ").append(page).append("<br1>");
if (items.keySet().size() > 1)
{
if (page > 1)
{
html.append("<a action=\"bypass -h npc_").append(getObjectId()).append("_auction ").append(page - 1).append(" ").append(search).append("\"><- Prev</a>");
}
if (items.keySet().size() > page)
{
html.append("<a action=\"bypass -h npc_").append(getObjectId()).append("_auction ").append(page + 1).append(" ").append(search).append("\">Next -></a>");
}
}
html.append("</center></body></html>");
NpcHtmlMessage htm = new NpcHtmlMessage(getObjectId());
htm.setHtml(html.toString());
player.sendPacket(htm);
}
[member=79]override[/member]
public String getHtmlPath(int npcId, int val)
{
String pom = "";
if (val == 0)
{
pom = "" + npcId;
}
else
{
pom = npcId + "-" + val;
}
return "data/html/mods/L2Auction/" + pom + ".html";
}
}