summaryrefslogtreecommitdiff
path: root/init.lua
blob: 10247fb08e1c96f94d570fc2641ada2c2b6a9ee8 (plain)
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
local creative_mode = core.settings:get_bool("creative_mode")

local function cyan(str)
	return core.colorize("#00FFFF",str)
end

local function red(str)
	return core.colorize("#FF5555",str)
end

local radius_large = core.settings:get("areasprotector_radius_large")
                     or core.settings:get("areasprotector_radius")
                     or 16

radius_large = tonumber(radius_large) or 16

local height_large = core.settings:get("areasprotector_height_large")
                     or core.settings:get("areasprotector_radius_large")
                     or core.settings:get("areasprotector_radius")
                     or 16

height_large = tonumber(height_large) or 16

local radius_small = core.settings:get("areasprotector_radius_small")
                     or 7

radius_small = tonumber(radius_small) or 7

local height_small = core.settings:get("areasprotector_height_small")
                     or core.settings:get("areasprotector_radius_small")
                     or 7

height_small = tonumber(height_small) or 7

local max_protectors = core.settings:get("areasprotector_max_protectors") or 16

max_protectors = tonumber(max_protectors) or 16

local function on_place(itemstack, player, pointed, radius, height, sizeword)
	local pos = pointed.above
	local pos1 = vector.add(pos,vector.new(radius, height, radius))
	local pos2 = vector.add(pos,vector.new(-radius, -height, -radius))
	local name = player:get_player_name()
	local perm,err = areas:canPlayerAddArea(pos1,pos2,name)
	if not perm then
		core.chat_send_player(name,red("You are not allowed to protect that area: ")..err)
		return itemstack
	end
	local conflicts = core.find_nodes_in_area(pos1,pos2,{"areasprotector:protector_small","areasprotector:protector_large",})
	if conflicts and #conflicts > 0 and not core.check_player_privs(name,"areas") then
		local message = red("Another protector block is too close: ")..
		                "another protector block was found at "..
		                cyan(core.pos_to_string(conflicts[1]))..
		                ", and this size of protector block cannot be placed within "..
		                cyan(tostring(radius).."m")..
		                " of others."
		core.chat_send_player(name,message)
		return itemstack
	end
	local userareas = 0
	for _,v in pairs(areas.areas) do
		if v.owner == name and string.sub(v.name,1,28) == "Protected by Protector Block" then
			userareas = userareas + 1
		end
	end
	if userareas >= max_protectors and not core.check_player_privs(name,"areas") then
		local message1 = red("You are using too many protector blocks:")..
		                 " this server allows you to use up to "..
		                 cyan(tostring(max_protectors))..
		                 " protector blocks, and you already have "..
		                 cyan(tostring(userareas))..
		                 "."
		local message2
		if sizeword == "small" then
			message2 = "If you need to protect more, "..
			           "please consider using the larger protector blocks, "..
			           "using the chat commands instead, "..
			           "or at the very least taking the time to rename some of your areas to something more descriptive first."
		else
			message2 = "If you need to protect more, "..
			           "please consider using the chat commands instead, "..
			           "or at the very least take the time to rename some of your areas to something more descriptive first."
		end
		core.chat_send_player(name,message1)
		core.chat_send_player(name,message2)
		return itemstack
	end
	local id = areas:add(name,"Protected by Protector Block at "..core.pos_to_string(pos, 0),pos1,pos2)
	areas:save()
	local msg = string.format("The area from %s to %s has been protected as #%s",cyan(core.pos_to_string(pos1)),cyan(core.pos_to_string(pos2)),cyan(id))
	core.chat_send_player(name,msg)
	core.set_node(pos,{name="areasprotector:protector_"..sizeword})
	local meta = core.get_meta(pos)
	local infotext = string.format("Protecting area %d owned by %s",id,name)
	meta:set_string("infotext",infotext)
	meta:set_int("area_id",id)
	meta:set_string("owner",name)
	if not creative_mode then
		itemstack:take_item()
	end
	return itemstack
end

local function after_dig(oldmetadata,digger,sizeword)
	if oldmetadata and oldmetadata.fields then
		local owner = oldmetadata.fields.owner
		local id = tonumber(oldmetadata.fields.area_id)
		local playername = digger:get_player_name()
		if areas.areas[id] and areas:isAreaOwner(id,owner) then
			if digger:get_player_control().sneak then
				local inv = digger:get_inventory()
				if not creative_mode then
					if inv:room_for_item("main", "default:steel_ingot 6") then
						inv:remove_item("main", "areasprotector:protector_"..sizeword.." 1")
						inv:add_item("main", "default:steel_ingot 6")
					else
						core.chat_send_player(playername, "No room for the replacement ingots, just digging the protector and deleting the area normally.")
						areas:remove(id)
						areas:save()
					end
				end
			else
				areas:remove(id)
				areas:save()
			end
		end
	end
end

local function on_punch(pos)
	if not core.global_exists("vizlib") then return end
	local meta = core.get_meta(pos)
	local areaid = meta:get_int("area_id")
	if not (areaid and areas.areas[areaid]) then return end
	local pos1 = areas.areas[areaid].pos1
	local pos2 = areas.areas[areaid].pos2
	if not pos1 and pos2 then return end
	vizlib.draw_area(pos1,pos2,{color="mulberry"})
end

core.register_node("areasprotector:protector_large", {
	description = "Protector Block (large volume)",
	groups = {cracky=1},
	tiles = {
		"default_steel_block.png",
		"default_steel_block.png",
		"default_steel_block.png^areasprotector_large_overlay.png^basic_materials_padlock.png"
	},
	paramtype = "light",
	on_place = function(itemstack,player,pointed_thing)
		return on_place(itemstack,player,pointed_thing,radius_large,height_large,"large")
	end,
	after_dig_node = function(_,_,oldmetadata,digger)
		after_dig(oldmetadata,digger,"large")
	end,
	on_punch = on_punch,
})

core.register_node("areasprotector:protector_small", {
	description = "Protector Block (small volume)",
	groups = {cracky=1},
	tiles = {
		"default_steel_block.png",
		"default_steel_block.png",
		"default_steel_block.png^basic_materials_padlock.png"
	},
	paramtype = "light",
	on_place = function(itemstack,player,pointed_thing)
		return on_place(itemstack,player,pointed_thing,radius_small,height_small,"small")
	end,
	after_dig_node = function(_,_,oldmetadata,digger)
		after_dig(oldmetadata,digger,"small")
	end,
	on_punch = on_punch,
})

core.register_craft({
	output = "areasprotector:protector_small 2",
	type = "shapeless",
	recipe = {"default:steelblock","basic_materials:padlock"},
})

core.register_craft({
	output = "areasprotector:protector_large",
	type = "shapeless",
	recipe = {
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small",
		"areasprotector:protector_small"
	}
})

core.register_alias("areasprotector:protector",    "areasprotector:protector_large")
core.register_alias("areasprotector:display_node", "areasprotector:display_node_large")