API cleanup for minetest-mods https://github.com/minetest-mods/minetest-mods.github.io/issues/55
This commit is contained in:
parent
412d3ffcc7
commit
88b41e24f4
70
api.lua
70
api.lua
@ -1,8 +1,3 @@
|
||||
local modname = minetest.get_current_modname()
|
||||
local modpath = minetest.get_modpath(modname)
|
||||
|
||||
local config = Settings(modpath .. "/settings.txt")
|
||||
|
||||
local carpet_proto = {
|
||||
drawtype = "nodebox",
|
||||
paramtype = "light",
|
||||
@ -20,67 +15,42 @@ local carpet_proto = {
|
||||
}
|
||||
}
|
||||
|
||||
if config:get_bool("WoolFeeling") then
|
||||
carpet_proto.sounds = default.node_sound_defaults()
|
||||
carpet_proto.groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, flammable = 3}
|
||||
end
|
||||
-- Register the carpet and recipe using material
|
||||
-- material - already registered material item the textures and sounds applied from
|
||||
-- def - optional additional - or overriding data passed to minetest.register_node()
|
||||
carpets = {}
|
||||
|
||||
function carpets.register(recipe, def)
|
||||
function carpets.register(material, def)
|
||||
local node = table.copy(carpet_proto)
|
||||
if def then
|
||||
for k,v in pairs(def) do
|
||||
node.k = v
|
||||
node[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
local material_def = minetest.registered_nodes[material]
|
||||
node.description = node.description or material_def.description.." Carpet"
|
||||
node.sounds = table.copy(node.sounds or material_def.sounds or default.node_sound_defaults())
|
||||
node.groups = table.copy(node.groups)
|
||||
|
||||
if node.tiles then
|
||||
node.tiles = table.copy(node.tiles)
|
||||
elseif material_def.tiles[6] then
|
||||
node.tiles = {material_def.tiles[6]}
|
||||
else
|
||||
def = {}
|
||||
node.tiles = table.copy(material_def.tiles)
|
||||
end
|
||||
|
||||
local recipe_def = minetest.registered_nodes[recipe]
|
||||
node.description = def.description or recipe_def.description.." Carpet"
|
||||
node.tiles = table.copy(def.tiles or recipe_def.tiles or {})
|
||||
node.sounds = table.copy(def.sounds or recipe_def.sounds or {})
|
||||
if def.groups then
|
||||
node.groups = table.copy(def.groups)
|
||||
end
|
||||
|
||||
if node.tiles[6] then
|
||||
node.tiles = {node.tiles[6]}
|
||||
end
|
||||
|
||||
if config:get_bool("FallingCarpet") and node.groups.falling_node == nil then
|
||||
node.groups.falling_node = 1
|
||||
elseif node.groups.falling_node == 1 then
|
||||
node.groups.falling_node = 0
|
||||
end
|
||||
|
||||
local name = "carpet:" .. (node.name or recipe:gsub(":", "_"))
|
||||
local name = "carpet:" .. (node.name or material: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
|
||||
|
||||
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
|
||||
return
|
||||
end
|
||||
return previous_on_place(itemstack, placer, pointed_thing, ...)
|
||||
end
|
||||
end
|
||||
|
||||
minetest.register_node(":" .. name, node)
|
||||
|
||||
minetest.register_craft({
|
||||
output = name .. " 32",
|
||||
recipe = {
|
||||
{"group:wool", "group:wool", "group:wool"},
|
||||
{"group:wool", recipe, "group:wool"}
|
||||
{"group:wool", material, "group:wool"}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
11
depends.txt
11
depends.txt
@ -1,10 +1 @@
|
||||
wool
|
||||
default?
|
||||
farming?
|
||||
bones?
|
||||
castle?
|
||||
darkage?
|
||||
moreblocks?
|
||||
plasticbox?
|
||||
princess?
|
||||
moretrees?
|
||||
wool?
|
||||
|
83
init.lua
83
init.lua
@ -1,68 +1,25 @@
|
||||
carpets = {}
|
||||
|
||||
local modname = minetest.get_current_modname()
|
||||
local modpath = minetest.get_modpath(modname)
|
||||
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
dofile(modpath .. "/api.lua")
|
||||
|
||||
local modutils = dofile(modpath .. "/modutils.lua")
|
||||
local depmod = modutils.get_depend_checker(modname)
|
||||
|
||||
local function filter(name, def)
|
||||
-- disable carpets from loaded modules but not defined in dependency
|
||||
if not depmod:check_depend_by_itemname(name) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- disable carpets for blocks without description
|
||||
if def.description == nil or def.description == "" then
|
||||
return false
|
||||
end
|
||||
|
||||
-- no 3rd hand carpets
|
||||
local ignore_groups = {
|
||||
not_in_creative_inventory = true,
|
||||
carpet = true,
|
||||
door = true,
|
||||
fence = true,
|
||||
stair = true,
|
||||
slab = true,
|
||||
wall = true,
|
||||
micro = true,
|
||||
panel = true,
|
||||
slope = true,
|
||||
if minetest.get_modpath("wool") then
|
||||
local nodenames = {
|
||||
"wool:black",
|
||||
"wool:blue",
|
||||
"wool:brown",
|
||||
"wool:cyan",
|
||||
"wool:dark_green",
|
||||
"wool:dark_grey",
|
||||
"wool:green",
|
||||
"wool:grey",
|
||||
"wool:magenta",
|
||||
"wool:orange",
|
||||
"wool:pink",
|
||||
"wool:red",
|
||||
"wool:violet",
|
||||
"wool:white",
|
||||
"wool:yellow",
|
||||
}
|
||||
for k,v in pairs(def.groups) do
|
||||
if ignore_groups[k] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- not supported node types for carpets
|
||||
local ignore_drawtype = {
|
||||
liquid = true,
|
||||
firelike = true,
|
||||
airlike = true,
|
||||
plantlike = true,
|
||||
nodebox = true,
|
||||
raillike = true,
|
||||
}
|
||||
if ignore_drawtype[def.drawtype] then
|
||||
return false
|
||||
end
|
||||
|
||||
-- no carpet for signs, rail, ladder
|
||||
if def.paramtype2 == "wallmounted" then
|
||||
return false
|
||||
end
|
||||
|
||||
-- all checks passed
|
||||
return true
|
||||
end
|
||||
|
||||
-- main execution
|
||||
for name, def in pairs(minetest.registered_nodes) do
|
||||
if filter(name, def) then
|
||||
carpets.register(name)
|
||||
for _, nodename in ipairs(nodenames) do
|
||||
carpets.register(nodename)
|
||||
end
|
||||
end
|
||||
|
71
modutils.lua
71
modutils.lua
@ -1,71 +0,0 @@
|
||||
local modutils = {}
|
||||
|
||||
function modutils.get_modname_by_itemname(itemname)
|
||||
local def = minetest.registered_items[itemname]
|
||||
if def then
|
||||
return def.mod_origin
|
||||
else --not loaded item
|
||||
local delimpos = string.find(itemname, ":")
|
||||
if delimpos then
|
||||
return string.sub(itemname, 1, delimpos - 1)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function modutils.get_depend_checker(modname)
|
||||
local self = {
|
||||
modname = modname,
|
||||
depends = {} -- depends.txt parsed
|
||||
}
|
||||
|
||||
-- Check if dependency exists to mod modname
|
||||
function self:check_depend(modname, deptype)
|
||||
if self.depends[modname] then
|
||||
if not deptype then --"required" or "optional" only
|
||||
return true
|
||||
else
|
||||
return (self.depends[modname] == deptype)
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
--Check if dependency exists to item origin mod
|
||||
function self:check_depend_by_itemname(itemname, deptype)
|
||||
local modname = modutils.get_modname_by_itemname(itemname)
|
||||
if modname then
|
||||
return self:check_depend(modname, deptype)
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- 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 line in dependsfile:lines() do
|
||||
if line:sub(-1) == "?" then
|
||||
line = line:sub(1, -2)
|
||||
self.depends[line] = "optional"
|
||||
else
|
||||
self.depends[line] = "required"
|
||||
end
|
||||
end
|
||||
dependsfile:close()
|
||||
return self
|
||||
end
|
||||
|
||||
return modutils
|
@ -1,3 +1,2 @@
|
||||
NoFlyCarpet = true
|
||||
FallingCarpet = false
|
||||
WoolFeeling = false
|
||||
|
Loading…
x
Reference in New Issue
Block a user