Skip to content

Commit

Permalink
move pipeworks.override_chest() to a new file
Browse files Browse the repository at this point in the history
fix crash with after_place_node() there
let mcl_barrels make use of the new function
  • Loading branch information
a-tour-ist committed Apr 3, 2024
1 parent 70688d4 commit 3eef218
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 167 deletions.
90 changes: 90 additions & 0 deletions chests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
pipeworks.chests = {}

-- register a chest to connect with pipeworks tubes.
-- will autoconnect to tubes and add tube inlets to the textures
-- it is highly recommended to allow the user to change the "splitstacks" int (1 to enable) in the node meta
-- but that can't be done by this function

-- @param override: additional overrides, such as stuff to modify the node formspec
-- @param connect_sides: which directions the chests shall connect to
function pipeworks.override_chest(chestname, override, connect_sides)
local old_def = minetest.registered_nodes[chestname]

local tube_entry = "^pipeworks_tube_connection_wooden.png"
override.tiles = override.tiles or old_def.tiles
-- expand the tiles table if it has been shortened
if #override.tiles < 6 then
for i = #override.tiles, 6 do
override.tiles[i] = override.tiles[#override.tiles]
end
end
-- add inlets to the sides that connect to tubes
local tile_directions = {"top", "bottom", "right", "left", "back", "front"}
for i, direction in ipairs(tile_directions) do
if connect_sides[direction] then
if type(override.tiles[i]) == "string" then
override.tiles[i] = override.tiles[i] .. tube_entry
elseif type(override.tiles[i]) == "table" and not override.tiles[i].animation then
override.tiles[i].name = override.tiles[i].name .. tube_entry
end
end
end

local old_after_place_node = override.after_place_node or old_def.after_place_node or function() end
override.after_place_node = function(pos, placer, itemstack, pointed_thing)
old_after_place_node(pos, placer, itemstack, pointed_thing)
pipeworks.after_place(pos)
end

local old_after_dig = override.after_dig or old_def.after_dig or function() end
override.after_dig_node = function(pos, oldnode, oldmetadata, digger)
old_after_dig(pos, oldnode, oldmetadata, digger)
pipeworks.after_dig(pos, oldnode, oldmetadata, digger)
end

local old_on_rotate
if override.on_rotate ~= nil then
old_on_rotate = override.on_rotate
elseif old_def.on_rotate ~= nil then
old_on_rotate = old_def.on_rotate
else
old_on_rotate = function() end
end
-- on_rotate = false -> rotation disabled, no need to update tubes
-- everything else: undefined by the most common screwdriver mods
if type(old_on_rotate) == "function" then
override.on_rotate = function(pos, node, user, mode, new_param2)
if old_on_rotate(pos, node, user, mode, new_param2) ~= false then
return pipeworks.on_rotate(pos, node, user, mode, new_param2)
else
return false
end
end
end

override.tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("main", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if meta:get_int("splitstacks") == 1 then
stack = stack:peek_item(1)
end
return inv:room_for_item("main", stack)
end,
input_inventory = "main",
connect_sides = connect_sides
}

-- Add the extra groups
override.groups = override.groups or old_def.groups or {}
override.groups.tubedevice = 1
override.groups.tubedevice_receiver = 1

minetest.override_item(chestname, override)
pipeworks.chests[chestname] = true
end
95 changes: 4 additions & 91 deletions compat-chests.lua
Original file line number Diff line number Diff line change
@@ -1,94 +1,3 @@
pipeworks.chests = {}

-- register a chest to connect with pipeworks tubes.
-- will autoconnect to tubes and add tube inlets to the textures
-- it is highly recommended to allow the user to change the "splitstacks" int (1 to enable) in the node meta
-- but that can't be done by this function

-- @param override: additional overrides, such as stuff to modify the node formspec
-- @param connect_sides: which directions the chests shall connect to
function pipeworks.override_chest(chestname, override, connect_sides)
local old_def = minetest.registered_nodes[chestname]

local tube_entry = "^pipeworks_tube_connection_wooden.png"
override.tiles = override.tiles or old_def.tiles
-- expand the tiles table if it has been shortened
if #override.tiles < 6 then
for i = #override.tiles, 6 do
override.tiles[i] = override.tiles[#override.tiles]
end
end
-- add inlets to the sides that connect to tubes
local tile_directions = {"top", "bottom", "right", "left", "back", "front"}
for i, direction in ipairs(tile_directions) do
if connect_sides[direction] then
if type(override.tiles[i]) == "string" then
override.tiles[i] = override.tiles[i] .. tube_entry
elseif type(override.tiles[i]) == "table" and not override.tiles[i].animation then
override.tiles[i].name = override.tiles[i].name .. tube_entry
end
end
end

local old_after_place_node = override.after_place_node or old_def.after_place_node or function() end
override.after_place_node = function(pos, placer)
old_after_place_node(pos, placer)
pipeworks.after_place(pos)
end

local old_after_dig = override.after_dig or old_def.after_dig or function() end
override.after_dig_node = function(pos, oldnode, oldmetadata, digger)
old_after_dig(pos, oldnode, oldmetadata, digger)
pipeworks.after_dig(pos, oldnode, oldmetadata, digger)
end

local old_on_rotate
if override.on_rotate ~= nil then
old_on_rotate = override.on_rotate
elseif old_def.on_rotate ~= nil then
old_on_rotate = old_def.on_rotate
else
old_on_rotate = function() end
end
-- on_rotate = false -> rotation disabled, no need to update tubes
-- everything else: undefined by the most common screwdriver mods
if type(old_on_rotate) == "function" then
override.on_rotate = function(pos, node, user, mode, new_param2)
if old_on_rotate(pos, node, user, mode, new_param2) ~= false then
return pipeworks.on_rotate(pos, node, user, mode, new_param2)
else
return false
end
end
end

override.tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("main", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if meta:get_int("splitstacks") == 1 then
stack = stack:peek_item(1)
end
return inv:room_for_item("main", stack)
end,
input_inventory = "main",
connect_sides = connect_sides
}

-- Add the extra groups
override.groups = override.groups or old_def.groups or {}
override.groups.tubedevice = 1
override.groups.tubedevice_receiver = 1

minetest.override_item(chestname, override)
pipeworks.chests[chestname] = true
end

-- this bit of code modifies the default and hades chests to be compatible
-- with pipeworks.

Expand Down Expand Up @@ -258,4 +167,8 @@ elseif minetest.get_modpath("hades_chests") then
pipeworks.override_chest(chestname, override, connect_sides)
pipeworks.override_chest(chestname_protected, override_protected, connect_sides)
end
elseif minetest.get_modpath("mcl_barrels") then
local connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1}
pipeworks.override_chest("mcl_barrels:barrel_closed", {}, connect_sides)
pipeworks.override_chest("mcl_barrels:barrel_open", {}, connect_sides)
end
8 changes: 4 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dofile(pipeworks.modpath.."/luaentity.lua")
dofile(pipeworks.modpath.."/item_transport.lua")
dofile(pipeworks.modpath.."/flowing_logic.lua")
dofile(pipeworks.modpath.."/filter-injector.lua")
dofile(pipeworks.modpath.."/chests.lua")
dofile(pipeworks.modpath.."/trashcan.lua")
dofile(pipeworks.modpath.."/wielder.lua")
dofile(pipeworks.modpath.."/tubes/registration.lua")
Expand Down Expand Up @@ -95,12 +96,11 @@ end
if pipeworks.enable_pipe_devices then
dofile(pipeworks.modpath.."/devices.lua")
end
if pipeworks.enable_redefines and (minetest.get_modpath("default") or minetest.get_modpath("hades_core")) then
if pipeworks.enable_redefines then
dofile(pipeworks.modpath.."/compat-chests.lua")
dofile(pipeworks.modpath.."/compat-furnaces.lua")
end
if pipeworks.enable_redefines and minetest.get_modpath("mcl_barrels") then
dofile(pipeworks.modpath.."/mcl_barrels.lua")
if pipeworks.enable_redefines and (minetest.get_modpath("default") or minetest.get_modpath("hades_core")) then
dofile(pipeworks.modpath.."/compat-furnaces.lua")
end
if pipeworks.enable_redefines and minetest.get_modpath("mcl_furnaces") then
dofile(pipeworks.modpath.."/mcl_furnaces.lua")
Expand Down
72 changes: 0 additions & 72 deletions mcl_barrels.lua

This file was deleted.

0 comments on commit 3eef218

Please sign in to comment.