From 30e50a64ef6ffcad961321ea1dc3cb07e51f640f Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sat, 27 Mar 2021 20:12:48 +0100 Subject: [PATCH 1/4] basic API (WIP) --- mods/PLAYER/mcl_death_drop/API.md | 12 ++++++++++ mods/PLAYER/mcl_death_drop/init.lua | 34 +++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 mods/PLAYER/mcl_death_drop/API.md diff --git a/mods/PLAYER/mcl_death_drop/API.md b/mods/PLAYER/mcl_death_drop/API.md new file mode 100644 index 000000000..6f1d1cde7 --- /dev/null +++ b/mods/PLAYER/mcl_death_drop/API.md @@ -0,0 +1,12 @@ +# mcl_death_drop +Drop registered inventories on player death. + +## mcl_death_drop.register_dropped_list(inv, listname, drop) +* inv: string of function returning a string +* listname: string +* drop: bool +-- if true the entire list will be dropped +-- if false, items with curse_of_vanishing enchantement will be broken. + +## mcl_death_drop.registered_dropped_lists +Table containing dropped list definition. \ No newline at end of file diff --git a/mods/PLAYER/mcl_death_drop/init.lua b/mods/PLAYER/mcl_death_drop/init.lua index 56e6ea522..6c51618a1 100644 --- a/mods/PLAYER/mcl_death_drop/init.lua +++ b/mods/PLAYER/mcl_death_drop/init.lua @@ -1,3 +1,18 @@ +local random = math.random + +mcl_death_drop = {} + +mcl_death_drop.registered_dropped_lists = {} + +function mcl_death_drop.register_dropped_list(inv, listname, drop) + table.insert(mcl_death_drop.registered_dropped_player_lists, {inv=inv, listname=listname, drop=drop}) +end + +mcl_death_drop.register_dropped_list("PLAYER", "main", true) +mcl_death_drop.register_dropped_list("PLAYER", "craft", true) +mcl_death_drop.register_dropped_list("PLAYER", "armor", true) +mcl_death_drop.register_dropped_list(function(player) return minetest.get_inventory({type="detached", name=player:get_player_name().."_armor"}) end , "armor", false) + minetest.register_on_dieplayer(function(player) local keep = minetest.settings:get_bool("mcl_keepInventory", false) if keep == false then @@ -7,20 +22,15 @@ minetest.register_on_dieplayer(function(player) local name, player_armor_inv, armor_armor_inv, pos = armor:get_valid_player(player, "[on_dieplayer]") -- No item drop if in deep void local void, void_deadly = mcl_worlds.is_in_void(pos) - local lists = { - { inv = inv, listname = "main", drop = true }, - { inv = inv, listname = "craft", drop = true }, - { inv = player_armor_inv, listname = "armor", drop = true }, - { inv = armor_armor_inv, listname = "armor", drop = false }, - } - for l=1,#lists do - local inv = lists[l].inv - local listname = lists[l].listname - local drop = lists[l].drop + + for l=1,#mcl_death_drop.registered_dropped_lists do + local inv = mcl_death_drop.registered_dropped_lists[l].inv + local listname = mcl_death_drop.registered_dropped_lists[l].listname + local drop = mcl_death_drop.registered_dropped_lists[l].drop if inv ~= nil then for i, stack in ipairs(inv:get_list(listname)) do - local x = math.random(0, 9)/3 - local z = math.random(0, 9)/3 + local x = random(0, 9)/3 + local z = random(0, 9)/3 pos.x = pos.x + x pos.z = pos.z + z if not void_deadly and drop and not mcl_enchanting.has_enchantment(stack, "curse_of_vanishing") then From 6a6b5970b351ca96ea12baf026055940b8880570 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sat, 27 Mar 2021 20:23:51 +0100 Subject: [PATCH 2/4] fix API --- mods/PLAYER/mcl_death_drop/init.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mods/PLAYER/mcl_death_drop/init.lua b/mods/PLAYER/mcl_death_drop/init.lua index 6c51618a1..693b21cd8 100644 --- a/mods/PLAYER/mcl_death_drop/init.lua +++ b/mods/PLAYER/mcl_death_drop/init.lua @@ -5,7 +5,7 @@ mcl_death_drop = {} mcl_death_drop.registered_dropped_lists = {} function mcl_death_drop.register_dropped_list(inv, listname, drop) - table.insert(mcl_death_drop.registered_dropped_player_lists, {inv=inv, listname=listname, drop=drop}) + table.insert(mcl_death_drop.registered_dropped_lists, {inv=inv, listname=listname, drop=drop}) end mcl_death_drop.register_dropped_list("PLAYER", "main", true) @@ -17,7 +17,7 @@ minetest.register_on_dieplayer(function(player) local keep = minetest.settings:get_bool("mcl_keepInventory", false) if keep == false then -- Drop inventory, crafting grid and armor - local inv = player:get_inventory() + local playerinv = player:get_inventory() local pos = player:get_pos() local name, player_armor_inv, armor_armor_inv, pos = armor:get_valid_player(player, "[on_dieplayer]") -- No item drop if in deep void @@ -25,6 +25,11 @@ minetest.register_on_dieplayer(function(player) for l=1,#mcl_death_drop.registered_dropped_lists do local inv = mcl_death_drop.registered_dropped_lists[l].inv + if inv == "PLAYER" then + inv = playerinv + elseif type(inv) == "function" then + inv = inv(player) + end local listname = mcl_death_drop.registered_dropped_lists[l].listname local drop = mcl_death_drop.registered_dropped_lists[l].drop if inv ~= nil then From 3ba7a402513faf9585e2a0018003608abd6759f6 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sat, 27 Mar 2021 23:07:46 +0100 Subject: [PATCH 3/4] Add API documentation --- mods/ITEMS/mcl_buckets/API.md | 20 ++++++++++---------- mods/PLAYER/mcl_death_drop/API.md | 10 ++++++---- mods/PLAYER/mcl_death_drop/init.lua | 3 +-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/API.md b/mods/ITEMS/mcl_buckets/API.md index 69ee4b21d..53f7d3698 100644 --- a/mods/ITEMS/mcl_buckets/API.md +++ b/mods/ITEMS/mcl_buckets/API.md @@ -5,17 +5,17 @@ Add an API to register buckets to mcl Register a new liquid Accept folowing params: -* source_place = a string or function. +* source_place: a string or function. * string: name of the node to place * function(pos): will returns name of the node to place with pos being the placement position -* source_take = table of liquid source node names to take -* itemname = itemstring of the new bucket item (or nil if liquid is not takeable) -* inventory_image = texture of the new bucket item (ignored if itemname == nil) -* name = user-visible bucket description -* longdesc = long explanatory description (for help) -* usagehelp = short usage explanation (for help) -* tt_help = very short tooltip help -* extra_check(pos, placer) = optional function(pos) which can returns false to avoid placing the liquid. Placer is object/player who is placing the liquid, can be nil. -* groups = optional list of item groups +* source_take: table of liquid source node names to take +* itemname: itemstring of the new bucket item (or nil if liquid is not takeable) +* inventory_image: texture of the new bucket item (ignored if itemname == nil) +* name: user-visible bucket description +* longdesc: long explanatory description (for help) +* usagehelp: short usage explanation (for help) +* tt_help: very short tooltip help +* extra_check(pos, placer): (optional) function(pos) which can returns false to avoid placing the liquid. Placer is object/player who is placing the liquid, can be nil. +* groups: optional list of item groups This function can be called from any mod (which depends on this one) \ No newline at end of file diff --git a/mods/PLAYER/mcl_death_drop/API.md b/mods/PLAYER/mcl_death_drop/API.md index 6f1d1cde7..70fbd1499 100644 --- a/mods/PLAYER/mcl_death_drop/API.md +++ b/mods/PLAYER/mcl_death_drop/API.md @@ -2,11 +2,13 @@ Drop registered inventories on player death. ## mcl_death_drop.register_dropped_list(inv, listname, drop) -* inv: string of function returning a string +* inv: can be: + * "PLAYER": player inventory (to avoid multiple calling to get_inventory()) + * function: must return inventory * listname: string * drop: bool --- if true the entire list will be dropped --- if false, items with curse_of_vanishing enchantement will be broken. + * true: the entire list will be dropped + * false: items with curse_of_vanishing enchantement will be broken. ## mcl_death_drop.registered_dropped_lists -Table containing dropped list definition. \ No newline at end of file +Table containing dropped list inventory, name and drop state. \ No newline at end of file diff --git a/mods/PLAYER/mcl_death_drop/init.lua b/mods/PLAYER/mcl_death_drop/init.lua index 693b21cd8..7c54334a9 100644 --- a/mods/PLAYER/mcl_death_drop/init.lua +++ b/mods/PLAYER/mcl_death_drop/init.lua @@ -11,7 +11,7 @@ end mcl_death_drop.register_dropped_list("PLAYER", "main", true) mcl_death_drop.register_dropped_list("PLAYER", "craft", true) mcl_death_drop.register_dropped_list("PLAYER", "armor", true) -mcl_death_drop.register_dropped_list(function(player) return minetest.get_inventory({type="detached", name=player:get_player_name().."_armor"}) end , "armor", false) +mcl_death_drop.register_dropped_list(function(player) return select(3, armor:get_valid_player(player)) end , "armor", false) minetest.register_on_dieplayer(function(player) local keep = minetest.settings:get_bool("mcl_keepInventory", false) @@ -19,7 +19,6 @@ minetest.register_on_dieplayer(function(player) -- Drop inventory, crafting grid and armor local playerinv = player:get_inventory() local pos = player:get_pos() - local name, player_armor_inv, armor_armor_inv, pos = armor:get_valid_player(player, "[on_dieplayer]") -- No item drop if in deep void local void, void_deadly = mcl_worlds.is_in_void(pos) From 57ce5143b9ee0fe0fe8b422521a9deb38ed36928 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sat, 27 Mar 2021 23:39:22 +0100 Subject: [PATCH 4/4] Clarify API documentation --- mods/PLAYER/mcl_death_drop/API.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mods/PLAYER/mcl_death_drop/API.md b/mods/PLAYER/mcl_death_drop/API.md index 70fbd1499..b19e2fd7c 100644 --- a/mods/PLAYER/mcl_death_drop/API.md +++ b/mods/PLAYER/mcl_death_drop/API.md @@ -3,8 +3,8 @@ Drop registered inventories on player death. ## mcl_death_drop.register_dropped_list(inv, listname, drop) * inv: can be: - * "PLAYER": player inventory (to avoid multiple calling to get_inventory()) - * function: must return inventory + * "PLAYER": will be interpreted like player inventory (to avoid multiple calling to get_inventory()) + * function(player): must return inventory * listname: string * drop: bool * true: the entire list will be dropped