-- by modelleicher
-- ls-modcompany.com

-- disables additionalAttacherBlock on Hauer Frontloader because we have a proper attacherBlock in this vehicle.
-- written for Deutz 6.61 Modelleicher Edit
-- 28.02.2020

-- this script uses the additionalAttacherBlock i3d mapping to find the additionalAttacherBlock on Hauer frontloader, if there is any other
-- frontloader mod out there using the same naming for something it might cause problems. But I don't think there is or will be. Otherwise I'd have to check for the XML names or something...
-- (tell me if there's such a problem) 

hauerLoaderConsole_disable = {};

function hauerLoaderConsole_disable.prerequisitesPresent(specializations)
    return true;
end;

function hauerLoaderConsole_disable.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onPostAttachImplement", hauerLoaderConsole_disable);
	SpecializationUtil.registerEventListener(vehicleType, "onPostDetachImplement", hauerLoaderConsole_disable);
end;

function hauerLoaderConsole_disable:onPostAttachImplement(attachable, inputJointDescIndex, jointDescIndex)
	local attacherJoint = attachable:getActiveInputAttacherJoint();
	if attacherJoint ~= nil then
		if attacherJoint.jointType == AttacherJoints.jointTypeNameToInt["attachableFrontloader"] then
			if attachable.i3dMappings ~= nil then
				if attachable.i3dMappings.additionalAttacherBlock ~= nil then
					setScale(I3DUtil.indexToObject(attachable.components, attachable.i3dMappings.additionalAttacherBlock, attachable.i3dMappings), 0.001, 0.001, 0.001);
				end; -- we use setScale because its actually set visible after this function, so we scale it down that way it stays invisible no matter what
			end;
		end;
	end;
end;

function hauerLoaderConsole_disable:onPostDetachImplement(implementIndex)
    local attachable = self:getObjectFromImplementIndex(implementIndex);
    if attachable ~= nil then
        local attacherJoint = attachable:getActiveInputAttacherJoint();
        if attacherJoint ~= nil then
			if attacherJoint.jointType == AttacherJoints.jointTypeNameToInt["attachableFrontloader"] then
				if attachable.i3dMappings ~= nil then
					if attachable.i3dMappings.additionalAttacherBlock ~= nil then
						setScale(I3DUtil.indexToObject(attachable.components, attachable.i3dMappings.additionalAttacherBlock, attachable.i3dMappings), 1, 1, 1);
					end; -- reset the scale back to normal after detaching 
				end;
			end;
        end;
    end;
end;
