2023-11-12 00:59:42 +02:00
|
|
|
-- created by Gerkiz for ComfyFactorio
|
|
|
|
|
2020-08-14 22:07:54 +02:00
|
|
|
local Token = require 'utils.token'
|
|
|
|
local Color = require 'utils.color_presets'
|
|
|
|
local Server = require 'utils.server'
|
|
|
|
local Event = require 'utils.event'
|
2020-10-24 16:07:37 +02:00
|
|
|
local Global = require 'utils.global'
|
2024-04-21 19:15:08 +02:00
|
|
|
local Core = require 'utils.core'
|
2024-06-04 23:27:12 +02:00
|
|
|
local Commands = require 'utils.commands'
|
2020-08-14 22:07:54 +02:00
|
|
|
|
|
|
|
local quickbar_dataset = 'quickbar'
|
2020-10-24 14:46:14 +02:00
|
|
|
local quickbar_dataset_modded = 'quickbar_modded'
|
2020-10-24 16:07:37 +02:00
|
|
|
local logistics_dataset = 'logistics'
|
|
|
|
local logistics_dataset_modded = 'logistics_modded'
|
2020-08-14 22:07:54 +02:00
|
|
|
local set_data = Server.set_data
|
|
|
|
local try_get_data = Server.try_get_data
|
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
local this = {
|
|
|
|
logistics = {}
|
|
|
|
}
|
|
|
|
|
2024-05-27 20:30:03 +02:00
|
|
|
local ignored_items = {
|
|
|
|
['blueprint'] = true,
|
|
|
|
['blueprint-book'] = true,
|
|
|
|
['deconstruction-planner'] = true,
|
|
|
|
['spidertron-remote'] = true,
|
|
|
|
['upgrade-planner'] = true
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
Global.register(
|
|
|
|
this,
|
2024-06-04 23:27:12 +02:00
|
|
|
function (t)
|
2020-10-24 16:07:37 +02:00
|
|
|
this = t
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2020-08-14 22:07:54 +02:00
|
|
|
local Public = {}
|
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
local function apply_stash(player)
|
2023-11-12 00:59:42 +02:00
|
|
|
local stash = this.logistics[player.name]
|
2020-10-24 16:07:37 +02:00
|
|
|
if stash then
|
|
|
|
for i, slot in pairs(stash) do
|
|
|
|
if slot and slot.name then
|
2024-06-04 23:27:12 +02:00
|
|
|
player.set_personal_logistic_slot(i, { name = slot.name, min = slot.min, max = slot.max })
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
|
|
|
end
|
2023-11-12 00:59:42 +02:00
|
|
|
this.logistics[player.name] = nil
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local fetch_quickbar =
|
2020-08-14 22:07:54 +02:00
|
|
|
Token.register(
|
2024-06-04 23:27:12 +02:00
|
|
|
function (data)
|
|
|
|
local key = data.key
|
|
|
|
local value = data.value
|
|
|
|
local player = game.players[key]
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if value then
|
|
|
|
for i, slot in pairs(value) do
|
|
|
|
if slot and slot ~= '' then
|
|
|
|
player.set_quick_bar_slot(i, slot)
|
|
|
|
end
|
2020-08-14 22:07:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
2020-08-14 22:07:54 +02:00
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
local fetch_logistics =
|
|
|
|
Token.register(
|
2024-06-04 23:27:12 +02:00
|
|
|
function (data)
|
|
|
|
local key = data.key
|
|
|
|
local value = data.value
|
|
|
|
local player = game.players[key]
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local tech = player.force.technologies['logistic-robotics'].researched
|
|
|
|
if value then
|
|
|
|
for i, slot in pairs(value) do
|
|
|
|
if slot and slot.name then
|
|
|
|
if tech then
|
|
|
|
player.set_personal_logistic_slot(i, { name = slot.name, min = slot.min, max = slot.max })
|
|
|
|
else
|
|
|
|
if not this.logistics[player.name] then
|
|
|
|
this.logistics[player.name] = {}
|
|
|
|
end
|
|
|
|
this.logistics[player.name][i] = { name = slot.name, min = slot.min, max = slot.max }
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
2020-10-24 16:07:37 +02:00
|
|
|
|
|
|
|
--- Tries to get data from the webpanel and applies the value to the player.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.fetch_quickbar(player)
|
|
|
|
local dataset = quickbar_dataset
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = quickbar_dataset_modded
|
|
|
|
end
|
|
|
|
|
|
|
|
try_get_data(dataset, player.name, fetch_quickbar)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Tries to get data from the webpanel and applies the value to the player.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.fetch_logistics(player)
|
|
|
|
local dataset = logistics_dataset
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = logistics_dataset_modded
|
|
|
|
end
|
|
|
|
|
|
|
|
try_get_data(dataset, player.name, fetch_logistics)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Saves the players quickbar table to the webpanel.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.save_quickbar(player)
|
|
|
|
local dataset = quickbar_dataset
|
|
|
|
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = quickbar_dataset_modded
|
|
|
|
end
|
|
|
|
|
|
|
|
local slots = {}
|
|
|
|
|
|
|
|
for i = 1, 100 do
|
|
|
|
local slot = player.get_quick_bar_slot(i)
|
2024-05-27 20:30:03 +02:00
|
|
|
if slot ~= nil and not ignored_items[slot.name] then
|
2020-10-24 16:07:37 +02:00
|
|
|
slots[i] = slot.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if next(slots) then
|
|
|
|
set_data(dataset, player.name, slots)
|
|
|
|
player.print('Your quickbar has been saved.', Color.success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Saves the players personal logistics table to the webpanel.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.save_logistics(player)
|
|
|
|
local dataset = logistics_dataset
|
|
|
|
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = logistics_dataset_modded
|
|
|
|
end
|
|
|
|
|
|
|
|
local slots = {}
|
|
|
|
|
2023-07-31 13:48:19 +02:00
|
|
|
for i = 1, 400 do
|
2020-10-24 16:07:37 +02:00
|
|
|
local slot = player.get_personal_logistic_slot(i)
|
|
|
|
if slot and slot.name then
|
2024-06-04 23:27:12 +02:00
|
|
|
slots[i] = { name = slot.name, min = slot.min, max = slot.max }
|
2020-10-24 14:46:14 +02:00
|
|
|
end
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
|
|
|
if next(slots) then
|
|
|
|
set_data(dataset, player.name, slots)
|
|
|
|
player.print('Your personal logistics has been saved.', Color.success)
|
|
|
|
end
|
|
|
|
end
|
2020-10-24 14:46:14 +02:00
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
--- Removes the quickbar key from the webpanel.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.remove_quickbar(player)
|
|
|
|
local dataset = quickbar_dataset
|
|
|
|
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = quickbar_dataset_modded
|
2020-08-14 22:07:54 +02:00
|
|
|
end
|
2020-10-24 16:07:37 +02:00
|
|
|
|
|
|
|
set_data(dataset, player.name, nil)
|
|
|
|
player.print('Your quickbar has been removed.', Color.success)
|
2020-08-14 22:07:54 +02:00
|
|
|
end
|
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
--- Removes the logistics key from the webpanel.
|
|
|
|
-- @param LuaPlayer
|
|
|
|
function Public.remove_logistics(player)
|
|
|
|
local dataset = logistics_dataset
|
|
|
|
|
|
|
|
local game_has_mods = is_game_modded()
|
|
|
|
if game_has_mods then
|
|
|
|
dataset = logistics_dataset_modded
|
|
|
|
end
|
|
|
|
|
|
|
|
set_data(dataset, player.name, nil)
|
|
|
|
player.print('Your personal logistics has been removed.', Color.success)
|
|
|
|
end
|
|
|
|
|
|
|
|
local fetch_quickbar_on_join = Public.fetch_quickbar
|
|
|
|
local fetch_logistics_on_join = Public.fetch_logistics
|
|
|
|
local save_quickbar = Public.save_quickbar
|
|
|
|
local save_logistics = Public.save_logistics
|
|
|
|
local remove_quickbar = Public.remove_quickbar
|
|
|
|
local remove_logistics = Public.remove_logistics
|
|
|
|
|
2024-06-04 23:27:12 +02:00
|
|
|
Commands.new('save-quickbar', 'Save your personal quickbar preset so it´s always the same.')
|
|
|
|
:require_backend()
|
|
|
|
:callback(
|
|
|
|
function (player)
|
|
|
|
save_quickbar(player)
|
|
|
|
player.print('Quickbar saved.')
|
2020-08-14 22:07:54 +02:00
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Commands.new('save-logistics', 'Save your personal logistics preset so it´s always the same.')
|
|
|
|
:require_backend()
|
|
|
|
:callback(
|
|
|
|
function (player)
|
|
|
|
local success, _ = pcall(save_logistics, player)
|
|
|
|
player.print('Notice: only the first 400 slots are saved.', Color.warning)
|
|
|
|
if not success then
|
|
|
|
player.print('An error occured while trying to save your logistics slots.', Color.warning)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
player.print('Logistics saved.')
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Commands.new('remove-quickbar', 'Removes your personal quickbar preset from the datastore.')
|
|
|
|
:require_backend()
|
|
|
|
:callback(
|
|
|
|
function (player)
|
|
|
|
remove_quickbar(player)
|
|
|
|
player.print('Quickbar removed.')
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Commands.new('remove-logistics', 'Removes your personal logistics preset from the datastore.')
|
|
|
|
:require_backend()
|
|
|
|
:callback(
|
|
|
|
function (player)
|
|
|
|
remove_logistics(player)
|
|
|
|
player.print('Logistics removed.')
|
2020-10-24 14:46:14 +02:00
|
|
|
end
|
2024-06-04 23:27:12 +02:00
|
|
|
)
|
2020-10-24 14:46:14 +02:00
|
|
|
|
2020-08-14 22:07:54 +02:00
|
|
|
|
|
|
|
Event.add(
|
|
|
|
defines.events.on_player_joined_game,
|
2024-06-04 23:27:12 +02:00
|
|
|
function (event)
|
2020-08-14 22:07:54 +02:00
|
|
|
local player = game.get_player(event.player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-10-24 16:07:37 +02:00
|
|
|
local secs = Server.get_current_time()
|
|
|
|
if not secs then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
fetch_quickbar_on_join(player)
|
|
|
|
fetch_logistics_on_join(player)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Event.add(
|
|
|
|
defines.events.on_research_finished,
|
2024-06-04 23:27:12 +02:00
|
|
|
function (event)
|
2020-10-24 16:07:37 +02:00
|
|
|
local research = event.research
|
|
|
|
if research.name == 'logistic-robotics' then
|
2024-04-21 19:15:08 +02:00
|
|
|
Core.iter_connected_players(
|
2024-06-04 23:27:12 +02:00
|
|
|
function (player)
|
2024-04-21 19:15:08 +02:00
|
|
|
apply_stash(player)
|
|
|
|
end
|
|
|
|
)
|
2020-10-24 16:07:37 +02:00
|
|
|
end
|
2020-08-14 22:07:54 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
return Public
|