init.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. minetest.registered_entities["__builtin:item"].set_item = function(self, item)
  2. local stack = ItemStack(item or self.itemstring)
  3. self.itemstring = stack:to_string()
  4. if self.itemstring == "" then
  5. -- item not yet known
  6. return
  7. end
  8. -- Backwards compatibility: old clients use the texture
  9. -- to get the type of the item
  10. local itemname = stack:is_known() and stack:get_name() or "unknown"
  11. local max_count = stack:get_stack_max()
  12. local count = math.min(stack:get_count(), max_count)
  13. local size = .5 + 0.1 * (count / max_count) ^ (1 / 3)
  14. local def = core.registered_items[itemname]
  15. local glow = def and def.light_source and
  16. math.floor(def.light_source / 2 + 0.5)
  17. local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting
  18. local c = {-size, -size, -size, size, size, size}
  19. self.object:set_properties({
  20. collide_with_objects = true,
  21. is_visible = true,
  22. visual = "wielditem",
  23. textures = {itemname},
  24. visual_size = {x = size + size_bias, y = size + size_bias},
  25. collisionbox = c,
  26. wield_item = self.itemstring,
  27. glow = glow,
  28. })
  29. self.object:set_yaw(math.random(1,6.2))
  30. -- cache for usage in on_step
  31. self._collisionbox = c
  32. end