]> git.lizzy.rs Git - dragonfireclient.git/blob - clientmods/preview/init.lua
Run on_item_use CSM callback even if item is not marked usable
[dragonfireclient.git] / clientmods / preview / init.lua
1 local modname = assert(core.get_current_modname())
2 local modstorage = core.get_mod_storage()
3 local mod_channel
4
5 dofile(core.get_modpath(modname) .. "example.lua")
6
7 core.register_on_shutdown(function()
8         print("[PREVIEW] shutdown client")
9 end)
10 local id = nil
11
12 local server_info = core.get_server_info()
13 print("Server version: " .. server_info.protocol_version)
14 print("Server ip: " .. server_info.ip)
15 print("Server address: " .. server_info.address)
16 print("Server port: " .. server_info.port)
17 mod_channel = core.mod_channel_join("experimental_preview")
18
19 core.after(4, function()
20         if mod_channel:is_writeable() then
21                 mod_channel:send_all("preview talk to experimental")
22         end
23 end)
24
25 core.after(1, function()
26         id = core.localplayer:hud_add({
27                         hud_elem_type = "text",
28                         name = "example",
29                         number = 0xff0000,
30                         position = {x=0, y=1},
31                         offset = {x=8, y=-8},
32                         text = "You are using the preview mod",
33                         scale = {x=200, y=60},
34                         alignment = {x=1, y=-1},
35         })
36 end)
37
38 core.register_on_modchannel_message(function(channel, sender, message)
39         print("[PREVIEW][modchannels] Received message `" .. message .. "` on channel `"
40                         .. channel .. "` from sender `" .. sender .. "`")
41         core.after(1, function()
42                 mod_channel:send_all("CSM preview received " .. message)
43         end)
44 end)
45
46 core.register_on_modchannel_signal(function(channel, signal)
47         print("[PREVIEW][modchannels] Received signal id `" .. signal .. "` on channel `"
48                         .. channel)
49 end)
50
51 core.register_on_inventory_open(function(inventory)
52         print("INVENTORY OPEN")
53         print(dump(inventory))
54         return false
55 end)
56
57 core.register_on_placenode(function(pointed_thing, node)
58         print("The local player place a node!")
59         print("pointed_thing :" .. dump(pointed_thing))
60         print("node placed :" .. dump(node))
61         return false
62 end)
63
64 core.register_on_item_use(function(itemstack, pointed_thing)
65         print("The local player used an item!")
66         print("pointed_thing :" .. dump(pointed_thing))
67         print("item = " .. itemstack:get_name())
68
69         if not itemstack:is_empty() then
70                 return false
71         end
72
73         local pos = vector.add(core.localplayer:get_pos(), core.camera:get_offset())
74         local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 100))
75
76         local rc = core.raycast(pos, pos2)
77         local i = rc:next()
78         print("[PREVIEW] raycast next: " .. dump(i))
79         if i then
80                 print("[PREVIEW] line of sight: " .. (core.line_of_sight(pos, i.above) and "yes" or "no"))
81
82                 local n1 = core.find_nodes_in_area(pos, i.under, {"default:stone"})
83                 local n2 = core.find_nodes_in_area_under_air(pos, i.under, {"default:stone"})
84                 print(("[PREVIEW] found %s nodes, %s nodes under air"):format(
85                                 n1 and #n1 or "?", n2 and #n2 or "?"))
86         end
87
88         return false
89 end)
90
91 -- This is an example function to ensure it's working properly, should be removed before merge
92 core.register_on_receiving_chat_message(function(message)
93         print("[PREVIEW] Received message " .. message)
94         return false
95 end)
96
97 -- This is an example function to ensure it's working properly, should be removed before merge
98 core.register_on_sending_chat_message(function(message)
99         print("[PREVIEW] Sending message " .. message)
100         return false
101 end)
102
103 -- This is an example function to ensure it's working properly, should be removed before merge
104 core.register_on_hp_modification(function(hp)
105         print("[PREVIEW] HP modified " .. hp)
106 end)
107
108 -- This is an example function to ensure it's working properly, should be removed before merge
109 core.register_on_damage_taken(function(hp)
110         print("[PREVIEW] Damage taken " .. hp)
111 end)
112
113 -- This is an example function to ensure it's working properly, should be removed before merge
114 core.register_chatcommand("dump", {
115         func = function(param)
116                 return true, dump(_G)
117         end,
118 })
119
120 core.register_chatcommand("colorize_test", {
121         func = function(param)
122                 return true, core.colorize("red", param)
123         end,
124 })
125
126 core.register_chatcommand("test_node", {
127         func = function(param)
128                 core.display_chat_message(dump(core.get_node({x=0, y=0, z=0})))
129                 core.display_chat_message(dump(core.get_node_or_nil({x=0, y=0, z=0})))
130         end,
131 })
132
133 local function preview_minimap()
134         local minimap = core.ui.minimap
135         if not minimap then
136                 print("[PREVIEW] Minimap is disabled. Skipping.")
137                 return
138         end
139         minimap:set_mode(4)
140         minimap:show()
141         minimap:set_pos({x=5, y=50, z=5})
142         minimap:set_shape(math.random(0, 1))
143
144         print("[PREVIEW] Minimap: mode => " .. dump(minimap:get_mode()) ..
145                         " position => " .. dump(minimap:get_pos()) ..
146                         " angle => " .. dump(minimap:get_angle()))
147 end
148
149 core.after(2, function()
150         print("[PREVIEW] loaded " .. modname .. " mod")
151         modstorage:set_string("current_mod", modname)
152         print(modstorage:get_string("current_mod"))
153         preview_minimap()
154 end)
155
156 core.after(5, function()
157         if core.ui.minimap then
158                 core.ui.minimap:show()
159         end
160
161         print("[PREVIEW] Time of day " .. core.get_timeofday())
162
163         print("[PREVIEW] Node level: " .. core.get_node_level({x=0, y=20, z=0}) ..
164                 " max level " .. core.get_node_max_level({x=0, y=20, z=0}))
165
166         print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
167                 {"group:tree", "default:dirt", "default:stone"})))
168 end)
169
170 core.register_on_dignode(function(pos, node)
171         print("The local player dug a node!")
172         print("pos:" .. dump(pos))
173         print("node:" .. dump(node))
174         return false
175 end)
176
177 core.register_on_punchnode(function(pos, node)
178         print("The local player punched a node!")
179         local itemstack = core.get_wielded_item()
180         --[[
181         -- getters
182         print(dump(itemstack:is_empty()))
183         print(dump(itemstack:get_name()))
184         print(dump(itemstack:get_count()))
185         print(dump(itemstack:get_wear()))
186         print(dump(itemstack:get_meta()))
187         print(dump(itemstack:get_metadata()
188         print(dump(itemstack:is_known()))
189         --print(dump(itemstack:get_definition()))
190         print(dump(itemstack:get_tool_capabilities()))
191         print(dump(itemstack:to_string()))
192         print(dump(itemstack:to_table()))
193         -- setters
194         print(dump(itemstack:set_name("default:dirt")))
195         print(dump(itemstack:set_count("95")))
196         print(dump(itemstack:set_wear(934)))
197         print(dump(itemstack:get_meta()))
198         print(dump(itemstack:get_metadata()))
199         --]]
200         print(dump(itemstack:to_table()))
201         print("pos:" .. dump(pos))
202         print("node:" .. dump(node))
203         return false
204 end)
205
206 core.register_chatcommand("privs", {
207         func = function(param)
208                 return true, core.privs_to_string(minetest.get_privilege_list())
209         end,
210 })
211
212 core.register_chatcommand("text", {
213         func = function(param)
214                 return core.localplayer:hud_change(id, "text", param)
215         end,
216 })
217
218
219 core.register_on_mods_loaded(function()
220         core.log("Yeah preview mod is loaded with other CSM mods.")
221 end)