Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Ruleset API

João Cardoso edited this page Jul 29, 2018 · 9 revisions

Bagnon and Combuctor allow you to create custom rulesets.

As rulesets are managed through a shared API in Wildpants, registering a rule in one addon is exactly the same as on the other. You can even make a plugin that supports both addons, if you just check first which one is being used:

  local Addon = Bagnon or Combuctor

In the remainder of the page, consider Addon to correspond to either one.

Registering Rulesets

Addon.Rules:New(id, name, icon, func)

Registers a new ruleset with the given id. If id is already registered, this will update the registered rule with the new parameters provided. Errors if id is not a string.

Addon.Rules:New('myrule', 'My Ruleset', 'bananas.tga', function(player, bag, slot, bagInfo, itemInfo)
  return itemInfo.link == bananaLink
end)

Rulesets can also be hierarchized into subsets. This is done through the rule id in a similar fashion to web URLs. However, to register a subset, the parent subset must already be registered:

-- works
Addon.Rules:New('myrule/subset', 'My Subset', 'single_banana.tga', function(player, bag, slot, bagInfo, itemInfo)
  return itemInfo.id == bananaID and itemInfo.count == 1
end)

-- will error
Addon.Rules:New('unknownrule/subset', 'My Subset', 'single_banana.tga', function(player, bag, slot, bagInfo, itemInfo)
  return slot == 2 and itemInfo.count > 1
end)

⚠️ While this API supports unlimited levels of hierarchization, Bagnon and Combuctor will only display the first two levels. Thus, registering any rule beyond that level will have no visible effect.

Accessing Rulesets

Addon.Rules:Get(id)

Returns a data structure with all the information pertaining the requested ruleset and its subsets.
If the ruleset is not registered, return nil.

In our example case, requesting myrule would return:

{
  id = 'myrule',
  name = 'My Ruleset',
  icon = 'bananas.tga',
  func = @firstFunction
  children = {
     {
        id = 'myrule/subset',
        name = 'My Subset',
        icon = 'single_banana.tga',
        func = @secondFunction,
        children = {}
     }
  }
}

Addon.Rules:Iterate()

Returns an iterator over all registered rules. In our example case, this would include both My Ruleset and My Subset.
Usage example:

for id, rule in Addon.Rules:Iterate() do
   print(rule.name .. ' is registered!')
end

Addon.Rules:IterateParents()

Similar to the previous method. Returns an iterator over all registered rules at the top level of the hierarchy. In our example case, this would only include My Ruleset.