RustTip

CanPurchaseItem

OxideCarbonVending

Called to determine whether to purchase item.

Return true to allow the action and false to block it.

object CanPurchaseItem(BasePlayer buyer, Item item, Action<BasePlayer, Item> onItemPurchased, VendingMachine vendingMachine, ItemContainer targetContainer)
Works — the game method in this build is the one the hook was written for.

Parameters

buyer
BasePlayer
item
Item
onItemPurchased
Action<BasePlayer, Item>
vendingMachine
VendingMachine
targetContainer
ItemContainer

Return value

objectThe returned value replaces the game result; null lets the game decide.

Example

object — you can affect the game: return a value to change or cancel the action.

private object CanPurchaseItem(BasePlayer buyer, Item item, Action<BasePlayer, Item> onItemPurchased, VendingMachine vendingMachine, ItemContainer targetContainer)
{
    Puts("CanPurchaseItem");

    // change nothing — the game continues as usual
    return null;

    // to affect the game:
    // return false; // your value instead of the game result
}

Works in both Oxide and Carbon: Carbon loads Oxide plugins as is.

Injection point

Rust.opj (Oxide)
bool VendingMachine.DoTransaction(BasePlayer, int, int, ItemContainer, Action<BasePlayer, Item>, Action<BasePlayer, Item>, MarketTerminal)

Passes: BasePlayer buyer, Item item, Action<BasePlayer, Item> onItemPurchased, VendingMachine vendingMachine, ItemContainer targetContainer

WorksReturnBehavior: ExitWhenNonNullArgumentBehavior: UseArgumentString (a0, l21, a5, this, a3)Assembly-CSharp.dll

Where it is called (this branch's game code)

// …
else
{
	num9 += item4.amount;
	RecordSaleAnalytics(item4, sellOrderId, sellOrder.currencyAmountPerItem);
	if (targetContainer == null)
	{
		GiveSoldItem(item4, buyer);
	}
	object returnvar = Interface.CallHook("CanPurchaseItem", buyer, item4, onItemPurchased, this, targetContainer);
	if (returnvar != null)
	{
		return returnvar is bool && (bool)returnvar;
	}
	else if (!item4.MoveToContainer(targetContainer))
	{
		item4.Drop(targetContainer.dropPosition, targetContainer.dropVelocity);
	}
// …

Sources: Rust.opj from OxideMod/Oxide.Rust (MIT) and hook metadata of the Carbon documentation (MIT). Method code is decompiled from this branch's server.