Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Object actions #1052

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions actions/add_item_to_object_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module.exports = {
// ---------------------------------------------------------------------
// Action Name
//
// This is the name of the action displayed in the editor.
// ---------------------------------------------------------------------

name: 'Add Item to Object',

// ---------------------------------------------------------------------
// Action Section
//
// This is the section the action will fall into.
// ---------------------------------------------------------------------

section: 'JSON Things',

// ---------------------------------------------------------------------
// Action Subtitle
//
// This function generates the subtitle displayed next to the name.
// ---------------------------------------------------------------------

subtitle(data, presets) {
const storage = presets.variables;
if (!data.value) return `Clear key "${data.key}" in ${storage[parseInt(data.storage, 10)]} (${data.varName})`;
return `Add "${data.value}" to key "${data.key}" in ${storage[parseInt(data.storage, 10)]} (${data.varName})`;
},

// ---------------------------------------------------------------------
// Action Meta Data
//
// Helps check for updates and provides info if a custom mod.
// If this is a third-party mod, please set "author" and "authorUrl".
//
// It's highly recommended "preciseCheck" is set to false for third-party mods.
// This will make it so the patch version (0.0.X) is not checked.
// ---------------------------------------------------------------------

meta: {
version: '2.1.7',
preciseCheck: false,
author: 'DBM Mods',
authorUrl: 'https://github.com/dbm-network/mods',
downloadURL: 'https://github.com/dbm-network/mods/blob/master/actions/add_item_to_object_MOD.js',
},

// ---------------------------------------------------------------------
// Action Fields
//
// These are the fields for the action. These fields are customized
// by creating elements with corresponding IDs in the HTML. These
// are also the names of the fields stored in the action's JSON data.
// ---------------------------------------------------------------------

fields: ['storage', 'varName', 'key', 'value'],

// ---------------------------------------------------------------------
// Command HTML
//
// This function returns a string containing the HTML used for
// editing actions.
//
// The "isEvent" parameter will be true if this action is being used
// for an event. Due to their nature, events lack certain information,
// so edit the HTML to reflect this.
// ---------------------------------------------------------------------

html(isEvent, data) {

Check warning on line 69 in actions/add_item_to_object_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'isEvent' is defined but never used

Check warning on line 69 in actions/add_item_to_object_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'data' is defined but never used
return `
<retrieve-from-variable dropdownLabel="Source Object" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></retrieve-from-variable>

<br><br><br>

<div style="display:flex; padding-top: 8px; width:100%">
<div style="flex:1 1 auto">
<span class="dbminputlabel">Key</span><br>
<input id="key" class="round" type="text"><br>
</div>
<div style="flex:1 1 auto; padding-left: 23px">
<span class="dbminputlabel">Value</span><br>
<input id="value" class="round" placeholder="Leave blank to clear" type="text" name="is-eval">
</div>
</div>`;
},

// ---------------------------------------------------------------------
// Action Editor Init Code
//
// When the HTML is first applied to the action editor, this code
// is also run. This helps add modifications or setup reactionary
// functions for the DOM elements.
// ---------------------------------------------------------------------

init() {},

// ---------------------------------------------------------------------
// Action Bot Function
//
// This is the function for the action within the Bot's Action class.
// Keep in mind event calls won't have access to the "msg" parameter,
// so be sure to provide checks for variable existence.
// ---------------------------------------------------------------------

action(cache) {
const data = cache.actions[cache.index];
const storage = parseInt(data.storage, 10);
const varName = this.evalMessage(data.varName, cache);
const key = this.evalMessage(data.key, cache);
const list = this.getVariable(storage, varName, cache);

let val = this.evalMessage(data.value, cache);
if (!val) {
delete list[key];
} else {
try {
val = this.eval(val, cache);
} catch (e) {
this.displayError(data, cache, e);
}
list[key] = val;
}

this.callNextAction(cache);
},

// ---------------------------------------------------------------------
// Action Bot Mod
//
// Upon initialization of the bot, this code is run. Using the bot's
// DBM namespace, one can add/modify existing functions if necessary.
// In order to reduce conflicts between mods, be sure to alias
// functions you wish to overwrite.
// ---------------------------------------------------------------------

mod() {},
};
147 changes: 147 additions & 0 deletions actions/add_items_to_object_MOD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
module.exports = {
// ---------------------------------------------------------------------
// Action Name
//
// This is the name of the action displayed in the editor.
// ---------------------------------------------------------------------

name: 'Add Items to Object',

// ---------------------------------------------------------------------
// Action Section
//
// This is the section the action will fall into.
// ---------------------------------------------------------------------

section: 'JSON Things',

// ---------------------------------------------------------------------
// Action Subtitle
//
// This function generates the subtitle displayed next to the name.
// ---------------------------------------------------------------------

subtitle(data, presets) {
const storage = presets.variables;
return `Add ${Object.entries(data.entriess).length} item${
Object.entries(data.entriess).length === 1 ? '' : 's'
} entries to ${storage[parseInt(data.storage, 10)]} (${data.varName})`;
},

// ---------------------------------------------------------------------
// Action Meta Data
//
// Helps check for updates and provides info if a custom mod.
// If this is a third-party mod, please set "author" and "authorUrl".
//
// It's highly recommended "preciseCheck" is set to false for third-party mods.
// This will make it so the patch version (0.0.X) is not checked.
// ---------------------------------------------------------------------

meta: {
version: '2.1.7',
preciseCheck: false,
author: 'DBM Mods',
authorUrl: 'https://github.com/dbm-network/mods',
downloadURL: 'https://github.com/dbm-network/mods/blob/master/actions/add_items_to_object_MOD.js',
},

// ---------------------------------------------------------------------
// Action Fields
//
// These are the fields for the action. These fields are customized
// by creating elements with corresponding IDs in the HTML. These
// are also the names of the fields stored in the action's JSON data.
// ---------------------------------------------------------------------

fields: ['storage', 'varName', 'entriess'],

// ---------------------------------------------------------------------
// Command HTML
//
// This function returns a string containing the HTML used for
// editing actions.
//
// The "isEvent" parameter will be true if this action is being used
// for an event. Due to their nature, events lack certain information,
// so edit the HTML to reflect this.
// ---------------------------------------------------------------------

html(isEvent, data) {

Check warning on line 70 in actions/add_items_to_object_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'isEvent' is defined but never used

Check warning on line 70 in actions/add_items_to_object_MOD.js

View workflow job for this annotation

GitHub Actions / ESLint

'data' is defined but never used
return `
<retrieve-from-variable dropdownLabel="Source Object" selectId="storage" variableContainerId="varNameContainer" variableInputId="varName"></retrieve-from-variable>

<br><br><br><br>

<dialog-list id="entriess" fields='["key", "value"]' dialogTitle="Add Entry" dialogWidth="360" dialogHeight="170" listLabel="Entries" listStyle="height: calc(100vh - 285px);" itemName="Entry" itemCols="2" itemHeight="40px;" itemStyle="padding: 10px; text-align: left; " itemTextFunction="data.key ? data.value ? data.key + ': ' + data.value : 'Clear \\'' + data.key + '\\'' : '<a style=\\'color:red\\'>Missing key</a>'">
<div style="display:flex; padding: 16px; width:100%">
<div style="flex:1 1 auto">
<span class="dbminputlabel">Key</span><br>
<input id="key" class="round" type="text"><br>
</div>
<div style="flex:1 1 auto; padding-left: 23px">
<span class="dbminputlabel">Value</span><br>
<input id="value" class="round" placeholder="Leave blank to clear" type="text" name="is-eval">
</div>
</div>
</dialog-list>


`;
},

// ---------------------------------------------------------------------
// Action Editor Init Code
//
// When the HTML is first applied to the action editor, this code
// is also run. This helps add modifications or setup reactionary
// functions for the DOM elements.
// ---------------------------------------------------------------------

init() {},

// ---------------------------------------------------------------------
// Action Bot Function
//
// This is the function for the action within the Bot's Action class.
// Keep in mind event calls won't have access to the "msg" parameter,
// so be sure to provide checks for variable existence.
// ---------------------------------------------------------------------

action(cache) {
const data = cache.actions[cache.index];
const storage = parseInt(data.storage, 10);
const varName = this.evalMessage(data.varName, cache);
const list = this.getVariable(storage, varName, cache);

for (let i = 0; i < data.entriess.length; i++) {
const key = this.evalMessage(data.entriess[i].key, cache);
let val = this.evalMessage(data.entriess[i].value, cache);
if (!key) continue;
if (!val) {
delete list[key];
continue;
} else {
try {
val = this.eval(val, cache);
} catch (e) {
this.displayError(data, cache, e);
}
list[key] = val;
}
}

this.callNextAction(cache);
},

// ---------------------------------------------------------------------
// Action Bot Mod
//
// Upon initialization of the bot, this code is run. Using the bot's
// DBM namespace, one can add/modify existing functions if necessary.
// In order to reduce conflicts between mods, be sure to alias
// functions you wish to overwrite.
// ---------------------------------------------------------------------

mod() {},
};
Loading
Loading