Last active
August 29, 2015 13:56
-
-
Save Mitch528/9040095 to your computer and use it in GitHub Desktop.
PlanetProtect Plugin for SharpStar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PlanetProtect Plugin | |
-- | |
-- Requires: admincommands.lua, permissions.lua | |
-- | |
-- | |
-- Commands: | |
-- /protectplanet | |
-- /unprotectplanet | |
-- /planetallow <player> | |
-- /planetunallow <player> | |
-- | |
-- Permissions: | |
-- planetprotect | |
require 'admincommands' | |
local planetProtect = nil | |
function onLoad() | |
planetProtect = properties:GetArray("planetProtect") | |
SubscribeToEvent("chatSent", cSent) | |
SubscribeToEvent("packetReceived", packetRecv) | |
SubscribeToEvent("entityCreate", entCreate) | |
end | |
function onUnload() | |
end | |
function packetRecv(packet, client) | |
local id = packet.PacketId | |
if id == 31 or id == 32 or id == 33 or id == 34 or id == 35 or id == 26 or id == 27 or id == 28 or id == 25 then | |
if not adminCommands.isAdmin(client.Server.Player.Name) then | |
if client.Server.Player.Coordinates ~= nil and not client.Server.Player.OnShip | |
and properties:Contains("planetProtect", client.Server.Player.Coordinates.Planet) then | |
if not properties:Contains("planetProtect_".. client.Server.Player.Coordinates.Planet, client.Server.Player.UUID) then | |
packet.Ignore = true | |
end | |
end | |
end | |
end | |
end | |
function entCreate(packet, client) | |
if not adminCommands.isAdmin(client.Server.Player.Name) then | |
if client.Server.Player.Coordinates ~= nil and not client.Server.Player.OnShip | |
and properties:Contains("planetProtect", client.Server.Player.Coordinates.Planet) then | |
if not properties:Contains("planetProtect_".. client.Server.Player.Coordinates.Planet, client.Server.Player.UUID) then | |
local ents = packet.Entities | |
local projectileFired = false | |
for i = 1, ents.Count do | |
local v = ents[i - 1] | |
if v.EntityType == EntityType.Projectile then | |
projectileFired = true | |
end | |
end | |
if projectileFired then | |
packet.Ignore = true | |
end | |
end | |
end | |
end | |
end | |
function cSent(packet, client) | |
local index = string.find(packet.Message, "%s") | |
if adminCommands.isAdmin(client.Server.Player.Name) or perms.hasPermission("planetprotect", client.Server.Player.Name) then | |
if string.sub(packet.Message, 1, 1) == "/" then | |
local command = string.lower(string.sub(packet.Message, 2, string.len(packet.Message))) | |
if index ~= nil then | |
command = string.lower(string.sub(packet.Message, 2, index - 1)) | |
local args = string.sub(packet.Message, index + 1, string.len(packet.Message)) | |
if command == "planetallow" then | |
packet.Ignore = true | |
if client.Server.Player.OnShip then | |
sendChatMessage(client, "You are not on a planet!") | |
else | |
if allowPlanet(args, client.Server.Player.Coordinates.Planet) then | |
sendChatMessage(client, args.. " is now allowed to build on this planet!") | |
end | |
end | |
elseif command == "planetunallow" then | |
packet.Ignore = true | |
if client.Server.Player.OnShip then | |
sendChatMessage(client, "You are not on a planet!") | |
else | |
if disallowPlanet(args, client.Server.Player.Coordinates.Planet) then | |
sendChatMessage(client, args.. " is now not allowed to build on this planet!") | |
end | |
end | |
end | |
else | |
if command == "protectplanet" then | |
packet.Ignore = true | |
if client.Server.Player.OnShip then | |
sendChatMessage(client, "You are not on a planet!") | |
else | |
if properties:Contains("planetProtect", client.Server.Player.Coordinates.Planet) then | |
sendChatMessage(client, "This planet is already protected!") | |
else | |
local allowedPlayers = { client.Server.Player.UUID } | |
properties:AppendArray("planetProtect", client.Server.Player.Coordinates.Planet) | |
properties:PutArray("planetProtect_".. client.Server.Player.Coordinates.Planet, ToArray(allowedPlayers)) | |
properties:Save() | |
sendChatMessage(client, "This planet is now protected!") | |
end | |
end | |
elseif command == "unprotectplanet" then | |
packet.Ignore = true | |
if client.Server.Player.OnShip then | |
sendChatMessage(client, "You are not on a planet!") | |
else | |
local existed = properties:Remove("planetProtect", client.Server.Player.Coordinates.Planet) | |
if existed then | |
properties:Save() | |
sendChatMessage(client, "This planet is no longer protected!") | |
else | |
sendChatMessage(client, "This planet is not protected!") | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
function allowPlanet(playerName, planet) | |
local client = getPlayerClientByName(playerName) | |
if client == nil then | |
return false | |
end | |
properties:AppendArray("planetProtect_".. planet, client.Server.Player.UUID) | |
properties:Save() | |
return true | |
end | |
function disallowPlanet(playerName, planet) | |
local client = getPlayerClientByName(playerName) | |
if client == nil then | |
return false | |
end | |
properties:Remove("planetProtect_".. planet, client.Server.Player.UUID) | |
properties:Save() | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment