blackMales = { [ 310] = true, [ 311] = true, [ 300] = true, [ 301] = true, [ 302] = true, [ 296] = true, [ 297] = true, [ 268] = true, [ 269] = true, [ 270] = true, [ 271] = true, [ 272] = true, [ 7] = true, [ 14] = true, [ 15] = true, [ 16] = true, [ 17] = true, [ 18] = true, [ 20] = true, [ 21] = true, [ 22] = true, [ 24] = true, [ 25] = true, [ 28] = true, [ 35] = true, [ 36] = true, [ 50] = true, [ 51] = true, [ 66] = true, [ 67] = true, [ 78] = true, [ 79] = true, [ 80] = true, [ 83] = true, [ 84] = true, [ 102] = true, [ 103] = true, [ 104] = true, [ 105] = true, [ 106] = true, [ 107] = true, [ 134] = true, [ 136] = true, [ 142] = true, [ 143] = true, [ 144] = true, [ 156] = true, [ 163] = true, [ 166] = true, [ 168] = true, [ 176] = true, [ 180] = true, [ 182] = true, [ 183] = true, [ 185] = true, [ 220] = true, [ 221] = true, [ 222] = true, [ 249] = true, [ 253] = true, [ 260] = true, [ 262 ] = true }
whiteMales = {[290] = true, [305] = true, [ 306] = true, [307] = true, [ 308] = true, [ 309] = true, [ 312] = true, [ 303] = true, [ 299] = true, [ 291] = true, [ 292] = true, [ 293] = true, [ 294] = true, [ 295] = true, [ 1] = true, [ 2] = true, [ 23] = true, [ 26] = true, [ 27] = true, [ 29] = true, [ 30] = true, [ 32] = true, [ 33] = true, [ 34] = true, [ 35] = true, [ 36] = true, [ 37] = true, [ 38] = true, [ 43] = true, [ 44] = true, [ 45] = true, [ 46] = true, [ 47] = true, [ 48] = true, [ 50] = true, [ 51] = true, [ 52] = true, [ 53] = true, [ 58] = true, [ 59] = true, [ 60] = true, [ 61] = true, [ 62] = true, [ 68] = true, [ 70] = true, [ 72] = true, [ 73] = true, [ 78] = true, [ 81] = true, [ 82] = true, [ 94] = true, [ 95] = true, [ 96] = true, [ 97] = true, [ 98] = true, [ 99] = true, [ 100] = true, [ 101] = true, [ 108] = true, [ 109] = true, [ 110] = true, [ 111] = true, [ 112] = true, [ 113] = true, [ 114] = true, [ 115] = true, [ 116] = true, [ 120] = true, [ 121] = true, [ 122] = true, [ 124] = true, [ 125] = true, [ 126] = true, [ 127] = true, [ 128] = true, [ 132] = true, [ 133] = true, [ 135] = true, [ 137] = true, [ 146] = true, [ 147] = true, [ 153] = true, [ 154] = true, [ 155] = true, [ 158] = true, [ 159] = true, [ 160] = true, [ 161] = true, [ 162] = true, [ 164] = true, [ 165] = true, [ 170] = true, [ 171] = true, [ 173] = true, [ 174] = true, [ 175] = true, [ 177] = true, [ 179] = true, [ 181] = true, [ 184] = true, [ 186] = true, [ 187] = true, [ 188] = true, [ 189] = true, [ 200] = true, [ 202] = true, [ 204] = true, [ 206] = true, [ 209] = true, [ 212] = true, [ 213] = true, [ 217] = true, [ 223] = true, [ 230] = true, [ 234] = true, [ 235] = true, [ 236] = true, [ 240] = true, [ 241] = true, [ 242] = true, [ 247] = true, [ 248] = true, [ 250] = true, [ 252] = true, [ 254] = true, [ 255] = true, [ 258] = true, [ 259] = true, [ 261] = true, [ 264] = true, [ 272 ] = true }
asianMales = {[ 49] = true, [ 57] = true, [ 58] = true, [ 59] = true, [ 60] = true, [ 117] = true, [ 118] = true, [ 120] = true, [ 121] = true, [ 122] = true, [ 123] = true, [ 170] = true, [ 186] = true, [ 187] = true, [ 203] = true, [ 210] = true, [ 227] = true, [ 228] = true, [ 229] = true, [ 294 ] = true }
blackFemales = {[304] = true, [ 298] = true, [ 10] = true, [ 11] = true, [ 12] = true, [ 13] = true, [ 40] = true, [ 41] = true, [ 63] = true, [ 64] = true, [ 69] = true, [ 76] = true, [ 91] = true, [ 139] = true, [ 148] = true, [ 190] = true, [ 195] = true, [ 207] = true, [ 215] = true, [ 218] = true, [ 219] = true, [ 238] = true, [ 243] = true, [ 244] = true, [ 245] = true, [ 256] = true, [ 304 ] = true }
whiteFemales = {[12] = true, [ 31] = true, [ 38] = true, [ 39] = true, [ 40] = true, [ 41] = true, [ 53] = true, [ 54] = true, [ 55] = true, [ 56] = true, [ 64] = true, [ 75] = true, [ 77] = true, [ 85] = true, [ 87] = true, [ 88] = true, [ 89] = true, [ 90] = true, [ 91] = true, [ 92] = true, [ 93] = true, [ 129] = true, [ 130] = true, [ 131] = true, [ 138] = true, [ 140] = true, [ 145] = true, [ 150] = true, [ 151] = true, [ 152] = true, [ 157] = true, [ 172] = true, [ 178] = true, [ 192] = true, [ 193] = true, [ 194] = true, [ 196] = true, [ 197] = true, [ 198] = true, [ 199] = true, [ 201] = true, [ 205] = true, [ 211] = true, [ 214] = true, [ 216] = true, [ 224] = true, [ 225] = true, [ 226] = true, [ 231] = true, [ 232] = true, [ 233] = true, [ 237] = true, [ 243] = true, [ 246] = true, [ 251] = true, [ 257] = true, [ 263] = true, [ 298 ] = true }
asianFemales = {[38] = true, [ 53] = true, [ 54] = true, [ 55] = true, [ 56] = true, [ 88] = true, [ 141] = true, [ 169] = true, [ 178] = true, [ 224] = true, [ 225] = true, [ 226] = true, [ 263 ] = true }
local fittingskins = {[0] = {[0] = blackMales, [1] = whiteMales, [2] = asianMales}, [1] = {[0] = blackFemales, [1] = whiteFemales, [2] = asianFemales}}
local badges = getBadges()
local masks = getMasks()

armor = {
	[219]  = {49, "type2", "reaches for their armored vest and puts it over their head", "grabs their armored vest and takes it off"},
	[220]  = {74, "type3", "reaches for their armored vest and puts it over their head", "grabs their armored vest and takes it off"},
	[221]  = {100, "type4", "reaches for their armored vest and puts it over their head", "grabs their armored vest and takes it off"},
}
-- Didn't make this local, since it is also required for the 'giveItem' function in s_item_management

local cokeBottles = {}

function removeAnimation(player)
	exports.global:removeAnimation(player)
end

function giveHealth(player, health)
	if health ~= nil then
		setElementHealth( player, math.min( 100, getElementHealth( player ) + health ) )
	end
end

function removeOOC(text)
	if not text then
		return ""
	end
	return tostring(text):gsub("%s*%(%(([^)]+)%)%)%s*","") -- removes the (( )) part
end

shields = { }

