add building_lib.replace
This commit is contained in:
parent
c23e610bff
commit
c8fc030463
@ -75,8 +75,10 @@ function building_lib.build(mapblock_pos, playername, building_name, rotation, c
|
||||
end
|
||||
end)
|
||||
|
||||
placement.place(placement, mapblock_pos, building_def, rotation, callback)
|
||||
building_lib.fire_event("placed", mapblock_pos, playername, building_def, rotation, size)
|
||||
placement.place(placement, mapblock_pos, building_def, rotation, function()
|
||||
callback()
|
||||
building_lib.fire_event("placed", mapblock_pos, playername, building_def, rotation, size)
|
||||
end)
|
||||
return true
|
||||
end
|
||||
|
||||
|
@ -10,6 +10,7 @@ building_lib.register_building("building_lib:dummy_v2", {
|
||||
mtt.register("build", function(callback)
|
||||
local mapblock_pos = {x=0, y=0, z=0}
|
||||
local building_name = "building_lib:dummy"
|
||||
local new_building_name = "building_lib:dummy_v2"
|
||||
local rotation = 0
|
||||
local playername = "singleplayer"
|
||||
|
||||
@ -41,6 +42,20 @@ mtt.register("build", function(callback)
|
||||
assert(info.size.y == 1)
|
||||
assert(info.size.z == 1)
|
||||
|
||||
-- try to replace
|
||||
success, err = building_lib.can_replace(mapblock_pos, playername, new_building_name)
|
||||
assert(not err)
|
||||
assert(success)
|
||||
|
||||
-- replace
|
||||
callback_called = false
|
||||
success, err = building_lib.replace(mapblock_pos, playername, new_building_name, function()
|
||||
callback_called = true
|
||||
end)
|
||||
assert(not err)
|
||||
assert(success)
|
||||
assert(callback_called)
|
||||
|
||||
-- try to remove
|
||||
success, err = building_lib.can_remove(mapblock_pos)
|
||||
assert(not err)
|
||||
|
1
init.lua
1
init.lua
@ -22,6 +22,7 @@ dofile(MP .. "/autoplace.lua")
|
||||
dofile(MP .. "/autoplace_tool.lua")
|
||||
dofile(MP .. "/remove.lua")
|
||||
dofile(MP .. "/remove_tool.lua")
|
||||
dofile(MP .. "/replace.lua")
|
||||
dofile(MP .. "/chat.lua")
|
||||
dofile(MP .. "/events.lua")
|
||||
dofile(MP .. "/hacks.lua")
|
||||
|
@ -10,6 +10,13 @@ local success, message = building_lib.can_build(mapblock_pos, playername, buildi
|
||||
-- build it there
|
||||
local success, message = building_lib.build(mapblock_pos, playername, building_name, rotation, callback)
|
||||
|
||||
-- check if it can be replaced
|
||||
-- NOTE: conditions are not checked on replace, just the size
|
||||
local success, err = building_lib.can_replace(mapblock_pos, playername, new_building_name)
|
||||
|
||||
-- replace with a building of the same size
|
||||
local success, err = building_lib.replace(mapblock_pos, playername, new_building_name, callback)
|
||||
|
||||
-- check if it can be removed
|
||||
local success, message = building_lib.can_remove(mapblock_pos)
|
||||
|
||||
|
57
replace.lua
Normal file
57
replace.lua
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
function building_lib.can_replace(mapblock_pos, _, building_name)
|
||||
local new_building_def = building_lib.get_building(building_name)
|
||||
if not new_building_def then
|
||||
return false, "New building not found: '" .. building_name .. "'"
|
||||
end
|
||||
|
||||
local info = building_lib.get_placed_building_info(mapblock_pos)
|
||||
if not info then
|
||||
return false, "no building found"
|
||||
end
|
||||
|
||||
local old_building_def = building_lib.get_building(info.name)
|
||||
if not old_building_def then
|
||||
return false, "Old building not found: '" .. info.name .. "'"
|
||||
end
|
||||
|
||||
local success, message = building_lib.can_remove(mapblock_pos)
|
||||
if not success then
|
||||
return false, message
|
||||
end
|
||||
|
||||
local new_placement = building_lib.get_placement(new_building_def.placement)
|
||||
if not info then
|
||||
return false, "placement not found"
|
||||
end
|
||||
|
||||
local old_size = info.size
|
||||
local new_size = new_placement.get_size(new_placement, mapblock_pos, new_building_def, info.rotation)
|
||||
|
||||
if not vector.equals(old_size, new_size) then
|
||||
return false, "replacement size does not match"
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function building_lib.replace(mapblock_pos, playername, new_building_name, callback)
|
||||
callback = callback or function() end
|
||||
|
||||
local success, message = building_lib.can_replace(mapblock_pos, playername, new_building_name)
|
||||
if not success then
|
||||
return false, message
|
||||
end
|
||||
|
||||
local info = building_lib.get_placed_building_info(mapblock_pos)
|
||||
local rotation = info.rotation or 0
|
||||
|
||||
-- remove old building
|
||||
success, message = building_lib.remove(mapblock_pos, playername)
|
||||
if not success then
|
||||
return false, message
|
||||
end
|
||||
|
||||
-- place new building
|
||||
return building_lib.build(mapblock_pos, playername, new_building_name, rotation, callback)
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user