Files
Bubberstation/code/datums/components/reagent_refiller.dm
Bloop 4c870f71ca Fixes a bunch of callbacks that were being qdeleted, and code cleanup (#77904)
## About The Pull Request


![image](https://github.com/tgstation/tgstation/assets/13398309/559eb50a-461c-4220-b628-55412baaffc3)

Continuing the work of
https://github.com/tgstation/tgstation/pull/77850.

it started with finding one that was being missed and causing a
runtime...then I noticed a whole lot more. While I was doing this I
found callbacks that weren't being nulled in `Destroy()`, so I added
that wherever I found these spots as well as some general code cleanup.

There were a lot more of these than I initially hoped to encounter so
I'm labeling it as a refactor.

## Why It's Good For The Game

Fixes lots of runtimes, improves code resiliency.

## Changelog

🆑
refactor: fixed a bunch of instances of callbacks being qdeleted and
cleaned up related code
/🆑
2023-08-25 16:03:27 -06:00

75 lines
2.3 KiB
Plaintext

/**
* ## Reagent refiller
* Refills any drinks poured out of the reagent container (and is allowed within the whitelisted reagents).
*/
/datum/component/reagent_refiller
/// Time to refill
var/time_to_refill
/// Callback to consume power
var/datum/callback/power_draw_callback
/// Amount of power to use from the cell
var/power_to_draw
/// Whitelist of reagents allowed to be synthesized
var/list/whitelisted_reagents
/datum/component/reagent_refiller/Initialize(
time_to_refill = 60 SECONDS,
datum/callback/power_draw_callback,
power_to_draw = 30,
whitelisted_reagents = list(/datum/reagent/consumable)
)
if(!is_reagent_container(parent))
return COMPONENT_INCOMPATIBLE
src.time_to_refill = time_to_refill
src.power_draw_callback = power_draw_callback
src.power_to_draw = power_to_draw
src.whitelisted_reagents = whitelisted_reagents
return ..()
/datum/component/reagent_refiller/Destroy(force, silent)
power_draw_callback = null
return ..()
/datum/component/reagent_refiller/RegisterWithParent()
RegisterSignal(parent, COMSIG_ITEM_AFTERATTACK, PROC_REF(refill))
RegisterSignal(parent, COMSIG_ATOM_EXITED, PROC_REF(delete_self))
/datum/component/reagent_refiller/UnregisterFromParent()
UnregisterSignal(parent, list(COMSIG_ITEM_AFTERATTACK, COMSIG_ATOM_EXITED))
/datum/component/reagent_refiller/proc/delete_self()
SIGNAL_HANDLER
qdel(src)
/// Preps the reagent container for being refilled
/datum/component/reagent_refiller/proc/refill()
SIGNAL_HANDLER
. |= COMPONENT_AFTERATTACK_PROCESSED_ITEM
var/obj/item/reagent_containers/container = parent
var/refill = container.reagents.get_master_reagent_id()
var/amount = min((container.amount_per_transfer_from_this + container.reagents.total_volume), container.reagents.total_volume)
if (amount == 0)
return
if (!is_path_in_list(refill, whitelisted_reagents))
return
addtimer(CALLBACK(src, PROC_REF(add_reagents), container, container.loc, refill, amount), time_to_refill)
/// Refills the reagent container, and uses cell power if applicable
/datum/component/reagent_refiller/proc/add_reagents(obj/item/reagent_containers/target, oldloc, reagent_to_refill, amount)
if (QDELETED(src) || QDELETED(target))
return
if (target.loc != oldloc)
return
target.reagents.add_reagent(reagent_to_refill, amount)
if (!isnull(power_draw_callback))
power_draw_callback.Invoke(power_to_draw)