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.Item = class
28 def || dragonblocks.error("Cannot register item: Missing argument");
30 dblib.copy(this, def);
32 this.name || dragonblocks.error("Cannot register item: Missing name");
33 this.texture || dragonblocks.error("Cannot register item: Missing texture");
34 dragonblocks.items[this.name] && dragonblocks.error("Cannot register item '" + this.name + "': Item already exists");
36 if (this.desc != "" && this.description != "")
37 this.desc = this.description || this.desc || this.name;
39 this.stacksize = this.stacksize || dragonblocks.settings.item.defaultStacksize;
41 this.groups = this.groups || [];
42 this.groups.push("default");
44 dragonblocks.items[this.name] = this;
45 dragonblocks.registeredItems.push(this);
50 return this.groups.includes(name);
55 let sound = this.sounds && this.sounds[s];
61 dragonblocks.playSound(sound);
63 for (let groupname of this.groups){
64 let group = dragonblocks.groups[groupname];
65 let sound = group && group.sounds && group.sounds[s];
71 dragonblocks.playSound(sound);
79 dragonblocks.registeredItems = [];
80 dragonblocks.items = {};
82 dragonblocks.registerItem = def => {
83 new dragonblocks.Item(def);
86 dragonblocks.onUseItemCallbacks = [];
87 dragonblocks.registerOnUseItem = func => {
88 dragonblocks.onUseItemCallbacks.push(func);
91 dragonblocks.itemMatch = (item1, item2) => {
92 item1 = dragonblocks.items[item1] || dragonblocks.groups[item1] || item1;
93 item2 = dragonblocks.items[item2] || dragonblocks.groups[item2] || item2;
94 return item1 == item2 || item1 && item2 && (item1.name == item2.name || item1.inGroup && item1.inGroup(item2.name) || item2.inGroup && item2.inGroup(item1.name));