local function Guns93AdjustPart(weapon, part)
	local newPart = nil
	local switch = false
	if part:getMaxRange() then
		if part:getMaxRange() >= 0 then
			if switch == false then
        		newPart = instanceItem(part:getFullType())
				switch = true
			end
			if newPart ~= nil then
				newPart:setMaxRange(0)
			end
		end
	end
	if part:getHitChance() then
		if part:getHitChance() >= 0 then
			if switch == false then
        		newPart = instanceItem(part:getFullType())
				switch = true
			end
			if newPart ~= nil then
				newPart:setHitChance(0)
			end
		end
	end
	if part:getDamage() then
		if part:getDamage() >= 0 then
			if switch == false then
        		newPart = instanceItem(part:getFullType())
				switch = true
			end
			if newPart ~= nil then
				newPart:setDamage(0)
			end
		end
	end
	if part:getModData().CriticalChanceModifier then
		if part:getModData().CriticalChanceModifier >= 0 then
			if switch == false then
        		newPart = instanceItem(part:getFullType())
				switch = true
			end
			if newPart ~= nil then
				newPart:getModData().CriticalChanceModifier = 0
			end
		end
	end
	if part:getModData().ProjectileSpreadModifier then
		if not weapon:getAmmoType() == "Base.ShotgunShells" then
			if part:getModData().ProjectileSpreadModifier >= 0 then
				if switch == false then
					newPart = instanceItem(part:getFullType())
					switch = true
				end
				if newPart ~= nil then
					newPart:getModData().ProjectileSpreadModifier = 0
				end
			end
		end
	end
	if switch == true then
		weapon:detachWeaponPart(part)
		weapon:attachWeaponPart(newPart)
	end
end

function AttachmentAdjust(player, weapon)
	if not instanceof(player, "IsoPlayer") then return end
    if weapon == nil or (not instanceof(weapon,"HandWeapon")) and (not weapon:hasTag(Gun93Registries.guns93.Guns93)) then return end
	if weapon:getWeaponPart("Stock") then
		local part = weapon:getWeaponPart("Stock")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("Irons") then
		local part = weapon:getWeaponPart("Irons")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("Trigger") then
		local part = weapon:getWeaponPart("Trigger")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("StockItem") then
		local part = weapon:getWeaponPart("StockItem")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("Scope") then
		local part = weapon:getWeaponPart("Scope")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) or weapon:getAmmoType() == "Base.ShotgunShells" then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("Receiver") then
		local part = weapon:getWeaponPart("Receiver")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) or weapon:getAmmoType() == "Base.ShotgunShells" then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
	if weapon:getWeaponPart("Canon") then
		local part = weapon:getWeaponPart("Canon")
		if weapon:hasTag(Gun93Registries.guns93.BayoUsed) or (part:hasTag(Gun93Registries.guns93.Buckshot) and weapon:getAmmoType() == "Base.SlugBullets") or (part:hasTag(Gun93Registries.guns93.Slug) and weapon:getAmmoType() == "Base.ShotgunShells") then
			Guns93AdjustPart(weapon, part)
		else
			weapon:detachWeaponPart(part)
			weapon:attachWeaponPart(instanceItem(part:getFullType()))
		end
	end
end

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