From 773d14406c4cd752aef65d9326812c7199084706 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 26 Dec 2024 17:53:04 +0300 Subject: [PATCH] feat: add base:loot property (WIP) --- res/content/base/blocks/grass.json | 3 ++- res/content/base/blocks/grass_block.json | 5 ++++- res/content/base/config/user-props.toml | 1 + res/content/base/modules/util.lua | 23 +++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/res/content/base/blocks/grass.json b/res/content/base/blocks/grass.json index 7308d1a1..52bfb11d 100644 --- a/res/content/base/blocks/grass.json +++ b/res/content/base/blocks/grass.json @@ -9,5 +9,6 @@ "grounded": true, "model": "X", "hitbox": [0.15, 0.0, 0.15, 0.7, 0.7, 0.7], - "base:durability": 0.0 + "base:durability": 0.0, + "base:drop": [] } diff --git a/res/content/base/blocks/grass_block.json b/res/content/base/blocks/grass_block.json index a6087afc..e89a774c 100644 --- a/res/content/base/blocks/grass_block.json +++ b/res/content/base/blocks/grass_block.json @@ -8,5 +8,8 @@ "grass_side", "grass_side" ], - "base:durability": 1.3 + "base:durability": 1.3, + "base:loot": [ + {"item": "base:dirt.item"} + ] } diff --git a/res/content/base/config/user-props.toml b/res/content/base/config/user-props.toml index b7306ba1..eab7a628 100644 --- a/res/content/base/config/user-props.toml +++ b/res/content/base/config/user-props.toml @@ -1 +1,2 @@ "base:durability" = {} +"base:loot" = {} diff --git a/res/content/base/modules/util.lua b/res/content/base/modules/util.lua index 2112c210..2a8326ec 100644 --- a/res/content/base/modules/util.lua +++ b/res/content/base/modules/util.lua @@ -11,4 +11,27 @@ function util.drop(ppos, itemid, count, pickup_delay) }}) end +local function calc_loot(loot_table) + local results = {} + for _, loot in ipairs(loot_table) do + local chance = loot.chance or 1 + local count = loot.count or 1 + + local roll = math.random() + + if roll < chance then + table.insert(results, {item=loot.item, count=count}) + end + end + return results +end + +function util.block_loot(blockid) + local lootscheme = block.properties[blockid]["base:loot"] + if lootscheme then + return calc_loot(lootscheme) + end + return {{block.get_picking_item(blockid), 1}} +end + return util