]> git.lizzy.rs Git - dragonblocks.git/blob - engine/itemstack.js
Reset file permissions to defaults
[dragonblocks.git] / engine / itemstack.js
1 /*
2  * itemstack.js
3  * 
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  * 
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.
10  * 
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.
15  * 
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,
19  * MA 02110-1301, USA.
20  * 
21  * 
22  */
23 dragonblocks.Itemstack = class{
24         constructor(){
25                 this.count = 0;
26                 this.item = null;
27                 this.id = dragonblocks.getToken();
28                 dragonblocks.itemstacks[this.id] = this;
29         }
30         parse(itemstring){
31                 this.item = itemstring ? itemstring.split(" ")[0] : null;
32                 this.count = itemstring ? parseInt(itemstring.split(" ")[1]) || 1 : 1;
33                 if(this.item && ! this.toItem())
34                         return false;
35                 this.update();
36                 return true;
37         }
38         getStacksize(){
39                 return (this.toItem() && this.toItem().stacksize) || dragonblocks.settings.item.defaultStacksize;
40         }
41         update(){
42                 if(this.count < 1)
43                         this.item = null;
44                 if(!this.item)
45                         this.count = 0;
46                 if(this.onupdate)
47                         this.onupdate();
48         }
49         addUpdateListener(func){
50                 let oldOnupdate = this.onupdate;
51                 this.onupdate = _ => {
52                         if(oldOnupdate)
53                                 oldOnupdate();
54                         func();
55                 }               
56         }
57         toItem(){
58                 return dragonblocks.items[this.item];
59         }
60         swap(itemstack){
61                 [this.count, itemstack.count] = [itemstack.count, this.count];
62                 [this.item, itemstack.item] = [itemstack.item, this.item];
63                 itemstack.update();
64                 this.update();
65         }
66         clear(){
67                 this.item = null;
68                 this.count = 0;
69                 this.update();
70         }
71         addItems(itemstack, count){
72                 this.update();
73                 itemstack.update();
74                 if(! itemstack.item)
75                         return false;
76                 if(! this.item)
77                         this.item = itemstack.item;
78                 if(this.item != itemstack.item)
79                         return false;
80                 if(this.count == this.getStacksize())
81                         return false;
82                 itemstack.count -= count;
83                 this.count += count;
84                 let less = -itemstack.count;
85                 if(less > 0){
86                         itemstack.count += less;
87                         this.count -= less;
88                 }
89                 let more = this.count - this.getStacksize();
90                 if(more > 0){
91                         this.count -= more;
92                         itemstack.count += more;
93                 }
94                 this.update();
95                 itemstack.update();
96                 return true;
97         }
98         add(itemstack){
99                 return this.addItems(itemstack, itemstack.count);
100         }
101         addOne(itemstack){
102                 return this.addItems(itemstack, 1);
103         }
104         addHalf(itemstack){
105                 return this.addItems(itemstack, Math.ceil(itemstack.count / 2));
106         }
107         stringify(){
108                 if(! this.item)
109                         return "";
110                 return this.item + " " + this.count;
111         }
112 }; 
113 dragonblocks.itemstacks = {};
114 dragonblocks.createItemstack = function(itemstring){
115         let itemstack = new dragonblocks.Itemstack();
116         if(itemstring)
117                 itemstack.parse(itemstring);
118         return itemstack;
119 }
120 dragonblocks.isValidItemstring = function(itemstring){
121         return new dragonblocks.Itemstack().parse(itemstring);
122 }