Minestom/src/main/java/fr/themode/minestom/item/ItemStack.java

157 lines
3.9 KiB
Java
Raw Normal View History

2019-08-12 08:30:59 +02:00
package fr.themode.minestom.item;
2019-08-29 02:15:52 +02:00
import fr.themode.minestom.data.Data;
import fr.themode.minestom.data.DataContainer;
2020-02-16 19:11:36 +01:00
import fr.themode.minestom.item.rule.VanillaStackingRule;
2019-08-29 02:15:52 +02:00
2020-02-13 15:14:41 +01:00
import java.util.ArrayList;
2019-08-29 02:15:52 +02:00
public class ItemStack implements DataContainer {
2019-08-12 08:30:59 +02:00
public static final ItemStack AIR_ITEM = new ItemStack(0, (byte) 1);
2020-02-17 17:33:53 +01:00
private static StackingRule defaultStackingRule = new VanillaStackingRule(64);
2019-08-12 08:30:59 +02:00
2019-08-22 14:52:32 +02:00
private Material material;
2019-08-13 17:52:09 +02:00
private byte amount;
2019-09-06 16:05:36 +02:00
private short damage;
2019-08-12 08:30:59 +02:00
2019-08-22 14:52:32 +02:00
private String displayName;
private boolean unbreakable;
2020-02-13 15:14:41 +01:00
private ArrayList<String> lore;
2019-08-22 14:52:32 +02:00
2020-02-17 17:33:53 +01:00
private StackingRule stackingRule;
2019-08-29 02:15:52 +02:00
private Data data;
2019-09-06 16:05:36 +02:00
public ItemStack(Material material, byte amount, short damage) {
2019-08-22 14:52:32 +02:00
this.material = material;
2019-08-13 17:52:09 +02:00
this.amount = amount;
2019-09-06 16:05:36 +02:00
this.damage = damage;
2020-02-13 15:14:41 +01:00
this.lore = new ArrayList<>();
2020-02-17 17:33:53 +01:00
this.stackingRule = defaultStackingRule;
2019-08-13 17:52:09 +02:00
}
2019-08-22 14:52:32 +02:00
public ItemStack(int id, byte amount) {
2019-09-06 16:05:36 +02:00
this(Material.fromId(id), amount, (short) 0);
2019-08-22 14:52:32 +02:00
}
2019-08-13 17:52:09 +02:00
public boolean isAir() {
2019-08-22 14:52:32 +02:00
return material == Material.AIR;
2019-08-13 17:52:09 +02:00
}
2020-02-13 15:14:41 +01:00
/**
* Do not take amount in consideration
*
* @param itemStack
* @return
*/
2019-08-13 17:52:09 +02:00
public boolean isSimilar(ItemStack itemStack) {
2019-09-06 16:05:36 +02:00
return itemStack.getMaterial() == material &&
itemStack.getDisplayName() == displayName &&
itemStack.isUnbreakable() == unbreakable &&
2020-02-13 15:14:41 +01:00
itemStack.getDamage() == damage &&
2020-02-16 19:11:36 +01:00
itemStack.getStackingRule() == stackingRule &&
2020-02-13 15:14:41 +01:00
itemStack.getData() == data;
2019-08-13 17:52:09 +02:00
}
public byte getAmount() {
return amount;
2019-08-12 08:30:59 +02:00
}
2019-09-06 16:05:36 +02:00
public short getDamage() {
return damage;
}
2019-08-22 14:52:32 +02:00
public Material getMaterial() {
return material;
2019-08-12 08:30:59 +02:00
}
2019-08-13 17:52:09 +02:00
public void setAmount(byte amount) {
this.amount = amount;
}
2019-09-06 16:05:36 +02:00
public void setDamage(short damage) {
this.damage = damage;
}
2019-08-22 14:52:32 +02:00
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
2020-02-13 15:14:41 +01:00
public boolean hasDisplayName() {
return displayName != null;
}
public ArrayList<String> getLore() {
return lore;
}
public void setLore(ArrayList<String> lore) {
this.lore = lore;
}
public boolean hasLore() {
return lore != null && !lore.isEmpty();
}
2019-08-22 14:52:32 +02:00
public boolean isUnbreakable() {
return unbreakable;
}
public void setUnbreakable(boolean unbreakable) {
this.unbreakable = unbreakable;
}
2020-02-13 15:14:41 +01:00
public boolean hasNbtTag() {
return hasDisplayName() || hasLore() || isUnbreakable();
}
2019-08-13 17:52:09 +02:00
public ItemStack clone() {
2019-09-06 16:05:36 +02:00
ItemStack itemStack = new ItemStack(material, amount, damage);
2019-08-22 14:52:32 +02:00
itemStack.setDisplayName(displayName);
itemStack.setUnbreakable(unbreakable);
2020-02-16 19:11:36 +01:00
itemStack.setLore(getLore());
itemStack.setStackingRule(getStackingRule());
2020-02-09 15:34:09 +01:00
Data data = getData();
if (data != null)
itemStack.setData(data.clone());
2019-08-22 14:52:32 +02:00
return itemStack;
2019-08-12 08:30:59 +02:00
}
2019-08-29 02:15:52 +02:00
2020-02-16 19:11:36 +01:00
public StackingRule getStackingRule() {
return stackingRule;
}
public void setStackingRule(StackingRule stackingRule) {
if (stackingRule == null)
throw new NullPointerException("StackingRule cannot be null!");
this.stackingRule = stackingRule;
}
2019-08-29 02:15:52 +02:00
@Override
public Data getData() {
return data;
}
@Override
public void setData(Data data) {
this.data = data;
}
2020-02-17 17:33:53 +01:00
public static StackingRule getDefaultStackingRule() {
return defaultStackingRule;
}
public static void setDefaultStackingRule(StackingRule defaultStackingRule) {
if (defaultStackingRule == null)
throw new NullPointerException("StackingRule cannot be null!");
ItemStack.defaultStackingRule = defaultStackingRule;
}
2019-08-12 08:30:59 +02:00
}