1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

implemented Utils.find_entities_by_last_user and undo warning.

This commit is contained in:
Valansch 2018-06-05 01:34:58 +02:00
parent ee70db72f6
commit e6a2ef8697
3 changed files with 52 additions and 13 deletions

View File

@ -1,5 +1,5 @@
local Event = require "utils.event"
local Utils = require "utils.utils"
Event.on_init(function()
global.ag_surface=game.create_surface("antigrief",{autoplace_controls={coal={frequency="normal",richness="normal",size="none"},["copper-ore"]={frequency="normal",richness="normal",size="none"},["crude-oil"]={frequency="normal",richness="normal",size="none"},desert={frequency="normal",richness="normal",size="none"},dirt={frequency="normal",richness="normal",size="none"},["enemy-base"]={frequency="normal",richness="normal",size="none"},grass={frequency="normal",richness="normal",size="very-high"},["iron-ore"]={frequency="normal",richness="normal",size="none"},sand={frequency="normal",richness="normal",size="none"},stone={frequency="normal",richness="normal",size="none"},trees={frequency="normal",richness="normal",size="none"},["uranium-ore"]={frequency="normal",richness="normal",size="none"}},cliff_settings={cliff_elevation_0=1024,cliff_elevation_interval=10,name="cliff"},height=2000000,peaceful_mode=false,seed=3461559752,starting_area="very-low",starting_points={{x=0,y=0}},terrain_segmentation="normal",water="none",width=2000000})
@ -7,7 +7,6 @@ Event.on_init(function()
end)
local function place_entity_on_surface(entity, surface, replace, player)
local new_entity = nil
for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do
@ -72,6 +71,18 @@ Module.undo = function(player)
local player = player
if type(player) == "nil" or type(player) == "string" then return --No support for strings!
elseif type(player) == "number" then player = game.players[player] end
--Remove all items from all surfaces that player placed an entity
for _,surface in pairs(game.surfaces) do
if surface ~= global.ag_surface then
for _,e in pairs(surface.find_entities_filtered{force = player.force.name}) do
if e.last_user == player then
e.destroy()
end
end
end
end
for _,e in pairs(global.ag_surface.find_entities_filtered{}) do
if e.last_user == player then
--Place removed entity IF no collision is detected
@ -87,15 +98,6 @@ Module.undo = function(player)
end
end
end
--Remove all items from all surfaces that player placed an entity
for _,surface in pairs(game.surfaces) do
for _,e in pairs(global.ag_surface.find_entities_filtered{force = player.force}) do
if e.last_user == player then
e.destroy()
end
end
end
end
Module.antigrief_surface_tp = function()
@ -108,4 +110,8 @@ Module.antigrief_surface_tp = function()
end
end
Module.count_removed_entities = function(player)
return #Utils.find_entities_by_last_user(player, global.ag_surface)
end
return Module

View File

@ -473,13 +473,23 @@ local function pool()
end
end
global.undo_warned_players = {}
local function undo(cmd)
if (not game.player) or not game.player.admin then
cant_run(cmd.name)
return
end
--warning
if cmd.parameter and game.players[cmd.parameter] then
if not global.undo_warned_players[game.player.index] then
global.undo_warned_players[game.player.index] = true
game.player.print(
string.format("Warning! You are about to remove %s entities and restore %s entities.",
#Utils.find_entities_by_last_user(game.players[cmd.parameter], game.surfaces.nauvis),
Antigrief.count_removed_entities(game.players[cmd.parameter]))
)
game.player.print("To execute the command please run it again.")
return
end
Antigrief.undo(game.players[cmd.parameter])
game.print(string.format("Undoing everything %s did...", cmd.parameter))
else
@ -532,4 +542,4 @@ commands.add_command('all-tech', 'researches all technologies', function() if ga
commands.add_command('hax', 'Toggles your hax', function() if game.player and game.player.admin then game.player.cheat_mode = not game.player.cheat_mode end end)
commands.add_command('pool', 'Spawns a pool', pool)
commands.add_command('undo', '<player> undoes everything a player has done (Admins only)', undo)
commands.add_command('antigrief_surface', 'move you to the antigrief surface or back', antigrief_surface_tp)
commands.add_command('antigrief_surface', 'moves you to the antigrief surface or back (Admins only)', antigrief_surface_tp)

View File

@ -37,4 +37,27 @@ Module.cast_bool = function(var)
if var then return true else return false end
end
Module.find_entities_by_last_user = function(player, surface, filters)
if type(player) == "string" or not player then
error("bad argument #1 to '" .. debug.getinfo(1, "n").name .. "' (number or LuaPlayer expected, got ".. type(player) .. ")", 1)
return
end
if type(surface) ~= "table" and type(surface) ~= "number" then
error("bad argument #2 to '" .. debug.getinfo(1, "n").name .. "' (number or LuaSurface expected, got ".. type(surface) .. ")", 1)
return
end
local entities = {}
local surface = surface
local player = player
local filters = filters or {}
if type(surface) == "number" then surface = game.surfaces[surface] end
if type(player) == "number" then player = game.players[player] end
filters.force = player.force.name
for _,e in pairs(surface.find_entities_filtered(filters)) do
if e.last_user == player then
table.insert(entities, e)
end
end
return entities
end
return Module