Skip to content

Instantly share code, notes, and snippets.

@Mitch528
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mitch528/9243561 to your computer and use it in GitHub Desktop.
Save Mitch528/9243561 to your computer and use it in GitHub Desktop.
PlayerCommands Plugin for SharpStar
-- PlayerCommands plugin
--
-- Commands:
-- /starterkit
-- /addkititem <item name> <amount> <interval in minutes> (-1 for only once)
-- /removekititem <item name>
-- /who <page number>
import("System")
local adminProperties = nil
local kitItems = nil
function onLoad()
properties:Save()
adminProperties = PluginProperties("admincommands.lua", plugindir)
adminProperties:Load()
kitItems = properties:GetPropertyArray("kitItems")
if kitItems == nil then
kitItems = PluginPropertyArray()
properties.Properties:Add(PluginProperty("kitItems", kitItems))
end
SubscribeToEvent("chatSent", cSent)
end
function cSent(packet, client)
local uuid = client.Server.Player.UUID
local message = packet.Message
if string.sub(message, 1, 1) == "/" then
local splitArr = SplitString(message, " ")
local command = string.sub(splitArr[0], 2, string.len(splitArr[0]))
if command == "who" then
packet.Ignore = true
if splitArr.length == 2 then
local page = tonumber(splitArr[1])
if page ~= nil then
sendPlayerList(client, page)
end
else
sendPlayerList(client, 1)
end
elseif command == "starterkit" then
packet.Ignore = true
giveStarterKit(client)
elseif command == "addkititem" then
packet.Ignore = true
if adminProperties:Contains("admins", uuid) then
if splitArr.length == 4 then
local kitItem = PluginPropertyObject()
kitItem:Add(PluginProperty("name", splitArr[1]))
kitItem:Add(PluginProperty("amount", tonumber(splitArr[2])))
kitItem:Add(PluginProperty("time", tonumber(splitArr[3])))
kitItem:Add(PluginProperty("players", PluginPropertyArray()))
kitItems:Add(kitItem)
properties:Save()
kitItems = properties:GetPropertyArray("kitItems")
sendChatMessage(client, "Added item to starter kit!")
else
sendChatMessage(client, "Invalid Syntax! Syntax: /addkititem <item name> <item amount> <interval in minutes> (-1 for only once)")
end
end
elseif command == "removekititem" then
packet.Ignore = true
if adminProperties:Contains("admins", uuid) then
if splitArr.length == 2 then
local toRemove = {}
for i = 1, kitItems.Count do
local item = properties:GetByIndex(kitItems, i - 1)
if item ~= nil and string.lower(item["name"]:ToObject(ctype(String))) == string.lower(splitArr[1]) then
table.insert(toRemove, item)
end
end
for k, v in ipairs(toRemove) do
v:Remove()
end
properties:Save()
else
sendChatMessage(client, "Invalid Syntax! Syntax: /removekititem <item name>")
end
end
end
end
end
function giveStarterKit(client)
if kitItems ~= nil then
local uuid = client.Server.Player.UUID
for i = 1, kitItems.Count do
local item = properties:GetByIndex(kitItems, i - 1)
local itemAmount = item["amount"]:ToObject(ctype(Int32))
local itemTime = item["time"]:ToObject(ctype(Int32))
local time = os.time()
if itemAmount ~= nil and itemTime ~= nil then
local players = item["players"]
local player = nil
for x = 1, players.Count do
local plr = properties:GetByIndex(players, x - 1)
if plr ~= nil and plr[uuid] ~= nil then
player = plr
break
end
end
if player == nil then
local plrObj = PluginPropertyObject()
plrObj:Add(PluginProperty("lastTime", time))
player = PluginPropertyObject(PluginProperty(uuid, plrObj))
players:Add(player)
end
local lastTime = player[uuid]["lastTime"]:ToObject(ctype(Int32))
if itemTime == -1 and lastTime ~= time then
else
if os.difftime(time, lastTime) >= (itemTime * 60) or time == lastTime then
properties:SetObject(player[uuid], "lastTime", os.time())
giveItem(client.Server.Player.Name, item["name"]:ToObject(ctype(String)), itemAmount)
end
end
end
end
properties:Save()
end
end
function sendPlayerList(client, page)
sendChatMessage(client, "Player List:")
local clients = GetPlayerClients()
for i = (page - 1) * 5, ((page - 1) * 5) + 5 do
if i < clients.length then
sendChatMessage(client, tostring(i + 1).. " ".. clients[i].Server.Player.Name)
else
break
end
end
end
function giveItem(playerName, itemName, amount)
local client = getPlayerClientByName(playerName)
if client == nil then
return false
end
local newPacket = GiveItemPacket()
newPacket.ItemName = itemName
newPacket.Count = amount + 1
SendPacket(client, newPacket)
return true
end
function sendChatMessage(client, message)
local chatRecvPacket = ChatReceivedPacket()
chatRecvPacket.Name = "Server"
chatRecvPacket.Message = message
SendPacket(client.Server.PlayerClient, chatRecvPacket)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment