Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.50 KB | None | 0 0
  1. package com.ruseps.shops.impl;
  2.  
  3. import java.sql.ResultSet;
  4. import java.util.HashMap;
  5.  
  6. import com.ruseps.Server;
  7. import com.ruseps.model.Item;
  8. import com.ruseps.model.definitions.ItemDefinition;
  9. import com.ruseps.model.item.Items;
  10. import com.ruseps.shops.Shop;
  11. import com.ruseps.util.Misc;
  12. import com.ruseps.world.content.dialogue.Dialogue;
  13. import com.ruseps.world.content.dialogue.DialogueExpression;
  14. import com.ruseps.world.content.dialogue.DialogueManager;
  15. import com.ruseps.world.content.dialogue.DialogueType;
  16. import com.ruseps.world.entity.impl.player.Player;
  17.  
  18. public class CurrencyShop extends Shop {
  19.  
  20.     protected int currency;
  21.  
  22.     private HashMap<Integer, Integer> prices = new HashMap<Integer, Integer>();
  23.  
  24.     public int getCurrency() {
  25.         return currency;
  26.     }
  27.  
  28.     /**
  29.      * @param id
  30.      * @param name
  31.      * @param container
  32.      * @param currency
  33.      * @param general
  34.      */
  35.     public CurrencyShop(int id, String name, int currency, boolean general) {
  36.         super(id, name, general);
  37.         this.currency = currency;
  38.         try {
  39.             loadPrices();
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.     }
  44.  
  45.     public HashMap<Integer, Integer> getPrices() {
  46.         return prices;
  47.     }
  48.  
  49.     /**
  50.      * Loads the prices for all items
  51.      *
  52.      * @throws Exception
  53.      */
  54.     private void loadPrices() throws Exception {
  55.      
  56.         if (currency == BLOOD_MONEY_ID) {
  57.             prices.put(4151, 2000); //abyssal whip
  58.             prices.put(15486, 2000); //staff of light
  59.             prices.put(11235, 750); //dark bow
  60.             prices.put(14484, 50000); //dragon claws
  61.             prices.put(19780, 15000); //korasi
  62.             prices.put(51003, 150000); //elder maul
  63.             prices.put(11696, 5000); //bandos godsword
  64.             prices.put(11698, 5000); //saradomin godsword
  65.             prices.put(11700, 5000); //zamorak godsword
  66.             prices.put(11694, 20000); //armadyl godsword
  67.             prices.put(49481, 150000); //ballista
  68.             prices.put(11128, 2000); //berserker necklace
  69.             prices.put(10887, 20000); //barrelchest anchor
  70.             prices.put(11730, 5000); //saradomin sword
  71.             prices.put(4716, 1500); //dh helm
  72.             prices.put(4724, 750); //guthan helm
  73.             prices.put(4745, 750); //torag helm
  74.             prices.put(4753, 750); //verac helm
  75.             prices.put(4708, 1000); //ahrim hood
  76.             prices.put(4732, 750); //karil coif
  77.             prices.put(11554, 15000); //abyssal tentacle
  78.             prices.put(15241, 15000); //hand cannon
  79.             prices.put(51902, 30000); //dragon crossbow
  80.             prices.put(51012, 35000); //dragon hunter crossbow
  81.             prices.put(4720, 1500); //dharok platebody
  82.             prices.put(4728, 1000); //guthan platebody
  83.             prices.put(4749, 1000); //torag platebody
  84.             prices.put(4757, 1000); //verac platebody
  85.             prices.put(4712, 1500); //ahrim robetop
  86.             prices.put(4736, 1000); //karil top
  87.             prices.put(6585, 750); //fury
  88.             prices.put(20000, 25000); //steads
  89.             prices.put(20002, 20000); //ragefires
  90.             prices.put(20001, 25000); //glaivens
  91.             prices.put(4722, 1500); //dharok legs
  92.             prices.put(4730, 1000); //guthan legs
  93.             prices.put(4751, 1000); //torag legs
  94.             prices.put(4759, 1000); //verac legs
  95.             prices.put(4714, 1500); //ahrim bottoms
  96.             prices.put(4738, 1000); //karil skirt
  97.             prices.put(15019, 1500); //archers ring (i)
  98.             prices.put(15018, 1500); //seers ring (i)
  99.             prices.put(15020, 1500); //warrior ring (i)
  100.             prices.put(15220, 3000); //berserker ring (i)
  101.             prices.put(4718, 1500); //dharok axe
  102.             prices.put(4726, 1000); //guthan warspear
  103.             prices.put(4747, 1000); //torag hammer
  104.             prices.put(4755, 1000); //verac's flail
  105.             prices.put(4710, 1000); //ahrim staff
  106.             prices.put(4734, 1000); //karil's crossbow
  107.             prices.put(12601, 45000); //ring of the gods
  108.             prices.put(6914, 500); //master wand
  109.             prices.put(12284, 35000); //toxic staff
  110.             prices.put(4706, 35000); //zaryte bow
  111.             prices.put(11726, 40000); //bandos tassets
  112.             prices.put(11724, 40000); //bandos chestplate
  113.             prices.put(49547, 700000); //aguish neck
  114.             prices.put(11732, 1000); //dragon boots
  115.             prices.put(6920, 1000); //infinity boots
  116.             prices.put(2577, 1000); //ranger boots
  117.             prices.put(11718, 6000); //armadyl helmet
  118.             prices.put(11720, 6000); //armadyl body
  119.             prices.put(11722, 6000); //armadyl legs
  120.             prices.put(12926, 30000); //blowpipe
  121.             prices.put(50214, 10000); //team cape x
  122.             prices.put(50217, 10000); //team cape i
  123.             prices.put(42695, 50); //super combat pot
  124.             prices.put(2581, 5000); //robin hood hat
  125.             prices.put(11848, 6000); //dharok set
  126.             prices.put(11850, 4000); //guthan set
  127.             prices.put(11854, 4000); //torag set
  128.             prices.put(11856, 4000); //verac set
  129.             prices.put(11846, 5500); //ahrim set
  130.             prices.put(11852, 4000); //karil set
  131.             prices.put(4740, 25); //bolt racks
  132.             prices.put(15332, 500); //overloads
  133.         }
  134.         if (currency == STARDUST) {
  135.             prices.put(50014, 6000); //ahrim set
  136.             prices.put(15259, 3500); //karil set
  137.             prices.put(13661, 1700); //bolt racks
  138.             prices.put(6585, 700); //overloads
  139.             prices.put(2581, 650); //ahrim set
  140.             prices.put(2577, 650); //karil set
  141.             prices.put(2417, 500); //bolt racks
  142.             prices.put(2415, 500); //overloads
  143.             prices.put(2416, 500); //overloads
  144.             prices.put(11704, 200); //ahrim set
  145.             prices.put(11706, 200); //karil set
  146.             prices.put(11708, 200); //bolt racks
  147.             prices.put(11710, 200); //overloads
  148.             prices.put(11712, 200); //overloads
  149.             prices.put(11714, 200); //overloads
  150.             prices.put(6918, 300); //overloads
  151.             prices.put(6916, 400); //overloads
  152.             prices.put(6924, 350); //overloads
  153.         }
  154.         if (currency == DUNG_COINS_ID) {
  155.             prices.put(18361, 250000); //ahrim set
  156.             prices.put(18349, 250000); //karil set
  157.             prices.put(18353, 250000); //bolt racks
  158.             prices.put(18357, 250000); //overloads
  159.             prices.put(18355, 250000); //ahrim set
  160.             prices.put(18359, 250000); //karil set
  161.             prices.put(18361, 250000); //bolt racks
  162.             prices.put(18363, 250000); //overloads
  163.             prices.put(16955, 1500000); //overloads
  164.             prices.put(18335, 75000); //ahrim set
  165.             prices.put(16425, 1500000); //karil set
  166.             prices.put(16909, 1500000); //bolt racks
  167.             prices.put(6500, 500000); //overloads
  168.             prices.put(18346, 80000); //overloads
  169.             prices.put(18782, 30000); //overloads
  170.             prices.put(18337, 75000); //overloads
  171.             prices.put(19669, 200000); //overloads
  172.             prices.put(6924, 350); //overloads
  173.         }
  174.         if (currency == AGILITY_TICKET) {
  175.             prices.put(14936, 300); //ahrim set
  176.             prices.put(14938, 250); //karil set
  177.         }
  178.         if (currency == ENERGY_FRAGMENT) {
  179.             prices.put(5509, 75); //ahrim set
  180.             prices.put(5510, 125); //karil set
  181.             prices.put(5512, 200); //ahrim set
  182.             prices.put(13626, 900); //karil set
  183.             prices.put(13624, 1100); //ahrim set
  184.             prices.put(13627, 1100); //karil set
  185.             prices.put(13628, 900); //ahrim set
  186.         }
  187.         if (currency == ZOMBIE_FRAGMENT) {
  188.             prices.put(18337, 200); //ahrim set
  189.             prices.put(10551, 450); //karil set
  190.             prices.put(10548, 500); //ahrim set
  191.             prices.put(10549, 500); //karil set
  192.             prices.put(10550, 1750); //ahrim set
  193.             prices.put(7592, 1750); //karil set
  194.             prices.put(7593, 1700); //ahrim set
  195.             prices.put(7594, 1850); //ahrim set
  196.             prices.put(7595, 1600); //karil set
  197.             prices.put(7596, 1650); //ahrim set
  198.             prices.put(15241, 280); //karil set
  199.             prices.put(15243, 1); //ahrim set
  200.             prices.put(4151, 450); //karil set
  201.             prices.put(6585, 200); //ahrim set
  202.             prices.put(4706, 1000); //karil set
  203.             prices.put(13263, 1500); //ahrim set
  204.             prices.put(11696, 1250); //karil set
  205.             prices.put(11698, 1250); //ahrim set
  206.             prices.put(11700, 1100); //karil set
  207.         }
  208.         if (currency == CASTLE_WARS_TICKET) {
  209.             prices.put(4071, 16); //ahrim set
  210.             prices.put(4069, 24); //karil set
  211.             prices.put(4068, 18); //ahrim set
  212.             prices.put(4072, 18); //karil set
  213.             prices.put(4070, 15); //ahrim set
  214.             prices.put(4506, 32); //karil set
  215.             prices.put(4504, 48); //ahrim set
  216.             prices.put(4503, 36); //ahrim set
  217.             prices.put(4507, 36); //karil set
  218.             prices.put(4505, 30); //ahrim set
  219.             prices.put(4511, 64); //karil set
  220.             prices.put(4509, 96); //ahrim set
  221.             prices.put(4508, 74); //karil set
  222.             prices.put(4512, 74); //ahrim set
  223.             prices.put(4510, 60); //karil set
  224.             prices.put(18782, 5); //ahrim set
  225.         }
  226.         if (currency == CASTLEWARS_TICKET) {
  227.             prices.put(4071, 16); //ahrim set
  228.             prices.put(4069, 24); //karil set
  229.             prices.put(4068, 18); //ahrim set
  230.             prices.put(4072, 18); //karil set
  231.             prices.put(4070, 15); //ahrim set
  232.             prices.put(4506, 32); //karil set
  233.             prices.put(4504, 48); //ahrim set
  234.             prices.put(4503, 36); //ahrim set
  235.             prices.put(4507, 36); //karil set
  236.             prices.put(4505, 30); //ahrim set
  237.             prices.put(4511, 64); //karil set
  238.             prices.put(4509, 96); //ahrim set
  239.             prices.put(4508, 74); //karil set
  240.             prices.put(4512, 74); //ahrim set
  241.             prices.put(4510, 60); //karil set
  242.             prices.put(18782, 5); //ahrim set
  243.         }
  244.         if (currency == TOKKUL_ID) {
  245.             prices.put(11978, 300000); //ahrim set
  246.             prices.put(438, 300); //karil set
  247.             prices.put(436, 300); //ahrim set
  248.             prices.put(440, 600); //karil set
  249.             prices.put(453, 1000); //ahrim set
  250.             prices.put(442, 1500); //karil set
  251.             prices.put(444, 1500); //ahrim set
  252.             prices.put(447, 2000); //ahrim set
  253.             prices.put(449, 2250); //karil set
  254.             prices.put(451, 2500); //ahrim set
  255.             prices.put(1623, 150); //karil set
  256.             prices.put(1621, 200); //ahrim set
  257.             prices.put(1619, 300); //karil set
  258.             prices.put(1617, 600); //ahrim set
  259.             prices.put(1631, 2000); //karil set
  260.             prices.put(6571, 4500); //ahrim set
  261.             prices.put(11128, 10000); //karil set
  262.             prices.put(6524, 10000); //ahrim set
  263.             prices.put(6523, 10000); //karil set
  264.             prices.put(6526, 10000); //ahrim set
  265.             prices.put(6522, 100); //karil set
  266.             prices.put(6528, 20000); //ahrim set
  267.             prices.put(6568, 20000); //karil set
  268.            
  269.         }
  270.     }
  271.  
  272.  
  273.     @Override
  274.     public void sellToShop(Player player, Item item) {
  275.         if (getCurrency() != COINS_ID) {
  276.             player.sendMessage("You can't sell to this shop.");
  277.             return;
  278.         }
  279.  
  280.         int normalId = item.getNormalId();
  281.  
  282.         int sellPrice = calculateUnitSellPrice(new Item(normalId));
  283.  
  284.         long payment = (long) sellPrice * (long) item.getCount();
  285.  
  286.         if (payment > Integer.MAX_VALUE) {
  287.             item.setAmount(Integer.MAX_VALUE / sellPrice);
  288.             payment = (long) sellPrice * (long) item.getCount();
  289.         }
  290.  
  291.         if (payment > 0) {
  292.             player.getInventory().delete(item.getId(), item.getAmount());
  293.             addGeneralShop(new Item(normalId, item.getAmount()), getStaticCount());
  294.             player.getInventory().add(currency, (int) payment, "Sell to shop " + item.getId());
  295.         }
  296.  
  297.         player.getPacketSender().sendItemContainer(player.getInventory(), 3823);
  298.         update();
  299.     }
  300.  
  301.     @Override
  302.     public void buyFromShop(Player player, Item item) {
  303.  
  304.         long price = (long) calculateUnitBuyPrice(item) * (long) item.getCount();
  305.  
  306.         int currencyInInventory = player.getInventory().getAmount(currency);
  307.         long coinsInPouch = player.getMoneyInPouch();
  308.  
  309.         boolean usePouch = false;
  310.  
  311.         if(currencyInInventory >= price) {
  312.  
  313.         } else if(coinsInPouch >= price && currency == COINS_ID) {
  314.             usePouch = true;
  315.         } else {
  316.             item.setAmount((int) (currencyInInventory / calculateUnitBuyPrice(item)));
  317.             price = (long) calculateUnitBuyPrice(item) * (long) item.getCount();
  318.         }
  319.  
  320.         long available = usePouch ? coinsInPouch : currencyInInventory;
  321.  
  322.         if (available >= price) {
  323.  
  324.             if (price < calculateUnitBuyPrice(item) * item.getCount()) {
  325.                 return;
  326.             }
  327.  
  328.             boolean showWarning = currency != COINS_ID && item.getAmount() == 1;
  329.  
  330.             if(currency == COINS_ID && price > 1_000_000) {
  331.                 showWarning = true;
  332.             }
  333.  
  334.             if(item.getAmount() == 1 && showWarning) {
  335.  
  336.                 final CurrencyShop shop = this;
  337.  
  338.                 long finalPrice = price;
  339.                 boolean finalUsePouch = usePouch;
  340.                 DialogueManager.start(player, new Dialogue() {
  341.  
  342.                     @Override
  343.                     public DialogueType type() {
  344.                         return DialogueType.ITEM_STATEMENT;
  345.                     }
  346.  
  347.                     @Override
  348.                     public String[] item() {
  349.                         return new String[] {
  350.                                 ""+item.getId()+"",
  351.                                 "180",
  352.                                 ""+item.getDefinition().getName()+""
  353.                         };
  354.                     }
  355.  
  356.                     @Override
  357.                     public boolean closeInterface() {
  358.                         return false;
  359.                     }
  360.  
  361.                     @Override
  362.                     public String[] dialogue() {
  363.                         return new String[] { "Are you sure you want to buy "+item.getAmount()+"x of this item?",
  364.                                 "Price: "+Misc.formatNumber(finalPrice)+" "+ItemDefinition.forId(currency).getName()};
  365.                     }
  366.  
  367.                     @Override
  368.                     public Dialogue nextDialogue() {
  369.  
  370.                         return new Dialogue() {
  371.  
  372.                             @Override
  373.                             public DialogueType type() {
  374.                                 return DialogueType.OPTION;
  375.                             }
  376.  
  377.                             @Override
  378.                             public DialogueExpression animation() {
  379.                                 return null;
  380.                             }
  381.  
  382.                             @Override
  383.                             public String[] dialogue() {
  384.                                 return new String[] { "Yes, purchase item.",
  385.                                         "No, cancel."};
  386.                             }
  387.  
  388.                             @Override
  389.                             public boolean closeInterface() {
  390.                                 return false;
  391.                             }
  392.  
  393.                             @Override
  394.                             public boolean action(int option) {
  395.                                 switch(option) {
  396.                                     case 1:
  397.                                         shop.buyItem(player, item, finalPrice, finalUsePouch);
  398.                                         player.getPacketSender().closeDialogueOnly();
  399.                                         break;
  400.                                     case 2:
  401.                                         player.getPacketSender().closeDialogueOnly();
  402.                                         break;
  403.                                 }
  404.                                 return true;
  405.                             }
  406.  
  407.                         };
  408.  
  409.                     }
  410.  
  411.                 });
  412.             } else {
  413.                 buyItem(player, item, price, usePouch);
  414.             }
  415.         } else {
  416.             if (currency == Items.COINS_1) {
  417.                 player.sendMessage("You don't have enough in your pouch to buy this item.");
  418.             } else {
  419.                 player.sendMessage("You don't have enough " + ItemDefinition.forId(currency).getName() + " to buy this item.");
  420.             }
  421.         }
  422.     }
  423.  
  424.     public void buyItem(Player player, Item item, long price, boolean usePouch) {
  425.         int amountCanBuy = this.getCount(item.getId());
  426.  
  427.         if(item.getAmount() > amountCanBuy && this.isGeneral() && !this.isStaticItem(item.getNormalId())) {
  428.             item.setAmount(amountCanBuy);
  429.             price = (long) amountCanBuy * (long) calculateUnitBuyPrice(item);
  430.         }
  431.  
  432.         if(this.isGeneral() && !this.isStaticItem(item.getNormalId())) {
  433.             if (!this.contains(item)) {
  434.                 player.sendMessage("This shop doesn't seem to carry this anymore.");
  435.                 return;
  436.             }
  437.         } else {
  438.             if (!this.contains(item.getId())) {
  439.                 player.sendMessage("This shop doesn't seem to carry this anymore.");
  440.                 return;
  441.             }
  442.         }
  443.  
  444.         if (player.getInventory().getAmount(item.getId()) + item.getCount() > Integer.MAX_VALUE || player.getInventory().getAmount(item.getId()) + item.getCount() < 0) {
  445.             player.sendMessage("You already have max amount of this item in your inventory.");
  446.             return;
  447.         }
  448.  
  449.         if (price < 0 || price > Long.MAX_VALUE) {
  450.             player.sendMessage("You can't afford that");
  451.             player.getLogging().log("DUPE: " + getName() + " tried to buy " + ItemDefinition.forId(item.getId()).getName() + " x " + item.getAmount() + " FOR " + price + " " + ItemDefinition.forId(currency).getName() + ".");
  452.             return;
  453.         }
  454.  
  455.         long coinsInPouch = player.getMoneyInPouch();
  456.         int currencyAmount = player.getInventory().getAmount(currency);
  457.  
  458.         if(usePouch) {
  459.             if(coinsInPouch >= price) {
  460.                 player.setMoneyInPouch(player.getMoneyInPouch() - price);
  461.                 player.getPacketSender().sendString(8135, "" + player.getMoneyInPouch() + "");
  462.             } else {
  463.                 player.sendMessage("Error buying this amount - please report to an admin");
  464.                 return;
  465.             }
  466.         } else {
  467.             if(player.getInventory().contains(new Item(getCurrency(), (int)price))) {
  468.                 player.getInventory().delete(getCurrency(), (int) price);
  469.             } else {
  470.                 player.sendMessage("Error buying this amount - please report to an admin");
  471.                 return;
  472.             }
  473.         }
  474.  
  475.         if (this.isGeneral() && !this.isStaticItem(item.getNormalId())) {
  476.             this.remove(item);
  477.         }
  478.  
  479.         player.getInventory().add(item.getId(), item.getAmount(), "Buy from currency shop");
  480.         player.getPacketSender().sendItemContainer(player.getInventory(), 3823);
  481.         player.getLogging().log("Bought: " + item.getId() + " x " + item.getAmount() + " from " + getName() + " for " + price + " " + ItemDefinition.forId(currency).getName() + ".");
  482.         update();
  483.     }
  484.  
  485.     @Override
  486.     public void valueSellItem(Player player, Item item) {
  487.         if (getCurrency() != COINS_ID) {
  488.             player.sendMessage("You can't sell to this shop.");
  489.             return;
  490.         }
  491.  
  492.         int price = calculateUnitSellPrice(item);
  493.  
  494.         player.sendMessage("The shop will buy a " + item.getDefinition().getName() + " for @dre@" + Misc.insertCommasToNumber(Integer.toString(price)) + "</col> " + ItemDefinition.forId(currency).getName() + ".");
  495.     }
  496.  
  497.     /**
  498.      * The sell price per unit.
  499.      *
  500.      * @param item
  501.      * @return
  502.      */
  503.     public int calculateUnitSellPrice(Item item) {
  504.         int price = -1;
  505.  
  506.         if (currency == Shop.COINS_ID) {
  507.             if (getById(item.getId()) != null && getPrices().get(item.getId()) != null) {
  508.                 price = (int) (getPrices().get(item.getId()) * 0.9);
  509.             } else {
  510.                 price = (int) (item.getDefinition().getValue() * 0.9);
  511.             }
  512.         } else {
  513.             price = (int) (getSpecialPrice(item.getId()) * 0.9);
  514.         }
  515.  
  516.         return Math.max(1, price);
  517.     }
  518.  
  519.     public int calculateUnitBuyPrice(Item item) {
  520.         int price = -1;
  521.  
  522.         if (currency == Shop.COINS_ID) {
  523.             if (prices.get(item.getId()) != null) {
  524.                 price = prices.get(item.getId());
  525.             } else {
  526.                 price = item.getDefinition().getValue();
  527.             }
  528.         } else {
  529.             price = getSpecialPrice(item.getId());
  530.         }
  531.  
  532.         return Math.max(1, price);
  533.     }
  534.  
  535.     private int getSpecialPrice(int itemId) {
  536.         int price = 1;
  537.         if (prices.containsKey(itemId)) {
  538.             price = prices.get(itemId);
  539.         }
  540.         price = currency == Shop.TOKKUL_ID && price == 1 ? 10000 : price;
  541.         return price;
  542.     }
  543.  
  544.     @Override
  545.     public void process() {
  546.  
  547.         update();
  548.     }
  549.  
  550.     @Override
  551.     public int getPrice(int itemId) {
  552.         int price = -1;
  553.  
  554.         if (currency == Shop.COINS_ID) {
  555.             if (prices.get(itemId) != null) {
  556.                 price = prices.get(itemId);
  557.             } else {
  558.                 price = ItemDefinition.forId(itemId).getValue();
  559.             }
  560.         } else {
  561.             price = getSpecialPrice(itemId);
  562.         }
  563.  
  564.         return Math.max(1, price);
  565.     }
  566.  
  567.     public int getStaticCount() {
  568.         int count = 0;
  569.         for (Item item : this.getStaticItems()) {
  570.             if (item != null)
  571.                 count++;
  572.         }
  573.         return count;
  574.     }
  575.  
  576.     @Override
  577.     public void valueBuyItem(Player player, Item item) {
  578.         int price = calculateUnitBuyPrice(item);
  579.  
  580.         if (price == 0) {
  581.             price = 1;
  582.         }
  583.  
  584.         player.sendMessage("The shop will sell a " + item.getDefinition().getName() + " for @dre@" + Misc.insertCommasToNumber(Integer.toString(price)) + "</col> " + ItemDefinition.forId(currency).getName() + ".");
  585.     }
  586.  
  587. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement