Mitch528
3/20/2014 - 4:06 PM

Permissions Plugin for SharpStar

Permissions Plugin for SharpStar

perms = {}
local permissions = nil

local properties = PluginProperties("permissions.lua", plugindir)
properties:Load()

permissions = properties:GetPropertyArray("permissions")

if permissions == nil then

	permissions = PluginPropertyArray()

	properties.Properties:Add("permissions", permissions)

end

function perms.hasPermission(permission, player)

	local client = getPlayerClientByName(player)

	if client == nil then
		return false
	end

	player = client.Server.Player.UUID

	local plrPerm = perms.getPermissionByUuid(player)

	if plrPerm == nil then
		return false
	end

	return plrPerm[string.lower(permission)] ~= nil and plrPerm[string.lower(permission)]

end

function perms.removePermission(permission, player)

	local client = getPlayerClientByName(player)

	if client == nil then
		return false
	end

	player = client.Server.Player.UUID

	local plrPerm = perms.getPermissionByUuid(player)

	if plrPerm == nil then
		return false
	end

	if plrPerm[string.lower(permission)] ~= nil then
		plrPerm:Remove(string.lower(permission))
	end

	properties:Save()

end

function perms.addPermission(permission, player)

	local client = getPlayerClientByName(player)

	if client == nil then
		return false
	end

	player = client.Server.Player.UUID

	local plrPerm = perms.getPermissionByUuid(player)

	if plrPerm == nil then

		plrPerm = PluginPropertyObject()
		plrPerm:Add(PluginProperty("uuid", player))
		plrPerm:Add(PluginProperty(string.lower(permission), true))

		permissions:Add(plrPerm)

	else
		plrPerm:Add(PluginProperty(string.lower(permission), true))
	end

	properties:Save()

end

function perms.getPermissionByUuid(uuid)

	local plrPerm = nil

	for i = 1, permissions.Count do

		local perm = properties:GetByIndex(permissions, i - 1)

		if perm["uuid"]:ToObject(ctype(String)) == uuid then

			plrPerm = perm

		end

	end

	return plrPerm

end

function perms.reloadPermissions()
	permissions:Load()
end

function getPlayerClientByName(playerName)

	playerName = string.lower(playerName)

	local clients = GetPlayerClients()

	local clientToReturn = nil

	for i = 1, clients.length do
	
		local clientName = string.lower(clients[i - 1].Server.Player.Name)

		if clientName == playerName then

			clientToReturn = clients[i - 1]

			break

		end

	end

	return clientToReturn

end