Skip to content

Instantly share code, notes, and snippets.

@aaaaagold
Last active June 10, 2021 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaaaagold/bfe3040525cc4ee82cc4e38ae786f8e6 to your computer and use it in GitHub Desktop.
Save aaaaagold/bfe3040525cc4ee82cc4e38ae786f8e6 to your computer and use it in GitHub Desktop.
an rmmv plugin
"use strict";
/*:
* @plugindesc This plugin makes limiting the number of a group of items possible
* @author agold404
*
* @help 有時你希望商店裡的某幾項東西,加上原先擁有的,"總共"只能買幾個,那麼你可以使用這個插件
* 用法:
* 1. 先在 道具/武器/防具 的note區寫下 <maxStack:數字> 以限制該 道具/武器/防具 的最大數量。0或NaN時會使用預設值
* 2. 再於 道具/武器/防具 的note區寫下 <unionCnt:[ [種類,id] , [種類,id] , ... ]> (format:JSON), 種類 可以是 "i" , "w" , "a" ,分別表示 道具、武器、防具。格式不符時會發生不可預期之錯誤。
*
* 範例:希望 道具1,武器2,防具4,武器8 這4項總共只能購買3個,則
* 於 道具1,武器2,防具4,武器8 的note區都寫下 <maxStack:3> <unionCnt:[["i",1],["w",2],["a",4],["w",8]]>
*
* Technical detail:
* 1. This plugin can be renamed as you want.
* 2. This plugin use 'Game_Party.prototype.numItems' frequently when needed.
*/
(()=>{ const s='maxStack',u='unionCnt';
let r,d;
Scene_Shop.prototype.maxBuy=function f(){ // overwrite
const max=$gameParty.maxItems(this._item)-$gameParty.unionCnt(this._item);
const price=this.buyingPrice();
return price>0 ? Math.min(~~(this.money() / price),max) : max;
};
{ const p=Game_Party.prototype;
{ const k='maxItems';
r=p[k];
d=p[k]=function f(o){
if(!Object.hasOwnProperty.call(o,s)) o[s] = o.meta && Object.hasOwnProperty.call(o.meta,s) ? Number(o.meta[s])||f.ori.call(this,o) : f.ori.call(this,o);
return o[s];
}; d.ori=r;
}
p.hasMaxItems=function f(o){ // overwrite
return this.unionCnt(o) >= this.maxItems(o);
};
p.unionCnt=function(o){
if(o.meta && Object.hasOwnProperty.call(o.meta,u)){
if(!Object.hasOwnProperty.call(o,u)) o[u]=JSON.parse(o.meta[u]);
let cnt=0;
for(let x=0,arr=o[u],c;x!==arr.length;++x){
switch(arr[x][0]){
default: c=0; break;
case 'a': c=$dataArmors; break;
case 'i': c=$dataItems; break;
case 'w': c=$dataWeapons; break;
}
if(c) cnt+=this.numItems(c[arr[x][1]]);
}
return cnt;
}else return this.numItems(o);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment