Sneak-digging an item stack prefers keeping the stack separate.

This is allows you to split a stack by throwing individual items
with sneak-drop, letting them settle into a single stack, and
then sneak-digging the stack to keep it separate in your inventory.

This makes it possible to split stacks into arbitrary quantities
without having to use the inventory GUI.
This commit is contained in:
Aaron Suen 2019-03-03 16:22:02 -05:00
parent 41f7b71fbf
commit 3ef32c0206

View File

@ -1,6 +1,6 @@
-- LUALOCALS < --------------------------------------------------------- -- LUALOCALS < ---------------------------------------------------------
local math, minetest, nodecore, pairs, type local ItemStack, math, minetest, nodecore, pairs, type
= math, minetest, nodecore, pairs, type = ItemStack, math, minetest, nodecore, pairs, type
local math_floor, math_pi, math_random, math_sqrt local math_floor, math_pi, math_random, math_sqrt
= math.floor, math.pi, math.random, math.sqrt = math.floor, math.pi, math.random, math.sqrt
-- LUALOCALS > --------------------------------------------------------- -- LUALOCALS > ---------------------------------------------------------
@ -136,14 +136,33 @@ nodecore.register_limited_abm({
-- DIG INVENTORY -- DIG INVENTORY
local digpos local digpos
local digplayer
local old_node_dig = minetest.node_dig local old_node_dig = minetest.node_dig
minetest.node_dig = function(pos, ...) minetest.node_dig = function(pos, node, digger, ...)
local function helper(...) local function helper(...)
digpos = nil digpos = nil
return ... return ...
end end
digpos = pos digpos = pos
return helper(old_node_dig(pos, ...)) digplayer = digger
return helper(old_node_dig(pos, node, digger, ...))
end
local function trydirect(stack)
stack = ItemStack(stack)
if stack:is_empty() then return end
local p = digplayer
if (not p) or (not p:is_player())
or (not p:get_player_control().sneak) then return end
minetest.log("playersneak")
local inv = p:get_inventory()
for i = 1, inv:get_size("main") do
if inv:get_stack("main", i):is_empty() then
inv:set_stack("main", i, stack)
return true
end
end
end end
local old_get_node_drops = minetest.get_node_drops local old_get_node_drops = minetest.get_node_drops
minetest.get_node_drops = function(...) minetest.get_node_drops = function(...)
@ -151,7 +170,7 @@ minetest.get_node_drops = function(...)
if not digpos then return drops end if not digpos then return drops end
drops = drops or {} drops = drops or {}
local stack = nodecore.stack_get(digpos) local stack = nodecore.stack_get(digpos)
if stack and not stack:is_empty() then if stack and not stack:is_empty() and (not trydirect(stack)) then
drops[#drops + 1] = stack drops[#drops + 1] = stack
end end
return drops return drops