-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
137 lines (118 loc) · 5.63 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--==[[ Grimoire - 0.3.1 ]]==--
--==[[ MIT 2024 (c) monk ]]==--
local path = minetest.get_modpath(minetest.get_current_modname()).."/pages/"
local grim_memory = {
page_name = "not set"
}
-- wrapper function for checking priv without adding to every page file
local check_player_privs = minetest.check_player_privs
local function priv_check(itemstack, player, pointed_thing, overriding_function)
if not check_player_privs(player, {server = true}) then
return itemstack
end
return overriding_function(itemstack, player, pointed_thing)
end
local function override(methods)
return minetest.override_item("grimoire:spellbook", {
on_use = function(itemstack, player, pointed_thing)
return priv_check(itemstack, player, pointed_thing, methods.on_use)
end,
on_place = function(itemstack, player, pointed_thing)
return priv_check(itemstack, player, pointed_thing, methods.on_place)
end,
on_secondary_use = function(itemstack, player, pointed_thing)
return priv_check(itemstack, player, pointed_thing, methods.on_secondary_use)
end,
on_drop = function(itemstack, player, pointed_thing)
return priv_check(itemstack, player, pointed_thing, methods.on_drop)
end,
})
end
-- Chat command to override tool callbacks, or invoke functions with dofile
minetest.register_chatcommand("grimoire", {
description = "Change Grimoire page or invoke a command",
params = "<page|invoke> <param> [<arguments>]",
privs = {server = true},
func = function(name, params)
local action, script, vargs = params:match("^(%S+)%s+(%S+)%s*([%S%s]*)$")
-- action is "page" or "invoke"
-- script is the name of the lua file after the first underscore: "action"_"script".lua
if not action or not script then
return false, "[Grimoire] Missing parameters! <page|invoke> <script>"
elseif action == "help" then
return false, "[Grimoire] Current page is: "..grim_memory.page_name
end
local dir_list = minetest.get_dir_list(path, false)
local requested_page = action.."_"..script..".lua"
local full_path = path..requested_page
for n = 1, #dir_list do
if dir_list[n] == requested_page then
if action == "page" then
local methods
local status, err = pcall(function()
methods = dofile(full_path)(grim_memory)
return methods
end)
if not status then
return false, "[Grimoire] Error: "..err
else
grim_memory.page_name = script
minetest.chat_send_player(name, "[Grimoire] Using page ".. script .. "!")
return override(methods)
end
elseif action == "invoke" then
local status, err = pcall(function()
return dofile(full_path)(grim_memory, name, vargs)
end)
if not status then
return false, "[Grimoire] Error: "..err
else
return true, "[Grimoire] Invoked ".. script .. "!"
end
end
end
end
return false, "[Grimoire] "..action.." "..script.." not found!"
end
})
-- default and disabled methods
local defaulted_methods = dofile(path.."page_disable.lua")()
minetest.register_tool("grimoire:spellbook", {
description = "魔導書",
inventory_image = "grimoire_spellbook.png",
wield_image = "grimoire_spellbook.png",
groups = {not_in_creative_inventory = 1},
damage_groups = {fleshy = -9},
stack_max = 1,
range = 230.0,
liquids_pointable = true,
light_source = 10,
on_use = defaulted_methods.on_use,
on_place = defaulted_methods.on_place,
on_secondary_use = defaulted_methods.on_secondary_use,
on_drop = defaulted_methods.on_drop,
})
minetest.register_alias("grimoire", "grimoire:spellbook")
-------------------------------------------------------------------------------------
-- MIT License --
-- --
-- Copyright (c) 2024 monk --
-- --
-- Permission is hereby granted, free of charge, to any person obtaining a copy --
-- of this software and associated documentation files (the "Software"), to deal --
-- in the Software without restriction, including without limitation the rights --
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --
-- copies of the Software, and to permit persons to whom the Software is --
-- furnished to do so, subject to the following conditions: --
-- --
-- The above copyright notice and this permission notice shall be included in all --
-- copies or substantial portions of the Software. --
-- --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --
-- SOFTWARE. --
-------------------------------------------------------------------------------------