From bb46d44ea3c51970e08a9d6cb65817e1f799da34 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 15 Apr 2021 23:24:27 +0100 Subject: [PATCH 1/3] Added admin markers --- config/_file_loader.lua | 1 + config/expcore/roles.lua | 1 + locale/en/commands.cfg | 8 ++- modules/commands/admin-markers.lua | 80 ++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 modules/commands/admin-markers.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 16a135af..376cb6d3 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -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', diff --git a/config/expcore/roles.lua b/config/expcore/roles.lua index 9b826202..0a629063 100644 --- a/config/expcore/roles.lua +++ b/config/expcore/roles.lua @@ -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', diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index dea7b935..4255195b 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -82,4 +82,10 @@ offline=You cannot connect as the server is currently offline: __1__ none-matching=No servers were found with that name, if you used an address please append true to the end of your command. [expcom-lastlocation] -response=Last location of __1__ was [gps=__2__,__3__] \ No newline at end of file +response=Last location of __1__ was [gps=__2__,__3__] + +[expcom-admin-marker] +exit=You have left admin marker mode, all new makers will not be protected. +enter=You have entered admin marker mode, all new makers will be protected. +edit=You have edited an admin marker. +revert=You cannot edit admin markers. \ No newline at end of file diff --git a/modules/commands/admin-markers.lua b/modules/commands/admin-markers.lua new file mode 100644 index 00000000..cc43510b --- /dev/null +++ b/modules/commands/admin-markers.lua @@ -0,0 +1,80 @@ +--[[-- 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 +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 +end) + +--- Listen for tags being removed or edited +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 added, 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) \ No newline at end of file From 573dfd781bb50d0f4306a0440f8be7bb9c051b37 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 15 Apr 2021 23:28:30 +0100 Subject: [PATCH 2/3] Added tag placed message --- locale/en/commands.cfg | 1 + modules/commands/admin-markers.lua | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 4255195b..1cb2e86f 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -87,5 +87,6 @@ response=Last location of __1__ was [gps=__2__,__3__] [expcom-admin-marker] exit=You have left admin marker mode, all new makers will not be protected. enter=You have entered admin marker mode, all new makers will be protected. +place=You have placed an admin marker. edit=You have edited an admin marker. revert=You cannot edit admin markers. \ No newline at end of file diff --git a/modules/commands/admin-markers.lua b/modules/commands/admin-markers.lua index cc43510b..95062705 100644 --- a/modules/commands/admin-markers.lua +++ b/modules/commands/admin-markers.lua @@ -36,16 +36,24 @@ Commands.new_command('admin-marker', 'Toggles admin marker mode, new markers can end end) ---- Listen for new map markers being added +--- 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 tags being removed or edited +--- 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 From c899674ef65b0d0a2d3de2133eb49933ad5b1d89 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 25 Apr 2021 20:17:46 +0100 Subject: [PATCH 3/3] Implimented Changes --- locale/en/commands.cfg | 4 ++-- modules/commands/admin-markers.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 1cb2e86f..b2fcb456 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -85,8 +85,8 @@ none-matching=No servers were found with that name, if you used an address pleas response=Last location of __1__ was [gps=__2__,__3__] [expcom-admin-marker] -exit=You have left admin marker mode, all new makers will not be protected. -enter=You have entered admin marker mode, all new makers will be protected. +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. \ No newline at end of file diff --git a/modules/commands/admin-markers.lua b/modules/commands/admin-markers.lua index 95062705..91fab3ec 100644 --- a/modules/commands/admin-markers.lua +++ b/modules/commands/admin-markers.lua @@ -60,7 +60,7 @@ local function maintain_tag(event) 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 added, tell them it was an admin marker + -- 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