HaniiPuppy

custom coin stacks

Feb 12th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. function giveChipstackName(chipstack)
  2.     chipInfo = getChipInfo(chipstack.script_code)
  3.  
  4.     if chipInfo ~= nil then
  5.         local pre = (chipInfo.prefix ~= nil) and (chipInfo.prefix) or ("")
  6.         local suf = (chipInfo.suffix ~= nil) and (chipInfo.suffix) or ("")
  7.         local preSingle = (chipInfo.prefixSingular ~= nil) and (chipInfo.prefixSingular) or (pre)
  8.         local sufSingle = (chipInfo.suffixSingular ~= nil) and (chipInfo.suffixSingular) or (suf)
  9.         local val = chipInfo.value
  10.         local amount = chipstack.getQuantity()
  11.  
  12.         chipstack.setName(" * " .. preSingle .. tostring(val) .. sufSingle .. " = " .. pre .. tostring(val * amount) .. suf)
  13.     end
  14. end
  15.  
  16. function giveChipName(chip)
  17.     chipInfo = getChipInfo(chip.script_code)
  18.  
  19.     if chipInfo ~= nil then
  20.         local pre = (chipInfo.prefixSingular ~= nil) and (chipInfo.prefixSingular) or ((chipInfo.prefix ~= nil) and (chipInfo.prefix) or "")
  21.         local suf = (chipInfo.suffixSingular ~= nil) and (chipInfo.suffixSingular) or ((chipInfo.suffix ~= nil) and (chipInfo.suffix) or "")
  22.         local val = chipInfo.value
  23.  
  24.         chip.setName(pre .. tostring(val) .. suf)
  25.     end
  26. end
  27.  
  28. function getChipInfo(chipScript)
  29.     local prefix = nil
  30.     local suffix = nil
  31.     local prefixSingular = nil
  32.     local suffixSingular = nil
  33.     local value = nil
  34.  
  35.     for line in chipScript:gmatch('[^\r\n]+') do
  36.         if (prefix == nil) and (line:find("^-- prefix:")) then
  37.             prefix = line:sub(#("-- prefix:") + 1)
  38.         elseif (suffix == nil) and (line:find("^-- suffix:")) then
  39.             suffix = line:sub(#("-- suffix:") + 1)
  40.         elseif (prefixSingular == nil) and (line:find("^-- prefixsingular:")) then
  41.             prefixSingular = line:sub(#("-- prefixsingular:") + 1)
  42.         elseif (suffixSingular == nil) and (line:find("^-- suffixsingular:")) then
  43.             suffixSingular = line:sub(#("-- suffixsingular:") + 1)
  44.         elseif line:find("^-- value:") then
  45.             value = tonumber(line:sub(#("-- value:") + 1))
  46.         end
  47.  
  48.         if (prefix ~= nil) and (suffix ~= nil) and (prefixSingular ~= nil) and (suffixSingular ~= nil) and (value ~= nil) then
  49.             break
  50.         end
  51.     end
  52.  
  53.     if value == nil then
  54.         return nil
  55.     else
  56.         return
  57.         {
  58.             ["prefix"] = prefix,
  59.             ["suffix"] = suffix,
  60.             ["prefixSingular"] = prefixSingular,
  61.             ["suffixSingular"] = suffixSingular,
  62.             ["value"] = value
  63.         }
  64.     end
  65. end
  66.  
  67. function onObjectEnterContainer(container, enter_object)
  68.     if container.name == "Custom_Model_Stack" then
  69.         giveChipstackName(container)
  70.     end
  71. end
  72.  
  73. function onObjectLeaveContainer(container, leave_object)
  74.     if container.name == "Custom_Model_Stack" then
  75.         giveChipstackName(container, leave_object)
  76.     end
  77. end
  78.  
  79. function onObjectSpawn(obj)
  80.     if (obj.tag == "Chip") then
  81.         if obj.getQuantity() == -1 then -- is single chip
  82.             giveChipName(obj)
  83.         else -- is chip stack
  84.             giveChipstackName(obj)
  85.         end
  86.     end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment