-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from S-S-X/containertool-upgrade-handling
Containertool upgrade handling
- Loading branch information
Showing
9 changed files
with
341 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,6 @@ read_globals = { | |
"signs_lib", | ||
"display_api", | ||
"digtron", | ||
"drawers", | ||
"jumpdrive", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name=containertool | ||
description=Provides metatool:containertool to copy/paste container settings | ||
depends=metatool | ||
optional_depends=default,technic_chests,more_chests,digilines | ||
optional_depends=default,technic_chests,more_chests,digilines,technic,drawers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
-- | ||
-- Register drawers for Container tool | ||
-- | ||
|
||
if not minetest.get_modpath('drawers') then | ||
return | ||
end | ||
|
||
local ns = metatool.ns('containertool') | ||
|
||
-- Helper functions | ||
local get_description = ns.description | ||
|
||
local properties = { | ||
on_construct = drawers.drawer_on_construct, | ||
on_destruct = drawers.drawer_on_destruct, | ||
on_dig = drawers.drawer_on_dig, | ||
allow_metadata_inventory_put = drawers.drawer_allow_metadata_inventory_put, | ||
-- not typo, take and put are same function in drawers API: | ||
allow_metadata_inventory_take = drawers.drawer_allow_metadata_inventory_put, | ||
on_metadata_inventory_put = drawers.add_drawer_upgrade, | ||
on_metadata_inventory_take = drawers.remove_drawer_upgrade, | ||
} | ||
|
||
local function is_drawer(nodedef) | ||
for key, value in pairs(properties) do | ||
if nodedef[key] ~= value then | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
|
||
-- Collect nodes | ||
local nodes = {} | ||
for nodename, nodedef in pairs(minetest.registered_nodes) do | ||
if is_drawer(nodedef) then | ||
table.insert(nodes, nodename) | ||
end | ||
end | ||
|
||
local definition = { | ||
name = 'drawer', | ||
nodes = nodes, | ||
group = 'drawer', | ||
protection_bypass_read = "interact", | ||
} | ||
|
||
function definition:copy(node, pos, player) | ||
local meta = minetest.get_meta(pos) | ||
|
||
local inv = meta:get_inventory() | ||
local invlist = inv:get_list("upgrades") | ||
local upgrades = {} | ||
for index, stack in pairs(invlist) do | ||
if not stack:is_empty() then | ||
upgrades[index] = ("%s %d"):format(stack:get_name(), stack:get_count()) | ||
end | ||
end | ||
|
||
return { | ||
description = get_description(meta, node, pos), | ||
owner = meta:get("owner"), | ||
inv = { | ||
upgrades = upgrades, | ||
} | ||
} | ||
end | ||
|
||
function definition:paste(node, pos, player, data) | ||
if type(data.inv) ~= "table" or type(data.inv.upgrades) ~= "table" then | ||
return | ||
end | ||
|
||
-- Handle possible machine upgrades and player inventory | ||
local meta = minetest.get_meta(pos) | ||
local require_update = false | ||
|
||
local playerinv = player:get_inventory() | ||
local inv = meta:get_inventory() | ||
for index, itemstring in pairs(data.inv.upgrades) do | ||
if inv:get_stack("upgrades", index):is_empty() then | ||
-- Target slot is empty, try to place upgrade item | ||
local datastack = ItemStack(itemstring) | ||
local itemcount = drawers.drawer_allow_metadata_inventory_put(pos, "upgrades", index, datastack, player) | ||
-- Usually if there's space for item itemcount should be same as datastack count | ||
if itemcount and itemcount > 0 and itemcount <= datastack:get_count() then | ||
datastack:set_count(itemcount) | ||
local playerstack = playerinv:remove_item("main", datastack) | ||
inv:set_stack("upgrades", index, playerstack) | ||
require_update = true | ||
end | ||
end | ||
end | ||
|
||
if require_update then | ||
drawers.update_drawer_upgrades(pos) | ||
end | ||
end | ||
|
||
return definition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
-- | ||
-- Register jumpdrive engines for Container tool | ||
-- | ||
|
||
if not minetest.get_modpath('jumpdrive') then | ||
return | ||
end | ||
|
||
local definition = { | ||
name = "jumpdrive_engine", | ||
nodes = { | ||
"jumpdrive:engine", | ||
"jumpdrive:area_engine", | ||
}, | ||
group = "jumpdrive_engine", | ||
protection_bypass_read = "interact", | ||
} | ||
|
||
local ns = metatool.ns('containertool') | ||
|
||
-- Base metadata reader and metadata setters | ||
local get_common_attributes = ns.get_common_attributes | ||
local set_digiline_meta = ns.set_digiline_meta | ||
|
||
function definition:copy(node, pos, player) | ||
local meta = minetest.get_meta(pos) | ||
|
||
-- Read common data like owner, splitstacks, channel etc. | ||
local data = get_common_attributes(meta, node, pos, player) | ||
|
||
-- Get installed upgrades | ||
data.inv = { upgrade = {} } | ||
local inv = meta:get_inventory() | ||
local invlist = inv:get_list("upgrade") | ||
local upgrades = data.inv.upgrade | ||
|
||
for index, stack in pairs(invlist) do | ||
if not stack:is_empty() then | ||
upgrades[index] = ("%s %d"):format(stack:get_name(), stack:get_count()) | ||
end | ||
end | ||
|
||
return data | ||
end | ||
|
||
function definition:paste(node, pos, player, data) | ||
local meta = minetest.get_meta(pos) | ||
|
||
-- Set common metadata values | ||
set_digiline_meta(meta, {channel = data.channel}, node) | ||
|
||
if type(data.inv) == "table" and type(data.inv.upgrade) == "table" then | ||
|
||
-- Handle machine upgrades and player inventory | ||
local require_update = false | ||
|
||
local playerinv = player:get_inventory() | ||
local inv = meta:get_inventory() | ||
for index, itemstring in pairs(data.inv.upgrades) do | ||
if inv:get_stack("upgrade", index):is_empty() then | ||
-- Target slot is empty, try to place upgrade item | ||
local datastack = ItemStack(itemstring) | ||
if datastack:get_count() > 0 then | ||
local playerstack = playerinv:remove_item("main", datastack) | ||
inv:set_stack("upgrade", index, playerstack) | ||
require_update = true | ||
end | ||
end | ||
end | ||
|
||
if require_update then | ||
jumpdrive.upgrade.calculate(pos) | ||
end | ||
end | ||
|
||
end | ||
|
||
return definition |
Oops, something went wrong.