Skip to content

Commit

Permalink
Fix CNC upgrade method (#222)
Browse files Browse the repository at this point in the history
Fixes #190
  • Loading branch information
S-S-X authored and OgelGames committed Oct 20, 2021
1 parent 3867b49 commit b2f456d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
26 changes: 16 additions & 10 deletions technic_cnc/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,25 @@ function technic_cnc.register_cnc_machine(nodename, def)

-- UPGRADE OLD CNC MACHINES
local function upgrade_machine(meta)
local product = meta:get("cnc_product")
if product then
local oldmeta = meta:get("technic_power_machine")
if oldmeta then
local product = meta:get("cnc_product")
-- Remove old metadata fields
meta:set_string("cnc_product", "")
meta:set_string("cnc_multiplier", "")
local program_raw = product:match("[%w_]+:[%w_]+_technic_cnc_([%w_]+)")
if program_raw then
local double = "_double"
local size = program_raw:sub(-#double) == double and 1 or 2
local program = size == 1 and program_raw:sub(1, #program_raw - #double) or program_raw
meta:set_string("program", program)
meta:set_int("size", size)
meta:set_string("technic_power_machine", "")
if product then
-- Only executed for active machines, old inactive machines do not have cnc_product set
local program_raw = product:match("[%w_]+:[%w_]+_technic_cnc_([%w_]+)")
if program_raw then
local double = "_double"
local size = program_raw:sub(-#double) == double and 1 or 2
local program = size == 1 and program_raw:sub(1, #program_raw - #double) or program_raw
meta:set_string("program", program)
meta:set_int("size", size)
end
meta:set_string("formspec", get_formspec(nodename, def, meta))
end
meta:set_string("formspec", get_formspec(nodename, def, meta))
end
end

Expand Down
6 changes: 4 additions & 2 deletions technic_cnc/spec/interaction_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ describe("CNC formspec interaction", function()
end)

it("updates old machines", function()
do -- Verify starting point
do -- Verify starting point and set meta value that is used to look for old machine
local meta = minetest.get_meta({x=98,y=98,z=98})
assert.equals("default:stone_technic_cnc_element_t", meta:get("cnc_product"))
meta:set_float("technic_power_machine", 1)
end
do -- Verify starting point
do -- Verify starting point and set meta value that is used to look for old machine
local meta = minetest.get_meta({x=99,y=99,z=99})
assert.equals("default:stone_technic_cnc_element_t_double", meta:get("cnc_product"))
meta:set_float("technic_power_machine", 1)
end

nodedef.on_receive_fields({x=98,y=98,z=98}, "", { quit = true, channel = "Sam" }, Sam)
Expand Down

0 comments on commit b2f456d

Please sign in to comment.