pure-full-lily/js/plugins/CallCommonEventWithParams.js
2025-09-16 10:12:44 -05:00

111 lines
3.8 KiB
JavaScript

/*:
* @target MZ
* @plugindesc 指定した変数・スイッチをセットしてからコモンイベントを呼び出すプラグイン v1.1.0
* @author IvI
*
* @command call
* @text コモンイベント呼び出し(変数&スイッチ設定)
* @desc 指定した変数・スイッチを設定後、コモンイベントを予約します。
*
* @arg CommonEventId
* @type common_event
* @text コモンイベントID
* @desc 実行するコモンイベント。
* @default 1
*
* @arg Variables
* @type multiline_string
* @text 変数(JSON)
* @desc {"31":850,"32":350} のように JSON 形式で指定。
* @default {}
*
* @arg Switches
* @type multiline_string
* @text スイッチ(JSON)
* @desc {"10":true,"11":false} のように JSON 形式で指定。
* @default {}
*
* @help CallCommonEventWithParams.js
* ------------------------------------------------------------
* このプラグインは、プラグインコマンドを通して
* 1. 任意のゲーム変数を設定
* 2. 任意のゲームスイッチを設定
* 3. その後にコモンイベントを予約
* という一連の処理を 1 回で行います。
*
* 【使い方】
* プラグインコマンド > コモンイベント呼び出し(変数&スイッチ設定)
* ▸ CommonEventId : 実行するコモンイベントID
* ▸ Variables : 変数を JSON で指定 (例 {"31":850,"32":$gamePlayer.x})
* ▸ Switches : スイッチを JSON で指定 (例 {"10":true})
*
* ▸ 値が文字列の場合は eval() で評価されるため、$gamePlayer.x 等の式も使用可能。
* ▸ JSON の書式が不正な場合、そのセクションはスキップします。
*
* ------------------------------------------------------------
* ◆ 更新履歴
* v1.1.0 2025/05/17 @command 記述漏れ修正、arg を multiline_string 化
* v1.0.0 2025/05/17 初版
* ------------------------------------------------------------
* このプラグインは MIT ライセンスです。
* ------------------------------------------------------------
*/
(() => {
const pluginName = "CallCommonEventWithParams";
/** JSON を安全にパース -------------------------------------- */
const safeParse = (text) => {
if (!text || text.trim() === "") return null;
try {
return JSON.parse(text);
} catch (e) {
console.error(`${pluginName}: JSON parse error`, e);
return null;
}
};
/** 文字列なら eval で評価 ------------------------------------ */
const evalIfString = (v) => {
if (typeof v === "string") {
try {
return eval(v);
} catch (e) {
console.error(`${pluginName}: eval error ->`, v, e);
return v;
}
}
return v;
};
/** プラグインコマンド ---------------------------------------- */
PluginManager.registerCommand(pluginName, "call", function(args) {
const commonEventId = Number(args.CommonEventId || 0);
// 変数設定
const vars = safeParse(args.Variables);
if (vars) {
for (const [id, value] of Object.entries(vars)) {
const vId = Number(id);
const vVal = evalIfString(value);
$gameVariables.setValue(vId, vVal);
}
}
// スイッチ設定
const sws = safeParse(args.Switches);
if (sws) {
for (const [id, value] of Object.entries(sws)) {
const sId = Number(id);
const sVal = evalIfString(value);
$gameSwitches.setValue(sId, !!sVal);
}
}
// コモンイベント予約
if (commonEventId > 0) {
$gameTemp.reserveCommonEvent(commonEventId);
} else {
console.warn(`${pluginName}: CommonEventId is 0. Skipped.`);
}
});
})();