Advertisement
Guest User

Add Weapon Mod 1.1

a guest
Jan 23rd, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. -- Add Weapon Mod 1.1
  2. -- A Payday 2 LUA script to add specific weapon mods to inventory
  3. -- Changelog:
  4. --   1.1 - support for DLC weapon mods
  5. -- Credits:
  6. --   * notbulletproof
  7. --   * acepilot1023978: original script
  8. --   * B1313: code review
  9. PD2 = class()
  10.  
  11. function PD2:getWeaponModPairs()
  12.     return pairs(tweak_data.blackmarket.weapon_mods)
  13. end
  14.  
  15. function PD2:getWeaponModName(mod_id)
  16.     return self:localize(self:getWeaponModData(mod_id).name_id)
  17. end
  18.  
  19. function PD2:getWeaponModDlc(mod_id)
  20.     return self:getWeaponModData(mod_id).dlc or "normal"
  21. end
  22.  
  23. function PD2:getWeaponModData(mod_id)
  24.     return tweak_data.blackmarket.weapon_mods[mod_id]
  25. end
  26.  
  27. function PD2:isLocalized(name_id)
  28.     return name_id and managers.localization:exists(name_id)
  29. end
  30.  
  31. function PD2:localize(name_id)
  32.     if (self:isLocalized(name_id)) then
  33.         return managers.localization:text(name_id)
  34.     end
  35.  
  36.     return name_id
  37. end
  38.  
  39. function PD2:getWeaponModAmount(mod_id)
  40.     local dlc = self:getWeaponModDlc(mod_id)
  41.  
  42.     return managers.blackmarket:get_item_amount(dlc, "weapon_mods", mod_id, true)
  43. end
  44.  
  45. function PD2:addWeaponMod(mod_id)
  46.     local dlc = self:getWeaponModDlc(mod_id)
  47.  
  48.     self:println("Adding " .. self:getWeaponModName(mod_id) .. " to inventory" .. ", id=" .. mod_id .. ", dlc=" .. dlc)
  49.  
  50.     managers.blackmarket:add_to_inventory(dlc, "weapon_mods", mod_id, true)
  51.  
  52.     self:println("Added " .. self:getWeaponModName(mod_id) .. " to inventory, current stock " .. self:getWeaponModAmount(mod_id))
  53. end
  54.  
  55. function PD2:println(text)
  56.     io.stdout:write(text .. "\n")
  57. end
  58.  
  59. function PD2:showDialog(title, options)
  60.     show_sorted_dialog(title, "", options, nil, 20, 1)
  61. end
  62.  
  63. WeaponModDialog = class()
  64.  
  65. function WeaponModDialog:init(pd2)
  66.     self.pd2 = pd2
  67. end
  68.  
  69. function WeaponModDialog:showIfNotInGame()
  70.     if not inGame() then
  71.         self:show()
  72.     end
  73. end
  74.  
  75. function WeaponModDialog:show()
  76.     self.pd2:showDialog("Add Weapon Mods", self:getDialogOptions())
  77. end
  78.  
  79. function WeaponModDialog:getDialogOptions()
  80.     local dialog_options = {}
  81.     local pd2 = self.pd2
  82.  
  83.     local function addWeaponModCallback(mod_id)
  84.         pd2:addWeaponMod(mod_id)
  85.     end
  86.  
  87.     local function sortByName(a, b)
  88.         return a.text < b.text
  89.     end
  90.  
  91.     for mod_id, data in pd2.getWeaponModPairs() do
  92.         local name_id = data.name_id
  93.  
  94.         if (pd2:isLocalized(name_id)) then
  95.             local qty = pd2:getWeaponModAmount(mod_id)
  96.             local name = pd2:localize(name_id) .. " / " .. mod_id .. " (" .. qty .. ")"
  97.             table.insert(dialog_options, {
  98.                 text = name,
  99.                 callback = addWeaponModCallback,
  100.                 data = mod_id })
  101.         end
  102.     end
  103.  
  104.     table.sort(dialog_options, sortByName)
  105.  
  106.     return dialog_options
  107. end
  108.  
  109. local pd2 = PD2:new()
  110. local weaponModDialog = WeaponModDialog:new(pd2)
  111. weaponModDialog:showIfNotInGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement