Advertisement
Guest User

[Share]Smart Enchanted Item Shop

a guest
Apr 19th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.52 KB | None | 0 0
  1. /* This program is free software; you can redistribute it and/or modify
  2.  * it under the terms of the GNU General Public License as published by
  3.  * the Free Software Foundation; either version 2, or (at your option)
  4.  * any later version.
  5.  *
  6.  * This program is distributed in the hope that it will be useful,
  7.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.  * GNU General Public License for more details.
  10.  *
  11.  * You should have received a copy of the GNU General Public License
  12.  * along with this program; if not, write to the Free Software
  13.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14.  * 02111-1307, USA.
  15.  *
  16.  * http://www.gnu.org/copyleft/gpl.html
  17.  */
  18. package com.l2jfrozen.gameserver.model.actor.instance;
  19.  
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.StringTokenizer;
  24.  
  25. import com.l2jfrozen.gameserver.ai.CtrlIntention;
  26. import com.l2jfrozen.gameserver.datatables.sql.ItemTable;
  27. import com.l2jfrozen.gameserver.network.SystemMessageId;
  28. import com.l2jfrozen.gameserver.network.serverpackets.ActionFailed;
  29. import com.l2jfrozen.gameserver.network.serverpackets.CharInfo;
  30. import com.l2jfrozen.gameserver.network.serverpackets.InventoryUpdate;
  31. import com.l2jfrozen.gameserver.network.serverpackets.MyTargetSelected;
  32. import com.l2jfrozen.gameserver.network.serverpackets.NpcHtmlMessage;
  33. import com.l2jfrozen.gameserver.network.serverpackets.SystemMessage;
  34. import com.l2jfrozen.gameserver.network.serverpackets.UserInfo;
  35. import com.l2jfrozen.gameserver.network.serverpackets.ValidateLocation;
  36. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jfrozen.gameserver.templates.L2NpcTemplate;
  38. import com.l2jfrozen.util.CloseUtil;
  39. import com.l2jfrozen.util.database.L2DatabaseFactory;
  40.  
  41. import javolution.text.TextBuilder;
  42.  
  43. /**
  44.  *
  45.  * @author Elfocrash
  46.  *
  47.  */
  48.  
  49. public class L2SmartMultisellInstance extends L2FolkInstance
  50. {
  51.   public L2SmartMultisellInstance(int objectId, L2NpcTemplate template)
  52.   {
  53.     super(objectId, template);
  54.   }
  55.  
  56.   @Override
  57. public void onBypassFeedback(L2PcInstance player, String command)
  58.   {
  59.          if(player == null)
  60.          {
  61.             return;
  62.          }
  63.          
  64.          if(command.startsWith("buyItem "))
  65.          {
  66.             String itemId = null;
  67.             StringTokenizer st = new StringTokenizer(command, " ");
  68.              
  69.             while (st.hasMoreTokens())
  70.             {
  71.                 itemId = st.nextToken();
  72.             }
  73.            
  74.             int id = Integer.parseInt(itemId);
  75.            
  76.             if(player.getInventory().getItemByItemId(getItemCostId(id)).getCount() >= getItemCostCount(id))
  77.             {
  78.                 player.getInventory().destroyItemByItemId("delete", getItemCostId(id), getItemCostCount(id), player, null);
  79.                
  80.                 L2ItemInstance item = null;          
  81.                 item = player.getInventory().addItem("Elfo", getItemId(id), 1, null, null);
  82.                 item.setEnchantLevel(getItemEnchant(id));
  83.                
  84.                 // send packets
  85.                 InventoryUpdate iu = new InventoryUpdate();
  86.                 iu.addItem(item);
  87.                 player.sendPacket(iu);
  88.                 player.broadcastPacket(new CharInfo(player));
  89.                 player.sendPacket(new UserInfo(player));
  90.            
  91.                 SystemMessage sm = new SystemMessage(SystemMessageId.YOU_PICKED_UP_S1_S2);
  92.                 sm.addItemName(item.getItemId());
  93.                 sm.addNumber(1);
  94.                 player.sendPacket(sm);
  95.                 iu = null;
  96.             }
  97.             else
  98.             {
  99.                 player.sendMessage("You don't have enough items in order to buy this one");
  100.                 return;
  101.             }
  102.          }
  103.        
  104.   }
  105.  
  106.   @Override
  107.     public void onAction(L2PcInstance player)
  108.       {
  109.         if (!canTarget(player)) {
  110.           return;
  111.         }
  112.    
  113.         if (this != player.getTarget())
  114.         {
  115.           player.setTarget(this);
  116.    
  117.           player.sendPacket(new MyTargetSelected(getObjectId(), 0));
  118.    
  119.           player.sendPacket(new ValidateLocation(this));
  120.         }
  121.         else if (!canInteract(player))
  122.         {
  123.           player.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, this);
  124.         }
  125.         else
  126.         {
  127.                 showHtmlWindow(player);
  128.         }
  129.    
  130.         player.sendPacket(new ActionFailed());
  131.       }
  132.  
  133.   private void showHtmlWindow(L2PcInstance activeChar)
  134.   {
  135.     NpcHtmlMessage nhm = new NpcHtmlMessage(5);
  136.     TextBuilder tb = new TextBuilder("");
  137.    
  138.     tb.append("<html><head><title>Smart Shop</title></head><body>");
  139.     tb.append("<center>");
  140.     tb.append("<table width=\"250\" cellpadding=\"5\" bgcolor=\"000000\">");
  141.     tb.append("<tr>");
  142.     tb.append("<td width=\"45\" valign=\"top\" align=\"center\"><img src=\"L2ui_ch3.menubutton4\" width=\"38\" height=\"38\"></td>");
  143.     tb.append("<td valign=\"top\"><font color=\"FF6600\">Smart Shop</font>");
  144.     tb.append("<br1><font color=\"00FF00\">"+activeChar.getName()+"</font>, Here you can buy Enchanted Gear.</td>");
  145.     tb.append("</tr>");
  146.     tb.append("</table>");
  147.     tb.append("</center>");
  148.     tb.append("<center>");
  149.    
  150.     for(int i = 1; i<= getRowsCount(); i++)
  151.         tb.append("<br><a action=\"bypass -h npc_" + getObjectId() + "_buyItem " + i + "\">Item name: " + ItemTable.getInstance().getTemplate(getItemId(i)).getName() + " Enchant: +"+ getItemEnchant(i) + " Cost: " + getItemCostCount(i) + " " + ItemTable.getInstance().getTemplate(getItemCostId(i)).getName() + "</a>");
  152.    
  153.     tb.append("</center>");
  154.     tb.append("<center>");
  155.     tb.append("<img src=\"l2ui_ch3.herotower_deco\" width=256 height=32 align=center>");
  156.     tb.append("<font color=\"FF6600\">By Elfocrash</font>");
  157.     tb.append("</center>");
  158.     tb.append("</body></html>");
  159.  
  160.     nhm.setHtml(tb.toString());
  161.     activeChar.sendPacket(nhm);
  162.  
  163.     activeChar.sendPacket(new ActionFailed());
  164.   }
  165.  
  166.   private int getRowsCount()
  167.   {
  168.       int rows = 0;
  169.       Connection con = null;
  170.       try
  171.       {
  172.               con = L2DatabaseFactory.getInstance().getConnection();
  173.                  
  174.               PreparedStatement statement = con.prepareStatement("SELECT * FROM smart_shop");
  175.              
  176.               ResultSet rset = statement.executeQuery();
  177.               while (rset.next())
  178.               {
  179.                   rows++;
  180.               }
  181.             rset.close();
  182.             statement.close();
  183.                  
  184.       }
  185.         catch(Exception e)
  186.         {
  187.             e.printStackTrace();
  188.         }finally{
  189.             CloseUtil.close(con);
  190.             con = null;
  191.         }
  192.       return rows;
  193.      
  194.   }
  195.  
  196.   private int getItemId(int itemId)
  197.   {
  198.       int itemIdd = 0;
  199.       Connection con = null;
  200.       try
  201.       {
  202.               con = L2DatabaseFactory.getInstance().getConnection();
  203.                  
  204.               PreparedStatement statement = con.prepareStatement("SELECT item_id FROM smart_shop WHERE id=?");
  205.               statement.setInt(1, itemId);
  206.              
  207.               ResultSet rset = statement.executeQuery();
  208.               while (rset.next())
  209.               {
  210.                   itemIdd = rset.getInt("item_id");
  211.               }
  212.                 rset.close();
  213.                 statement.close();
  214.                  
  215.       }
  216.         catch(Exception e)
  217.         {
  218.             e.printStackTrace();
  219.         }finally{
  220.             CloseUtil.close(con);
  221.             con = null;
  222.         }
  223.       return itemIdd;
  224.      
  225.   }
  226.  
  227.   private int getItemCostId(int costid)
  228.   {
  229.       int costIt = 0;
  230.       Connection con = null;
  231.       try
  232.       {
  233.               con = L2DatabaseFactory.getInstance().getConnection();
  234.                  
  235.               PreparedStatement statement = con.prepareStatement("SELECT cost_item_id FROM smart_shop WHERE id=?");
  236.               statement.setInt(1, costid);
  237.              
  238.               ResultSet rset = statement.executeQuery();
  239.               while (rset.next())
  240.               {
  241.                   costIt = rset.getInt("cost_item_id");
  242.               }
  243.                 rset.close();
  244.                 statement.close();
  245.                  
  246.       }
  247.         catch(Exception e)
  248.         {
  249.             e.printStackTrace();
  250.         }finally{
  251.             CloseUtil.close(con);
  252.             con = null;
  253.         }
  254.       return costIt;
  255.      
  256.   }
  257.  
  258.   private int getItemCostCount(int costid)
  259.   {
  260.       int costIt = 0;
  261.       Connection con = null;
  262.       try
  263.       {
  264.               con = L2DatabaseFactory.getInstance().getConnection();
  265.                  
  266.               PreparedStatement statement = con.prepareStatement("SELECT cost_item_count FROM smart_shop WHERE id=?");
  267.               statement.setInt(1, costid);
  268.              
  269.               ResultSet rset = statement.executeQuery();
  270.               while (rset.next())
  271.               {
  272.                   costIt = rset.getInt("cost_item_count");
  273.               }
  274.                 rset.close();
  275.                 statement.close();
  276.                  
  277.       }
  278.         catch(Exception e)
  279.         {
  280.             e.printStackTrace();
  281.         }finally{
  282.             CloseUtil.close(con);
  283.             con = null;
  284.         }
  285.       return costIt;
  286.      
  287.   }
  288.  
  289.   private int getItemEnchant(int id)
  290.   {
  291.       int itemEnch = 0;
  292.       Connection con = null;
  293.       PreparedStatement statement = null;
  294.       try
  295.       {
  296.               con = L2DatabaseFactory.getInstance().getConnection();
  297.                  
  298.               statement = con.prepareStatement("SELECT item_enchant FROM smart_shop WHERE id=?");
  299.               statement.setInt(1, id);
  300.              
  301.               ResultSet rset = statement.executeQuery();
  302.               while (rset.next())
  303.               {
  304.                   itemEnch = rset.getInt("item_enchant");
  305.               }
  306.                 rset.close();
  307.                 statement.close();
  308.                  
  309.       }
  310.         catch(Exception e)
  311.         {
  312.             e.printStackTrace();
  313.         }finally{
  314.             CloseUtil.close(con);
  315.             con = null;
  316.         }
  317.       return itemEnch;
  318.      
  319.   }
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement