-- Серверная часть смены скинов
local PLUGIN = PLUGIN

local allowedEntities = {
    ["entity_drone_base"] = true,
    ["entity_drone_bomb"] = true,
    ["sw_m1151"] = true,
    ["sw_gaz2330"] = true,
}

local skinNames = {
    ["entity_drone_base"] = {
        [0] = "Стандартный дрон",
        [1] = "Белый дрон",
        [2] = "Медицинский дрон",
        [3] = "Полицейский дрон",
		[4] = "Блатной дрон",
		[5] = "Золотой дрон",
		[6] = "Дрон невидимка",
		[7] = "Камуфляжный дрон",
		[8] = "Потрёпаный дрон",
    },
    ["entity_drone_bomb"] = {
        [0] = "Стандартная бомба",
        [1] = "Камуфляжная бомба",
        [2] = "Золотая бомба",
    },
    ["sw_m1151"] = {
        [0] = "Стандартный M1151",
        [1] = "Хамви Военной полиции",
    },
    ["sw_gaz2330"] = {
        [0] = "Стандартный GAZ-2330",
        [1] = "ГАЗ-2330 Военной Полиции",
    },
}

net.Receive("CheckAllowedEntity", function(len, ply)
    local ent = net.ReadEntity()
    if IsValid(ent) and allowedEntities[ent:GetClass()] then
        net.Start("EntityAllowed")
        net.Send(ply)
    end
end)

net.Receive("RequestSkinData", function(len, ply)
    local ent = net.ReadEntity()
    if IsValid(ent) and allowedEntities[ent:GetClass()] then
        local class = ent:GetClass()
        local totalSkins = ent:SkinCount()
        local skins = skinNames[class] or {}

        net.Start("ReceiveSkinData")
        net.WriteInt(totalSkins, 8)
        net.WriteTable(skins)
        net.Send(ply)
    end
end)

net.Receive("ChangeEntitySkin", function(len, ply)
    local ent = net.ReadEntity()
    local skinID = net.ReadInt(8)

    if IsValid(ent) and allowedEntities[ent:GetClass()] then
        local totalSkins = ent:SkinCount() - 1
        if skinID >= 0 and skinID <= totalSkins then
            ent:SetSkin(skinID)
        end
    end
end)

