Guest User

kits

a guest
Jan 19th, 2015
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.68 KB | None | 0 0
  1. PLUGIN.Name = "kits"
  2. PLUGIN.Title = "Kits"
  3. PLUGIN.Version = V(1, 1, 0)
  4. PLUGIN.Description = "Kits"
  5. PLUGIN.Author = "Reneb"
  6. PLUGIN.HasConfig = true
  7.  
  8. local Table = {}
  9. function PLUGIN:Init()
  10. self:LoadSavedData()
  11. command.AddChatCommand( "kit",self.Object, "cmdKit" )
  12. end
  13. function PLUGIN:OnServerInitialized()
  14. self:InitializeTable()
  15. end
  16. function PLUGIN:InitializeTable()
  17. Table = {}
  18. local itemlist = global.ItemManager.GetItemDefinitions();
  19. local it = itemlist:GetEnumerator()
  20. while (it:MoveNext()) do
  21. local correctname = string.lower(it.Current.displayname)
  22. Table[correctname] = tostring(it.Current.shortname)
  23. end
  24. end
  25. local function ChatMessage(player,msg)
  26. player:SendConsoleCommand( "chat.add \"SERVER\" \"" .. tostring(msg) .. "\"" );
  27. end
  28. function PLUGIN:LoadDefaultConfig()
  29. self.Config.Kits = {
  30. ["Starter"] = {
  31. cooldown = 1800,
  32. description = "Starter kit - 30 minutes cool down",
  33. main = {
  34. { name = "Cloth", amount = 50 },
  35. { name = "Wood", amount = 1500 }
  36. },
  37. wear = {
  38. { name = "burlap shoes", amount = 1 }
  39. },
  40. belt = {
  41. { name = "Bandage", amount = 2 }
  42. }
  43. },
  44. ["Daily"] = {
  45. cooldown = 86400,
  46. description = "Daily kit - 24 hours cool down",
  47. main = {
  48. { name = "Wood", amount = 3000 },
  49. { name = "Cloth", amount = 100 }
  50. }
  51. },
  52. ["Admin"] = {
  53. admin = true,
  54. description = "for admins",
  55. main = {
  56. { name = "Building Plan", amount = 1 },
  57. { name = "ammo_rifle", amount = 1000 },
  58. { name = "bed", amount = 3 },
  59. { name = "lock", amount = 3 },
  60. { name = "box_wooden_large", amount = 3 },
  61. { name = "wood", amount = 500000 },
  62. { name = "metal_fragments", amount = 500000 },
  63. { name = "stones", amount = 500000 },
  64. { name = "lock.code", amount = 3 },
  65. { name = "lantern", amount = 20 },
  66. { name = "trap_bear", amount = 20 },
  67. { name = "cloth", amount = 250000 }
  68. },
  69. wear = {
  70. { name = "metal_plate_torso", amount = 1 },
  71. { name = "metal_facemask", amount = 1 },
  72. { name = "urban_jacket", amount = 1 },
  73. { name = "urban_pants", amount = 1 },
  74. { name = "urban_boots", amount = 1 }
  75. },
  76. belt = {
  77. { name = "Rifle_ak", amount = 1 },
  78. { name = "axe_salvaged", amount = 1 },
  79. { name = "ammo_rifle", amount = 5000 }
  80. }
  81. }
  82. }
  83. self.Config.AutoKits = {
  84. allowed = true,
  85. main = {},
  86. wear = {},
  87. belt = {
  88. { name = "Thompson BP", amount = 100 },
  89. { name = "Foundation", amount = 1 },
  90. { name = "Hammer", amount = 1 }
  91. },
  92. overrideDefaultKit = true,
  93. RustDefaultKit = {
  94. "Rock",
  95. "Torch"
  96. }
  97. }
  98.  
  99. self.Config.Messages = {
  100. KitList = "Avaible Kits",
  101. NoKitAvaible = "You have no avaible kits",
  102. ErrorKit = "This kit doesn't exist",
  103. NoKitLeft = "You don't have any kits left for this kit",
  104. NoPermissionKit = "You don't have enough permissions to use this kit",
  105. CoolDown = "This kit is still in cooldown",
  106. HelpMessage = "/kit - to get the list of kits",
  107. InvalidItem = "Invalid item in kit",
  108. }
  109. self.Config.AuthLevel = 1
  110. end
  111. local function getItem(iname)
  112. if(string.sub(string.lower(iname),-3) == " bp") then
  113. return string.sub(string.lower(iname),0,-4), true
  114. end
  115. return string.lower(iname), false
  116. end
  117. function PLUGIN:LoadSavedData()
  118. KitData = datafile.GetDataTable( "kits" )
  119. KitData = KitData or {}
  120. end
  121. function PLUGIN:SaveData()
  122. if( KitData ) then
  123. if ( self:Count( KitData ) == 0 ) then
  124. KitData = nil
  125. end
  126. end
  127. datafile.SaveDataTable( "kits" )
  128. end
  129. function PLUGIN:Count( tbl )
  130. local count = 0
  131. for _ in pairs( tbl ) do
  132. count = count + 1
  133. end
  134. return count
  135. end
  136. function PLUGIN:cmdKit( player, cmd, args )
  137. local userID = tostring(rust.UserIDFromPlayer( player ))
  138. local authLevel = player:GetComponent("BaseNetworkable").net.connection.authLevel
  139. if(args.Length == 0) then
  140. local avaible = false
  141. ChatMessage(player,self.Config.Messages.KitList)
  142. for kitname,data in pairs(self.Config.Kits) do
  143. if(self:CanUseKit(player,userID,authLevel,kitname,data)) then
  144. avaible = true
  145. local text = ""
  146. if(data.description) then text = data.description end
  147. if(data.cooldown) then
  148. if(text == "") then text = data.cooldown .. "s Cooldown"
  149. else text = text .. ", " .. data.cooldown .. "s Cooldown" end
  150. end
  151. if(data.max) then
  152. if(text == "") then text = data.max .. " max uses"
  153. else text = text .. ", " .. data.max .. " max uses" end
  154. end
  155. ChatMessage(player,"/kit " .. tostring(kitname) .. " - " .. text)
  156. end
  157. end
  158. if(not avaible) then
  159. ChatMessage(player,self.Config.Messages.NoKitAvaible)
  160. end
  161. elseif(args.Length >= 1) then
  162. local targetkit = args[0]
  163. if(not self.Config.Kits[targetkit]) then
  164. ChatMessage(player,self.Config.Messages.ErrorKit)
  165. return
  166. end
  167. local allowed, err = self:CanUseKit(player,userID,authLevel,targetkit,self.Config.Kits[targetkit])
  168. if(not allowed) then
  169. ChatMessage(player,err)
  170. return
  171. end
  172. self:RedeemKit(player,targetkit, userID)
  173. end
  174. end
  175. function PLUGIN:RedeemAutoKit( player )
  176. if(not self.Config.AutoKits.allowed) then return end
  177. local data = self.Config.AutoKits
  178. local inv = player.inventory
  179. if(inv:AllItems().Length > #self.Config.AutoKits.RustDefaultKit) then
  180. return
  181. else
  182. local defaultkitnum = #self.Config.AutoKits.RustDefaultKit
  183. for i=0, inv:AllItems().Length-1 do
  184. for o=1, #self.Config.AutoKits.RustDefaultKit do
  185. if(self.Config.AutoKits.RustDefaultKit[o] == inv:AllItems()[i].info.displayname) then
  186. defaultkitnum = defaultkitnum - 1
  187. end
  188. end
  189. end
  190. if(defaultkitnum > 0) then return end
  191. end
  192. if(self.Config.AutoKits.overrideDefaultKit) then
  193. inv:Strip()
  194. end
  195. if(data.main) then
  196. for i,subdata in pairs(data.main) do
  197. if(subdata.name and subdata.amount) then
  198. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"main")
  199. if(not giveitem) then print("Error while giving kit AutoKit: " .. err) end
  200. end
  201. end
  202. end
  203. if(data.belt) then
  204. for i,subdata in pairs(data.belt) do
  205. if(subdata.name and subdata.amount) then
  206. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"belt")
  207. if(not giveitem) then print("Error while giving kit AutoKit: " .. err) end
  208. end
  209. end
  210. end
  211. if(data.wear) then
  212. for i,subdata in pairs(data.wear) do
  213. if(subdata.name and subdata.amount) then
  214. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"wear")
  215. if(not giveitem) then print("Error while giving kit AutoKit: " .. err) end
  216. end
  217. end
  218. end
  219. end
  220. function PLUGIN:RedeemKit( player, kit, userID )
  221. local data = self.Config.Kits[kit]
  222. local inv = player.inventory
  223.  
  224. if(data.main) then
  225. for i,subdata in pairs(data.main) do
  226. if(subdata.name and subdata.amount) then
  227. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"main")
  228. if(not giveitem) then print("Error while giving kit " .. kit .. ": " .. err) end
  229. end
  230. end
  231. end
  232. if(data.belt) then
  233. for i,subdata in pairs(data.belt) do
  234. if(subdata.name and subdata.amount) then
  235. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"belt")
  236. if(not giveitem) then print("Error while giving kit " .. kit .. ": " .. err) end
  237. end
  238. end
  239. end
  240. if(data.wear) then
  241. for i,subdata in pairs(data.wear) do
  242. if(subdata.name and subdata.amount) then
  243. local giveitem, err = self:GiveItem(inv,subdata.name,subdata.amount,"wear")
  244. if(not giveitem) then print("Error while giving kit " .. kit .. ": " .. err) end
  245. end
  246. end
  247. end
  248. if(data.max) then
  249. if(not KitData[userID]) then KitData[userID] = {} end
  250. if(not KitData[userID][kit]) then KitData[userID][kit] = {} end
  251. if(not KitData[userID][kit]["u"]) then KitData[userID][kit]["u"] = "0" end
  252. KitData[userID][kit]["u"] = tostring(tonumber(KitData[userID][kit]["u"]) + 1)
  253. end
  254. if(data.cooldown) then
  255. if(not KitData[userID]) then KitData[userID] = {} end
  256. if(not KitData[userID][kit]) then KitData[userID][kit] = {} end
  257. if(not KitData[userID][kit]["c"]) then KitData[userID][kit]["c"] = "0" end
  258. KitData[userID][kit]["c"] = tostring(time.GetUnixTimestamp())
  259. end
  260. self:SaveData()
  261. end
  262. function PLUGIN:checkPlugins(player)
  263. local arr = util.TableToArray( { player } )
  264. util.ConvertAndSetOnArray(arr, 0, player, UnityEngine.Object._type)
  265. local thereturn = plugins.CallHook("canRedeemKit", arr )
  266. if(thereturn == nil) then
  267. return true
  268. end
  269. return false, tostring(thereturn)
  270. end
  271. function PLUGIN:CanUseKit( player, userID, authLevel,kitname,data)
  272. local allowed = true
  273. local err = ""
  274. if(data.admin and (not authLevel or (authLevel and authLevel < 2))) then
  275. allowed = false
  276. err = self.Config.Messages.NoPermissionKit
  277. return allowed, err
  278. end
  279. if(data.moderator and (not authLevel or (authLevel and authLevel < 1))) then
  280. allowed = false
  281. err = self.Config.Messages.NoPermissionKit
  282. return allowed, err
  283. end
  284. if(data.max and KitData[userID] and KitData[userID][kitname] and KitData[userID][kitname]["u"] and tonumber(KitData[userID][kitname]["u"]) >= data.max) then
  285. allowed = false
  286. err = self.Config.Messages.NoKitLeft
  287. return allowed, err
  288. end
  289. if(data.cooldown and KitData[userID] and KitData[userID][kitname] and KitData[userID][kitname]["c"] and (time.GetUnixTimestamp() - tonumber(KitData[userID][kitname]["c"])) < data.cooldown ) then
  290. allowed = false
  291. err = self.Config.Messages.CoolDown
  292. return allowed, err
  293. end
  294. allowed, err = self:checkPlugins(player)
  295. if(not allowed) then
  296. return allowed, err
  297. end
  298. return allowed, err
  299. end
  300. function PLUGIN:PermissionsCheck(player)
  301. local authLevel = player:GetComponent("BaseNetworkable").net.connection.authLevel
  302. local neededLevel = tonumber(self.Config.AuthLevel) or 2
  303.  
  304. if (authLevel and authLevel >= neededLevel) then
  305. return true
  306. else
  307. return false
  308. end
  309. end
  310.  
  311. function PLUGIN:GiveItem(inv,rawname,amount,type)
  312. local itemname = false
  313. name, isBP = getItem(rawname)
  314. if(Table[name]) then
  315. itemname = Table[name]
  316. else
  317. itemname = name
  318. end
  319. if(tonumber(amount) == nil) then
  320. return false, "amount is not valid"
  321. end
  322. local container
  323. if(type == "belt") then
  324. container = inv.containerBelt
  325. elseif(type == "main") then
  326. container = inv.containerMain
  327. elseif(type == "wear") then
  328. container = inv.containerWear
  329. else
  330. return false, "wrong type: belt, main or wear"
  331. end
  332. arr = util.TableToArray( { itemname } )
  333. definition = global.ItemManager.FindItemDefinition.methodarray[1]:Invoke(nil, arr )
  334. if(not definition) then
  335. return false, self.Config.Messages.InvalidItem .. ": " .. tostring(itemname)
  336. end
  337. local giveitem = global.ItemManager.CreateByItemID(definition.itemid,amount,isBP)
  338. if(not giveitem) then
  339. return false, itemname .. " is not a valid item name"
  340. end
  341. inv:GiveItem(giveitem,container);
  342. return giveitem
  343. end
  344.  
  345. function PLUGIN:OnRunCommand(arg, wantsfeedback)
  346. if (not arg) then return end
  347. if (not arg.connection) then return end
  348. if (not arg.connection.player) then return end
  349. if (not arg.cmd) then return end
  350. if (not arg.cmd.name) then return end
  351. if(arg.cmd.name ~= "wakeup") then return end
  352. if(arg.connection.player == nil) then return end
  353. if(not arg.connection.player:IsSleeping()) then return end
  354. if(arg.connection.player:IsSpectating()) then return end
  355. if(self:checkPlugins(arg.connection.player)) then
  356. self:RedeemAutoKit(arg.connection.player)
  357. end
  358. return
  359. end
  360.  
  361. function PLUGIN:SendHelpText(player)
  362. ChatMessage(player,self.Config.Messages.HelpMessage)
  363. end
Advertisement
Add Comment
Please, Sign In to add comment