GravityCube

KeySystem

Oct 29th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local me = nil
  2. local pim = nil
  3. --------------------------------------------------------
  4. -->                    Key Names                     <--
  5. --------------------------------------------------------
  6. key_name = string.char(194) .. string.char(167) .. string.char(108) .. "Vote Crate Key"
  7.  
  8. function setup(pimP, meP)
  9.     pim = pimP
  10.     me = meP
  11. end
  12.  
  13. function priceToKeys(price)
  14.             local keys_qty = math.floor((price/100)+0.5)
  15.             if keys_qty < 1 then
  16.                 keys_qty = 1
  17.             end
  18.             return keys_qty
  19. end
  20.  
  21. function withdraw(price)
  22.     local keys_qty = priceToKeys(price)
  23.     local ok, err = moveKeysToSystem(keys_qty)
  24.     return ok, err
  25. end
  26.  
  27. function getKeysInInventory()
  28.     local id = 131
  29.     local name = key_name
  30.    
  31.     local qty = 0
  32.     for i,item in pairs(pim.getAllStacks()) do
  33.         if item["id"] == 131 then
  34.             if item["name"] == name then
  35.                 if item["ench"] ~= nil then
  36.                     if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  37.                         qty = qty + item["qty"]
  38.                     end
  39.                 end
  40.             end
  41.         end
  42.     end
  43.    
  44.     return qty
  45. end
  46.  
  47. function moveKeysToSystem(amount)
  48.     local name = key_name
  49.     if amount > getKeysInInventory() then
  50.         return false, "Not enough keys!"
  51.     end
  52.     local qty = 0
  53.     for i,item in pairs(pim.getAllStacks()) do
  54.         if item["id"] == 131 then
  55.             if item["name"] == name then
  56.                 if item["ench"] ~= nil then
  57.                     if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  58.                         local keysMoved = me.insertItem(i, amount, "up")
  59.                         qty = qty + keysMoved
  60.                         amount = amount - keysMoved
  61.                     end
  62.                 end
  63.             end
  64.         end
  65.     end
  66.     return qty
  67. end
Add Comment
Please, Sign In to add comment