Skip to content

Instantly share code, notes, and snippets.

@NochEinKamel
Created March 2, 2019 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NochEinKamel/0e794c10298b4a648a31b93ebab8b0b1 to your computer and use it in GitHub Desktop.
Save NochEinKamel/0e794c10298b4a648a31b93ebab8b0b1 to your computer and use it in GitHub Desktop.
justgo control.lua for 0.17
local acceptedTypes = {
car = true,
locomotive = true,
}
local filterCache = {}
local sPickupKey = "folk-justgo-pickup"
local pickup = setmetatable({}, {
__index = function(self, id)
local v = settings.get_player_settings(game.players[id])[sPickupKey].value
rawset(self, id, v)
return v
end
})
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
if not event or not event.setting then return end
if event.setting == sPickupKey then
pickup[event.player_index] = nil
end
end)
local function onBuildEntity(event)
local e = event.created_entity
if not e or not e.valid or not acceptedTypes[e.type] then return end
local player = game.players[event.player_index]
if player.driving or (player.vehicle ~= nil and player.vehicle.valid) then return end
--local inv = player.get_inventory(defines.inventory.player_quickbar)
--if not inv or not inv.valid or not inv.is_filtered() then return end
--## is_filtered() => If this inventory supports filters and has at least 1 filter set.
--## Quick-bar is always filtered, but there is no way to figure out anymore if a filter is set at all.
--## also there are 10 quickbars now and quickbar is no LuaInventory anymore
-- We check for inv.is_filtered first because I presume it's cheaper than can_reach_entity
if not player.can_reach_entity(e) then return end
local playerIndex = event.player_index
if not filterCache[playerIndex] then filterCache[playerIndex] = {} end
if filterCache[playerIndex] and filterCache[playerIndex][e.name] then
--local f = inv.get_filter(filterCache[playerIndex][e.name])
-- LuaItemPrototype
local f = player.get_quick_bar_slot(filterCache[playerIndex][e.name])
if f ~= nil and f.name == e.name then
if not global.ent then global.ent = {} end
player.teleport(e.position)
player.driving = true
global.ent[player.index] = e
return -- We are done, just go!
else
filterCache[playerIndex][e.name] = nil
end
end
for i = 1, 100 do
local f = player.get_quick_bar_slot(i)
if f ~= nil and f.name == e.name then
if not global.ent then global.ent = {} end
filterCache[playerIndex][e.name] = i
player.teleport(e.position)
player.driving = true
global.ent[player.index] = e
return -- We are done, just go!
end
end
end
script.on_event(defines.events.on_built_entity, onBuildEntity)
local function driving(event)
if not global.ent or not global.ent[event.player_index] then return end
local player = game.players[event.player_index]
if not player or not player.valid or player.driving or player.vehicle then return end
if not pickup[event.player_index] then return end
-- I'm not even sure we need to check .can_reach. But just to be sure, we do.
if global.ent[player.index].valid and global.ent[player.index].minable and player.can_reach_entity(global.ent[player.index]) then
player.mine_entity(global.ent[player.index])
end
global.ent[player.index] = nil
end
script.on_event(defines.events.on_player_driving_changed_state, driving)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment