diff --git a/README.md b/README.md index b4beaa7..df2762b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # minetest-carpets -see https://forum.minetest.net/viewtopic.php?f=9&t=15459#p232311 +See https://forum.minetest.net/viewtopic.php?t=15459 diff --git a/carpet_api.lua b/api.lua similarity index 58% rename from carpet_api.lua rename to api.lua index 3b2c8c1..59a5552 100644 --- a/carpet_api.lua +++ b/api.lua @@ -1,4 +1,7 @@ -local config = Settings(minetest.get_modpath("carpets").."/settings.txt") +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) + +local config = Settings(modpath .. "/settings.txt") local carpet_proto = { drawtype = "nodebox", @@ -7,16 +10,14 @@ local carpet_proto = { node_box = { type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -0.45, 0.5} - }, + } } if config:get_bool("WoolFeeling") then - local wool = minetest.registered_nodes["wool:white"] - carpet_proto.sounds = wool.sounds - carpet_proto.groups = wool.groups + carpet_proto.sounds = default.node_sound_defaults() + carpet_proto.groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, flammable = 3} end - function carpets.register(recipe, def) local node = {} @@ -32,14 +33,15 @@ function carpets.register(recipe, def) local recipe_def = minetest.registered_nodes[recipe] - node.description = node.description or recipe_def.description.." Carpet" + node.description = node.description or recipe_def.description .. " Carpet" node.tiles = node.tiles or recipe_def.tiles - if node.tiles[6] then -- prefer "front" site for carpet + + if node.tiles[6] then -- prefer "front" site for carpet node.tiles = {node.tiles[6]} end - node.sounds = node.sounds or recipe_def.sounds - node.groups = node.groups or recipe_def.groups or {} + node.sounds = node.sounds or recipe_def.sounds + node.groups = node.groups or recipe_def.groups or {} node.groups.leafdecay = nil @@ -49,30 +51,34 @@ function carpets.register(recipe, def) node.groups.falling_node = 0 end - local name = "carpet:"..(node.name or recipe:gsub(":", "_")) + local name = "carpet:" .. (node.name or recipe:gsub(":", "_")) node.name = nil if config:get_bool("NoFlyCarpet") then local previous_on_place = node.on_place or minetest.item_place node.on_place = function(itemstack, placer, pointed_thing, ...) - if not pointed_thing then return end + if not pointed_thing then + return + end local above = pointed_thing.above local under = pointed_thing.under local under_node = minetest.get_node(under) - if above.y == under.y + 1 and under_node.name == name then + if above.y == (under.y + 1) and under_node.name == name then return end return previous_on_place(itemstack, placer, pointed_thing, ...) end end - minetest.register_node(":"..name, node) + minetest.register_node(":" .. name, node) minetest.register_craft({ - output = name.." 32", - recipe = {{"group:wool", "group:wool", "group:wool"}, - {"group:wool", recipe, "group:wool"}} + output = name .. " 32", + recipe = { + {"group:wool", "group:wool", "group:wool"}, + {"group:wool", recipe, "group:wool"} + } }) end diff --git a/init.lua b/init.lua index 5b6913f..658f2a2 100644 --- a/init.lua +++ b/init.lua @@ -1,46 +1,62 @@ carpets = {} -dofile(minetest.get_modpath("carpets").."/carpet_api.lua") -dofile(minetest.get_modpath("carpets").."/modutils.lua") +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) -depmod = carpets.modutils.get_depmod("carpets") +dofile(modpath .. "/api.lua") +local depends = (function() + local file = io.open(modpath .. "/depends.txt") + if not file then + return {} + end -local function enabledfilter(name, def) --- disable carpets from loaded modules but not defined in dependency - if depmod:check_depmod(name) == false then + local depends = {} + for line in file:lines() do + if line:sub(-1) == "?" then + line = line:sub(1, -2) + end + depends[line] = true + end + + file:close() + + return depends +end)() + +local function filter(name, def) + -- disable carpets from loaded modules but not defined in dependency + if not depends[def.mod_origin] then return false end --- disable carpets for blocks without description + -- disable carpets for blocks without description if def.description == nil or def.description == "" then return false end --- not supported node types for carpets - if def.drawtype == "liquid" or - def.drawtype == "firelike" or - def.drawtype == "airlike" or - def.drawtype == "plantlike" or - def.drawtype == "nodebox" or - def.drawtype == "raillike" then + -- not supported node types for carpets + if def.drawtype == "liquid" or + def.drawtype == "firelike" or + def.drawtype == "airlike" or + def.drawtype == "plantlike" or + def.drawtype == "nodebox" or + def.drawtype == "raillike" then return false end --- no carpet for signs, rail, ladder + -- no carpet for signs, rail, ladder if def.paramtype2 == "wallmounted" then return false end --- all checks passed + -- all checks passed return true end - ------------------------------------------------- -- main execution for name, def in pairs(minetest.registered_nodes) do - if enabledfilter(name, def) then + if filter(name, def) then carpets.register(name) end end diff --git a/modutils.lua b/modutils.lua deleted file mode 100644 index 76341e1..0000000 --- a/modutils.lua +++ /dev/null @@ -1,70 +0,0 @@ --- not modutils related, will be moved to a framework, if the utils framework will be developed :/ --- The envroot is accessable by global variable with module name as name (like if used in "carpets" mod, the modutils are at carpets.modutils available) --- Different per module environment root allow the use of diffent versions of the same tool in different mods - -local currentmod = minetest.get_current_modname() -local envroot = nil - -if not currentmod or currentmod == "" then --not minetest or something hacky - envroot = _G --fallback for hacky calls, populate global -else - if not _G[currentmod] then - _G[currentmod] = {} - end - envroot = _G[currentmod] -end --- framework stuff done. -- Now the tool ----------------------------------------- - -envroot.modutils = {} - --- Definition of returning object methods -local _check_depmod = function(this, checknode) - -- check if the node (checknode) is from dependent module - local delimpos = string.find(checknode, ":") - if delimpos then - local checkmodname = string.sub(checknode, 1, delimpos - 1) - for name, ref in pairs(this.deplist) do - if name == checkmodname then - return true - end - end - return false - end -end - -function envroot.modutils.get_depmod(modname) - - -- Definition of returning object attributes - local this = { } - this.modname = modname - this.deplist = {} -- depends.txt parsed - this.check_depmod = _check_depmod -- method - -- get module path - local modpath = minetest.get_modpath(modname) - if not modpath then - return nil -- module not found - end - - -- read the depends file - local dependsfile = io.open(modpath.."/depends.txt") - if not dependsfile then - return nil - end - - -- parse the depends file - for dependsline in dependsfile:lines() do - local depentry = {} -- Entry in deplist - local depmodname - if string.sub(dependsline, -1) == "?" then - depentry.required = false - depmodname = string.sub(dependsline, 1, -2) - else - depentry.required = true - depmodname = dependsline - end - this.deplist[depmodname] = depentry - end - - return this -end