001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.inventory;
019
020import net.tridentsdk.base.Substance;
021
022/**
023 * Inventory item, holding all properties of the item
024 *
025 * @author The TridentSDK Team
026 * @since 0.3-alpha-DP
027 */
028public class Item implements Cloneable {
029    private final int id;
030    private final Substance substance;
031
032    private volatile short durability;
033    private volatile short quantity;
034    private volatile short damageValue;
035
036    /**
037     * Creates a new item with the specified substance and quantity 1, 0 data
038     *
039     * @param substance the substance for the item
040     */
041    public Item(Substance substance) {
042        this(substance, (short) 1, (short) 0, (short) 0);
043    }
044
045    /**
046     * Creates a new item with the specified properties
047     *
048     * @param substance the substance
049     * @param quantity the quantity
050     * @param durability the durability
051     * @param damageValue the damage value
052     */
053    public Item(Substance substance, int quantity, short durability, short damageValue) {
054        if (substance == null) {
055            substance = Substance.AIR;
056            // The item is clicked on in the inventory
057        }
058
059        this.id = substance.id();
060        this.substance = substance;
061
062        this.durability = durability;
063        this.quantity = (short) quantity;
064        this.damageValue = damageValue;
065    }
066
067    public int id() {
068        return this.id;
069    }
070
071    public Substance type() {
072        return this.substance;
073    }
074
075    public short durability() {
076        return this.durability;
077    }
078
079    public void durability(short durability) {
080        this.durability = durability;
081    }
082
083    public short quantity() {
084        return this.quantity;
085    }
086
087    public void setQuantity(short quantity) {
088        this.quantity = quantity;
089
090    }
091
092    public short damageValue() {
093        return this.damageValue;
094    }
095
096    public void setDamageValue(short damageValue) {
097        this.damageValue = damageValue;
098    }
099
100    public boolean isSimilar(Item i) {
101        if (id != i.id) {
102            return false;
103        } else if (substance != i.substance) {
104            return false;
105        } else if (quantity != i.quantity) {
106            return false;
107        } else if (damageValue != i.damageValue) {
108            return false;
109        }
110
111        return true;
112    }
113
114    public boolean isSimilarIgnoreQuantity(Item i) {
115        if (id != i.id) {
116            return false;
117        } else if (substance != i.substance) {
118            return false;
119        } else if (damageValue != i.damageValue) {
120            return false;
121        }
122
123        return true;
124    }
125
126    @Override
127    public Item clone(){
128        return new Item(substance, quantity, durability, damageValue);
129    }
130
131    @Override
132    public String toString(){
133        String data = "Item{substance=" + substance;
134        if(quantity() > 0) data += ", quantity=" + quantity;
135        if(durability() > 0) data += ", durability=" + durability;
136        if(damageValue() > 0) data += ", damageValue=" + damageValue;
137        return data + '}';
138    }
139}