AoD_Talons

Clothing shop lua

Feb 17th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. -- a basic skinshop implementation
  2.  
  3. local cfg = module("cfg/skinshops")
  4. local lang = vRP.lang
  5. local skinshops = cfg.skinshops
  6.  
  7. -- parse part key (a ped part or a prop part)
  8. -- return is_proppart, index
  9. local function parse_part(key)
  10. if type(key) == "string" and string.sub(key,1,1) == "p" then
  11. return true,tonumber(string.sub(key,2))
  12. else
  13. return false,tonumber(key)
  14. end
  15. end
  16.  
  17. -- open the skin shop for the specified ped parts
  18. -- name = partid
  19. function vRP.openSkinshop(source,parts)
  20. local user_id = vRP.getUserId(source)
  21. if user_id ~= nil then
  22. -- notify player if wearing a uniform
  23. local data = vRP.getUserDataTable(user_id)
  24. if data.cloakroom_idle ~= nil then
  25. vRPclient.notify(source,{lang.common.wearing_uniform()})
  26. end
  27.  
  28. -- get old customization to compute the price
  29. vRPclient.getCustomization(source,{},function(old_custom)
  30. old_custom.modelhash = nil
  31.  
  32. -- start building menu
  33. local menudata = {
  34. name=lang.skinshop.title(),
  35. css={top = "75px", header_color="rgba(0,255,125,0.75)"}
  36. }
  37.  
  38. local drawables = {}
  39. local textures = {}
  40.  
  41. local ontexture = function(player, choice)
  42. -- change texture
  43. local texture = textures[choice]
  44. texture[1] = texture[1]+1
  45. if texture[1] >= texture[2] then texture[1] = 0 end -- circular selection
  46.  
  47. -- apply change
  48. local custom = {}
  49. custom[parts[choice]] = {drawables[choice][1],texture[1]}
  50. vRPclient.setCustomization(source,{custom})
  51. end
  52.  
  53. local ondrawable = function(player, choice, mod)
  54. if mod == 0 then -- tex variation
  55. ontexture(player,choice)
  56. else
  57. local isprop, index = parse_part(parts[choice])
  58.  
  59. -- change drawable
  60. local drawable = drawables[choice]
  61. drawable[1] = drawable[1]+mod
  62.  
  63. if isprop then
  64. if drawable[1] >= drawable[2] then drawable[1] = -1 -- circular selection (-1 for prop parts)
  65. elseif drawable[1] < -1 then drawable[1] = drawable[2]-1 end
  66. else
  67. if drawable[1] >= drawable[2] then drawable[1] = 0 -- circular selection
  68. elseif drawable[1] < 0 then drawable[1] = drawable[2] end
  69. end
  70.  
  71. -- apply change
  72. local custom = {}
  73. custom[parts[choice]] = {drawable[1],textures[choice][1]}
  74. vRPclient.setCustomization(source,{custom})
  75.  
  76. -- update max textures number
  77. vRPclient.getDrawableTextures(source,{parts[choice],drawable[1]},function(n)
  78. textures[choice][2] = n
  79.  
  80. if textures[choice][1] >= n then
  81. textures[choice][1] = 0 -- reset texture number
  82. end
  83. end)
  84. end
  85. end
  86.  
  87. for k,v in pairs(parts) do -- for each part, get number of drawables and build menu
  88.  
  89. drawables[k] = {0,0} -- {current,max}
  90. textures[k] = {0,0} -- {current,max}
  91.  
  92. -- init using old customization
  93. local old_part = old_custom[v]
  94. if old_part then
  95. drawables[k][1] = old_part[1]
  96. textures[k][1] = old_part[2]
  97. end
  98.  
  99. -- get max drawables
  100. vRPclient.getDrawables(source,{v},function(n)
  101. drawables[k][2] = n -- set max
  102. end)
  103.  
  104. -- get max textures for this drawable
  105. vRPclient.getDrawableTextures(source,{v,drawables[k][1]},function(n)
  106. textures[k][2] = n -- set max
  107. end)
  108.  
  109. -- add menu choices
  110. menudata[k] = {ondrawable}
  111. end
  112.  
  113. menudata.onclose = function(player)
  114. -- compute price
  115. vRPclient.getCustomization(source,{},function(custom)
  116. local price = 0
  117. custom.modelhash = nil
  118. for k,v in pairs(custom) do
  119. local old = old_custom[k]
  120. if v[1] ~= old[1] then price = price + cfg.drawable_change_price end -- change of drawable
  121. if v[2] ~= old[2] then price = price + cfg.texture_change_price end -- change of texture
  122. end
  123.  
  124. if vRP.tryPayment(user_id,price) then
  125. if price > 0 then
  126. vRPclient.notify(source,{lang.money.paid({price})})
  127. end
  128. else
  129. vRPclient.notify(source,{lang.money.not_enough()})
  130. -- revert changes
  131. vRPclient.setCustomization(source,{old_custom})
  132. end
  133. end)
  134. end
  135.  
  136. -- open menu
  137. vRP.openMenu(source,menudata)
  138. end)
  139. end
  140. end
  141.  
  142. local function build_client_skinshops(source)
  143. local user_id = vRP.getUserId(source)
  144. if user_id ~= nil then
  145. for k,v in pairs(skinshops) do
  146. local parts,x,y,z = table.unpack(v)
  147.  
  148. local skinshop_enter = function(source)
  149. local user_id = vRP.getUserId(source)
  150. if user_id ~= nil then
  151.  
  152. vRP.openSkinshop(source,parts)
  153. end
  154. end
  155.  
  156. local function skinshop_leave(source)
  157. vRP.closeMenu(source)
  158. end
  159.  
  160. vRPclient.addBlip(source,{x,y,z,73,3,lang.skinshop.title()})
  161. vRPclient.addMarker(source,{x,y,z-1,0.7,0.7,0.5,0,255,125,125,150})
  162.  
  163. vRP.setArea(source,"vRP:skinshop"..k,x,y,z,1,1.5,skinshop_enter,skinshop_leave)
  164. end
  165. end
  166. end
  167.  
  168. AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
  169. if first_spawn then
  170. build_client_skinshops(source)
  171. end
  172. end)
Advertisement
Add Comment
Please, Sign In to add comment