summaryrefslogtreecommitdiff
path: root/init.lua
blob: a1f8f8220d9dff353977e1e57880e30f0911d757 (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
local schems = {
	single = minetest.register_schematic(string.format("schems%sstreetlight-single.mts",DIR_DELIM)),
	double = minetest.register_schematic(string.format("schems%sstreetlight-double.mts",DIR_DELIM)),
}

local singleMaterials = {
	ItemStack("streets:bigpole 6"),
	ItemStack("streets:bigpole_edge 2"),
	ItemStack("homedecor:glowlight_quarter 1"),
}

local doubleMaterials = {
	ItemStack("streets:bigpole 7"),
	ItemStack("streets:bigpole_edge 2"),
	ItemStack("streets:bigpole_tjunction 1"),
	ItemStack("homedecor:glowlight_quarter 2"),
}

local offsets = {
	single = {
		[0] = {x = 0,y = 0,z = 0},
		[90] = {x = 0,y = 0,z = 0},
		[180] = {x = 0,y = 0,z = -2},
		[270] = {x = -2,y = 0,z = 0},
	},
	double = {
		[0] = {x = 0,y = 0,z = -2},
		[90] = {x = -2,y = 0,z = 0},
		[180] = {x = 0,y = 0,z = -2},
		[270] = {x = -2,y = 0,z = 0},
	},
}

local function takeMaterials(player,materials)
	local name = player:get_player_name()
	if creative and creative.is_enabled_for(name) then return true end
	local inv = minetest.get_inventory({type = "player",name = name})
	local hasMaterials = true
	for _,i in ipairs(materials) do
		if not inv:contains_item("main",i) then hasMaterials = false end
	end
	if hasMaterials then
		for _,i in ipairs(materials) do inv:remove_item("main",i) end
		return true
	else
		minetest.chat_send_player(name,"You don't have the necessary materials to do that!")
		return false
	end
end

local function place(itemstack,player,pointed)
	if not player then return end
	local name = player:get_player_name()
	if not minetest.check_player_privs(name,{streetlight = true}) then
		minetest.chat_send_player(name,"*** You don't have permission to use a streetlight spawner.")
		return
	end
	local pos = pointed.above
	if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass = true}) then
		minetest.record_protection_violation(pos,name)
	end
	local isDouble = string.sub(itemstack:get_name(),-6,-1) == "double"
	if not takeMaterials(player,isDouble and doubleMaterials or singleMaterials) then return end
	local facedir = minetest.facedir_to_dir(minetest.dir_to_facedir(player:get_look_dir()))
	local schemDir = 0
	if facedir.x == 1 then schemDir = 180
	elseif facedir.z == 1 then schemDir = 90
	elseif facedir.z == -1 then schemDir = 270 end
	local offset = offsets[isDouble and "double" or "single"][schemDir]
	local pos = vector.add(pos,offset)
	minetest.place_schematic(pos,isDouble and schems.double or schems.single,schemDir,nil,false)
end

minetest.register_tool("minedot_streetlights:spawner_single",{
	description = "MineDOT-style Street Light Spawner (single-sided)",
	inventory_image = "minedot_streetlights_single.png",
	on_place = place,
})

minetest.register_tool("minedot_streetlights:spawner_double",{
	description = "MineDOT-style Street Light Spawner (double-sided)",
	inventory_image = "minedot_streetlights_double.png",
	on_place = place,
})

if not minetest.get_modpath("simple_streetlights") then
	minetest.register_privilege("streetlight",{
		description = "Can use streetlight spawners",
	})
end