-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemStackIngredient.java
More file actions
82 lines (69 loc) · 2.52 KB
/
ItemStackIngredient.java
File metadata and controls
82 lines (69 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package fr.traqueur.recipes.impl.domains.ingredients;
import fr.traqueur.recipes.api.domains.Ingredient;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Objects;
/**
* This class represents an ingredient that is an item stack
*/
public class ItemStackIngredient extends Ingredient {
/**
* The item of the ingredient
*/
protected final ItemStack item;
/**
* Create a new ItemStackIngredient
* @param item The item of the ingredient
* @param sign The sign of the ingredient
*/
public ItemStackIngredient(ItemStack item, Character sign) {
super(sign);
this.item = item;
}
/**
* Create a new ItemStackIngredient
* @param item The item of the ingredient
*/
public ItemStackIngredient(ItemStack item) {
this(item, null);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSimilar(ItemStack item) {
return item.getType() == this.item.getType()
&& item.getAmount() >= this.item.getAmount()
&& item.hasItemMeta() == this.item.hasItemMeta()
&& (!item.hasItemMeta() || similarMeta(item.getItemMeta(), this.item.getItemMeta()));
}
/**
* Check if the meta of the two items are similar
* @param sourceMeta The source meta
* @param ingredientMeta The ingredient meta
* @return True if the meta of the two items are similar
*/
private boolean similarMeta(ItemMeta sourceMeta, ItemMeta ingredientMeta) {
for (NamespacedKey key : sourceMeta.getPersistentDataContainer().getKeys()) {
if (!ingredientMeta.getPersistentDataContainer().has(key)) {
System.out.println("Key " + key + " not found in ingredient meta");
return false;
}
}
boolean lore = sourceMeta.hasLore() == ingredientMeta.hasLore() && (!sourceMeta.hasLore()
|| Objects.equals(sourceMeta.getLore(), ingredientMeta.getLore()));
boolean customData = sourceMeta.hasCustomModelData() == ingredientMeta.hasCustomModelData()
&& (!sourceMeta.hasCustomModelData()
|| sourceMeta.getCustomModelData() == ingredientMeta.getCustomModelData());
return lore && customData;
}
/**
* {@inheritDoc}
*/
@Override
public RecipeChoice choice() {
return new RecipeChoice.MaterialChoice(this.item.getType());
}
}