bf5fb3db810acfbe031dec11ff8f2d85e5e7b906
[oweals/minetest.git] / clientmods / preview / init.lua
1 local modname = core.get_current_modname() or "??"
2 local modstorage = core.get_mod_storage()
3 local mod_channel
4
5 dofile("preview:example.lua")
6 -- This is an example function to ensure it's working properly, should be removed before merge
7 core.register_on_shutdown(function()
8         print("[PREVIEW] shutdown client")
9 end)
10
11 core.register_on_connect(function()
12         print("[PREVIEW] Player connection completed")
13         local server_info = core.get_server_info()
14         print("Server version: " .. server_info.protocol_version)
15         print("Server ip: " .. server_info.ip)
16         print("Server address: " .. server_info.address)
17         print("Server port: " .. server_info.port)
18
19         mod_channel = core.mod_channel_join("experimental_preview")
20 end)
21
22 core.register_on_modchannel_message(function(channel, sender, message)
23         print("[PREVIEW][modchannels] Received message `" .. message .. "` on channel `"
24                         .. channel .. "` from sender `" .. sender .. "`")
25         core.after(1, function()
26                 mod_channel:send_all("CSM preview received " .. message)
27         end)
28 end)
29
30 core.register_on_modchannel_signal(function(channel, signal)
31         print("[PREVIEW][modchannels] Received signal id `" .. signal .. "` on channel `"
32                         .. channel)
33 end)
34
35 core.register_on_placenode(function(pointed_thing, node)
36         print("The local player place a node!")
37         print("pointed_thing :" .. dump(pointed_thing))
38         print("node placed :" .. dump(node))
39         return false
40 end)
41
42 core.register_on_item_use(function(itemstack, pointed_thing)
43         print("The local player used an item!")
44         print("pointed_thing :" .. dump(pointed_thing))
45         print("item = " .. itemstack:get_name())
46         return false
47 end)
48
49 -- This is an example function to ensure it's working properly, should be removed before merge
50 core.register_on_receiving_chat_message(function(message)
51         print("[PREVIEW] Received message " .. message)
52         return false
53 end)
54
55 -- This is an example function to ensure it's working properly, should be removed before merge
56 core.register_on_sending_chat_message(function(message)
57         print("[PREVIEW] Sending message " .. message)
58         return false
59 end)
60
61 -- This is an example function to ensure it's working properly, should be removed before merge
62 core.register_on_hp_modification(function(hp)
63         print("[PREVIEW] HP modified " .. hp)
64 end)
65
66 -- This is an example function to ensure it's working properly, should be removed before merge
67 core.register_on_damage_taken(function(hp)
68         print("[PREVIEW] Damage taken " .. hp)
69 end)
70
71 -- This is an example function to ensure it's working properly, should be removed before merge
72 core.register_globalstep(function(dtime)
73         -- print("[PREVIEW] globalstep " .. dtime)
74 end)
75
76 -- This is an example function to ensure it's working properly, should be removed before merge
77 core.register_chatcommand("dump", {
78         func = function(param)
79                 return true, dump(_G)
80         end,
81 })
82
83 core.register_chatcommand("colorize_test", {
84         func = function(param)
85                 return true, core.colorize("red", param)
86         end,
87 })
88
89 core.register_chatcommand("test_node", {
90         func = function(param)
91                 core.display_chat_message(dump(core.get_node({x=0, y=0, z=0})))
92                 core.display_chat_message(dump(core.get_node_or_nil({x=0, y=0, z=0})))
93         end,
94 })
95
96 local function preview_minimap()
97         local minimap = core.ui.minimap
98         if not minimap then
99                 print("[PREVIEW] Minimap is disabled. Skipping.")
100                 return
101         end
102         minimap:set_mode(4)
103         minimap:show()
104         minimap:set_pos({x=5, y=50, z=5})
105         minimap:set_shape(math.random(0, 1))
106
107         print("[PREVIEW] Minimap: mode => " .. dump(minimap:get_mode()) ..
108                         " position => " .. dump(minimap:get_pos()) ..
109                         " angle => " .. dump(minimap:get_angle()))
110 end
111
112 core.after(2, function()
113         print("[PREVIEW] loaded " .. modname .. " mod")
114         modstorage:set_string("current_mod", modname)
115         print(modstorage:get_string("current_mod"))
116         preview_minimap()
117 end)
118
119 core.after(4, function()
120         if mod_channel:is_writeable() then
121                 mod_channel:send_all("preview talk to experimental")
122         end
123 end)
124
125 core.after(5, function()
126         if core.ui.minimap then
127                 core.ui.minimap:show()
128         end
129
130         print("[PREVIEW] Day count: " .. core.get_day_count() ..
131                 " time of day " .. core.get_timeofday())
132
133         print("[PREVIEW] Node level: " .. core.get_node_level({x=0, y=20, z=0}) ..
134                 " max level " .. core.get_node_max_level({x=0, y=20, z=0}))
135
136         print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
137                 {"group:tree", "default:dirt", "default:stone"})))
138 end)
139
140 core.register_on_dignode(function(pos, node)
141         print("The local player dug a node!")
142         print("pos:" .. dump(pos))
143         print("node:" .. dump(node))
144         return false
145 end)
146
147 core.register_on_punchnode(function(pos, node)
148         print("The local player punched a node!")
149         local itemstack = core.get_wielded_item()
150         --[[
151         -- getters
152         print(dump(itemstack:is_empty()))
153         print(dump(itemstack:get_name()))
154         print(dump(itemstack:get_count()))
155         print(dump(itemstack:get_wear()))
156         print(dump(itemstack:get_meta()))
157         print(dump(itemstack:get_metadata()
158         print(dump(itemstack:is_known()))
159         --print(dump(itemstack:get_definition()))
160         print(dump(itemstack:get_tool_capabilities()))
161         print(dump(itemstack:to_string()))
162         print(dump(itemstack:to_table()))
163         -- setters
164         print(dump(itemstack:set_name("default:dirt")))
165         print(dump(itemstack:set_count("95")))
166         print(dump(itemstack:set_wear(934)))
167         print(dump(itemstack:get_meta()))
168         print(dump(itemstack:get_metadata()))
169         --]]
170         print(dump(itemstack:to_table()))
171         print("pos:" .. dump(pos))
172         print("node:" .. dump(node))
173         return false
174 end)
175
176 core.register_chatcommand("privs", {
177         func = function(param)
178                 return true, core.privs_to_string(minetest.get_privilege_list())
179         end,
180 })