diff options
Diffstat (limited to 'railcart')
-rw-r--r-- | railcart/init.lua | 26 | ||||
-rw-r--r-- | railcart/railcart.lua | 31 |
2 files changed, 56 insertions, 1 deletions
diff --git a/railcart/init.lua b/railcart/init.lua index c18bc82..c637e3f 100644 --- a/railcart/init.lua +++ b/railcart/init.lua @@ -9,7 +9,14 @@ if input then if data then local carts = minetest.deserialize(data) or {} for id, ref in pairs(carts) do - railcart.allcarts[id] = railcart.cart:new(ref) + local cart = railcart.cart:new(ref) + local inv = railcart:create_detached_inventory(cart.id) + ref.inv = ref.inv or {} + for i, stack in pairs(ref.inv) do + inv:set_stack("main", i, stack) + end + cart.inv = inv + railcart.allcarts[id] = cart end end input = nil @@ -92,6 +99,22 @@ minetest.register_entity("railcart:cart_entity", { if not is_valid_player(clicker) then return end + if clicker:get_player_control().sneak then + local name = clicker:get_player_name() + local cart = self.cart or {} + if cart.id and name then + local formspec = "size[8,9]".. + default.gui_bg..default.gui_bg_img..default.gui_slots.. + "list[detached:railcart_"..cart.id..";main;0,0.3;8,4;]".. + "list[current_player;main;0,4.85;8,1;]".. + "list[current_player;main;0,6.08;8,3;8]".. + "listring[detached:railcart_"..cart.id..";main]".. + "listring[current_player;main]".. + default.get_hotbar_bg(0,4.85) + minetest.show_formspec(name, "inventory", formspec) + end + return + end if self.driver and clicker == self.driver then self.driver = nil clicker:set_detach() @@ -138,6 +161,7 @@ minetest.register_craftitem("railcart:cart", { end local cart = railcart.cart:new() cart.id = #railcart.allcarts + 1 + cart.inv = railcart:create_detached_inventory(cart.id) cart.pos = pos cart.prev = vector.new(pos) cart.accel = railtrack:get_acceleration(pos) diff --git a/railcart/railcart.lua b/railcart/railcart.lua index 68efda5..68174b6 100644 --- a/railcart/railcart.lua +++ b/railcart/railcart.lua @@ -19,6 +19,7 @@ railcart.cart = { target = nil, prev = nil, accel = nil, + inv = nil, dir = {x=0, y=0, z=0}, vel = {x=0, y=0, z=0}, acc = {x=0, y=0, z=0}, @@ -75,6 +76,14 @@ function railcart:save() for k, v in pairs(cart) do ref[k] = v end + local inv = {} + if ref.inv then + local list = ref.inv:get_list("main") + for i, stack in ipairs(list) do + inv[i] = stack:to_string() + end + end + ref.inv = inv ref.entity = nil table.insert(carts, ref) end @@ -85,6 +94,28 @@ function railcart:save() end end +function railcart:create_detached_inventory(id) + local inv = minetest.create_detached_inventory("railcart_"..tostring(id), { + on_put = function(inv, listname, index, stack, player) + railcart:save() + end, + on_take = function(inv, listname, index, stack, player) + railcart:save() + end, + allow_put = function(inv, listname, index, stack, player) + return 1 + end, + allow_take = function(inv, listname, index, stack, player) + return stack:get_count() + end, + allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + return count + end, + }) + inv:set_size("main", 32) + return inv +end + function railcart:get_cart_ref(id) local cart_ref = {} for _, ref in pairs(minetest.luaentities) do |