2018-02-17 02:31:53 +01:00
|
|
|
|
local pipeworks_enabled = minetest.get_modpath("pipeworks") ~= nil
|
|
|
|
|
|
2017-10-12 09:55:45 +02:00
|
|
|
|
-- Sends a message onto the Digilines network.
|
|
|
|
|
-- pos: the position of the Digilines chest node.
|
|
|
|
|
-- action: the action string indicating what happened.
|
|
|
|
|
-- stack: the ItemStack that the action acted on (optional).
|
|
|
|
|
-- side: which side of the chest the action occurred (optional).
|
|
|
|
|
local function send_message(pos, action, stack, side)
|
|
|
|
|
local channel = minetest.get_meta(pos):get_string("channel")
|
|
|
|
|
local msg = {
|
|
|
|
|
action = action,
|
|
|
|
|
stack = stack and stack:to_table(),
|
|
|
|
|
-- Duplicate the vector in case the caller expects it not to change.
|
|
|
|
|
side = side and vector.new(side)
|
|
|
|
|
}
|
|
|
|
|
digilines.receptor_send(pos, digilines.rules.default, channel, msg)
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-10-12 09:55:45 +02:00
|
|
|
|
-- Checks if the inventory has become empty and, if so, sends an empty message.
|
|
|
|
|
local function check_empty(pos)
|
|
|
|
|
if minetest.get_meta(pos):get_inventory():is_empty("main") then
|
|
|
|
|
send_message(pos, "empty")
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-10-12 09:55:45 +02:00
|
|
|
|
-- Checks if the inventory has become full for a particular type of item and,
|
|
|
|
|
-- if so, sends a full message.
|
|
|
|
|
local function check_full(pos, stack)
|
|
|
|
|
local one_item_stack = ItemStack(stack)
|
|
|
|
|
one_item_stack:set_count(1)
|
|
|
|
|
if not minetest.get_meta(pos):get_inventory():room_for_item("main", one_item_stack) then
|
|
|
|
|
send_message(pos, "full", one_item_stack)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-02-17 02:31:53 +01:00
|
|
|
|
local tubeconn = pipeworks_enabled and "^pipeworks_tube_connection_wooden.png" or ""
|
|
|
|
|
local tubescan = pipeworks_enabled and function(pos) pipeworks.scan_for_tube_objects(pos) end or nil
|
2014-11-19 19:27:42 +01:00
|
|
|
|
|
2017-10-12 09:55:45 +02:00
|
|
|
|
-- A place to remember things from allow_metadata_inventory_put to
|
|
|
|
|
-- on_metadata_inventory_put. This is a hack due to issue
|
|
|
|
|
-- minetest/minetest#6534 that should be removed once that’s fixed.
|
|
|
|
|
local last_inventory_put_index
|
|
|
|
|
local last_inventory_put_stack
|
|
|
|
|
|
|
|
|
|
-- A place to remember things from allow_metadata_inventory_take to
|
|
|
|
|
-- tube.remove_items. This is a hack due to issue minetest-mods/pipeworks#205
|
|
|
|
|
-- that should be removed once that’s fixed.
|
|
|
|
|
local last_inventory_take_index
|
|
|
|
|
|
2016-05-28 01:08:53 +02:00
|
|
|
|
minetest.register_alias("digilines_inventory:chest", "digilines:chest")
|
|
|
|
|
minetest.register_node("digilines:chest", {
|
2014-01-26 08:48:46 +01:00
|
|
|
|
description = "Digiline Chest",
|
2014-11-19 19:27:42 +01:00
|
|
|
|
tiles = {
|
|
|
|
|
"default_chest_top.png"..tubeconn,
|
|
|
|
|
"default_chest_top.png"..tubeconn,
|
|
|
|
|
"default_chest_side.png"..tubeconn,
|
|
|
|
|
"default_chest_side.png"..tubeconn,
|
|
|
|
|
"default_chest_side.png"..tubeconn,
|
|
|
|
|
"default_chest_front.png",
|
2014-01-26 08:48:46 +01:00
|
|
|
|
},
|
2014-11-19 19:27:42 +01:00
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
|
legacy_facedir_simple = true,
|
|
|
|
|
groups = {choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1},
|
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
2014-01-26 08:48:46 +01:00
|
|
|
|
on_construct = function(pos)
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
meta:set_string("infotext", "Digiline Chest")
|
|
|
|
|
meta:set_string("formspec", "size[8,10]"..
|
|
|
|
|
((default and default.gui_bg) or "")..
|
|
|
|
|
((default and default.gui_bg_img) or "")..
|
|
|
|
|
((default and default.gui_slots) or "")..
|
|
|
|
|
"label[0,0;Digiline Chest]"..
|
|
|
|
|
"list[current_name;main;0,1;8,4;]"..
|
|
|
|
|
"field[2,5.5;5,1;channel;Channel;${channel}]"..
|
|
|
|
|
((default and default.get_hotbar_bg) and default.get_hotbar_bg(0,6) or "")..
|
2018-02-17 02:31:53 +01:00
|
|
|
|
"list[current_player;main;0,6;8,4;]"..
|
|
|
|
|
"listring[]")
|
2014-11-19 19:27:42 +01:00
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
|
inv:set_size("main", 8*4)
|
|
|
|
|
end,
|
|
|
|
|
after_place_node = tubescan,
|
|
|
|
|
after_dig_node = tubescan,
|
2017-03-01 11:31:48 +01:00
|
|
|
|
can_dig = function(pos)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
return minetest.get_meta(pos):get_inventory():is_empty("main")
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end,
|
2017-03-01 11:31:48 +01:00
|
|
|
|
on_receive_fields = function(pos, _, fields, sender)
|
2016-05-23 20:13:24 +02:00
|
|
|
|
local name = sender:get_player_name()
|
|
|
|
|
if minetest.is_protected(pos, name) and not minetest.check_player_privs(name, {protection_bypass=true}) then
|
|
|
|
|
minetest.record_protection_violation(pos, name)
|
|
|
|
|
return
|
|
|
|
|
end
|
2014-06-05 07:55:29 +02:00
|
|
|
|
if fields.channel ~= nil then
|
|
|
|
|
minetest.get_meta(pos):set_string("channel",fields.channel)
|
|
|
|
|
end
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end,
|
2014-11-19 19:27:42 +01:00
|
|
|
|
digiline = {
|
|
|
|
|
receptor = {},
|
|
|
|
|
effector = {
|
2017-03-01 11:31:48 +01:00
|
|
|
|
action = function() end
|
2014-11-19 19:27:42 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
tube = {
|
|
|
|
|
connect_sides = {left=1, right=1, back=1, front=1, bottom=1, top=1},
|
2014-01-26 08:48:46 +01:00
|
|
|
|
connects = function(i,param2)
|
|
|
|
|
return not pipeworks.connects.facingFront(i,param2)
|
|
|
|
|
end,
|
2014-11-19 19:27:42 +01:00
|
|
|
|
input_inventory = "main",
|
2017-10-12 09:55:45 +02:00
|
|
|
|
can_insert = function(pos, _, stack, direction)
|
|
|
|
|
local ret = minetest.get_meta(pos):get_inventory():room_for_item("main", stack)
|
|
|
|
|
if not ret then
|
|
|
|
|
-- The stack cannot be accepted. It will never be passed to
|
|
|
|
|
-- insert_object, but it should be reported as a toverflow.
|
|
|
|
|
-- Here, direction = direction item is moving, which is into
|
|
|
|
|
-- side.
|
|
|
|
|
local side = vector.multiply(direction, -1)
|
|
|
|
|
send_message(pos, "toverflow", stack, side)
|
|
|
|
|
end
|
|
|
|
|
return ret
|
2014-11-19 19:27:42 +01:00
|
|
|
|
end,
|
2017-10-12 09:55:45 +02:00
|
|
|
|
insert_object = function(pos, _, stack, direction)
|
|
|
|
|
-- Here, direction = direction item is moving, which is into side.
|
|
|
|
|
local side = vector.multiply(direction, -1)
|
|
|
|
|
local leftover = minetest.get_meta(pos):get_inventory():add_item("main", stack)
|
|
|
|
|
local leftover_count = leftover:get_count()
|
|
|
|
|
local stack_count = stack:get_count()
|
|
|
|
|
if leftover_count ~= stack_count then
|
|
|
|
|
-- Some items were added. Report them.
|
|
|
|
|
stack:set_count(stack_count - leftover_count)
|
|
|
|
|
send_message(pos, "tput", stack, side)
|
|
|
|
|
check_full(pos, stack)
|
|
|
|
|
end
|
|
|
|
|
if leftover_count ~= 0 then
|
|
|
|
|
-- Some items could not be added and bounced back. Report them.
|
|
|
|
|
send_message(pos, "toverflow", leftover, side)
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end
|
|
|
|
|
return leftover
|
|
|
|
|
end,
|
2017-10-12 09:55:45 +02:00
|
|
|
|
remove_items = function(pos, _, stack, dir, count)
|
|
|
|
|
-- Here, stack is the ItemStack in our own inventory that is being
|
|
|
|
|
-- pulled from, NOT the stack that is actually pulled out.
|
|
|
|
|
-- Combining it with count gives the stack that is pulled out.
|
|
|
|
|
-- Also, note that Pipeworks doesn’t pass the index to this
|
|
|
|
|
-- function, so we use the one recorded in
|
|
|
|
|
-- allow_metadata_inventory_take; because we don’t implement
|
|
|
|
|
-- tube.can_remove, Pipeworks will call
|
|
|
|
|
-- allow_metadata_inventory_take instead and will pass it the
|
|
|
|
|
-- index.
|
|
|
|
|
local taken = stack:take_item(count)
|
|
|
|
|
minetest.get_meta(pos):get_inventory():set_stack("main", last_inventory_take_index, stack)
|
|
|
|
|
send_message(pos, "ttake", taken, dir)
|
|
|
|
|
check_empty(pos)
|
|
|
|
|
return taken
|
|
|
|
|
end,
|
2014-11-19 19:27:42 +01:00
|
|
|
|
},
|
2017-10-12 09:55:45 +02:00
|
|
|
|
allow_metadata_inventory_put = function(pos, _, index, stack)
|
|
|
|
|
-- Remember what was in the target slot before the put; see
|
|
|
|
|
-- on_metadata_inventory_put for why we care.
|
|
|
|
|
last_inventory_put_index = index
|
|
|
|
|
last_inventory_put_stack = minetest.get_meta(pos):get_inventory():get_stack("main", index)
|
2014-01-26 08:48:46 +01:00
|
|
|
|
return stack:get_count()
|
|
|
|
|
end,
|
2017-10-12 09:55:45 +02:00
|
|
|
|
allow_metadata_inventory_take = function(_, _, index, stack)
|
|
|
|
|
-- Remember the index value; see tube.remove_items for why we care.
|
|
|
|
|
last_inventory_take_index = index
|
|
|
|
|
return stack:get_count()
|
|
|
|
|
end,
|
|
|
|
|
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
minetest.log("action", player:get_player_name().." moves stuff in chest at "..minetest.pos_to_string(pos))
|
|
|
|
|
end,
|
2017-10-12 09:55:45 +02:00
|
|
|
|
on_metadata_inventory_put = function(pos, _, index, stack, player)
|
|
|
|
|
-- Get what was in the target slot before the put; it has disappeared
|
|
|
|
|
-- by now (been replaced by the result of the put action) but we saved
|
|
|
|
|
-- it in allow_metadata_inventory_put. This should always work
|
|
|
|
|
-- (allow_metadata_inventory_put should AFAICT always be called
|
|
|
|
|
-- immediately before on_metadata_inventory_put), but in case of
|
|
|
|
|
-- something weird happening, just fall back to using an empty
|
|
|
|
|
-- ItemStack rather than crashing.
|
|
|
|
|
local old_stack
|
|
|
|
|
if last_inventory_put_index == index then
|
|
|
|
|
old_stack = last_inventory_put_stack
|
|
|
|
|
last_inventory_put_index = nil
|
|
|
|
|
last_inventory_put_stack = nil
|
2014-01-26 08:48:46 +01:00
|
|
|
|
else
|
2017-10-12 09:55:45 +02:00
|
|
|
|
old_stack = ItemStack(nil)
|
|
|
|
|
end
|
|
|
|
|
-- If the player tries to place a stack into an inventory, there’s
|
|
|
|
|
-- already a stack there, and the existing stack is either of a
|
|
|
|
|
-- different item type or full, then obviously the stacks can’t be
|
|
|
|
|
-- merged; instead the stacks are swapped. This information is not
|
|
|
|
|
-- reported to mods (Minetest core neither tells us that a particular
|
|
|
|
|
-- action was a swap, nor tells us a take followed by a put). In core,
|
|
|
|
|
-- the condition for swapping is that you try to add the new stack to
|
|
|
|
|
-- the existing stack and the leftovers are as big as the original
|
|
|
|
|
-- stack to put. Replicate that logic here using the old stack saved in
|
|
|
|
|
-- allow_metadata_inventory_put. If a swap happened, report it to the
|
|
|
|
|
-- Digilines network as a utake followed by a uput.
|
|
|
|
|
local leftovers = old_stack:add_item(stack)
|
|
|
|
|
if leftovers:get_count() == stack:get_count() then
|
|
|
|
|
send_message(pos, "utake", old_stack)
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end
|
2017-10-12 09:55:45 +02:00
|
|
|
|
send_message(pos, "uput", stack)
|
|
|
|
|
check_full(pos, stack)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
minetest.log("action", player:get_player_name().." puts stuff into chest at "..minetest.pos_to_string(pos))
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end,
|
2017-10-12 09:55:45 +02:00
|
|
|
|
on_metadata_inventory_take = function(pos, _, index, stack, player)
|
|
|
|
|
send_message(pos, "utake", stack)
|
|
|
|
|
check_empty(pos)
|
2014-11-19 19:27:42 +01:00
|
|
|
|
minetest.log("action", player:get_player_name().." takes stuff from chest at "..minetest.pos_to_string(pos))
|
2014-01-26 08:48:46 +01:00
|
|
|
|
end
|
|
|
|
|
})
|
2015-02-05 21:24:22 +01:00
|
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
|
type = "shapeless",
|
2016-05-28 01:08:53 +02:00
|
|
|
|
output = "digilines:chest",
|
2015-02-05 21:24:22 +01:00
|
|
|
|
recipe = {"default:chest", "digilines:wire_std_00000000"}
|
|
|
|
|
})
|