Advertisement
Guest User

keySystem

a guest
Jun 3rd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | None | 0 0
  1. local me = nil
  2. local pim = nil
  3. --------------------------------------------------------
  4. -->                    Key Names                     <--
  5. --------------------------------------------------------
  6. key_name = nil
  7. l_key_name = nil
  8. if fs.exists("/keyNames") then
  9.     local list = gcapi.getListFromFile("/keyNames")
  10.     key_name = list["key_name"]
  11.     l_key_name = list["l_key_name"]
  12. else
  13.     error("You need to create the keyNames file with the names of the keys ({key_name and l_key_name})")
  14. end
  15.  
  16. function setup(pimP, meP)
  17.     pim = pimP
  18.     me = meP
  19. end
  20.  
  21. function priceToKeys(price)
  22.             local keys_qty = math.floor((price/100)+0.5)
  23.             if keys_qty < 1 then
  24.                 keys_qty = 1
  25.             end
  26.             return keys_qty
  27. end
  28.  
  29. function withdraw(price)
  30.     local keys_qty = priceToKeys(price)
  31.     local ok, err = moveKeysToSystem(keys_qty)
  32.     return ok, err
  33. end
  34.  
  35. function getKeysInInventory()
  36.     local id = 131
  37.     local name = key_name
  38.    
  39.     local qty = 0
  40.     for i,item in pairs(pim.getAllStacks()) do
  41.         if item["id"] == 131 then
  42.             if item["name"] == name then
  43.                 if item["ench"] ~= nil then
  44.                     if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  45.                         qty = qty + item["qty"]
  46.                     end
  47.                 end
  48.             end
  49.         end
  50.     end
  51.    
  52.     return qty
  53. end
  54.  
  55. function moveKeysToSystem(amount)
  56.     local name = key_name
  57.     if amount > getKeysInInventory() then
  58.         return false, "Not enough keys!"
  59.     end
  60.     local qty = 0
  61.     for i,item in pairs(pim.getAllStacks()) do
  62.         if item["id"] == 131 then
  63.             if item["name"] == name then
  64.                 if item["ench"] ~= nil then
  65.                     if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  66.                         local keysMoved = me.insertItem(i, amount, "up")
  67.                         qty = qty + keysMoved
  68.                         amount = amount - keysMoved
  69.                     end
  70.                 end
  71.             end
  72.         end
  73.     end
  74.     return qty
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement