Page 1 of 1

[kovarex] [1.1.39] Shift+Copy+Paste ignores on_player_configured_blueprint

Posted: Tue Sep 07, 2021 4:03 am
by DaveMcW
There is a loophole in the modding API that allows a player to bypass blueprint editing mods. This example is a rather hostile mod, but it also affects mods that add helpful tags to blueprints.

Steps to reproduce:
1. Create a simple mod that deletes all transport belts from new blueprints.

Code: Select all

/c function delete_belts(event)
  local bp = game.get_player(event.player_index).cursor_stack
  if not bp or not bp.valid_for_read or not bp.is_blueprint or not bp.is_blueprint_setup() then return end
  local entities = bp.get_blueprint_entities()
  if not entities then return end
  for key, entity in pairs(entities) do
    if entity.name:find("transport-belt", 1, true) then
      entities[key] = nil
    end
  end
  bp.set_blueprint_entities(entities)
end
script.on_event(defines.events.on_player_configured_blueprint, delete_belts)
script.on_event(defines.events.on_player_setup_blueprint, delete_belts)
2. Select the copy tool.
3. Hold down shift while creating a blueprint that includes transport belts.
4. Click "Create blueprint".
5. Select the paste tool.

Expected result: Paste tool is missing transport belts.

Actual result: Paste tool has transport belts.


The other ways of creating blueprints all work as expected, the belts are gone:
  • Copy tool without shift
  • Blueprint tool with shift
  • Blueprint tool without shift

Re: [1.1.39] Shift+Copy+Paste ignores on_player_configured_blueprint

Posted: Tue Sep 07, 2021 6:35 am
by Honktown
You need to also check player.blueprint_to_setup (sorry for the change in spacing) :

Code: Select all

/c
function delete_belts(event)
	local player = game.get_player(event.player_index)
	local bp1 = player.blueprint_to_setup
	local tick = event.tick
	if not bp1.valid_for_read then
		game.print(tick.." player blueprint_to_setup is invalid to read")
		bp1 = nil
	else
		game.print(tick.." player blueprint_to_setup is valid to read")
	end

	local bp2 = player.cursor_stack
	if not bp2 or not bp2.valid_for_read or not bp2.is_blueprint or not bp2.is_blueprint_setup() then
		game.print(tick.." player cursor blueprint is invalid")
		bp2 = nil
	else
		game.print(tick.." player cursor blueprint is valid")
	end

	local bp = bp1 or bp2
	if not bp then
		return
	end
	local entities = bp.get_blueprint_entities()
	if not entities then return end
	for key, entity in pairs(entities) do
		if entity.name:find("transport-belt", 1, true) then
			entities[key] = nil
		end
	end
	bp.set_blueprint_entities(entities)
end

script.on_event(defines.events.on_player_configured_blueprint, delete_belts)
script.on_event(defines.events.on_player_setup_blueprint, delete_belts)

Re: [1.1.39] Shift+Copy+Paste ignores on_player_configured_blueprint

Posted: Tue Sep 07, 2021 6:43 am
by Honktown
Going back-and-forth, it is interesting that *configuring* copy-paste causes paste to be loaded with an unconfigured blueprint. (editing the setup entities is different than paste not being filled OR being filled with the post-event configured blueprint)

Re: [1.1.39] Shift+Copy+Paste ignores on_player_configured_blueprint

Posted: Tue Sep 07, 2021 7:42 am
by DaveMcW
Honktown wrote: Tue Sep 07, 2021 6:35 amYou need to also check player.blueprint_to_setup
Thanks! Using player.blueprint_to_setup with the on_player_setup_blueprint event is enough to make my mod work. on_player_configured_blueprint is still buggy but I don't need it anymore.

Re: [kovarex] [1.1.39] Shift+Copy+Paste ignores on_player_configured_blueprint

Posted: Wed Sep 22, 2021 2:55 pm
by DaveMcW
This bug still exists in 1.1.40.