Better Home System

This commit is contained in:
Cooldude2606
2018-10-26 17:08:11 +01:00
parent b9435d08ba
commit f91fed2196
6 changed files with 88 additions and 25 deletions

View File

@@ -8,28 +8,40 @@ local global = global{}
--- Sets the home for a player
-- @command set-home
commands.add_command('set-home', 'Set your home position', {}, function(event,args)
commands.add_command('home', 'Allows you to set, remove and goto your homes', {
['command'] = {false,'string-list',{'set','remove','goto','list','return'}},
['name'] = {false,'string-len',10}
}, function(event,args)
local player = Game.get_player(event)
if not global[player.index] then global[player.index] = {player.force.get_spawn_position(player.surface),player.force.get_spawn_position(player.surface)} end
global[player.index][1] = {math.floor(player.position.x),math.floor(player.position.y)}
player_return('Home set at: ('..math.floor(player.position.x)..','..math.floor(player.position.y)..')')
end)
--- Teleports a player back to their home
-- @command home
commands.add_command('home', 'Go to you home position', {}, function(event,args)
local player = Game.get_player(event)
if not global[player.index] then global[player.index] = {player.force.get_spawn_position(player.surface),player.force.get_spawn_position(player.surface)} end
global[player.index][2] = {math.floor(player.position.x),math.floor(player.position.y)}
player.teleport(player.surface.find_non_colliding_position('player',global[player.index][1],32,1),player.surface)
end)
--- Returns a player back to the place before using /home
-- @command return
commands.add_command('return', 'Return to your previous position after using /home', {}, function(event,args)
local player = Game.get_player(event)
if not global[player.index] then global[player.index] = {player.force.get_spawn_position(player.surface),player.force.get_spawn_position(player.surface)} end
local _temp = {math.floor(player.position.x),math.floor(player.position.y)}
player.teleport(player.surface.find_non_colliding_position('player',global[player.index][2],32,1),player.surface)
global[player.index][2] = _temp
if not global[player.index] then local spawn_pos = player.force.get_spawn_position(player.surface) global[player.index] = {Spawn={spawn_pos.x,spawn_pos.y},_m=3,_n=1,_r={spawn_pos.x,spawn_pos.y}} end
local homes = global[player.index]
local command = args.command
local name = args.name
if command == 'set' then
local pos = {math.floor(player.position.x),math.floor(player.position.y)}
if homes._n+1 > homes._m then player_return{'ExpGamingCommands-home.too-many-homes',homes._m} return commands.error end
homes[name] = pos
homes._n=homes._n+1
player_return{'ExpGamingCommands-home.set',name,pos[1],pos[2]}
elseif command == 'remove' then
if not homes[name] then player_return{'ExpGamingCommands-home.invalid',name} end
homes[name] = nil
homes._n=homes._n-1
player_return{'ExpGamingCommands-home.remove',name}
elseif command == 'goto' then
if not homes[name] then player_return{'ExpGamingCommands-home.invalid',name} end
local pos = {math.floor(player.position.x),math.floor(player.position.y)}
player.teleport(player.surface.find_non_colliding_position('player',homes[name],32,1),player.surface)
homes._r = pos
player_return{'ExpGamingCommands-home.goto',name}
elseif command == 'return' then
local pos = {math.floor(player.position.x),math.floor(player.position.y)}
player.teleport(player.surface.find_non_colliding_position('player',homes._r,32,1),player.surface)
homes._r = pos
player_return{'ExpGamingCommands-home.return',pos[1],pos[2]}
else
player_return{'ExpGamingCommands-home.homes',homes._n,homes._m}
local index = 1
for name,pos in pairs(homes) do if name ~= '_n' and name ~= '_r' and name ~= '_m' then player_return{'ExpGamingCommands-home.home',index,name,pos[1],pos[2]} index=index+1 end end
end
end)

View File

@@ -0,0 +1,9 @@
[ExpGamingCommands-home]
too-many-homes=You have too many homes, to add more you must remove one. Your max is __1__.
homes=Your Homes: (__1__/__2__)
home=__1__) __2__: __3__ , __4__
set=Your home "__1__" as been set to __2__ , __3__
remove=Your home "__1__" as been removed
goto=You are now at "__1__"
return=You are now at your previous location: __1__ , __2__
invalid=Invalid name, __1__

View File

@@ -57,6 +57,9 @@ commands.validate = {
['boolean']=function(value,event) local value = value.lower() if value == 'true' or valule == 'yes' or value == 'y' or value == '1' then return true else return false end end,
['string']=function(value,event) return tostring(value) end,
['string-inf']=function(value,event) return tostring(value) end,
['string-list']=function(value,event,list)
local rtn = tostring(value) and table.includes(list,tostring(value)) and tostring(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-list',table.concat(list,', ')} end return rtn end,
['string-len']=function(value,event,max)
local rtn = tostring(value) and tostring(value):len() <= max and tostring(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-len',max} end return rtn end,
@@ -159,13 +162,13 @@ function commands.validate_args(event)
for name,data in pairs(command.inputs) do
index = index+1
local arg = words[index]
if not arg and not data[1] then return commands.error('invalid-inputs') end
if not arg and data[1] then return commands.error('invalid-inputs') end
if data[2] == 'string-inf' then rtn[name] = table.concat(words,' ',index) break end
local valid = is_type(data[2],'function') and data[2] or commands.validate[data[2]] or error('Invalid type for command: "'..command.name..'/'..name..'"')
local temp_tbl = table.deepcopy(data) table.remove(temp_tbl,1) table.remove(temp_tbl,1)
local value, err = valid(arg,event,unpack(temp_tbl))
if value == commands.error then return value, err end
rtn[name] = value
rtn[name] = is_type(value,'string') and value:gsub('"','') or value
end
return rtn
end

View File

@@ -1,5 +1,6 @@
[ExpGamingCore_Command]
unauthorized=401 - Unauthorized: Access is denied due to invalid credentials
error-string-list=Invalid Option, Must be one of: __1__
error-string-len=Invalid Length, Max: __1__
error-number=Invalid Number: Command failed to run
error-number-range=Invalid Range, Min (exclusive): __1__, Max (inclusive): __2__

View File

@@ -66,6 +66,35 @@ function table.find(tbl, func, ...)
return nil
end
--- Finds the first index at which the value appears in the table
-- @usage table.index({'foo','bar','baz'},'bar') -- retuns 2,'bar'
-- @tparam table tbl the table to search though
-- @param value the value you want the index of
-- @treturn number the index that the value apeears at
-- @return the value in that possition
function table.index(tbl, value)
for k, v in pairs(tbl) do
if v == value then
return k, v
end
end
return nil
end
--- Returns a boolean on weather the table includes the value or not
-- @usage table.includes({'foo','bar','baz'},'bar') -- retuns true
-- @tparam table tbl the table to search though
-- @param value the value you want the index of
-- @treturn boolean if the table includes the value
function table.includes(tbl, value)
for k, v in pairs(tbl) do
if v == value then
return true
end
end
return false
end
--- Given a candidate search function, iterates over the table, calling the function
-- for each element in the table, and returns true if search function returned true.
-- Passes the index as second argument to the function.