Skip to content

Approval mode

By default the server accepts what players' games report about their items. With approval mode on, it holds everything a client does to its items, and to the inventories it moves items between, until your bundle approves or rejects it. Use it when you care about potential cheaters and spoofing, e.g. if your economy depends on it.

re.Inventory.SetApprovalMode(true);

In short

Each held change has a kind. This is what it means and what happens when you reject it:

Kind What happened If you reject it
Transfer The player wants to take, put or drop an item Nothing moves
Added The player's game gave them an item (bought, crafted, a quest reward) The item is taken away
Initial The player joined with this item (PlayerDevelopmentSystem given) The item is taken away
Updated An item the player has changed (an upgrade, a mod installed) The item goes back to how it was. A mod installed in it is added back as an item
Generated The player's game first filled in an item the server made (given with AddItem) A weapon or other single item is taken away. A stack goes back to its previous state
Claimed The player's game rolled loot for a container or drop The item is not in the container

Removed is never held and a player can always lose items. That is a technical limitation. If you reject what crafting or disassembling made, the player does not get back what it used up in the process.

Not answering within 5 seconds counts as a reject. The sections below explain the details.

The rules

  • Every held change arrives in an InventoryChangeRequested event, one event per client action, and waits until a bundle answers
  • Any bundle may answer, the first answer counts
  • If no bundle subscribes to InventoryChangeRequested, everything is approved
  • A change nobody answers within 5 seconds is rejected
  • Turning the mode off approves everything still waiting

Until you answer, the player sees nothing happen. Taking something from the inventory does not do anything visually, a container iner shows the player's own loot roll until its loot is (potentially) overwritten by a server bundle.

What is held

Each InventoryChangeRequest in Changes has a Transaction to answer with and describes one change:

Kind Fields If you reject
Transfer Source and Destination (0 for a drop, its inventory is made when it goes ahead), Item as the server holds it Nothing moves. Rejecting one item of a drop refuses the whole drop
Added Item as the client describes it The item is taken away again
Initial Item The item is taken away
Updated Item the new state, Previous the state the server has. Item.Quantity is the whole stack The server's state is put back
Generated Item what the game made, Previous what the server gave (without the game's rolls). Item.Quantity is the whole stack A single item is removed. A stack keeps its quantity and goes back to its record's plain state
Claimed Item, Destination, Distance (meters from the player to it) The item is not in the container

Items you gave: Generated

When you give an item with AddItem, the server only knows its record. The player's game fills in the rest (rolls, mods, stats). The first time the game reports that item, it arrives as Generated. This also covers an item the player takes from an inventory you filled with AddItem, and AddItemInfo or AddItemData of an item without modifiers, parts and loot record.

  • Approve: the item stays as the game made it.
  • Reject: a single item (a weapon, clothing) is removed, because the server has no state to put back and the game would make the same again. A stack (ammo, money) keeps its quantity and goes back to its record's plain state.

After that first report, every change to the item is Updated.

Items you gave fully built (AddItemData, or AddItemInfo with modifiers or parts) are known to the server. If the game reports them differently, that is Updated with Previous being what you gave.

Rejecting Updates of mod installations

Installing a mod takes it out of the player's inventory and puts it into an item. If you reject that item's Updated or Generated change (or it times out), the mod item gets added back to the player inventory.

Answering

Answer one change at a time or a whole event at once:

re.Inventory.Approve(change.Transaction);
re.Inventory.Reject(change.Transaction);

bool[] results = re.Inventory.Approve(transactions); // uint[]

The result is false only if the transaction is unknown or already answered. true does not imply that an item moved, as an approved move is checked again (is the item still there, is the player still close) and can still be refused. This is important because another client could've looted the item in the time your bundle was processing the move. What actually happened arrives in InventoryChanged. Use that event to persist changes to inventories, not the result of an Approve call.

Checking that an item is sane

The server knows the game's records and refuses items the game wouldn't make. It does not compute item stats, though, so there is no list of "correct" stats to compare with. What you can check is whether an item stays within what your server allows:

  • Record: is it an item players may have at all? Allow or deny lists of records work well
  • Quantity: absurd stacks
  • Modifiers: stats a weapon should never contain, or values far beyond what upgrades give
  • Parts: too many mods, or mod records you do not allow
  • Loot: PowerLevel beyond the level cap (only for items with a LootRecord)
  • Updated and Generated: what changed between Previous and Item
sv/Approval.cs
static bool IsSane(re.ItemInfo item)
{
    if (item.Quantity > 10000 || item.Parts.Length > 8)
    {
        return false;
    }

    if (item.LootRecord.Get() != 0 && item.PowerLevel > 60)
    {
        return false;
    }

    foreach (var modifier in item.Modifiers)
    {
        if (modifier.Kind == StatModifierType.Additive && modifier.Value > 1000)
        {
            return false;
        }
    }

    return true;
}

re.Events.On(
    "InventoryChangeRequested",
    (re.InventoryChangeRequested x) =>
    {
        var approved = new List<uint>();
        var rejected = new List<uint>();
        foreach (var change in x.Changes)
        {
            (IsSane(change.Item) ? approved : rejected).Add(change.Transaction);
        }

        re.Inventory.Approve(approved.ToArray());
        re.Inventory.Reject(rejected.ToArray());
    }
);

re.Inventory.SetApprovalMode(true);

The numbers are examples. Choose meaningful limits that fit your server.

Controlling loot

The game rolls container and drop loot on the client. To decide yourself what players find, reject every Claimed change and put your own items in when the inventory is created:

re.Events.On(
    "WorldInventoryCreated",
    (re.WorldInventoryCreated x) =>
    {
        // Your own loot, e.g. from a table of your own
        re.Inventory.AddItem(x.Inventory, new TDBID("Ammo.HandgunAmmo"), 30);
    }
);

// In the InventoryChangeRequested handler
if (change.Kind == re.InventoryChangeKind.Claimed)
{
    rejected.Add(change.Transaction);
}

Containers then contain exactly what you added. This also covers drops created by NPCs or vending machines. A vending purchase still deducts the money, so add the item you want it to give.

Keep handlers quick

Everything a player does with items waits for your answer, so answer in the handler itself when you can. If you need to look something up first (a database, an HTTP call), remember the 5 second limit.