diff options
author | cheapie <no-email-for-you@example.com> | 2018-11-02 16:42:41 -0500 |
---|---|---|
committer | cheapie <no-email-for-you@example.com> | 2018-11-02 16:42:41 -0500 |
commit | 206a730e89ec7db09f4570a4cfbd26671f8b6ddc (patch) | |
tree | 030de83a49e501c72ca232a2a53fb5780a25bfe0 /init.lua | |
download | minedot_streetlights-master.tar minedot_streetlights-master.tar.gz minedot_streetlights-master.tar.bz2 minedot_streetlights-master.tar.xz minedot_streetlights-master.zip |
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..a1f8f82 --- /dev/null +++ b/init.lua @@ -0,0 +1,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 |