-
Notifications
You must be signed in to change notification settings - Fork 12
/
ALOE_Sync_Variables_Items.js
176 lines (162 loc) · 6.19 KB
/
ALOE_Sync_Variables_Items.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
/*:
* @plugindesc Synchronize Variables and Items/Equipment
* @author Aloe Guvner
*
*
* @param syncItems
* @text Items to Sync
* @type struct<syncItemStruct>[]
* @desc Set which variable(s) and item(s)
* should be synced.
*
* @help
*
* Some menu plugins require data to be stored as variables, which also has to be
* represented as item counts, or vice-versa.
* If multiple currency systems (ex. using gold and gems) require the additional
* currencies to be represented as variables, it would be very useful to
* represent it as items in the inventory as well.
*
* This plugin will synchronize the value of a variable with the inventory count
* of a certain item/weapon/armor, making sure that they are always the same.
*
*
* For example, if Item #1 (Potion) and Variable #22 are synchronized:
*
* 1.) The party gains 30 potions. The value of Variable #22 is automatically set
* to 30.
* 2.) The value of Variable #22 is set to 50. The party automatically gains 20
* potions - for a total of 50 potions.
* 3.) The value of Variable #22 is set to 46. The party automatically loses 4
* potions - for a total of 46 potions.
*
* Note:
* Only pairs of item/weapon/armor + variable can be synchronized.
* Item/Weapon/Armor <---> Variable
*
* This plugin does not currently support synchronization across groups of multiple
* Item/Weapon/Armor/Variables.
*
* //=============================================================================
* Version History:
* //=============================================================================
*
* v1.0.0 (June 14 2018)
* --Initial release
*
* //=============================================================================
* End of Help File
* //=============================================================================
*/
/*~struct~syncItemStruct:
* @param varId
* @text Variable ID
* @type number
* @desc Variable ID to syncronize.
*
* @param itemType
* @text Type of Item/Equip
* @type select
* @option Item
* @value item
* @option Weapon
* @value weapon
* @option Armor
* @value armor
* @desc The type of item to syncronize with the
* variable: Item, Weapon, Armor
* @default item
*
* @param itemId
* @text Item/Equip ID
* @type number
* @desc ID Number of the item/weapon/armor
* to syncronize with the variable.
*
*/
(function () {
"use strict";
var Alias = {};
//=============================================================================
// Utils
//=============================================================================
// Create a utility function to parse complex parameters.
//=============================================================================
Utils.recursiveParse = function (param) {
try {
return JSON.parse(param, function (key, value) {
try {
return this.recursiveParse(value);
} catch (e) {
return value;
}
}.bind(this));
} catch (e) {
return param;
}
};
//=============================================================================
// Parameters
//=============================================================================
// Read and parse parameters into a locally scoped Parameters object.
//=============================================================================
var Parameters = {};
Object.keys(PluginManager.parameters("ALOE_Sync_Variables_Items")).forEach(function (a) {
return Parameters[a] = Utils.recursiveParse(PluginManager.parameters("ALOE_Sync_Variables_Items")[a]);
});
//=============================================================================
// Sync Item to Variable
//=============================================================================
// When a variable changes in value, increase/decrease the linked item count.
//=============================================================================
Alias.Game_Variables_setValue = Game_Variables.prototype.setValue;
Game_Variables.prototype.setValue = function (variableId, value) {
Alias.Game_Variables_setValue.call(this, variableId, value);
var index = Parameters.syncItems.findIndex(function (a) {
return a.varId === variableId;
});
if (index >= 0) {
switch (Parameters.syncItems[index].itemType) {
case "item":
var item = $dataItems[Parameters.syncItems[index].itemId];
break;
case "weapon":
var item = $dataWeapons[Parameters.syncItems[index].itemId];
break;
case "armor":
var item = $dataArmors[Parameters.syncItems[index].itemId];
break;
}
var numItems = $gameParty.numItems(item);
if (this.value(variableId) !== numItems) {
$gameParty.gainItem(item, this.value(variableId) - numItems, true);
}
}
};
//=============================================================================
// Sync Variable to Item
//=============================================================================
// When a item count changes, increase/decrease the linked variable.
//=============================================================================
Alias.Game_Party_gainItem = Game_Party.prototype.gainItem;
Game_Party.prototype.gainItem = function (item, amount, includeEquip) {
Alias.Game_Party_gainItem.call(this, item, amount, includeEquip);
var type = void 0;
if (DataManager.isItem(item)) {
type = "item";
} else if (DataManager.isWeapon(item)) {
type = "weapon";
} else if (DataManager.isArmor(item)) {
type = "armor";
}
var index = Parameters.syncItems.findIndex(function (a) {
return a.itemId === item.id && a.itemType === type;
});
if (index >= 0) {
var variableId = Parameters.syncItems[index].varId;
if ($gameVariables.value(variableId) !== this.numItems(item)) ;
$gameVariables.setValue(Parameters.syncItems[index].varId, this.numItems(item));
}
};
})();