local presents = { 1, 7, 8, 15, 11, 12, 19 }
local glowstickColor = 1
--
-- callbacks
function useItem(itemSlot, additional)
	if client and client ~= source then return end

	if not itemSlot then
		return
	end

	local items = getItems(source)
	local itemID = items[itemSlot][1]
	local itemValue = items[itemSlot][2]
	local itemName = getItemName( itemID, itemValue )
	local metadata = items[itemSlot][5]

	if isPedDead(source) or getElementData(source, "injuriedanimation") then return end

	local hasItemProtect = hasItem(source, tonumber( itemID ), tostring(itemValue)) or  hasItem(source, tonumber( itemID ), tonumber(itemValue))
	if not hasItemProtect then
		return
	end

	if itemID then
		if (itemID==1) then -- hotdog
			setElementHealth(source, 100)
			triggerEvent('sendAme', source, "eats a hotdog.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==3) then -- car key
			local veh = getPedOccupiedVehicle(source)
			if veh and getElementData(veh, "dbid") == itemValue then
				triggerEvent("lockUnlockInsideVehicle", source, veh)
			else
				-- unlock nearby cars
				local value = exports.pool:getElement( "vehicle", itemValue )
				if value then
					local vx, vy, vz = getElementPosition(value)
					local x, y, z = getElementPosition(source)

					if getDistanceBetweenPoints3D(x, y, z, vx, vy, vz) <= 30 then -- car found
						triggerEvent("lockUnlockOutsideVehicle", source, value)
					else
						outputChatBox("You are too far from the vehicle.", source, 255, 194, 14)
					end
				else
					outputChatBox("Invalid Vehicle.", source, 255, 194, 14)
				end
			end
		elseif (itemID==4) or (itemID==5) then -- house key or business key
			local itemValue = tonumber(itemValue)
			local found = false

			local posX, posY, posZ = getElementPosition(source)
			local dimension = getElementDimension(source)
			local possibleInteriors = getElementsByType("interior")
			for _, interior in ipairs(possibleInteriors) do
				local interiorEntrance = getElementData(interior, "entrance")
				local interiorExit = getElementData(interior, "exit")
				local interiorID = getElementData(interior, "dbid")
				if interiorID == itemValue then
					for _, point in ipairs( { interiorEntrance, interiorExit } ) do
						if (point.dim == dimension) then
							local distance = getDistanceBetweenPoints3D(posX, posY, posZ, point.x, point.y, point.z)
							if (distance <= 6) then
								found = interiorID
								break
							end
						end
					end
				end
			end

			if not found then
				local possibleElevators = getElementsByType("elevator")
				for _, elevator in ipairs(possibleElevators) do
					local elevatorEntrance = exports.interior_system:tempFix( getElementData(elevator, "entrance") )
					local elevatorExit = exports.interior_system:tempFix( getElementData(elevator, "exit") )
					for _, point in ipairs( { elevatorEntrance, elevatorExit } ) do
						if (point.dim == dimension) then
							local distance = getDistanceBetweenPoints3D(posX, posY, posZ, point.x, point.y, point.z)
							if (distance < 6) then
								if elevatorEntrance.dim == itemValue then
									found = elevatorEntrance.dim
									break
								elseif elevatorExit.dim == itemValue  then
									found = elevatorExit.dim
									break
								end
							end
						end
					end
				end
			end

			if found then
				local dbid, entrance, exit, interiorType, interiorElement = exports.interior_system:findProperty( source, found )
				local interiorStatus = getElementData(interiorElement, "status")
				local locked = interiorStatus.locked and 1 or 0

				locked = 1 - locked -- Invert status


				local newRealLockedValue = false
				mysql:query_free("UPDATE interiors SET locked='" .. locked .. "' WHERE id='" .. found .. "' LIMIT 1")
				if locked == 0 then
					triggerEvent('sendAme', source, "puts the key in the door to unlock it.")
				else
					newRealLockedValue = true
					triggerEvent('sendAme', source, "puts the key in the door to lock it.")
				end

				interiorStatus.locked = newRealLockedValue
				exports.anticheat:changeProtectedElementDataEx(interiorElement, "status", interiorStatus, true)
			else
				outputChatBox("You are too far from the door.", source, 255, 194, 14)
			end
		elseif (itemID==73) then -- elevator remote
			local itemValue = tonumber(itemValue)
			local found = nil

			local dimension = getElementDimension(source)
			local posX, posY, posZ = getElementPosition(source)
			local possibleElevators = getElementsByType("elevator")
			for _, elevator in ipairs(possibleElevators) do
				local elevatorEntrance = exports.interior_system:tempFix( getElementData(elevator, "entrance") )
				local elevatorExit = exports.interior_system:tempFix( getElementData(elevator, "exit") )
				local elevatorID = getElementData(elevator, "dbid")
				if elevatorID == itemValue then
					for _, point in ipairs( { elevatorEntrance, elevatorExit } ) do
						if (point.dim == dimension) then
							local distance = getDistanceBetweenPoints3D(posX, posY, posZ, point.x, point.y, point.z)
							if (distance < 6) then
								found = elevator
								break
							end
						end
					end
				end
			end

			if not found then
				outputChatBox("You are too far from the door.", source, 255, 194, 14)
			else
				triggerEvent( "toggleCarTeleportMode", found, source )
			end
		elseif (itemID==8) then -- sandwich
			giveHealth(source, 50)
			exports.global:applyAnimation(source, "food", "eat_burger", 4000, false, true, true)
			toggleAllControls(source, true, true, true)
			triggerEvent('sendAme', source, "eats a sandwich.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==9) then -- sprunk
			giveHealth(source, 30)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			toggleAllControls(source, true, true, true)
			triggerEvent('sendAme', source, "drinks a sprunk.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==10) then -- red dice
			local max = tonumber(itemValue) or 6
			if max == 1 then
				max = 6
			end
			exports.global:sendLocalText(source, " ✪ " .. getPlayerName(source):gsub("_", " ") .. " rolls a " .. (max == 6 and "" or (max .. "-sided ")) .. "dice and gets " .. math.random( 1, max ) ..".", 255, 51, 102)
		elseif (itemID==11) then -- taco
			giveHealth(source, 10)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a taco.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==12) then -- cheeseburger
			giveHealth(source, 10)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			setTimer(removeAnimation, 4000, 1, source)
			triggerEvent('sendAme', source, "eats a cheeseburger.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==13) then -- donut
			setElementHealth(source, 100)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a donut.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==14) then -- cookie
			giveHealth(source, 80)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a cookie.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==15) then -- water
			giveHealth(source, 90)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			triggerEvent('sendAme', source, "drinks a bottle of water.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==16) then -- clothes
			--disallowing dogs, only via /setskin
			if skin == 300 then return false end

			local gender = getElementData(source, 'gender')
			local race = getElementData(source, 'race')

			--local skin = tonumber(itemValue)
			local skin, clothingid = unpack(split(tostring(itemValue), ':'))
			skin = tonumber(skin)
			clothingid = tonumber(clothingid) or nil
			local fits = false

			local skinTable = exports.npc:getFittingSkins()
			for _, skin_id in pairs(skinTable[gender][race]) do
				if skin_id == skin then
					fits = true
				end
			end
			if clothingid or fits then
				setElementModel(source, skin)
				exports.anticheat:setEld(source, "clothing:id", clothingid, 'all')
				dbExec ( exports.mysql:getConn('mta') , "UPDATE characters SET skin=?, clothingid="..( clothingid or 'NULL' ) .. " WHERE id=?", skin, getElementData( source, "dbid" ) )
				triggerEvent('sendAme', source, "changes their clothes.")
			else
				outputChatBox("These clothes do not fit you.", source, 255, 0, 0)
			end
		elseif (itemID==17) then -- watch
			local metaName = getItemName(itemID, itemValue, metadata)
			triggerEvent('sendAme', source, "looks at their " ..metaName .. ".")
			outputChatBox("The time is " .. string.format("%02d:%02d", getTime()) .. ".", source, 255, 194, 14)
			exports.global:applyAnimation(source, "COP_AMBIENT", "Coplook_watch", 4000, false, true, true)
		elseif (itemID==20) then -- STANDARD FIGHTING
			setPedFightingStyle(source, 4)
			outputChatBox("You read a book on how to do Standard Fighting.", source, 255, 194, 14)
			mysql:query_free("UPDATE characters SET fightstyle = 4 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==21) then -- BOXING
			setPedFightingStyle(source, 5)
			outputChatBox("You read a book on how to do Boxing.", source, 255, 194, 14)
			mysql:query_free("UPDATE characters SET fightstyle = 5 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==22) then -- KUNG FU
			setPedFightingStyle(source, 6)
			outputChatBox("You read a book on how to do Kung Fu.", source, 255, 194, 14)
			mysql:query_free("UPDATE characters SET fightstyle = 6 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==23) then -- KNEE HEAD
			--setPedFightingStyle(source, 7)
			outputChatBox("You open the book, and notice that the book is written in old greek.", source, 255, 194, 14)
			--mysql:query_free("UPDATE characters SET fightstyle = 7 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==24) then -- GRAB KICK
			setPedFightingStyle(source, 15)
			outputChatBox("You read a book on how to do Grab Kick Fighting.", source, 255, 194, 14)
			mysql:query_free( "UPDATE characters SET fightstyle = 15 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==25) then -- ELBOWS
			setPedFightingStyle(source, 16)
			outputChatBox("You read a book on how to do Elbow Fighting.", source, 255, 194, 14)
			mysql:query_free("UPDATE characters SET fightstyle = 16 WHERE id = " .. getElementData( source, "dbid" ) )
		elseif (itemID==27) then -- FLASHBANG
			takeItemFromSlot(source, itemSlot)

			local obj = createObject(343, unpack(additional))
			exports.pool:allocateElement(obj)
			setTimer(explodeFlash, math.random(400, 800), 1, obj)
			triggerEvent('sendAme', source, "throws a flashbang.")
			setElementInterior(obj, getElementInterior(source))
			setElementDimension(obj, getElementDimension(source))
		elseif (itemID==28) then -- GLOWSTICK
			takeItemFromSlot(source, itemSlot)

			local x, y, groundz, int, dim = unpack(additional)
			local marker = nil
			if tostring(itemValue) == "2" then
				marker = createMarker(x, y, groundz, "corona", 1, 255, 0, 0, 150)
			elseif tostring(itemValue) == "3" then
				marker = createMarker(x, y, groundz, "corona", 1, 0, 255, 0, 150)
			elseif tostring(itemValue) == "4" then
				marker = createMarker(x, y, groundz, "corona", 1, 255, 255, 0, 150)
			elseif tostring(itemValue) == "5" then
				marker = createMarker(x, y, groundz, "corona", 1, 255, 0, 255, 150)
			elseif tostring(itemValue) == "6" then
				marker = createMarker(x, y, groundz, "corona", 1, 0, 255, 255, 150)
			elseif tostring(itemValue) == "7" then
				marker = createMarker(x, y, groundz, "corona", 1, 255, 255, 255, 150)
			else
				marker = createMarker(x, y, groundz, "corona", 1, 0, 0, 255, 150)
            end
            if isElement(marker) then
               setElementInterior(marker, int)
               setElementDimension(marker, dim)
            end
			exports.pool:allocateElement(marker)
			triggerEvent('sendAme', source, "drops a glowstick.")
			setTimer(destroyElement, 600000, 1, marker)
		elseif (itemID==29) then -- RAM
			outputChatBox("Please report to use this item.", source, 255, 0, 0)
		elseif (itemID==34) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "sniffs some Cocaine.")
		elseif (itemID==35) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "swallows a Morphine pill.")
		elseif (itemID==36) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "swallows a Ecstasy pill.")
		elseif (itemID==37) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "injects some Heroin.")
		elseif (itemID==38) then
            local sucess, key, itemvalue = hasItem(source, 181)
            local itemVal = {}
            local i = 0
            for token in string.gmatch(tostring(itemValue), "[^%s]+") do
                i = i + 1
                itemVal[i] = token
            end
            if (tonumber(itemVal[1]) > 0) then
                if sucess then
                    if (tonumber(itemvalue) >= 1) then
                        if (hasSpaceForItem(source, 182, 1)) then
                            takeItemFromSlot(source, itemSlot)
                            giveItem(source, itemID, tonumber(itemVal[1]) - 1)
                            giveItem(source, 182, 1)
                            takeItem(source, 181, itemvalue)
                            exports.global:sendLocalMeAction(source, "grabs a marijuana bud out of the bag, begining to roll the joint afterwards.")
                            giveItem(source, 181, itemvalue - 1)
                        else
                            outputChatBox( "Your Inventory is full.", source, 255, 0, 0 )
                        end
                    else
                        outputChatBox("That Rolling Papers pack is over!", source, 255, 0, 0)
                    end
                else
                    outputChatBox("You need a pack of Smoking Rolling Papers to use this.", source, 255, 0, 0)
                end
            else
                triggerEvent('sendAme', source, "looks inside their bag of Marijuana. It's empty.")
            end
		elseif (itemID==39) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "sniffs some Methamphetamine.")
		elseif (itemID==40) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "injects a Epinephrine pen.")
		elseif (itemID==41) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "pours a drop of LSD down into his mouth.")
		elseif (itemID==42) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "eats some dried shrooms")
		elseif (itemID==43) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "swallows some PCP pills")
		elseif itemID == 48 then -- Briefcase
			triggerEvent("artifacts:toggle", source, source, "backpack")
		elseif (itemID==49) then
			triggerEvent("artifacts:toggle", source, source, "rod")
			triggerEvent( "fish", source )
		elseif (itemID==50) then -- highway code book
			local bookTitle = "The Los Santos Highway Code"
			local bookName = "LSHighwayCode"
			triggerEvent('sendAme', source, "reads ".. bookTitle ..".")
			triggerClientEvent( source, "showBook", source, bookName, bookTitle )
		elseif (itemID==51) then -- chemistry book
			local bookTitle = "Chemistry 101"
			local bookName = "Chemistry101"
			triggerEvent('sendAme', source, "reads ".. bookTitle ..".")
			triggerClientEvent( source, "showBook", source, bookName, bookTitle )
		elseif (itemID==52) then -- PD manual book
			local bookTitle = "The Police Officer's Manual"
			local bookName = "PDmanual"
			triggerEvent('sendAme', source, "reads ".. bookTitle ..".")
			triggerClientEvent( source, "showBook", source, bookName, bookTitle )
		elseif (itemID==54) then -- GHETTOBLASTER
			triggerEvent('sendAme', source, "places a ghettoblaster on the ground.")
			local x, y, z = unpack(additional)

			triggerEvent("dropItem", source, itemSlot, x, y, z+0.3)
		elseif (itemID==57) then -- FUEL CAN
			local nearbyVehicles = exports.global:getNearbyElements(source, "vehicle")

			if #nearbyVehicles < 1 then return end

			local found = nil
			local shortest = 6
			local x, y, z = getElementPosition(source)
			for i, veh in ipairs(nearbyVehicles) do
				local distanceToVehicle = getDistanceBetweenPoints3D(x, y, z, getElementPosition(veh))
				if shortest > distanceToVehicle then
					shortest = distanceToVehicle
					found = veh
				end
			end

			if found then
				triggerEvent("fillFuelTankVehicle", source, found, itemValue)
			else
				outputChatBox("You are too far from a vehicle.", source, 255, 194, 14)
			end
		elseif (itemID==58) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "drinks some good Ziebrand Beer.")
			setElementHealth(source,getElementHealth(source)-5)
		elseif (itemID==59) then -- MUDKIP
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "eats a mudkip.")
			--killPed(source)
		elseif (itemID==60) then
			local dim = getElementDimension( source )
			local int = getElementInterior( source )
			local pos = { getElementPosition( source ) }
			local rot = getElementRotation( source, 'ZXY' )
			-- world map.
			if dim == 0 then
				exports.hud:sendBottomNotification( source, itemName, "This safe can only be placed inside an interior." )
				playSoundFrontEnd( source, 4 )
			-- vehicle interiors
			elseif dim >= 20000 then
				local vid = dim - 20000
				if exports.vehicle:getSafe( vid ) then
					exports.hud:sendBottomNotification( source, itemName, "There is already a safe in this property. Type /movesafe to move it to where you stand." )
				elseif hasItem( source, 3, vid ) then
					pos[3] = pos[3] - 0.5
					rot = rot + 180
					if exports.vehicle:addSafe( vid, unpack( pos ), rot, int ) then
						dbExec( exports.mysql:getConn('mta'), "UPDATE vehicles SET safepositionX=?, safepositionY=?, safepositionZ=?, safepositionRZ=? WHERE id=? ", unpack( pos ), rot, vid )
					end
				end
			-- temp vehicle interiors
			elseif dim >= 19000 then
				exports.hud:sendBottomNotification( source, itemName, "You can not place a safe in a temporary vehicle interior." )
				playSoundFrontEnd( source, 4 )
			-- normal interiors.
			elseif hasItem( source, 5, dim ) or hasItem( source, 4, dim) then
				if exports.interior_system:getSafe( dim ) then
					exports.hud:sendBottomNotification( source, itemName, "There is already a safe in this property. Type /movesafe to move it." )
					playSoundFrontEnd( source, 4 )
				else
					if exports.interior_system:addSafe( dim, nil, pos, int, rot, true, false ) then
						exports.hud:sendBottomNotification( source, itemName, "Placed." )
						takeItemFromSlot( source, itemSlot )
					end
				end
			end
		elseif (itemID==62) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "drinks some pure Bastradov Vodka.")
			setElementHealth(source,getElementHealth(source)-10)
		elseif (itemID==63) then
			takeItemFromSlot(source, itemSlot)
			triggerEvent('sendAme', source, "drinks some Scottish Whiskey.")
			setElementHealth(source,getElementHealth(source)-10)
		elseif (itemID==68) then
			triggerEvent('sendAme', source, "looks at their lottery ticket.")
			outputChatBox("You glance at the lottery ticket and read the number: " .. itemValue, source, 0, 255, 0)
		elseif (itemID==69) then -- Dictionary
			local learned = call(getResourceFromName("language-system"), "learnLanguage", source, itemValue, true)
			local lang = call(getResourceFromName("language-system"), "getLanguageName", itemValue)

			if (learned) then
				takeItem(source, itemID, itemValue)
				outputChatBox("You have learnt basic " .. lang .. ", Press F6 to manage your languages.", source, 0, 255, 0)
			end
		elseif (itemID==72) then -- Note
			triggerEvent('sendAme', source, "reads a note.")
		--[=[ REMOVED :> mabako, 11th January 2012 (plus it'd be broken, /lazy)
		elseif (itemID==74) then
			takeItemFromSlot(source, itemSlot)
			local x, y, z = getElementPosition(source)
			createExplosion( x, y, z, 10, source )
			createExplosion( x, y, z, 10, source )
		elseif (itemID==75) then
			triggerEvent('sendAme', source, "pushes some kind of red button which reads 'do not push'.")
			local px, py, pz = getElementPosition(source)
			for k, v in pairs( getElementsByType( "object", getResourceRootElement( ) ) ) do
				if isElement( v ) and getElementData( v, "itemID" ) == 74 and getElementData( v, "itemValue" ) == itemValue then
					local x, y, z = getElementPosition( v )
					if getDistanceBetweenPoints3D( x, y, z, px, py, pz ) < 200 then
						mysql:query_free("DELETE FROM worlditems WHERE id = " .. getElementData( v, "id" ) )
						createExplosion( x, y, z, 10, source )
						createExplosion( x, y, z, 10, source )
						destroyElement( v )
					end
				end
			end

			for k, v in pairs( exports.global:getNearbyElements(source, "vehicle", 200) ) do
				if hasItem( v, 74, itemValue ) then
					blowVehicle( v )

					while hasItem( v, 74 ) do
						takeItem( v, 74 )
					end
				end
			end

			for k, v in pairs( exports.global:getNearbyElements(source, "player", 200) ) do
				if hasItem( v, 74, itemValue ) then
					local x, y, z = getElementPosition( v )
					createExplosion( x, y, z, 10, source )
					createExplosion( x, y, z, 10, source )

					while hasItem( v, 74 ) do
						takeItem( v, 74 )
					end
				end
			end
		]=]
		elseif (itemID==76) then -- SHIELD
			--[[
			if (shields[source]) then -- Already using the shield
				destroyElement(shields[source])
				shields[source] = nil
			else
				local x, y, z = getElementPosition(source)
				local rot = getPedRotation(source)

				x = x + math.sin(math.rad(rot)) * 1.5
				y = y + math.cos(math.rad(rot)) * 1.5

				local object = createObject(1631, x, y, z)
				attachElements(object, source, 0, 1, 0)
				shields[source] = object
			end
			--]]
			triggerEvent("artifacts:toggle", source, source, "shield")
		elseif (itemID==77) then -- Card Deck
			exports.cards:openCardDeck(source, itemValue, itemName)
		elseif (itemID==79) then -- Porn tape
			exports.global:applyAnimation( source, "PAULNMAC", "wank_loop", -1, true, false, false)
		elseif (itemID==80) then -- Generic Item
			local metaName = getItemName(itemID, itemValue, metadata)
			showItem(removeOOC(metaName))
		elseif (itemID==83) then -- Coffee
			giveHealth(source, 40)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			triggerEvent('sendAme', source, "drinks a cup of coffee.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==88) then -- earpiece
			outputChatBox("You can use this earpiece with an radio.", source, 255, 194, 14)
		elseif (itemID==89) then -- Generic Food
			giveHealth(source, 50)
			exports.global:applyAnimation(source, "food", "eat_burger", 2000, false, true, true)
			local itemExploded = explode(":", itemValue)
			triggerEvent('sendAme', source, "eats a " .. itemExploded[1] .. ".")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==91) then
			takeItemFromSlot(source, itemSlot)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			triggerEvent('sendAme', source, "drinks some good Eggnog.")
			setElementHealth(source, 100)
		elseif (itemID==92) then
			setElementHealth(source, 100)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats some Turkey.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==93) then
			setElementHealth(source, 100)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats some Christmas Pudding.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==94) then

			local id = math.random(1, 10)
			local prizeID = presents[id]
			takeItemFromSlot(source, itemSlot)
			giveItem(source, prizeID, 1)
			triggerEvent('sendAme', source, "opens a Christmas Present")
		elseif (itemID==95) then -- Generic Drink
			giveHealth(source, 50)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 2000, false, true, true)
			local itemExploded = explode(":", itemValue)
			triggerEvent('sendAme', source, "drinks some " .. itemExploded[1] .. ".")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==96) then -- PDA
			triggerEvent('sendAme', source, "turns their " .. itemName .. " on.")
			triggerClientEvent(source, "useCompItem", source, itemName)
		elseif (itemID==97) then -- SFES Procedures Manual (book)
			--[[local bookTitle = "SFES Procedure Manual"
			local bookName = "SFESProcedureManual"
			triggerEvent('sendAme', source, "reads ".. bookTitle ..".")
			triggerClientEvent( source, "showBook", source, bookName, bookTitle )]]
			outputChatBox("This item has been disabled and is no longer in use.", source, 255, 0, 0)
		elseif (itemID==98) then -- Garage Remote
			local id = tonumber( itemValue )
			if id and id >= 0 and id <= 49 then
				setGarageOpen(itemValue, not isGarageOpen(itemValue))

				local garages = {}
				for i = 0, 49 do
					garages[i] = isGarageOpen(i)
				end
				mysql:query_free("UPDATE settings SET value = '" .. mysql:escape_string( toJSON( garages ) ) .. "' WHERE name = 'garagestates'" )
			end
		elseif (itemID==99) then --[ADDED: 2/22/10 by herbjr] tray
			giveHealth(source, 50)
			exports.global:applyAnimation(source, "food", "eat_burger", 4000, false, true, true)
			toggleAllControls(source, true, true, true)

			triggerEvent('sendAme', source, "eats the food from the tray.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==100) then --[ADDED: 2/22/10 by herbjr] milk
			giveHealth(source, 30)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			toggleAllControls(source, true, true, true)

			triggerEvent('sendAme', source, "drinks a small carton of milk.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==101) then --[ADDED: 2/22/10 by herbjr] juice
			giveHealth(source, 30)
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			toggleAllControls(source, true, true, true)

			triggerEvent('sendAme', source, "drinks a small carton of juice.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==102) then --[ADDED: 2/23/10 by herbjr] Cabbage (Asked for by Misha)
			giveHealth(source, 15)
			exports.global:applyAnimation(source, "food", "eat_burger", 4000, false, true, true)
			toggleAllControls(source, true, true, true)

			triggerEvent('sendAme', source, "eats a head of Cabbage.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==104) then -- portable TV
			triggerEvent("useTV", source, source)
			local isTvUsed = getElementData(source, "isTvUsed")
			if isTvUsed == nil or isTvUsed == false then
				triggerEvent('sendAme', source, "turns on a small portable TV.")
				exports.anticheat:changeProtectedElementDataEx(source, "isTvUsed", true, false)
			else
				exports.anticheat:changeProtectedElementDataEx(source, "isTvUsed", false, false)
			end
		elseif (itemID == 105) then -- Pack of cigarettes. Withdraw one if it still has one
			if (itemValue > 0) then
				if (hasSpaceForItem(source, 106, 1)) then
					takeItemFromSlot(source, itemSlot)
					giveItem(source, itemID, itemValue - 1)
					giveItem(source, 106, 1)
					triggerEvent('sendAme', source, "looks inside their pack of cigarettes, and takes one out.")
				else
					outputChatBox( "Your Inventory is full.", source, 255, 0, 0 )
				end
			else
				triggerEvent('sendAme', source, "looks inside their pack of cigarettes. It's empty.")
			end
		elseif (itemID == 106) then -- Cigarette
			if hasItem( source, 107 ) then
				triggerEvent('sendAme', source, "lights up a cigarette.")
				outputChatBox( "(( /throwaway to throw it away, /switchhand to change hand ))", source )
				triggerEvent("realism:startsmoking", source, 0) -- 0 = left hand -- 1 = right hand
				takeItemFromSlot(source, itemSlot)
			else
				triggerEvent('sendAme', source, "shows everyone a cigarette.")
				outputChatBox( "You'll need a lighter to use it properly, tho.", source, 255, 0, 0 )
			end
		elseif (itemID == 107) then
			triggerEvent('sendAme', source, "shows everyone a lighter.")
		elseif (itemID==108) then -- donut
			setElementHealth(source, 100)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a pancake.")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID==109) or (itemID==110) then -- Fruit
			giveHealth(source, tonumber(getItemValue(itemID, itemValue)))
			exports.global:applyAnimation(source, "food", "eat_burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a " .. itemName .. ".")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID == 113) then -- Pack of glowsticks, Withdraw one if it still has one
			if tonumber(itemValue) and tonumber(itemValue) > 0 then
				if (hasSpaceForItem(source, 28, 1)) then
					glowStickColour = 1 + ( ( glowStickColour or 0 ) + 1 ) % 7

					takeItemFromSlot(source, itemSlot)
					giveItem(source, itemID, itemValue - 1)
					giveItem(source, 28, glowStickColour)
					triggerEvent('sendAme', source, "looks inside their pack of glowsticks, sliding one out.")
				else
					outputChatBox( "Your Inventory is full.", source, 255, 0, 0 )
				end
			else
				triggerEvent('sendAme', source, "looks inside their pack of glowsticks. It's empty.")
			end
		elseif itemID == 114 then
			local vehicle = getPedOccupiedVehicle(source)
			local noUpgrades = { Boat = true, Helicopter = true, Plane = true, Train = true, BMX = true }

			if vehicle and not noUpgrades[getVehicleType(vehicle)] --[[and getElementDimension(vehicle) > 0]] and getItemDescription(itemID, itemValue) ~= "?" then
				addUpgrade( source, vehicle, itemSlot, itemID, itemValue )
			else
				outputChatBox("Use this in a vehicle to add it as permanent upgrade.", source, 255, 194, 14)
			end
		elseif itemID == 126 then
			outputChatBox("Nice duty belt you got there. ;)", source, 0, 255, 0)
		elseif itemID == 130 then
			outputChatBox("Place this alarm system in a vehicle inventory to install it.", source, 0, 255, 0)
		elseif itemID == 132 then
			takeItemFromSlot(source, itemSlot)
			outputChatBox("You took your " .. itemValue .. " prescription.", source)
			triggerEvent('sendAme', source, "takes some prescription medicine.")
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
		elseif itemID == 134 then
			outputChatBox("$" .. exports.global:formatMoney(itemValue) .. " of currency.", source)
		elseif itemID == 137 then -- New snake cam, Adams
			triggerEvent('snakecam:toggleSnakeCam', root, source)
		elseif itemID == 138 then
			outputChatBox("Place this device in a vehicle inventory to install it.", source, 0, 255, 0)
		elseif itemID == 160 then -- Briefcase
			triggerEvent("artifacts:toggle", source, source, "briefcase")
		elseif itemID == 163 then
			triggerEvent("artifacts:toggle", source, source, "dufflebag")
		elseif itemID == 164 then
			triggerEvent("artifacts:toggle", source, source, "medicbag")
		elseif badges[itemID] then
			toggleBadge(source, badges, itemID ,itemValue) --TO BE ABLE TO USED BY MULTIPLE SYSTEMS / MAXIME
		elseif masks[itemID] then
			local data = masks[itemID]
			local mask = getElementData(source, data[1])

			if mask then
				triggerEvent('sendAme', source, data[3]:gsub("#name",itemName) .. ".")
				exports.anticheat:changeProtectedElementDataEx(source, data[1], false, true)

				if itemID == 90 then -- Helmet
					triggerEvent("artifacts:remove", source, source, "helmet")
				elseif itemID == 26 then -- Gas mask
					triggerEvent("artifacts:remove", source, source, "gasmask")
				elseif itemID == 171 then -- Biker Helmet
					triggerEvent("artifacts:remove", source, source, "bikerhelmet")
				elseif itemID == 172 then -- Full Face Helmet
					triggerEvent("artifacts:remove", source, source, "fullfacehelmet")
				elseif itemID == 240 then -- Christmas Hat
					triggerEvent("artifacts:remove", source, source, "christmashat")
				end
			elseif getPedOccupiedVehicle(source) and getElementData(getPedOccupiedVehicle(source), "job") > 0 and getVehicleOccupant(getPedOccupiedVehicle(source)) == source then
				outputChatBox("You can't use this in a Civilian vehicle.", source, 255, 0, 0)
			else
				triggerEvent('sendAme', source, data[2]:gsub("#name",itemName) ..".")
				exports.anticheat:changeProtectedElementDataEx(source, data[1], true, true)

				if itemID == 90 then -- Helmet
					if getElementData(source, "bikerhelmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "bikerhelmet", false, true)
						triggerEvent("artifacts:remove", source, source, "bikerhelmet")
					end
					if getElementData(source, "fullfacehelmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "fullfacehelmet", false, true)
						triggerEvent("artifacts:remove", source, source, "fullfacehelmet")
					end
					local customTexture = getItemTexture(itemID, itemValue, metadata)
					triggerEvent("artifacts:add", source, source, "helmet", false, customTexture)
				elseif itemID == 26 then -- Gas mask
					triggerEvent("artifacts:add", source, source, "gasmask")
				elseif itemID == 171 then -- Biker Helmet
					if getElementData(source, "helmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "helmet", false, true)
						triggerEvent("artifacts:remove", source, source, "helmet")
					end
					if getElementData(source, "fullfacehelmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "fullfacehelmet", false, true)
						triggerEvent("artifacts:remove", source, source, "fullfacehelmet")
					end
					local customTexture = getItemTexture(itemID, itemValue)
					triggerEvent("artifacts:add", source, source, "bikerhelmet", false, customTexture)
				elseif itemID == 172 then -- Full Face Helmet
					if getElementData(source, "helmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "helmet", false, true)
						triggerEvent("artifacts:remove", source, source, "helmet")
					end
					if getElementData(source, "bikerhelmet") then
						exports.anticheat:changeProtectedElementDataEx(source, "bikerhelmet", false, true)
						triggerEvent("artifacts:remove", source, source, "bikerhelmet")
					end
					local customTexture = getItemTexture(itemID, itemValue)
					triggerEvent("artifacts:add", source, source, "fullfacehelmet", false, customTexture)
				elseif itemID == 240 then -- Christmas Hat
					triggerEvent("artifacts:add", source, source, "christmashat")
				end
			end
		elseif itemID == 139 then
			outputChatBox("((Have an Admin install this in your vehicle.))", source, 255, 0, 0)
		elseif itemID == 143 then
			outputChatBox("Place this device in a vehicle inventory to install it.", source, 0, 255, 0)
		elseif itemID == 173 then
			local px, py, pz = getElementPosition ( source )
			local distance = getDistanceBetweenPoints3D(px, py, pz, 1086.8466796875, -1761.6396484375, 13.368750572205)
			if distance < 25 then
				triggerClientEvent(source, "build_carsale_gui", source, itemValue)
			else
				outputChatBox("In order to sell your vehicle, you must be at the DMV parking.", source, 0, 255, 0)
			end
		elseif itemID == 151 then
			local px, py, pz = getElementPosition(source)
			for i, v in ipairs(getElementsByType("object")) do
				local x, y, z = getElementPosition(v)
				local distance = getDistanceBetweenPoints3D(px, py, pz, x, y, z)
				if distance < 30 and getElementData(v, "dbid") == tonumber(itemValue) then
					triggerClientEvent(source, "showRampControls", source, v)
				end
			end
        elseif itemID == 181 then
        	triggerEvent("sendAme", source, "shows everyone a Smoking package.")
    	elseif (itemID == 182) then
            if hasItem( source, 107 ) then -- If da player has a lighter, how else would he smoke it dawg
                exports.global:sendLocalMeAction(source, "lights up a rolled joint.")
                outputChatBox( "(( /throwaway to throw it away, /switchhand to change hand, /passjoint to pass your joint ))", source )
                setElementData(source, "realism:joint", true) -- For the cigarette + new joint system
                triggerEvent("realism:startsmoking", source, 0) -- For the cigarette + new joint system
                takeItemFromSlot(source, itemSlot)
            else
                exports.global:sendLocalMeAction(source, "shows everyone a rolled joint.")
                outputChatBox("You'll need a lighter to use it properly.", source, 255, 0, 0)
            end
        elseif (itemID == 209) then
        	triggerEvent("gunlicense:weaponlicenses", root, source)
		elseif (itemID==210) then -- Coca-Cola Christmas Edition
			if cokeBottles[source] then
				return
			end

			takeItemFromSlot(source, itemSlot)
			local x, y, z = getElementPosition(source)
			local int = getElementInterior(source)
			local dim = getElementDimension(source)
			local bottleObj = createObject(2880, x, y, z)
			cokeBottles[source] = bottleObj
			setElementInterior(bottleObj, int)
			setElementDimension(bottleObj, dim)
			setObjectScale(bottleObj, 1)
			setElementDoubleSided(bottleObj, false)
			exports.bone_attach:attachElementToBone(bottleObj,source,11,0,0,0,0,0,0)
			thePlayer = source
			exports.global:applyAnimation(thePlayer, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			toggleAllControls(thePlayer, true, true, true)
			--triggerClientEvent(thePlayer, "onClientPlayerWeaponCheck", thePlayer)
			--exports.global:sendLocalMeAction(thePlayer, "drinks a coke.")
			triggerEvent('sendAme', thePlayer, "drinks a coke.")
			setTimer(giveHealth,4000,1,thePlayer,5)

			if getResourceState(getResourceFromName("xmas")) == "running" then
				if exports.xmas:isItChristmas() then
					setTimer(function(thePlayer)
						exports.xmas:christmasCoke(thePlayer)
					end, 4000, 1, thePlayer)
				end
			end

			setTimer(function(bottleObj, thePlayer)
					destroyElement(bottleObj)
					cokeBottles[thePlayer] = nil
				end
			,5000,1,bottleObj, thePlayer)
		elseif (itemID==213) then --pinnekjott
			giveHealth(source, 10)
			exports.global:applyAnimation(source, "FOOD", "EAT_Burger", 4000, false, true, true)
			triggerEvent('sendAme', source, "eats a pinnekjott.")
			takeItemFromSlot(source, itemSlot)
			--local thisAnimFunc = exports.global:applyAnimation
			setTimer(function()
				exports.global:applyAnimation(source, "BSKTBALL", "BBALL_Jump_Shot", 4000, false, true, true)
			end, 5000, 1)
			if getResourceState(getResourceFromName("xmas")) == "running" then
				setTimer(triggerClientEvent, 7000, 1, "xmas:santaSound", getRootElement(), "hoho", source)
			end
		elseif (itemID==214) then
			triggerEvent('sendAme', source, "takes a ".. itemName ..".")
			takeItemFromSlot(source, itemSlot)
		elseif (itemID >= 224 and itemID <= 228) then
			triggerEvent('sendAme', source, "takes some ".. itemName ..".")
			takeItemFromSlot(source, itemSlot)
		elseif itemID == 232 then -- car battery charger
			local veh = additional
			local batt = getElementData( veh, 'battery' ) or 100
			if batt == 100 then
				exports.hud:sendBottomNotification( source, itemName, "The battery of this "..exports.global:getVehicleName(veh).." is already full." )
			else
				if takeItemFromSlot( source, itemSlot) then
					exports.anticheat:setEld( veh, 'battery', math.min( 100, batt + 20 ) )
					exports.hud:sendBottomNotification( source, itemName, "The battery of this "..exports.global:getVehicleName(veh).." has been charged up by 20%." )
					triggerEvent( 'fuel:sync', veh, source )
				end
			end
		elseif itemID == 260 then
			exports.global:applyAnimation(source, "VENDING", "VEND_Drink_P", 4000, false, true, true)
			exports.global:sendLocalMeAction(source, "drinks some Ammonia.")
			setElementHealth(source, 0)
			takeItemFromSlot(source, itemSlot)
		elseif itemID == 268 then --Surfboard
			--exports.activities:toggleSurfboard(source)
		elseif itemID == 270 then
			local metaName = getItemName(itemID, itemValue, metadata)
			if getElementData(source, "gloves") then
				triggerEvent('sendAme', source, "slips a pair of " .. metaName .. " from their hands.")
				exports.anticheat:changeProtectedElementDataEx(source, "gloves", false, true)
				exports.anticheat:changeProtectedElementDataEx(source, "gloves:name", false, true)
			else
				triggerEvent('sendAme', source, "slips a pair of " .. metaName .. " onto their hands.")
				exports.anticheat:changeProtectedElementDataEx(source, "gloves", true, true)
				exports.anticheat:changeProtectedElementDataEx(source, "gloves:name", metaName, true)
			end
		elseif (armor[itemID]) then
			local data = armor[itemID]
			triggerEvent('sendAme', source, data[3]:gsub("#name",itemName) ..".")
			setPedArmor(source, armor[itemID][1])
			takeItemFromSlot(source, itemSlot)
		else
			-- WHY? This does not really mean there's a bug. Just that we didnt add any click functionality to the item. Thus, commented out. --Exciter 2014-07-11
			--outputChatBox("Error J" .. ("%04d"):format(tonumber(itemID) or 9999) .. " - Report on http://forums.owlgaming.net/forms.php?do=form&fid=2", source, 255, 0, 0)
		end
	end
end
addEvent("useItem", true)
addEventHandler("useItem", getRootElement(), useItem)
addCommandHandler("useitem",
	function(thePlayer, commandName, itemID, ...)
		if tonumber( itemID ) then
			local args = {...}
			local itemValue
			if #args > 0 then
				itemValue = table.concat(args, " ")
				itemValue = tonumber(itemValue) or itemValue
			end

			local has, slot = hasItem(thePlayer, tonumber( itemID ), itemValue)
			if has then
				triggerEvent("useItem", thePlayer, slot)
			end
		end
	end
)

addEventHandler("onPlayerSpawn", getRootElement(), function()
    if getElementData(source, "gloves") then
		exports.anticheat:changeProtectedElementDataEx(source, "gloves", false, true)
		exports.anticheat:changeProtectedElementDataEx(source, "gloves:name", false, true)
    end
end)

function npcUseItem(element, itemID)
	local source = element
	if itemID then
		local hasItem, key, itemValue, itemIndex = hasItem(element, tonumber(itemID))
		--outputDebugString(" hasItem="..tostring(hasItem).." key="..tostring(key).." itemValue="..tostring(itemValue).." itemIndex="..tostring(itemIndex))
		if(hasItem) then
			local pedname = getElementData(element, "rpp.npc.name")
			if badges[itemID] then
				if ( getElementData( source, badges[itemID][1] ) ) then
					exports.anticheat:changeProtectedElementDataEx(source, badges[itemID][1], false, true)
					if pedname then
						--exports.global:sendLocalMeAction(source, "removes " .. badges[itemID][2] .. ".")
						exports.global:sendLocalText(source, "* "..tostring(pedname).." removes "..badges[itemID][2]..".", 255, 51, 102, 5)
					end
				else
					for key, badge in pairs ( badges ) do
						if key ~= itemID then
							if ( getElementData ( source, badge[1] ) ) then
								exports.anticheat:changeProtectedElementDataEx( source, badge[1], false, true )
								if pedname then
									--exports.global:sendLocalMeAction( source, "removes " .. badge[2] .. "." )
									exports.global:sendLocalText(source, "* "..tostring(pedname).." removes "..badge[2]..".", 255, 51, 102, 5)
								end
							end
						end
					end

					exports.anticheat:changeProtectedElementDataEx( source, badges[itemID][1], removeOOC(tostring(itemValue)), true )
					if pedname then
						--exports.global:sendLocalMeAction( source, "puts on " .. badges[itemID][2] .. "." )
						exports.global:sendLocalText(source, "* "..tostring(pedname).." puts on "..badges[itemID][2]..".", 255, 51, 102, 5)
					end
					return true
				end
				--exports.global:updateNametagColor(source)
			elseif masks[itemID] then
				local data = masks[itemID]
				local mask = getElementData(source, data[1])

				if mask then
					--exports.global:sendLocalMeAction(source, data[3] .. ".")
					exports.anticheat:changeProtectedElementDataEx(source, data[1], false, true)
					if pedname then
						exports.global:sendLocalText(source, "* "..tostring(pedname).." "..data[3]..".", 255, 51, 102, 5)
					end
					return false
				elseif getPedOccupiedVehicle(source) and getElementData(getPedOccupiedVehicle(source), "job") > 0 and getVehicleOccupant(getPedOccupiedVehicle(source)) == source then
					--outputChatBox("You can't use this in a Civilian vehicle.", source, 255, 0, 0)
					return false
				else
					--exports.global:sendLocalMeAction(source, data[2] ..".")
					exports.anticheat:changeProtectedElementDataEx(source, data[1], true, true)
					if pedname then
						exports.global:sendLocalText(source, "* "..tostring(pedname).." "..data[2]..".", 255, 51, 102, 5)
					end
					return true
				end
			else
				return false
			end
		else
			outputDebugString("The ped does not have that item")
			return false
		end
	end
end

function isBadge(item)
	if badges[item] then
		return true
	else
		return false
	end
end
function isWearingBadge(element, item)
	if item then
		if isBadge(item) then
			if(getElementData(source, badges[item][1])) then
				return true
			else
				for key, badge in pairs ( badges ) do
					if key ~= item then
						if ( getElementData ( source, badge[1] ) ) then
							return true
						end
					end
				end
			end
		end
	else
		for key, badge in pairs ( badges ) do
			if ( getElementData ( source, badge[1] ) ) then
				return true
			end
		end
	end
	return false
end

function addPlayerArtifacts(player)
	if not player then player = client or source end
	if hasItem(player,48) then --backpack
		triggerEvent("artifacts:add", player, player, "backpack", true)
	end
	if hasItem(player,162) then --body armour
		triggerEvent("artifacts:add", player, player, "kevlar")
	end
	if hasItem(player,163) then --duffle bag
		triggerEvent("artifacts:add", player, player, "dufflebag")
	end
	if hasItem(player,164) then --medical bag
		triggerEvent("artifacts:add", player, player, "medicbag")
	end
	if hasItem(player,268) then --surfboard
		local hasItem, slot, itemValue, itemIndex, metadata = hasItem(player, 268)
		local customTexture = getItemTexture(268, itemValue, metadata)
		triggerEvent("artifacts:add", player, player, "surfboard", false, customTexture)
	end
end
addEvent("item-system:addPlayerArtifacts", true)
addEventHandler("item-system:addPlayerArtifacts", getRootElement(), addPlayerArtifacts)
function addAllArtifacts()
	for k,v in ipairs(getElementsByType("player")) do
		addPlayerArtifacts(v)
	end
end
addEvent("item-system:addAllArtifacts", true)
addEventHandler("item-system:addAllArtifacts", getRootElement(), addAllArtifacts)

function explodeFlash(obj)
	local players = exports.global:getNearbyElements(obj, "player")

	destroyElement(obj)
	for key, value in ipairs(players) do
		local gasmask = getElementData(value, "gasmask")

		if not (gasmask) then
			playSoundFrontEnd(value, 47)
			fadeCamera(value, false, 0.5, 255, 255, 255)
			setTimer(cancelEffect, 5000, 1, value)
			setTimer(playSoundFrontEnd, 1000, 1, value, 48)
		end
	end
end

function cancelEffect(thePlayer)
	fadeCamera(thePlayer, true, 6.0)
end

tags = {1524, 1525, 1526, 1527, 1528, 1529, 1530, 1531 }

function destroyGlowStick(marker)
	destroyElement(marker)
end

function destroyItem(itemID)
	if isPedDead(source) or getElementData(source, "injuriedanimation") then return end
	local itemName = ""
	if itemID and itemID > 0 then
		local itemSlot = itemID
		local item = getItems( source )[itemSlot]
		local metadata = item[5]
		if item then
			local itemID = item[1]
			local itemValue = item[2]

			if itemID == 48 and countItems( source, 48 ) == 1 then -- backpack
				if getCarriedWeight( source ) - getItemWeight( 48, 1 ) > 10 then
					return
				end
			end


			if itemID == 126 and countItems( source, 126 ) == 1 then -- duty belt
				if getCarriedWeight( source ) - getItemWeight( 126, 1 ) > 10 then
					return
				end
			end

			if itemID == 134 then
				exports.global:takeMoney(source, itemValue)
			end

			itemName = getItemName( itemID, itemValue, metadata )
			takeItemFromSlot(source, itemSlot)

			triggerClientEvent(source, "item:updateclient", source)

			doItemGiveawayChecks(source, itemID)
		
		else
			return
		end
	else
		if itemID == -100 then
			setPedArmor(source, 0)
			itemName = "Body Armor"
		else
			exports.global:takeWeapon(source, tonumber(-itemID))
			itemName = getWeaponNameFromID( -itemID )
		end
	end
	outputChatBox("You destroyed a " .. itemName .. ".", source, 255, 194, 14)
	triggerEvent('sendAme', source, "destroyed a "..itemName..".")
end
addEvent("destroyItem", true)
addEventHandler("destroyItem", getRootElement(), destroyItem)

function isVowel(text)
	if not text or text == "" then
		return false
	end
	text = string.sub(text, 1, 1)
	text = string.lower(text)
	return text == "a" or text == "o" or text == "i" or text == "u" or text == "e"
end

function showItem(itemName)
	if isPedDead(source) or getElementData(source, "injuriedanimation") then return end
	triggerEvent('sendAme', source, "shows everyone "..(isVowel(itemName) and "an" or "a").." " .. removeOOC(itemName) .. ".")
end
addEvent("showItem", true)
addEventHandler("showItem", getRootElement(), showItem)

function resetAnim(thePlayer)
	exports.global:removeAnimation(thePlayer)
end

function showInventoryRemote(thePlayer, commandName, targetPlayer)
	if exports.integration:isPlayerTrialAdmin(thePlayer) then
		if not (targetPlayer) then
			outputChatBox("SYNTAX: /" .. commandName .. " [Player Partial Nick / ID]", thePlayer, 255, 194, 14)
		else
			local targetPlayer = exports.global:findPlayerByPartialNick(thePlayer, targetPlayer)
			if targetPlayer then
				triggerEvent("subscribeToInventoryChanges",thePlayer,targetPlayer)
				triggerClientEvent(thePlayer,"showInventory",thePlayer,targetPlayer, "admin")
			end
		end
	end
end
addCommandHandler("showinv", showInventoryRemote, false, false)



--
-- are we watching TV?
--
function isWatchingTV( player )
	return exports['freecam-tv']:isPlayerFreecamEnabled( player )
end

function sortMoney(thePlayer, commandName, moneyAmountCurrent, moneyAmountSort)
	if not (moneyAmountCurrent) or not (moneyAmountSort) or tonumber(moneyAmountCurrent) < 0 or tonumber(moneyAmountSort) < 0 then
		outputChatBox("SYNTAX: /" .. commandName .. " [Money amount you have] [Money amount to sort]", thePlayer, 255, 194, 14)
	else
		local playerMoney = getElementData(thePlayer, "money")
		if tonumber(moneyAmountCurrent) <= tonumber(playerMoney) and tonumber(moneyAmountSort) <= tonumber(playerMoney) then
			if hasItem(thePlayer, 134, tonumber(moneyAmountCurrent)) then
				outputChatBox("You sorted $" .. exports.global:formatMoney(moneyAmountSort) .. " out of $" .. exports.global:formatMoney(moneyAmountCurrent) .. ".", thePlayer)
				takeItem(thePlayer, 134, tonumber(moneyAmountCurrent))
				giveItem(thePlayer, 134, tonumber(moneyAmountSort))
				giveItem(thePlayer, 134, tonumber(moneyAmountCurrent)-tonumber(moneyAmountSort))
			else
				outputChatBox("You don't have that amount of money in one sorted item.", thePlayer, 255, 0, 0)
			end
		else
			outputChatBox("You don't have that amount of money.", thePlayer, 255, 0, 0)
		end
	end
end
addCommandHandler("sortmoney", sortMoney)

function combineMoney(thePlayer, commandName)
		for i = 134, 134 do
			while exports['item-system']:takeItem(thePlayer, i) do
			end
		end
		local playerMoney = getElementData(thePlayer, "money")
		if tonumber(playerMoney) > 0 then
			exports.global:giveItem(thePlayer, 134, playerMoney)
			outputChatBox("You combined $" .. exports.global:formatMoney(playerMoney) .. ".", thePlayer)
		else
			outputChatBox("You can't combine money you don't have!", thePlayer, 255, 0, 0)
		end
end
addCommandHandler("combinemoney", combineMoney)

function toggleBadge(source, badges, itemID , itemValue)
	if ( getElementData( source, badges[itemID][1] ) ) then
		exports.anticheat:changeProtectedElementDataEx(source, badges[itemID][1], false, true)
		if itemID == 122 or itemID == 123 or itemID == 124 or itemID == 125 or itemID == 135 or itemID == 136 or itemID == 158 or itemID == 168 then
			triggerEvent('sendAme',  source, "removes a " .. badges[itemID][1] .. " from their head." )
		else
			triggerEvent('sendAme', source, "removes a " .. badges[itemID][1] .. ".")
		end
	else
		for key, badge in pairs ( badges ) do
			if key ~= itemID then
				if ( getElementData ( source, badge[1] ) ) then
					exports.anticheat:changeProtectedElementDataEx( source, badge[1], false, true )
					if itemID == 122 or itemID == 123 or itemID == 124 or itemID == 125 or itemID == 135 or itemID == 136 or itemID == 158 or itemID == 168 then
					triggerEvent('sendAme',  source, "removes a " .. badge[1] .. " from their head." )
					else
					triggerEvent('sendAme',  source, "removes a " .. badge[1] .. "." )
					end
				end
			end
		end

		if itemID == 122 or itemID == 123 or itemID == 124 or itemID == 125 or itemID == 135 or itemID == 136 or itemID == 158 or itemID == 168 then
		exports.anticheat:changeProtectedElementDataEx( source, badges[itemID][1], true )
		triggerEvent('sendAme',  source, "wraps a " .. badges[itemID][1] .. " around their head." )
		else
			exports.anticheat:changeProtectedElementDataEx( source, badges[itemID][1], removeOOC(itemValue), true )
			triggerEvent('sendAme',  source, "puts on a " .. badges[itemID][1] .. "." )
		end
	end
	exports.global:updateNametagColor(source)
end
addEvent("item-system:toggleBadge", true)
addEventHandler("item-system:toggleBadge", getRootElement(), toggleBadge)


addEventHandler("onPlayerQuit", getRootElement(),
function()
	if cokeBottles[source] then
		destroyElement(cokeBottles[source])
		cokeBottles[source] = nil
	end
end)

addEventHandler("accounts:characters:change", getRootElement(),
function()
	if cokeBottles[client] then
		destroyElement(cokeBottles[client])
		cokeBottles[client] = nil
	end
end)