Skip to content

Commit

Permalink
Merge pull request #210 from Cooldude2606/feature/admin-markers
Browse files Browse the repository at this point in the history
Admin map markers
  • Loading branch information
Cooldude2606 authored Apr 25, 2021
2 parents 1660cf8 + 62f3e75 commit 40ce588
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/_file_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ return {
'modules.commands.me',
'modules.commands.kill',
'modules.commands.admin-chat',
'modules.commands.admin-markers',
'modules.commands.teleport',
'modules.commands.cheat-mode',
'modules.commands.ratio',
Expand Down
1 change: 1 addition & 0 deletions config/expcore/roles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Roles.new_role('Trainee','TrMod')
:set_parent('Veteran')
:allow{
'command/admin-chat',
'command/admin-marker',
'command/teleport',
'command/bring',
'command/goto',
Expand Down
9 changes: 8 additions & 1 deletion locale/en/commands.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ none-matching=No servers were found with that name, if you used an address pleas
[expcom-lastlocation]
response=Last location of __1__ was [gps=__2__,__3__]
[expcom-admin-marker]
exit=You have left admin marker mode, all new markers will not be protected.
enter=You have entered admin marker mode, all new markers will be protected.
place=You have placed an admin marker.
edit=You have edited an admin marker.
revert=You cannot edit admin markers.
[expcom-inv-search]
reject-item=No item was found with internal name __1__; try using rich text selection.
results-heading=Players found with [item=__1__]:
results-item=__1__) __2__ has __3__ items. (__4__)
results-none=No players have [item=__1__]
results-none=No players have [item=__1__]
88 changes: 88 additions & 0 deletions modules/commands/admin-markers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--[[-- Commands Module - Admin Markers
- Adds a command that creates map markers which can only be edited by admins
@commands Admin-Markers
]]

local Commands = require 'expcore.commands' --- @dep expcore.commands
local Global = require 'utils.global' --- @dep utils.global
local Event = require 'utils.event' --- @dep utils.event

local admins = {} -- Stores all players in admin marker mode
local markers = {} -- Stores all admin markers

--- Global variables
Global.register({
admins = admins,
markers = markers
}, function(tbl)
admins = tbl.admins
markers = tbl.markers
end)

--- Toggle admin marker mode, can only be applied to yourself
-- @command admin-marker
Commands.new_command('admin-marker', 'Toggles admin marker mode, new markers can only be edited by admins')
:set_flag('admin_only')
:add_alias('am', 'admin-markers')
:register(function(player)
if admins[player.name] then
-- Exit admin mode
admins[player.name] = nil
return Commands.success{'expcom-admin-marker.exit'}
else
-- Enter admin mode
admins[player.name] = true
return Commands.success{'expcom-admin-marker.enter'}
end
end)

--- Listen for new map markers being added, add admin marker if done by player in admin mode
Event.add(defines.events.on_chart_tag_added, function(event)
if not event.player_index then return end
local player = game.get_player(event.player_index)
if not admins[player.name] then return end
local tag = event.tag
markers[tag.force.name..tag.tag_number] = true
Commands.print({'expcom-admin-marker.place'}, nil, player)
end)

--- Listen for players leaving the game, leave admin mode to avoid unexpected admin markers
Event.add(defines.events.on_player_left_game, function(event)
if not event.player_index then return end
local player = game.get_player(event.player_index)
admins[player.name] = nil
end)

--- Listen for tags being removed or edited, maintain tags edited by non admins
local function maintain_tag(event)
local tag = event.tag
if not event.player_index then return end
if not markers[tag.force.name..tag.tag_number] then return end
local player = game.get_player(event.player_index)
if player.admin then
-- Player is admin, tell them it was an admin marker
Commands.print({'expcom-admin-marker.edit'}, nil, player)
elseif event.name == defines.events.on_chart_tag_modified then
-- Tag was modified, revert the changes
tag.text = event.old_text
tag.last_user = event.old_player
if event.old_icon then tag.icon = event.old_icon end
player.play_sound{path='utility/wire_pickup'}
Commands.print({'expcom-admin-marker.revert'}, nil, player)
else
-- Tag was removed, remake the tag
player.play_sound{path='utility/wire_pickup'}
Commands.print({'expcom-admin-marker.revert'}, 'orange_red', player)
local new_tag = tag.force.add_chart_tag(tag.surface, {
last_user = tag.last_user,
position = tag.position,
icon = tag.icon,
text = tag.text,
})
markers[tag.force.name..tag.tag_number] = nil
markers[new_tag.force.name..new_tag.tag_number] = true
end
end

Event.add(defines.events.on_chart_tag_modified, maintain_tag)
Event.add(defines.events.on_chart_tag_removed, maintain_tag)

0 comments on commit 40ce588

Please sign in to comment.