I'm trying to make a plugin using YEP_ItemCore that combines ingredients' effects into a single item. All the UI works, but the item doesn't get added to the inventory. I need help with how YEP_ItemCore makes individual items I think
JavaScript:
/*:
* @plugindesc [v1.7] Adds a Cooking menu that lets the player create a sandwich by combining ingredients. Requires Yanfly's Item Core.
*
* @help
* Plugin Command:
* startCookingMenu
*
* In your ingredients’ note boxes, use one of the following tags exactly:
* <Bread>
* <Meat>
* <Vegetable>
* <Cheese>
* <Sauce>
* <Seasoning>
*
* A sandwich is valid only if it has at least one Bread and one other ingredient.
* When finalized, a clone of the base Sandwich item (defined in the database with ID 1 by default)
* is created with its effects replaced by the combined effects of all chosen ingredients.
*
* Upon creating a valid sandwich, a text message ("Made sandwich successfully!") is displayed.
*
* The sandwich item is marked as non-stackable (unique) so that it is added individually.
*
* @requires YEP_ItemCore
*/
(function() {
"use strict";
//=============================================================================
// Plugin Command Registration
//=============================================================================
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === "startCookingMenu") {
SceneManager.push(Scene_Cooking);
}
};
if (PluginManager.registerCommand) {
PluginManager.registerCommand('CookingPlugin', 'startCookingMenu', args => {
SceneManager.push(Scene_Cooking);
});
}
//=============================================================================
// Ingredient Helper
//=============================================================================
function getIngredientType(item) {
if (item && item.note) {
if (/<Bread>/i.test(item.note)) return "Bread";
if (/<Meat>/i.test(item.note)) return "Meat";
if (/<Vegetable>/i.test(item.note)) return "Vegetable";
if (/<Cheese>/i.test(item.note)) return "Cheese";
if (/<Sauce>/i.test(item.note)) return "Sauce";
if (/<Seasoning>/i.test(item.note)) return "Seasoning";
}
return null;
}
//=============================================================================
// Sandwich Data Structure
//=============================================================================
function Sandwich() {
this.ingredients = {}; // keys are ingredient types; each holds one ingredient
}
Sandwich.prototype.addIngredient = function(ingredient) {
var type = getIngredientType(ingredient);
if (type) {
// Only one ingredient per type is allowed; this replaces any previous selection.
this.ingredients[type] = ingredient;
}
};
Sandwich.prototype.isValid = function() {
// A valid sandwich must have at least one Bread and one additional ingredient.
if (!this.ingredients["Bread"]) return false;
for (var key in this.ingredients) {
if (key !== "Bread") return true;
}
return false;
};
Sandwich.prototype.getCombinedEffects = function() {
var combinedEffects = [];
for (var key in this.ingredients) {
var ingredient = this.ingredients[key];
if (ingredient && ingredient.effects) {
combinedEffects = combinedEffects.concat(ingredient.effects);
}
}
return combinedEffects;
};
//=============================================================================
// Scene_Cooking
//=============================================================================
function Scene_Cooking() {
this.initialize.apply(this, arguments);
}
Scene_Cooking.prototype = Object.create(Scene_MenuBase.prototype);
Scene_Cooking.prototype.constructor = Scene_Cooking;
Scene_Cooking.prototype.initialize = function() {
Scene_MenuBase.prototype.initialize.call(this);
this._sandwich = new Sandwich();
};
Scene_Cooking.prototype.create = function() {
Scene_MenuBase.prototype.create.call(this);
this.createIngredientWindow();
this.createCommandWindow();
this.createPreviewWindow();
};
Scene_Cooking.prototype.createIngredientWindow = function() {
// List available ingredients from the party's inventory.
this._ingredientWindow = new Window_IngredientList(0, 0, Graphics.boxWidth / 2, Graphics.boxHeight);
this._ingredientWindow.setHandler('ok', this.onIngredientOk.bind(this));
this._ingredientWindow.setHandler('cancel', this.onIngredientCancel.bind(this));
this.addWindow(this._ingredientWindow);
this._ingredientWindow.activate();
};
Scene_Cooking.prototype.createCommandWindow = function() {
// Command window to finish sandwich creation or cancel.
var wx = Graphics.boxWidth / 2;
var ww = Graphics.boxWidth / 2;
var wh = 200;
var wy = Graphics.boxHeight - wh;
this._commandWindow = new Window_CookingCommand(wx, wy, ww, wh);
this._commandWindow.setHandler('confirm', this.onConfirm.bind(this));
this._commandWindow.setHandler('cancel', this.popScene.bind(this));
this.addWindow(this._commandWindow);
};
Scene_Cooking.prototype.createPreviewWindow = function() {
// Window showing the current sandwich composition.
var wx = Graphics.boxWidth / 2;
var ww = Graphics.boxWidth / 2;
var wh = Graphics.boxHeight - 200;
this._previewWindow = new Window_SandwichPreview(wx, 0, ww, wh);
this._previewWindow.setSandwich(this._sandwich);
this.addWindow(this._previewWindow);
};
Scene_Cooking.prototype.onIngredientOk = function() {
var ingredient = this._ingredientWindow.item();
if (!ingredient) {
this._ingredientWindow.activate();
return;
}
var type = getIngredientType(ingredient);
if (type) {
// Add the selected ingredient.
this._sandwich.addIngredient(ingredient);
// Remove one instance of the ingredient from the party's inventory.
$gameParty.loseItem(ingredient, 1, false);
this._ingredientWindow.refresh();
this._previewWindow.refresh();
}
this._ingredientWindow.activate();
};
Scene_Cooking.prototype.onIngredientCancel = function() {
this._ingredientWindow.activate();
};
Scene_Cooking.prototype.onConfirm = function() {
if (this._sandwich.isValid()) {
// Create the sandwich item.
// CHANGE sandwichBaseId if your Sandwich item is defined with a different database id.
var sandwichBaseId = 1;
var sandwichItem = JsonEx.makeDeepCopy($dataItems[sandwichBaseId]);
// Combine the ingredient effects.
var effects = this._sandwich.getCombinedEffects();
sandwichItem.effects = effects;
// Mark the sandwich as non-stackable so each instance is unique.
sandwichItem.meta = sandwichItem.meta || {};
sandwichItem.meta.NoStack = true;
// Add the new sandwich item to the party's inventory.
$gameParty.gainItem(sandwichItem, 1, false);
SoundManager.playOk();
// Show a success message.
$gameMessage.add("Made sandwich successfully!");
// Close the cooking scene.
this.popScene();
} else {
// For an invalid sandwich, simply close the scene and return to the overworld.
this.popScene();
}
};
//=============================================================================
// Window_IngredientList
//=============================================================================
function Window_IngredientList() {
this.initialize.apply(this, arguments);
}
Window_IngredientList.prototype = Object.create(Window_Selectable.prototype);
Window_IngredientList.prototype.constructor = Window_IngredientList;
Window_IngredientList.prototype.initialize = function(x, y, width, height) {
Window_Selectable.prototype.initialize.call(this, x, y, width, height);
this.refresh();
};
Window_IngredientList.prototype.maxItems = function() {
return this._data ? this._data.length : 0;
};
Window_IngredientList.prototype.item = function() {
return this._data[this.index()];
};
Window_IngredientList.prototype.makeItemList = function() {
// Filter the party's inventory to list only items with an ingredient notetag.
this._data = $gameParty.allItems().filter(function(item) {
return getIngredientType(item);
});
};
Window_IngredientList.prototype.refresh = function() {
this.makeItemList();
this.createContents();
Window_Selectable.prototype.refresh.call(this);
};
Window_IngredientList.prototype.drawItem = function(index) {
var item = this._data[index];
var rect = this.itemRect(index);
this.drawItemName(item, rect.x, rect.y, rect.width);
};
//=============================================================================
// Window_CookingCommand
//=============================================================================
function Window_CookingCommand() {
this.initialize.apply(this, arguments);
}
Window_CookingCommand.prototype = Object.create(Window_Command.prototype);
Window_CookingCommand.prototype.constructor = Window_CookingCommand;
Window_CookingCommand.prototype.initialize = function(x, y, width, height) {
Window_Command.prototype.initialize.call(this, []);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.refresh();
};
Window_CookingCommand.prototype.makeCommandList = function() {
this.addCommand("Finish Sandwich", 'confirm');
this.addCommand("Cancel", 'cancel');
};
Window_CookingCommand.prototype.refresh = function() {
this.clearCommandList();
this.makeCommandList();
Window_Command.prototype.refresh.call(this);
};
//=============================================================================
// Window_SandwichPreview
//=============================================================================
function Window_SandwichPreview() {
this.initialize.apply(this, arguments);
}
Window_SandwichPreview.prototype = Object.create(Window_Base.prototype);
Window_SandwichPreview.prototype.constructor = Window_SandwichPreview;
Window_SandwichPreview.prototype.initialize = function(x, y, width, height) {
Window_Base.prototype.initialize.call(this, x, y, width, height);
this._sandwich = null;
};
Window_SandwichPreview.prototype.setSandwich = function(sandwich) {
this._sandwich = sandwich;
this.refresh();
};
Window_SandwichPreview.prototype.refresh = function() {
this.contents.clear();
if (this._sandwich) {
var y = 0;
for (var type in this._sandwich.ingredients) {
var ingredient = this._sandwich.ingredients[type];
this.drawText(type + ": " + ingredient.name, 0, y, this.contents.width);
y += this.lineHeight();
}
if (!this._sandwich.isValid()) {
this.drawText("Invalid sandwich: You need at least one Bread and one filling.", 0, y, this.contents.width);
}
}
};
})();