Skip to content

Commit 5879783

Browse files
committed
bones inventory reordering
1 parent dfd0805 commit 5879783

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

mods/bones/init.lua

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,45 @@ end
6464
local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200
6565
local share_bones_time_early = tonumber(minetest.settings:get("share_bones_time_early")) or share_bones_time / 4
6666

67+
local function find_next_empty(inv,listname,start)
68+
while start <= inv:get_size(listname) do
69+
if inv:get_stack(listname, start):get_count() == 0 then
70+
return start
71+
end
72+
start = start + 1
73+
end
74+
return -1
75+
end
76+
77+
local function find_next_populated(inv, listname, start)
78+
while start <= inv:get_size(listname) do
79+
if inv:get_stack(listname, start):get_count() > 0 then
80+
return start
81+
end
82+
start = start + 1
83+
end
84+
return -1
85+
end
86+
87+
-- slot reordering to make sure the first rows of the bone are always populated
88+
local function bones_inv_reorder(meta)
89+
local next_empty=1 -- there are no empty slots inside the bones before this
90+
local next_populated=1 -- there are no populated slots preceded by unpopulated slots before this
91+
local inv=meta:get_inventory()
92+
next_empty=find_next_empty(inv,"main",next_empty)
93+
if next_empty < 0 then
94+
return
95+
end
96+
next_populated=find_next_populated(inv,"main",next_empty+1)
97+
while next_populated > 0 do
98+
local stack = inv:get_stack("main", next_populated)
99+
inv:set_stack("main", next_populated, ItemStack())
100+
inv:set_stack("main", next_empty, stack)
101+
next_empty=find_next_empty(inv,"main",next_empty+1)
102+
next_populated=find_next_populated(inv,"main",next_populated+1)
103+
end
104+
end
105+
67106
local bones_def = {
68107
description = S("Bones"),
69108
tiles = {
@@ -115,6 +154,8 @@ local bones_def = {
115154
minetest.add_item(pos, "bones:bones")
116155
end
117156
minetest.remove_node(pos)
157+
else
158+
bones_inv_reorder(meta)
118159
end
119160
end,
120161

@@ -127,7 +168,8 @@ local bones_def = {
127168
return
128169
end
129170

130-
local inv = minetest.get_meta(pos):get_inventory()
171+
local meta = minetest.get_meta(pos)
172+
local inv = meta:get_inventory()
131173
local player_inv = player:get_inventory()
132174
local has_space = true
133175

@@ -142,14 +184,17 @@ local bones_def = {
142184
end
143185
end
144186

145-
-- remove bones if player emptied them
146187
if has_space then
188+
-- remove bones if player emptied them
147189
if player_inv:room_for_item("main", {name = "bones:bones"}) then
148190
player_inv:add_item("main", {name = "bones:bones"})
149191
else
150192
minetest.add_item(pos,"bones:bones")
151193
end
152194
minetest.remove_node(pos)
195+
else
196+
-- reorder items if player haven't emptied the bones
197+
bones_inv_reorder(meta)
153198
end
154199
end,
155200

0 commit comments

Comments
 (0)