Guest User

Untitled

a guest
May 25th, 2026
64
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. -- Checks if a table contains an object as a key or as a value
  2. -- Returns a key of the table if this object is found
  3. function table.containsObj(tab, obj, valueOnly)
  4. if type(tab) ~= "table" then
  5. error("Can't use table.containsObj with a " .. type(tab) .. "!", 2)
  6. end
  7. for k, v in pairs(tab) do
  8. if (not valueOnly and k == obj) or v == obj then
  9. return k
  10. end
  11. end
  12. return false
  13. end
  14.  
  15. -- Copies a table
  16. function table.copy(orig)
  17. local orig_type = type(orig)
  18. local copy
  19. if orig_type == 'table' then
  20. copy = {}
  21. for orig_key, orig_value in next, orig, nil do
  22. copy[table.copy(orig_key)] = table.copy(orig_value)
  23. end
  24. setmetatable(copy, table.copy(getmetatable(orig)))
  25. else -- number, string, boolean, etc
  26. copy = orig
  27. end
  28. return copy
  29. end
  30.  
  31. -- Checks if a string ends with another string
  32. function string.ends_with(str, ending)
  33. return ending == "" or str:sub(-#ending) == ending
  34. end
  35.  
  36. -- Takes a string and returns a table of strings between the characters we were searching for
  37. -- Ex: string.split("test:testy", ":") --> { "test", "testy" }
  38. -- Improved by WD200019
  39. function string.split(inputstr, sep, isPattern)
  40. if sep == nil then
  41. sep = "%s"
  42. end
  43. local t = { }
  44. if isPattern then
  45. while string.find(inputstr, sep) ~= nil do
  46. local matchrange = { string.find(inputstr, sep) }
  47. local preceding = string.sub(inputstr, 0, matchrange[1] - 1)
  48. table.insert(t, preceding ~= "" and preceding or nil)
  49. inputstr = string.sub(inputstr, matchrange[2] + 1)
  50. end
  51. table.insert(t, inputstr)
  52. else
  53. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  54. table.insert(t, str)
  55. end
  56. end
  57. return t
  58. end
  59.  
  60. -- Parses a base 10 number into a base 16 number (unsigned)
  61. function NumberToHex(number)
  62. number = math.abs(number)
  63. local startNumber = number
  64. local hex = ""
  65. if type(number) == "number" then
  66. repeat
  67. local tempHex = number % 16
  68. number = math.floor(number / 16)
  69. hex = (tempHex < 10 and tostring(tempHex) or tostring(string.char(string.byte('a') + tempHex - 10))) .. hex
  70. until number == 0
  71. else
  72. error("NumberToHex() needs a number variable.", 2)
  73. end
  74.  
  75. return hex
  76. end
  77.  
  78. -- Counts the number of digits in a number
  79. -- The minus is counted as a digit
  80. function CountDigits(number)
  81. if number == 0 then return 1 end
  82.  
  83. local count = 0
  84. while number ~= 0 do
  85. if number < 0 then
  86. number = -number
  87. else
  88. number = math.floor(number / 10)
  89. end
  90. count = count + 1
  91. end
  92. return count
  93. end
  94.  
  95. -- Loads an Entity file
  96. function LoadEntityFile(_ENV_BASE, path, CYK)
  97. -- Very complex _ENV swap
  98. sandboxENV = table.copy(_ENV_BASE)
  99. sandboxENV._G = { oldENV = _ENV }
  100. sandboxENV.CYK = CYK
  101. sandboxENV.Encounter = _ENV
  102. sandboxENV.self = sandboxENV
  103. _ENV = sandboxENV
  104.  
  105. local modName = GetModName()
  106. dofile (modName .. "/Lua/Libraries/CYK/Sandboxing/Entity.lua")(_ENV)
  107. dofile (modName .. "/Lua/" .. path .. ".lua")(_ENV)
  108.  
  109. -- Back to the old _ENV
  110. _G.oldENV.newENV = _ENV
  111. _ENV = _G.oldENV
  112. local newENV2 = newENV
  113. sandboxENV = nil
  114. newENV = nil
  115.  
  116. return newENV2
  117. end
  118.  
  119. -- Checks if a variable is a valid text or text container
  120. function CheckText(text, allowNil, isTextContainer)
  121. -- Text is nil
  122. if not text then
  123. if allowNil then return true, nil
  124. else return false, "nil"
  125. end
  126. -- Text is a table
  127. elseif type(text) == "table" then
  128. if #text == 0 then
  129. return false, "empty"
  130. end
  131. for j = 1, #text do
  132. -- One of the values isn't a string (or table if text is a text container)
  133. if type(text[j]) ~= "string" and not (type(text[j]) == "table" and isTextContainer) then
  134. return false, tostring(i) .. "nostr"
  135. end
  136. end
  137. return true, text
  138. -- Text is a string and not a text container
  139. elseif not isTextContainer and type(text) == "string" then
  140. return true, { text }
  141. else
  142. return false, "nostr"
  143. end
  144. end
  145.  
  146. -- Returns the name of the mod
  147. -- Credits to WD for this handy function!
  148. function GetModName()
  149. local testError = function()
  150. CreateProjectile("asdbfiosdjfaosdijcfiosdjsdo", 0, 0)
  151. end
  152.  
  153. local _, output = xpcall(testError, debug.traceback)
  154.  
  155. -- Find the position of "Sprites/asdbfiosdjfaosdijcfiosdjsdo"
  156. local SpritesFolderPos = output:find("asdbfiosdjfaosdijcfiosdjsdo") - 10
  157. output = output:sub(1, SpritesFolderPos)
  158.  
  159. local paths = string.split(output, "Attempted to load ", true)
  160. return paths[#paths]
  161. end
  162.  
  163. function PlaySoundOnceThisFrame(sound)
  164. if not NewAudio.Exists(sound) then
  165. NewAudio.CreateChannel(sound)
  166. end
  167. NewAudio.PlaySound(sound, sound)
  168. end
  169.  
  170. -- Allows to get characters from a string like we'd get a value in tables
  171. -- Ex: a = "abcdef"; a[4] --> d
  172. getmetatable('').__index = function(str,i)
  173. if type(i) == 'number' then
  174. return string.sub(str,i,i)
  175. else
  176. return string[i]
  177. end
  178. end
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment