Skip to content

JavaScript Scripting

EmakiGem registers a scripting module through the CoreLib scripting system. The module ID is gem. Inside a script you obtain the module facade with emaki.module("gem") to register socket rules and set bonuses.

This page covers only the EmakiGem-specific script methods, registration fields, context and examples. For the generic scripting mechanism see CoreLib JavaScript Scripting.

Always guard with available() before calling any business method. EmakiGem is a CoreLib softdepend.

Obtaining the module

js
function main(ctx) {
  const gem = emaki.module("gem");
  if (!gem.available()) {
    return { skipped: true, message: "EmakiGem unavailable" };
  }
  // ...
  return true;
}

Probe methods

MethodReturnsDescription
available()booleanWhether the module is available.
ready()booleanWhether the module finished initialization.
apiVersion()stringEmakiGem API version.
pluginName()stringPlugin name.

Socket rules

Socket rules run during socket resolution. They can read the context and modify the success rate, allow/deny socketing, and set the prompt message.

MethodReturnsDescription
registerSocketRule(definition)booleanRegister a socket rule.
registerSocketRule(id, definition)booleanOverload: merge id into the definition, then register.
unregisterSocketRule(id)voidUnregister a rule by id.
registeredSocketRules()listReturn the list of registered socket rule ids.

Rule definition fields

FieldTypeRequiredDefaultDescription
idstringyesnoneUnique rule id (normalized); registration fails when blank.
functionstringnocheckSocketRule callback function name; execute also works.
priorityintno0Priority; lower runs first, ties broken by id.
gemIdsstring / listnoempty (all gems)Gem ids the rule applies to; gems also works.
socketTypesstring / listnoempty (all sockets)Socket types the rule applies to; sockets also works.
timeoutMillislongnoengine defaultPer-call script timeout.

Rule callback ctx fields

FieldTypeDescription
ruleIdstringCurrent rule id.
playerUuidstringPlayer UUID.
playerNamestringPlayer name.
itemIdstringTarget item id.
slotIndexintSocket index.
socketTypestringSocket type.
gemIdstringGem id.
gemTypestringGem type.
gemLevelintGem level.
inlaidGemslistGems already socketed on the target item (before this socketing), each with slot / gemId / gemType / gemLevel.
inlaidGemCountintNumber of gems currently socketed.
allowedbooleanWhether socketing is currently allowed.
originalSuccessRatedoubleOriginal success rate.
successRatedoubleCurrent accumulated success rate.
messageKeystringCurrent message key, defaults to gem.error.condition_not_met.
messagestringCurrent message.
traceslistTraces from previous rules, each with id / before / after / message.

Rule callback return fields

Return fieldTypeDescription
cancelbooleanWhen true, forcibly disallows socketing.
allowedbooleanWhether allowed; combined with !cancel.
successRatenumberSet the success rate directly (takes priority).
successBonusnumberAdd to the current success rate (when no successRate).
successMultipliernumberMultiply the current success rate (applied after bonus).
messageKeystringOverride the message key.
messagestringOverride the message text.

The final success rate is clamped to 0~100. Rules run serially in ascending priority order and stop as soon as a rule disallows socketing.

Set bonuses

A set bonus is evaluated per item and can append name actions and lore actions.

MethodReturnsDescription
registerSetBonus(definition)booleanRegister a set bonus.
registerSetBonus(id, definition)booleanOverload: merge id into the definition, then register.
unregisterSetBonus(id)voidUnregister a set bonus by id.
registeredSetBonuses()listReturn the list of registered set bonus ids.

Bonus definition fields

FieldTypeRequiredDefaultDescription
idstringyesnoneUnique bonus id; registration fails when blank.
functionstringnoapplySetBonusCallback function name; execute also works.
timeoutMillislongnoengine defaultPer-call script timeout.

Set bonuses have no priority and no gem/socket filtering; all bonuses are evaluated per item in id lexical order.

Bonus callback ctx fields

FieldTypeDescription
bonusIdstringCurrent bonus id.
itemIdstringItem id.
gemslistSocketed gems, each with id / type / level.
gemIdslistList of gem ids.
gemTypeslistList of gem types.
socketCountintNumber of socket assignments.

Bonus callback return fields

Return fieldTypeDescription
nameActionslistItem name modification actions passed to the action system.
loreActionslistItem lore modification actions, e.g. {op:"append", value:"..."}.

The result is only collected and applied when at least one of nameActions or loreActions is non-empty.

Full example

EmakiGem ships the example script scripts/examples/gem_status.js:

js
function register() {
  const gem = emaki.module("gem");

  gem.registerSocketRule({
    id: "example_socket_bonus",
    priority: 10,
    function: "checkSocket"
  });

  gem.registerSetBonus({
    id: "example_lore_bonus",
    function: "applySetBonus"
  });
}

function checkSocket(ctx) {
  if (ctx.gemLevel >= 3) {
    return {
      successBonus: 5,
      message: "Example: +5% socket success for gems above level 3"
    };
  }
  return {};
}

function applySetBonus(ctx) {
  if (ctx.gemIds.length >= 3) {
    return {
      loreActions: [{
        op: "append",
        value: "<gold>JavaScript set example: 3+ gems socketed"
      }]
    };
  }
  return {};
}

Enabling extension scripts

Put the script into CoreLib's global extension directory and reload:

text
plugins/EmakiCoreLib/scripts/extensions/global/gem_status.js
text
/corelib script reload

The example file is released to plugins/EmakiCoreLib/scripts/examples/ by default and is not auto-enabled.

Script debugging

When you enable the debug submodule named script, EmakiGem writes the per-rule execution trace to the server console every time its JS rules run, which helps server administrators debug script rules.

Enable command:

text
/gem debug module script

Trace fields: rule id, before (value before execution), after (value after execution) and message. This output goes to the server console for server administrators to debug; it is not shown to players.

Notes

  • Every registration method requires the CoreLib scripting system to be enabled; otherwise registration returns false or is silently skipped.
  • When CoreLib is missing the module degrades gracefully: rules and bonuses have no effect but raise no error.
  • Socket rules run in ascending priority order (lower first) and stop subsequent rules as soon as one disallows socketing.