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,
24 dragonblocks.ItemStack = class extends EventTarget
26 constructor(itemstring)
32 this.id = dragonblocks.getToken();
35 this.deserialize(itemstring);
42 return this.item + " " + this.count;
45 deserialize(itemstring)
47 this.item = itemstring ? itemstring.split(" ")[0] : null;
48 this.count = itemstring ? parseInt(itemstring.split(" ")[1]) || 1 : 1;
55 return (this.toItem() && this.toItem().stacksize) || dragonblocks.settings.item.defaultStacksize;
60 this.dispatchEvent(new dragonblocks.ItemStack.Event(eventType, this));
71 this.trigger("update");
76 return dragonblocks.items[this.item];
81 [this.count, itemstack.count] = [itemstack.count, this.count];
82 [this.item, itemstack.item] = [itemstack.item, this.item];
94 addItems(itemstack, count)
103 this.item = itemstack.item;
104 else if (this.item != itemstack.item)
107 if (this.count == this.getStacksize())
110 itemstack.count -= count;
113 let less = -itemstack.count;
115 itemstack.count += less;
119 let more = this.count - this.getStacksize();
122 itemstack.count += more;
133 return this.addItems(itemstack, itemstack.count);
138 return this.addItems(itemstack, 1);
143 return this.addItems(itemstack, Math.ceil(itemstack.count / 2));
148 return document.getElementById("dragonblocks.itemstack[" + this.id + "]");
153 let display = parent.appendChild(document.createElement("div"));
154 display.id = "dragonblocks.itemstack[" + this.id + "]";
155 display.stackid = this.id;
156 display.style.borderStyle = "solid";
157 display.style.borderWidth = "1px";
158 display.style.borderColor = "#2D2D2D";
159 display.style.width = dragonblocks.settings.inventory.scale + "px";
160 display.style.height = dragonblocks.settings.inventory.scale + "px";
161 display.style.backgroundColor = "#343434";
162 display.style.position = "absolute";
163 display.style.left = x + "px";
164 display.style.top = y + "px";
166 let countDisplay = display.appendChild(document.createElement("span"));
167 countDisplay.id = "dragonblocks.itemstack[" + this.id + "].count";
168 countDisplay.stackid = this.id;
169 countDisplay.style.position = "absolute";
170 countDisplay.style.right = "5px";
171 countDisplay.style.bottom = "5px";
172 countDisplay.style.color = "white";
173 countDisplay.style.cursor = "default";
177 display.addEventListener("mousedown", event => {
178 let out = dragonblocks.outStack;
181 return self.action(out, event.which);
183 switch (event.which) {
186 self.add(out) || self.swap(out);
193 self.addOne(out) || self.swap(out);
199 display.addEventListener("mouseover", event => {
204 display.addEventListener("mouseleave", event => {
205 self.focused = false;
209 this.addEventListener("update", _ => {
218 let display = this.getDisplay();
223 let countDisplay = document.getElementById("dragonblocks.itemstack[" + this.id + "].count");
226 let item = this.toItem();
228 display.title = item.desc;
229 display.style.background = dragonblocks.getTexture(item.texture);
232 countDisplay.innerHTML = this.count;
235 display.style.background = "none";
237 countDisplay.innerHTML = "";
240 display.style.backgroundColor = this.focused ? "#7E7E7E" : "#343434";
242 this.trigger("redraw");
246 dragonblocks.ItemStack.Event = class extends Event
248 constructor(type, stack)
255 dragonblocks.isValidItemstring = itemstring => {
256 let item = itemstring && itemstring.split(" ")[0];
258 if (item && ! dragonblocks.items[item])