-- Silencer.modName = "Silencer";
-- Silencer.version = "1.0";
-- Silencer.author = "Nolan";
-- Unofficial fixed version made by 00912
-- PSA version. credit for the base code goes to Nolan and 00912 

local silencedVolumeValue = 0.1
local silencedSoundRadius = 0.1
local APsilencedVolumeValue = 0.07
local APsilencedSoundRadius = 0.07
local silencedSwingSound = 'PBSshoot'
local APsilencedSwingSound = 'APBshoot'

--for singleplayer trading 
if(ItemValueTable == nil) then ItemValueTable = {}; end
ItemValueTable["Base.PBS1suppressor"] = 5.00;
ItemValueTable["Base.PBS4suppressor"] = 6.00;
ItemValueTable["Base.APBsuppressor"] = 4.00;

-- hook function for silencer handling
local silencerOnEquipPrimary = function(character, inventoryItem)
	if inventoryItem ~= nil then
		local scriptItem = inventoryItem:getScriptItem()
		local ssv = scriptItem:getSoundVolume()
		local ssr = scriptItem:getSoundRadius()
		if inventoryItem:getStringItemType() == "RangedWeapon" then 
			if inventoryItem:getCanon() then
				local canonName = inventoryItem:getCanon():getFullType()
				if canonName  == "Base.PBS1suppressor" then
					inventoryItem:setSoundVolume(silencedVolumeValue*ssv)
					inventoryItem:setSoundRadius(silencedSoundRadius*ssr)
					inventoryItem:setSwingSound(silencedSwingSound)
					return
				elseif canonName  == "Base.PBS4suppressor" then
					inventoryItem:setSoundVolume(silencedVolumeValue*ssv)
					inventoryItem:setSoundRadius(silencedSoundRadius*ssr)
					inventoryItem:setSwingSound(silencedSwingSound)
					return
				elseif canonName == "Base.APBsuppressor" then
					inventoryItem:setSoundVolume(APsilencedVolumeValue*ssv)
					inventoryItem:setSoundRadius(APsilencedSoundRadius*ssr)
					inventoryItem:setSwingSound(APsilencedSwingSound)
					return
				end
			end
			if SUFVsilencerWeaponTable[scriptItem:getFullName()] ~= nil then
				inventoryItem:setSoundVolume(ssv)
				inventoryItem:setSoundRadius(ssr)
				inventoryItem:setSwingSound(scriptItem:getSwingSound())
			end
		end
	end
end

Events.OnEquipPrimary.Add(silencerOnEquipPrimary)
Events.OnGameStart.Add(function()
	local player = getPlayer()
	silencerOnEquipPrimary(player, player:getPrimaryHandItem())
end)