diff options
author | stujones11 <stujones111@gmail.com> | 2016-01-23 17:10:53 +0000 |
---|---|---|
committer | stujones11 <stujones111@gmail.com> | 2016-01-23 17:10:53 +0000 |
commit | a23e20f05301849a7902c6b11c55e2a581b0831b (patch) | |
tree | 0f0c5cb5a6287b048b6a4943877f7a4f46ebb350 | |
parent | 0e9a29ddfc38ce8c198be21bd9597a7d5f03a9d4 (diff) |
Don't assume table index is equal to cart id
-rw-r--r-- | railcart/init.lua | 5 | ||||
-rw-r--r-- | railcart/railcart.lua | 24 |
2 files changed, 24 insertions, 5 deletions
diff --git a/railcart/init.lua b/railcart/init.lua index 8d60051..07edea3 100644 --- a/railcart/init.lua +++ b/railcart/init.lua @@ -68,8 +68,7 @@ minetest.register_entity("railcart:cart_entity", { if puncher:get_player_control().sneak then if self.cart then if self.cart.id then - railcart.allcarts[self.cart.id] = nil - railcart:save() + railcart:remove_cart(self.cart.id) end end self.object:remove() @@ -167,7 +166,7 @@ minetest.register_craftitem("railcart:cart", { end local cons = railtrack:get_connections(pos) local cart = railcart.cart:new() - cart.id = #railcart.allcarts + 1 + cart.id = railcart:get_new_id() cart.inv = railcart:create_detached_inventory(cart.id) cart.pos = pos cart.prev = vector.new(pos) diff --git a/railcart/railcart.lua b/railcart/railcart.lua index 655fffe..c80b507 100644 --- a/railcart/railcart.lua +++ b/railcart/railcart.lua @@ -52,7 +52,7 @@ function railcart.cart:on_step(dtime) return end self.timer = RAILCART_OBJECT_UPDATE_TIME - local entity = railcart:get_cart_ref(self.id) + local entity = railcart:get_cart_entity(self.id) if entity.object then return end @@ -116,7 +116,27 @@ function railcart:create_detached_inventory(id) return inv end -function railcart:get_cart_ref(id) +function railcart:remove_cart(id) + for i, cart in pairs(railcart.allcarts) do + if cart.id == id then + railcart.allcarts[i] = nil + railcart:save() + break + end + end +end + +function railcart:get_new_id() + local id = 0 + for _, cart in pairs(railcart.allcarts) do + if cart.id > id then + id = cart.id + end + end + return id + 1 +end + +function railcart:get_cart_entity(id) local cart_ref = {} for _, ref in pairs(minetest.luaentities) do if ref.cart then |