if !SERVER then return end

local entTable = {
  ["zpizmak_customertable"] = true,
  ["zpizmak_opensign"] = true,
  ["zpizmak_oven"] = true,
  ["zpizmak_fridge"] = true,
}

hook.Add("playerBoughtCustomEntity", "zpizmak_SetOwnerOnEntBuy", function(ply, enttbl, ent, price)
    if entTable[ent:GetClass()] then
        zpizmak.f.SetOwnerID(ent, ply)
    end
end)

local DeletebyJobChangeTable = {
    ["zpizmak_customertable"] = true,
    ["zpizmak_fridge"] = true,
    ["zpizmak_ingredient"] = true,
    ["zpizmak_opensign"] = true,
    ["zpizmak_oven"] = true,
    ["zpizmak_pizza"] = true,
}

local function zpizmak_RemovePlayerEntities(ply)
    for k, v in pairs(ents.GetAll()) do
        if (IsValid(v) and DeletebyJobChangeTable[v:GetClass()] and zpizmak.f.IsOwner(ply, v)) then
            v:Remove()
        end
    end
end

hook.Add("OnPlayerChangedTeam", "zpizmak_OnPlayerChangedTeam", function(ply, before, after)
    zpizmak_RemovePlayerEntities(ply)
end)

hook.Add("PlayerDisconnected", "zpizmak_PlayerDisconnected", function(ply)
    zpizmak_RemovePlayerEntities(ply)
end)
