4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 dragonblocks.Itemstack = class{
27 this.id = dragonblocks.getToken();
28 dragonblocks.itemstacks[this.id] = this;
31 this.item = itemstring ? itemstring.split(" ")[0] : null;
32 this.count = itemstring ? parseInt(itemstring.split(" ")[1]) || 1 : 1;
33 if(this.item && ! this.toItem())
39 return (this.toItem() && this.toItem().stacksize) || dragonblocks.settings.item.defaultStacksize;
49 addUpdateListener(func){
50 let oldOnupdate = this.onupdate;
51 this.onupdate = _ => {
58 return dragonblocks.items[this.item];
61 [this.count, itemstack.count] = [itemstack.count, this.count];
62 [this.item, itemstack.item] = [itemstack.item, this.item];
71 addItems(itemstack, count){
77 this.item = itemstack.item;
78 if(this.item != itemstack.item)
80 if(this.count == this.getStacksize())
82 itemstack.count -= count;
84 let less = -itemstack.count;
86 itemstack.count += less;
89 let more = this.count - this.getStacksize();
92 itemstack.count += more;
99 return this.addItems(itemstack, itemstack.count);
102 return this.addItems(itemstack, 1);
105 return this.addItems(itemstack, Math.ceil(itemstack.count / 2));
110 return this.item + " " + this.count;
113 dragonblocks.itemstacks = {};
114 dragonblocks.createItemstack = function(itemstring){
115 let itemstack = new dragonblocks.Itemstack();
117 itemstack.parse(itemstring);
120 dragonblocks.isValidItemstring = function(itemstring){
121 return new dragonblocks.Itemstack().parse(itemstring);