Wire Shortcuts X


Adds a shortcut (Alt-W) for giving free wires, repeat presses cycle between wire types. Adds nicer looking wire shortcuts than vanilla.

Utilities
7 hours ago
1.1 - 2.0
5.96K
Circuit network

FAQ

Source code (v1.1.1)

control.lua

local next_wire = {
  ["red-wire"] = "green-wire",
  ["green-wire"] = "copper-cable",
  ["copper-cable"] = "red-wire",
}

local function switch_to_wire(wire_name, player)
  -- cursor_stack must be empty before calling this function
  local cursor_stack = player.cursor_stack

  cursor_stack.set_stack({name = wire_name, count = 1})
  player.create_local_flying_text{
    text = {"item-name." .. wire_name},
    create_at_cursor = true,
  }
  global.players_last_wire[player.index] = wire_name

  global.holding_temporary_wire[player.index] = true
  created_temporary_wire = true  -- Only used within a tick to prevent on_player_cursor_stack_changed
end

script.on_event({defines.events.on_lua_shortcut, "wsx-give-wire"},
  function(event)
    if event.prototype_name and event.prototype_name ~= "wsx-give-wire" then return end
    local player = game.get_player(event.player_index)

    -- Try to cycle wire
    local cursor_stack = player.cursor_stack
    if cursor_stack and cursor_stack.valid_for_read then
      local wire_name = cursor_stack.name
      local next_wire_name = next_wire[wire_name]
      if next_wire_name then
        -- Clear the cursor first
        if global.holding_temporary_wire[player.index] then
          global.holding_temporary_wire[player.index] = nil
          player.cursor_stack.clear()  -- Deletes items
        else
          local cleared = player.clear_cursor()  -- Returns items to inventory
          if not cleared then return end
        end
        switch_to_wire(next_wire_name, player)
        return
      end
    end

    -- Couldn't cycle wire, create wire instead
    local cleared = player.clear_cursor()
    if cleared then
      local wire_name = global.players_last_wire[event.player_index]
      if not wire_name then
        wire_name = "red-wire"
      end
      switch_to_wire(wire_name, player)
    end
  end
)

script.on_event("wsx-clear",
  function(event)
    local player = game.get_player(event.player_index)

    -- Destroy wire in hand if there is no hand_location
    local cursor_stack = player.cursor_stack
    if cursor_stack and cursor_stack.valid_for_read then
      local wire_name = cursor_stack.name
      local is_wire = next_wire[wire_name]
      if global.holding_temporary_wire[event.player_index] then
        player.cursor_stack.clear()
        player.play_sound{path = "utility/clear_cursor"}
      end
    end
  end
)

script.on_event(defines.events.on_player_cursor_stack_changed,
  function(event)
    if created_temporary_wire then
      created_temporary_wire = nil
      return
    end
    global.holding_temporary_wire[event.player_index] = nil
  end
)

local function setup_global()
  global.players_last_wire = global.players_last_wire or {}
  global.holding_temporary_wire = global.holding_temporary_wire or {}
end

script.on_init(setup_global)
script.on_configuration_changed(setup_global)

data.lua

data:extend{
  {
    type = "shortcut",
    name = "wsx-give-wire",
    action = "lua",
    associated_control_input = "wsx-give-wire",
    icon = {
        filename = "__WireShortcutX__/graphics/give-wire.png",
        priority = "extra-high-no-scale",
        size = 32,
        scale = 1,
        flags = {"icon"}
    },
  },
  {
    type = "custom-input",
    name = "wsx-give-wire",
    localised_name = { "shortcut-name.wsx-give-wire" },
    key_sequence = "ALT + W",
    order = "a",
  },
  {
    type = "custom-input",
    name = "wsx-clear",
    key_sequence = "",
    linked_game_control = "clear-cursor"
  },
}

data.raw.item["red-wire"].wire_count = 0
data.raw.item["green-wire"].wire_count = 0
data.raw.item["copper-cable"].wire_count = 0