init.lua~ 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. christmas = {}
  2. christmas.players = {}
  3. christmas.data = minetest.get_mod_storage("christmas")
  4. local function xplayer(player)
  5. if not player:is_player() then
  6. return
  7. end
  8. local name = player:get_player_name()
  9. return christmas.players[name]
  10. end
  11. function christmas.get_present_formspec(pos)--Taken from default chest
  12. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  13. local formspec =
  14. "size[8,9]" ..
  15. "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
  16. "list[current_player;main;0,4.85;8,1;]" ..
  17. "list[current_player;main;0,6.08;8,3;8]" ..
  18. "listring[nodemeta:" .. spos .. ";main]" ..
  19. "listring[current_player;main]"
  20. return formspec
  21. end
  22. local function to_time(time)
  23. local remaining = time % 86400
  24. remaining = remaining % 3600
  25. local minutes = math.floor(remaining/60)
  26. remaining = remaining % 60
  27. local seconds = remaining
  28. if (minutes < 10) then
  29. minutes = "0" .. tostring(minutes)
  30. end
  31. if (seconds < 10) then
  32. seconds = "0" .. tostring(seconds)
  33. end
  34. answer = minutes..':'..seconds
  35. return answer
  36. end
  37. function christmas.eat_candy(hp_change, replace_with_item)
  38. return function(itemstack, user, pointed_thing)
  39. local name = user:get_player_name()
  40. local p = xplayer(user)
  41. christmas.players[name].candy = p.candy +1
  42. if p.candy == 8 then
  43. p.time = 60
  44. p.hud.ui = user:hud_add({
  45. hud_elem_type = "image",
  46. position = {x = 0.1, y = 0.5},
  47. offset = {x = -220, y = -260},
  48. text = "christmas_powerup_ui.png",
  49. scale = { x = 16, y = 17},
  50. alignment = { x = 1, y = 0 },
  51. })
  52. p.hud.icon = user:hud_add({
  53. hud_elem_type = "image",
  54. position = {x = 0.1, y = 0.5},
  55. offset = {x = -90, y = -251},
  56. text = "christmas_candy_cane.png",
  57. scale = { x = 16, y = 16},
  58. alignment = { x = 1, y = 0 },
  59. })
  60. p.hud.time = user:hud_add({
  61. hud_elem_type = "text",
  62. position = {x = 0.1, y = 0.5},
  63. offset = {x = 10, y = -10},
  64. text = to_time (p.time),
  65. number = 0xffffff,
  66. scale = { x = 16, y = 16},
  67. alignment = { x = 0, y = 0 },
  68. })
  69. end
  70. if p.time > 0 then
  71. p.time = p.time + 3
  72. end
  73. return minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  74. end
  75. end
  76. minetest.register_craftitem("christmas:candy_cane", {
  77. description = "Candy Cane",
  78. inventory_image = "christmas_candy_cane.png",
  79. on_use = christmas.eat_candy(1)
  80. })
  81. minetest.register_craftitem("christmas:mince_pie", {
  82. description = "Mince Pie",
  83. inventory_image = "christmas_mincepie.png",
  84. on_use = minetest.item_eat(2)
  85. })
  86. minetest.register_craftitem("christmas:gingerbread_man", {
  87. description = "Gingerbread Man",
  88. inventory_image = "christmas_gingerbread_man.png",
  89. on_use = minetest.item_eat(2)
  90. })
  91. minetest.register_craftitem("christmas:cracker", {
  92. description = "Christmas Cracker\n (To be shared with a friend)",
  93. inventory_image = "christmas_cracker.png",
  94. on_use = minetest.item_eat(2)
  95. })
  96. minetest.register_node("christmas:eggnog", {
  97. description = "Eggnog",
  98. drawtype = "plantlike",
  99. tiles = {"christmas_eggnog.png"},
  100. inventory_image = "christmas_eggnog.png",
  101. on_use = minetest.item_eat(10),
  102. groups = {vessel = 1, dig_immediate = 3, attached_node = 1},
  103. })
  104. minetest.register_node("christmas:present", {
  105. description = "Christmas present",
  106. tiles = {
  107. "christmas_present.png",
  108. "christmas_present_top.png"
  109. },
  110. drawtype = "mesh",
  111. paramtype = "light",
  112. mesh = "christmas_present.obj",
  113. groups = {oddly_breakable_by_hand = 3, attached_node = 1},
  114. on_construct = function(pos, itemstack, placer, pointed_thing)
  115. local meta = minetest.get_meta(pos)
  116. meta:set_string("infotext", "Christmas Present")
  117. meta:set_string("owner", "")
  118. local inv = meta:get_inventory()
  119. inv:set_size("main", 1)
  120. end,
  121. after_place_node = function(pos, placer)
  122. local meta = minetest.get_meta(pos)
  123. meta:set_string("owner", placer:get_player_name() or "")
  124. meta:set_string("infotext", "Present from ".. meta:get_string("owner"))
  125. end,
  126. on_rightclick = function(pos, node, player, itemstack, pointed_thing)
  127. minetest.after(0.2,
  128. minetest.show_formspec,
  129. player:get_player_name(),
  130. "christmas:present",
  131. christmas.get_present_formspec(pos))
  132. end,
  133. })
  134. minetest.register_node("christmas:stocking", {
  135. description = "Christmas Stocking",
  136. drawtype = "signlike",
  137. tiles = {"christmas_stocking.png"},
  138. inventory_image = "christmas_stocking.png",
  139. paramtype = "light",
  140. paramtype2 = "wallmounted",
  141. sunlight_propagates = true,
  142. selection_box = {
  143. type = "wallmounted",
  144. },
  145. groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3},
  146. walkable = false,
  147. on_construct = function(pos, itemstack, player)
  148. local meta = minetest.get_meta(pos)
  149. meta:set_string("infotext", player:get_player_name().."'s Stocking")
  150. end,
  151. })
  152. minetest.register_on_joinplayer(function(player)
  153. local name = player:get_player_name()
  154. christmas.players[name] = {}
  155. christmas.players[name].candy = 0
  156. christmas.players[name].hud = {}
  157. christmas.players[name].time = 0
  158. end)
  159. local t = 0
  160. minetest.register_globalstep (function(dtime)
  161. t = t + dtime
  162. if t > 1 then
  163. t = 0
  164. end
  165. for _, player in ipairs(minetest.get_connected_players()) do
  166. local p = xplayer(player)
  167. if p.time > 0 and t > 1-dtime then
  168. p.time = p.time - 1
  169. player:hud_change(p.hud.time, "text", to_time(p.time))
  170. elseif math.floor(p.time) == 1 then
  171. p.candy = 0
  172. end
  173. if p.time > 0 then
  174. player:set_physics_override({
  175. speed = 2.5,
  176. })
  177. end
  178. --minetest.chat_send_all(p.candy)
  179. if p.time == 0 then
  180. player:set_physics_override({
  181. speed = 1,
  182. })
  183. player:hud_remove(p.hud.ui)
  184. player:hud_remove(p.hud.icon)
  185. player:hud_remove(p.hud.time)
  186. end
  187. end
  188. end)