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 0 You must be signed in to fork a gist
  • Save Mitch528/9038222 to your computer and use it in GitHub Desktop.
Save Mitch528/9038222 to your computer and use it in GitHub Desktop.
AdminCommands Plugin for SharpStar
-- AdminCommands plugin
--
-- Requires: permissions.lua
--
-- Commands:
-- /firstop
-- /banuuid <player name>
-- /banip <player name>
-- /kick <player name>
-- /giveitem <item name> <item amount> <player name>
-- /op <player name>
-- /deop <player name>
-- /warp <player name>
-- /warpother <player name>
-- /broadcast <message>
-- /addpermission <permission> <player>
-- /removepermission <permission> <player>
-- Permissions:
-- kick
-- ban
-- giveitem
-- op
-- deop
-- warp
-- broadcast
-- addperm
-- removeperm
-- setmotd
import("System")
assert(require 'permissions', "Permissions plugin is required!")
local uuidbans = nil
local ipBans = nil
local firstOp = nil
local properties = PluginProperties("admincommands.lua", plugindir)
properties:Load()
adminCommands = {}
function onLoad()
uuidbans = properties:GetArray("uuidbans")
ipBans = properties:GetArray("ipBans")
firstOp = properties:GetString("firstOp")
if firstOp == "" then
firstOp = "true"
end
SubscribeToEvent("connectionResponse", cResp)
SubscribeToEvent("afterConnectionResponse", acResp)
RegisterCommand("op", opCmd)
RegisterCommand("deop", deopCmd)
RegisterCommand("firstop", firstOpCmd)
RegisterCommand("kick", kickBanCmd)
RegisterCommand("banuuid", kickBanCmd)
RegisterCommand("banip", kickBanCmd)
RegisterCommand("giveitem", giveItemCmd)
RegisterCommand("warp", warpCmd)
RegisterCommand("broadcast", broadcastCmd)
RegisterCommand("addpermission", addPermCmd)
RegisterCommand("removepermission", removePermCmd)
RegisterCommand("setmotd", setMotdCmd)
end
function onUnload()
end
function acResp(packet, client)
local motd = properties:GetString("motd")
if motd ~= nil and motd ~= "" then
sendChatMessage(client, motd)
end
end
function cResp(packet, client)
if properties:Contains("uuidbans", client.Server.Player.UUID) then
packet.Success = false
packet.RejectionReason = properties:GetString("uuidban_".. client.Server.Player.UUID)
client:ForceDisconnect()
elseif properties:Contains("ipBans", client.Server.PlayerClient.IPAddress) then
packet.Success = false
packet.RejectionReason = properties:GetString("ipban_".. client.Server.PlayerClient.IPAddress)
client:ForceDisconnect()
end
end
function setMotdCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if isAdmin or perms.hasPermission("setmotd", client.Server.Player.Name) then
properties:PutString("motd", joinArgs)
sendChatMessage(client, "MOTD Set!")
end
end
function opCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if isAdmin or perms.hasPermission("op", client.Server.Player.Name) then
adminCommands.op(joinArgs)
sendChatMessage(client, "Player ".. client.Server.Player.Name.. " is now an op!")
end
end
function deopCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if isAdmin or perms.hasPermission("deop", client.Server.Player.Name) then
adminCommands.deop(joinArgs)
sendChatMessage(client, "Player ".. client.Server.Player.Name.. " is no longer an op!")
end
end
function firstOpCmd(client, command, args)
if firstOp == "true" then
if adminCommands.op(client.Server.Player.Name) then
sendChatMessage(client, "You are now an op!")
firstOp = "false"
properties:PutString("firstOp", "false")
properties:Save()
else
sendChatMessage(client, "Could not op player!")
end
end
end
function kickBanCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local playerToBan = ""
local reason = ""
local joinArgs = JoinString(args, " ")
local quoteIndex = string.find(joinArgs, "\"")
if quoteIndex ~= nil then
reason = string.sub(joinArgs, quoteIndex + 1, string.len(joinArgs) - 1)
playerToBan = string.sub(joinArgs, 1, quoteIndex - 2)
else
playerToBan = joinArgs
end
if string.lower(command) == "kick" then
reason = "You have been kicked!"
else
reason = "You have been banned!"
end
local ban = nil
local hasPerm = false
if perms.hasPermission("ban", client.Server.Player.Name) or isAdmin then
if string.lower(command) == "banuuid" then
ban = adminCommands.banPlayerByName(playerToBan, reason)
elseif string.lower(command) == "banip" then
ban = adminCommands.banPlayerIP(playerToBan, reason)
end
hasPerm = true
end
local kick = nil
if perms.hasPermission("kick", client.Server.Player.Name) or isAdmin then
kick = adminCommands.kickPlayer(playerToBan, reason)
hasPerm = true
end
if hasPerm == false then
sendChatMessage(client, "You do not have permission to use this command!")
elseif (kick ~= nil and kick) or (ban ~= nil and ban) then
sendChatMessage(client, "Done!")
else
sendChatMessage(client, "There is no player by that name!")
end
end
function giveItemCmd(client, command, args)
if args.length < 3 then
sendChatMessage(client, "Invalid Syntax!")
else
local item = args[0]
local amount = tonumber(args[1])
local player = ""
if args.length >= 3 then
for i = 3, args.length do
if i == 3 then
player = args[i - 1]
else
player = player.. " ".. args[i - 1]
end
end
end
if amount == nil then
sendChatMessage(client, "Invalid Amount!")
else
adminCommands.giveItem(player, item, amount)
end
end
end
function warpCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if perms.hasPermission("warp", client.Server.Player.Name) or isAdmin then
adminCommands.warpPlayerToShip(client.Server.Player.Name, joinArgs)
end
end
function warpOtherCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if perms.hasPermission("warp", client.Server.Player.Name) or isAdmin then
adminCommands.warpPlayerToShip(joinArgs, client.Server.Player.Name)
end
end
function broadcastCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if perms.hasPermission("broadcast", client.Server.Player.Name) or isAdmin then
adminCommands.broadcastMessage(joinArgs)
end
end
function addPermCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if perms.hasPermission("addperm", client.Server.Player.Name) or isAdmin then
local perm = args[0]
local plr = string.sub(joinArgs, string.len(perm) + 2, string.len(joinArgs))
perms.addPermission(perm, plr)
end
end
function removePermCmd(client, command, args)
local isAdmin = adminCommands.isAdmin(client.Server.Player.Name)
local joinArgs = JoinString(args, " ")
if perms.hasPermission("removeperm", client.Server.Player.Name) or isAdmin then
local perm = args[0]
local plr = string.sub(joinArgs, string.len(perm) + 2, string.len(joinArgs))
perms.removePermission(perm, plr)
end
end
function adminCommands.isAdmin(player)
local client = getPlayerClientByName(player)
if client == nil then
return false
end
return properties:Contains("admins", client.Server.Player.UUID)
end
function adminCommands.broadcastMessage(message)
local clients = GetPlayerClients()
for i = 1, clients.length do
sendChatMessage(clients[i - 1], message)
end
end
function adminCommands.warpPlayerToShip(fromPlayer, toPlayer)
local client = getPlayerClientByName(fromPlayer)
if client == nil then
return false
end
local newPacket = WarpCommandPacket()
newPacket.WarpType = WarpType.WarpOtherShip
newPacket.Player = toPlayer
SendPacket(client.Server.ServerClient, newPacket)
return true
end
function adminCommands.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 adminCommands.op(playerName)
local client = getPlayerClientByName(playerName)
if client == nil then
return false
end
properties:AppendArray("admins", client.Server.Player.UUID)
properties:Save()
return true
end
function adminCommands.deop(playerName)
local client = getPlayerClientByName(playerName)
local ops = properties:GetArray("admins")
local opsTable = toTable(ops)
if client == nil then
return false
end
for i, v in ipairs(opsTable) do
if v == client.Server.Player.UUID then
table.remove(opsTable, i)
end
end
properties:PutArray("admins", ToArray(opsTable))
properties:Save()
return true
end
function adminCommands.kickPlayer(playerName, kickReason)
local client= getPlayerClientByName(playerName)
if client == nil then
return false
end
sendChatMessage(client, kickReason)
client:ForceDisconnect()
return true
end
function adminCommands.banPlayerByName(playerName, banReason)
local client = getPlayerClientByName(playerName)
if client == nil then
return false
end
if uuidbans == nil then
uuidbans = {}
else
local newBans = toTable(uuidbans);
uuidbans = newBans
end
local uuid = client.Server.Player.UUID
table.insert(uuidbans, uuid)
properties:PutString("uuidban_".. uuid, banReason)
properties:PutArray("uuidbans", ToArray(uuidbans))
properties:Save()
uuidbans = properties:GetArray("uuidbans")
return true
end
function adminCommands.banPlayerIP(playerName, banReason)
local client = getPlayerClientByName(playerName)
if client == nil then
return false
end
if ipBans == nil then
ipBans = {}
else
ipBans = toTable(ipBans)
end
local ipa = client.Server.PlayerClient.IPAddress
table.insert(ipBans, ipa)
properties:PutString("ipban_".. ipa, banReason)
properties:PutArray("ipBans", ToArray(ipBans))
properties:Save()
ipBans = properties:GetArray("ipBans")
return true
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
function sendChatMessage(client, message)
local chatRecvPacket = ChatReceivedPacket()
chatRecvPacket.Name = "Server"
chatRecvPacket.Message = message
SendPacket(client.Server.PlayerClient, chatRecvPacket)
end
function toTable(array)
local tbl = {}
for i = 1, array.length do
table.insert(tbl, array[i - 1])
end
return tbl
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment