-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
231 lines (214 loc) · 7.22 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
minetest.log("ChestLock loading...")
local protectedBlocks = {
"mcl_chests:chest",
"mcl_chests:chest_small",
"mcl_chests:chest_left",
"mcl_chests:chest_right",
"mcl_hoppers:hopper",
"mcl_hoppers:hopper_side",
"mcl_furnaces:furnace",
"mcl_barrels:barrel_open",
"mcl_barrels:barrel_closed",
"mcl_blast_furnace:blast_furnace",
"mcl_smoker:smoker",
"mcl_enchanting:table",
"mcl_jukebox:jukebox",
"mcl_smithing_table:table",
"mcl_fletching_table:fletching_table",
"mcl_grindstone:grindstone",
"mcl_loom:loom",
"mcl_anvils:anvil",
"mcl_anvils:anvil_damage_1",
"mcl_anvils:anvil_damage_2",
}
local signs = {
"mcl_signs:wall_sign",
"mcl_signs:wall_sign_birchwood",
"mcl_signs:wall_sign_sprucewood",
"mcl_signs:wall_sign_darkwood",
"mcl_signs:wall_sign_junglewood",
"mcl_signs:wall_sign_acaciawood",
"mcl_signs:wall_sign_mangrove_wood",
"mcl_signs:wall_sign_warped_hyphae_wood",
"mcl_signs:wall_sign_crimson_hyphae_wood",
"mcl_signs:wall_sign_bamboo",
"mcl_signs:wall_sign_cherrywood",
}
chestlock = {}
chestlock.message = function(name, str)
minetest.chat_send_player(name, minetest.colorize("#FFAA00", "[ChestLock] " .. str))
end
string.startswith = function(self, str)
return self:find("^" .. tostring(str)) ~= nil
end
local blockBehindSign = {
vector.new(0, 0, 1), --dir: 0
vector.new(1, 0, 0), --dir: 1
vector.new(0, 0, -1), --dir: 2
vector.new(-1, 0, 0), --dir: 3
}
local function patchSign(nodename)
getmetatable(core.registered_nodes[nodename])["__newindex"] = nil --unwritelock the table
local patchednode = core.registered_nodes[nodename]
local oldplace = core.registered_nodes[nodename].on_place
if oldplace ~= nil then
patchednode.on_place = function(itemstack, placer, pointed_thing)
--other mods can place blocks as no players
if placer == nil then
return oldplace(itemstack, placer, pointed_thing)
end
local playername = placer:get_player_name()
--get the block the sign is attached on
local above = pointed_thing.above
local under = pointed_thing.under
local dir = vector.subtract(under, above)
local fdir = minetest.dir_to_facedir(dir)
local blockpos = vector.add(above, blockBehindSign[fdir + 1])
local owner = minetest.get_meta(blockpos):get_string("chestlock:owner") or "" --might return an empty string by default, oh well
if owner ~= playername and owner ~= "" then --make sure player A cant lock player B's chest, also make sure if owner isnt set then just ignore it (post mod install)
chestlock.message(playername, "You are not the owner of this block")
elseif not minetest.is_protected(blockpos, playername) then
return oldplace(itemstack, placer, pointed_thing)
end
end
end
end
local smallBlockNeighbors = {
vector.new(1, 0, 0), -- +x
vector.new(-1, 0, 0), -- -x
vector.new(0, 0, 1), -- +z
vector.new(0, 0, -1), -- -z
}
--the correct direction for a sign to protect a chest
local smallBlockAttachedSigns = {
3, -- +x
2, -- -x
5, -- +z
4, -- -z
}
--direction from chest_left, to find chest_right
local doublechestFindRight = {
vector.new(1, 0, 0), --dir: 0
vector.new(0, 0, -1), --dir: 1
vector.new(-1, 0, 0), --dir: 2
vector.new(0, 0, 1), --dir: 3
}
function blockCheckTrust(pos, name) --returns true if player name is trusted to the protection
if minetest.get_meta(pos):get_string("chestlock:owner") == name then
return true
end
local istrusted = 0 --unlocked by default
for i = 1, 4 do
local cpos = vector.add(pos, smallBlockNeighbors[i]) --find some signs
local node = minetest.get_node_or_nil(cpos)
if
node ~= nil
and node.param2 == smallBlockAttachedSigns[i]
and node.name:startswith("mcl_signs:wall_sign")
then --make sure the sign is attached to the chest
local nodemeta = minetest.get_meta(cpos)
local text = string.split(nodemeta:get_string("text") or "", "\n")
if text ~= nil and string.lower(text[1] or "") == "[private]" then
if istrusted ~= 2 then
istrusted = 1
end --theres probably a better way
for i = 2, 4 do
if text[i] == name then
istrusted = 2 --they are trusted
end
end
end
end
end
if istrusted == 0 or istrusted == 2 then
return true
end
return false
end
function testDouble(pos, name)
local node = minetest.get_node_or_nil(pos)
local otherpos
if node.name == "mcl_chests:chest_left" then
otherpos = vector.add(doublechestFindRight[node.param2 + 1], pos)
elseif node.name == "mcl_chests:chest_right" then
otherpos = vector.add(vector.multiply(doublechestFindRight[node.param2 + 1], -1), pos)
else
return blockCheckTrust(pos, name)
end
return blockCheckTrust(pos, name) and blockCheckTrust(otherpos, name)
end
local old_isprotected = minetest.is_protected
minetest.is_protected = function(pos, name)
local node = minetest.get_node_or_nil(pos)
local short = string.split(node.name, ":")
local shortname = short[#short]
if node.name:startswith("mcl_signs:wall_sign") then
local nodemeta = minetest.get_meta(pos)
if nodemeta:get_string("chestlock:owner") == name then
return old_isprotected(pos, name)
end
local text = string.split(nodemeta:get_string("text") or "", "\n")
if text ~= nil and string.lower(text[1] or "") == "[private]" then
for i = 2, 4 do
if text[i] == name then
return old_isprotected(pos, name)
end
end
chestlock.message(name, "You do not have access to this sign")
return true
end
end
if node.name == "mcl_chests:chest_left" or node.name == "mcl_chests:chest_right" then
if not testDouble(pos, name) then
chestlock.message(name, "You do not have access to this locked " .. shortname)
return true
end
end
for i = 1, #protectedBlocks do
if node.name == protectedBlocks[i] then
if not blockCheckTrust(pos, name) then
chestlock.message(name, "You do not have access to this locked " .. shortname)
return true
end
end
end
return old_isprotected(pos, name)
end
minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
--im very, very sorry
for i = 1, #protectedBlocks do
if protectedBlocks[i] == newnode.name then
minetest.get_meta(pos):set_string("chestlock:owner", placer:get_player_name())
end
end
for i = 1, #signs do
if signs[i] == newnode.name then
minetest.get_meta(pos):set_string("chestlock:owner", placer:get_player_name())
end
end
end)
minetest.register_on_mods_loaded(function()
for i = 1, #signs do
patchSign(signs[i])
end
for i = 1, #core.registered_abms do
local abm = core.registered_abms[i]
if abm.label == "Hopper/container item exchange" then --im sorry jon
local oldaction = abm.action
abm.action = function(pos, node, active_object_count, active_object_count_wider)
local canmove = true
local uppos = vector.offset(pos, 0, 1, 0)
local upnode = minetest.get_node(uppos)
if not minetest.registered_nodes[upnode.name] then
return
end
for j = 1, #protectedBlocks do
if upnode.name == protectedBlocks[j] and not blockCheckTrust(uppos, "#HOPPER") then
return
end
end
oldaction(pos, node, active_object_count, active_object_count_wider)
end
end
end
end)