From 6a021676b98a0ba46e14b6f2f94d7d824c1bae22 Mon Sep 17 00:00:00 2001 From: snoutie Date: Mon, 13 Jan 2025 20:39:57 +0100 Subject: [PATCH] Enable Wear for Buckets --- bucket.lua | 153 +++++++++++++++++++++++++++++++++-------------- settingtypes.txt | 5 +- 2 files changed, 112 insertions(+), 46 deletions(-) diff --git a/bucket.lua b/bucket.lua index d43bc32..c3a199a 100644 --- a/bucket.lua +++ b/bucket.lua @@ -118,12 +118,33 @@ local function bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) return end + local held_bucket_name = itemstack:get_name() + + local is_bucket_filled = held_bucket_name ~= get_bucket_name_empty() + local bucket_contents = is_bucket_filled and get_liquid_name(held_bucket_name) + local bucket_fill_level = internal_bucket.get_liquid_level(itemstack:get_wear()) + local liquid = liquid_physics.get_liquid_at(pointed_thing.under) + -- Bucket is filled and pointed at liquid is not registered + -- -> Don't scoop + if bucket_contents and liquid == nil then + return itemstack + end + + -- Pointed at liquid is not registered + -- -> Scoop via fallback if liquid == nil then return on_use_fallback(itemstack, user, pointed_thing) end + -- Bucket is filled and pointed at liquid is of different kind + -- -> Don't scoop + local liquid_source_name = liquid_physics.get_liquid_node_names(liquid.liquid_id)[8] + if bucket_contents and bucket_contents ~= liquid_source_name then + return itemstack + end + local node = core.get_node(pointed_thing.under) if check_protection(pointed_thing.under, @@ -132,38 +153,43 @@ local function bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) return end - local liquid_source = liquid_physics.get_liquid_node_names(liquid.liquid_id)[8] - local bucket_name = get_bucket_name_filled(liquid_source) + local bucket_name_filled = get_bucket_name_filled(liquid_source_name) - if bucket_name == nil then + -- Don't know how to handle + -- -> Fallback + if bucket_name_filled == nil then return on_use_fallback(itemstack, user, pointed_thing) end - -- Filled Bucket - local bucket_item = ItemStack(bucket_name) - set_bucket_item_liquid_level(bucket_item, liquid.liquid_level) + local item_bucket = ItemStack(bucket_name_filled) + if not is_bucket_filled then + set_bucket_item_liquid_level(item_bucket, liquid.liquid_level) - local return_item = bucket_item + local item_count = user:get_wielded_item():get_count() + if item_count > 1 then + local inv = user:get_inventory() + if inv:room_for_item("main", { name = bucket_name_filled }) then + inv:add_item("main", item_bucket) + else + local pos = user:getpos() + pos.y = math.floor(pos.y + 0.5) + core.add_item(pos, item_bucket) + end - local item_count = user:get_wielded_item():get_count() - - if item_count > 1 then - local inv = user:get_inventory() - if inv:room_for_item("main", { name = bucket_name }) then - inv:add_item("main", bucket_item) - else - local pos = user:getpos() - pos.y = math.floor(pos.y + 0.5) - core.add_item(pos, bucket_item) + -- set to return empty buckets minus 1 + item_bucket = ItemStack(get_bucket_name_empty() .. tostring(item_count - 1)) end - -- set to return empty buckets minus 1 - return_item = ItemStack(get_bucket_name_empty() .. tostring(item_count - 1)) + liquid_physics.set_liquid_at(pointed_thing.under, 0, 0) + return item_bucket + else + local give_amount = math.min(8 - bucket_fill_level, liquid.liquid_level) + + set_bucket_item_liquid_level(item_bucket, bucket_fill_level + give_amount) + liquid_physics.set_liquid_at(pointed_thing.under, liquid.liquid_id, liquid.liquid_level - give_amount) + + return item_bucket end - - liquid_physics.set_liquid_at(pointed_thing.under, 0, 0) - - return return_item end local function bucket_on_place(on_place_fallback, bucket_liquid_id, source_name, itemstack, user, pointed_thing) @@ -240,24 +266,33 @@ function internal_bucket.register_empty_bucket(bucket_name) if base_default then local on_use_fallback = bucket_tool.on_use - local on_use_wrapper = function(itemstack, user, pointed_thing) - return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) - end - core.override_item(bucket_name, { on_use = on_use_wrapper }, nil) + core.override_item(bucket_name, { + on_use = function(itemstack, user, pointed_thing) + return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) + end + }, nil) elseif base_mcl_buckets then local on_use_fallback = bucket_tool.on_place - local on_use_wrapper = function(itemstack, user, pointed_thing) - local use_select_box = core.settings:get_bool("mcl_buckets_use_select_box", false) - if use_select_box == false then - -- TODO: Understand why this is nil - if user.get_pos == nil then - return itemstack + if core.settings:get_bool("liquid_physics_voxelibre_enable_scooping_via_use", true) then + core.override_item(bucket_name, { + on_use = function(itemstack, user, pointed_thing) + return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) end - pointed_thing = mcl_get_pointed_thing(user) - end - return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) + }, nil) end - core.override_item(bucket_name, { on_place = on_use_wrapper }, nil) + core.override_item(bucket_name, { + on_place = function(itemstack, user, pointed_thing) + local use_select_box = core.settings:get_bool("mcl_buckets_use_select_box", false) + if use_select_box == false then + -- TODO: Understand why this is nil + if user.get_pos == nil then + return itemstack + end + pointed_thing = mcl_get_pointed_thing(user) + end + return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) + end + }, nil) end end @@ -276,20 +311,50 @@ function internal_bucket.register_filled_bucket(name) if base_default then local on_place_fallback = bucket_tool.on_place - local on_place_wrapper = function(itemstack, user, pointed_thing) - return bucket_on_place(on_place_fallback, bucket_liquid_id, source_name, itemstack, user, pointed_thing) - end + local on_use_fallback = bucket_tool.on_place core.override_item(name, { - on_place = on_place_wrapper + on_use = function(itemstack, user, pointed_thing) + return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) + end, + on_place = function(itemstack, user, pointed_thing) + return bucket_on_place(on_place_fallback, bucket_liquid_id, source_name, itemstack, user, pointed_thing) + end, + wear_color = { + blend = "linear", + color_stops = { + [0.0] = "#ff0000", + [0.5] = "slateblue", + [1.0] = { r = 0, g = 255, b = 0, a = 150 }, + } + }, }, nil) + + core.register_tool(":" .. name, core.registered_items[name]) elseif base_mcl_buckets then local on_place_fallback = bucket_tool.on_place - local on_place_wrapper = function(itemstack, user, pointed_thing) - return bucket_on_place(on_place_fallback, bucket_liquid_id, source_name, itemstack, user, pointed_thing) + if core.settings:get_bool("liquid_physics_voxelibre_enable_scooping_via_use", true) then + local on_use_fallback = bucket_tool.on_use + core.override_item(name, { + on_use = function(itemstack, user, pointed_thing) + return bucket_on_use(on_use_fallback, itemstack, user, pointed_thing) + end + }, nil) end core.override_item(name, { - on_place = on_place_wrapper + on_place = function(itemstack, user, pointed_thing) + return bucket_on_place(on_place_fallback, bucket_liquid_id, source_name, itemstack, user, pointed_thing) + end, + wear_color = { + blend = "linear", + color_stops = { + [0.0] = "#ff0000", + [0.5] = "slateblue", + [1.0] = { r = 0, g = 255, b = 0, a = 150 }, + } + }, }, nil) + + core.register_tool(":" .. name, core.registered_items[name]) end end diff --git a/settingtypes.txt b/settingtypes.txt index e3da911..d4c9f70 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -1,2 +1,3 @@ -liquid_physics_enable_water_physics (Enable physics for water) bool true -liquid_physics_enable_lava_physics (Enable physics for lava) bool true +liquid_physics_enable_water_physics (Enable Physics For Water) bool true +liquid_physics_enable_lava_physics (Enable Physics For Lava) bool true +liquid_physics_voxelibre_enable_scooping_via_use (VoxeLibre: Enable Scooping Via Use/Punch) bool true