-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemKnowledgeBookRecipes.java
121 lines (102 loc) · 3.78 KB
/
ItemKnowledgeBookRecipes.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.KnowledgeBookMeta;
import java.util.ArrayList;
import java.util.List;
public class ItemKnowledgeBookRecipes implements Property {
public static boolean describes(ObjectTag item) {
return item instanceof ItemTag && ((ItemTag) item).getItemStack().getType() == Material.KNOWLEDGE_BOOK;
}
public static ItemKnowledgeBookRecipes getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemKnowledgeBookRecipes((ItemTag) _item);
}
}
public static final String[] handledTags = new String[] {
"knowledge_book_recipes"
};
public static final String[] handledMechs = new String[] {
"knowledge_book_recipes"
};
private ItemKnowledgeBookRecipes(ItemTag _item) {
item = _item;
}
public ListTag recipeList() {
ListTag output = new ListTag();
ItemStack itemStack = item.getItemStack();
if (itemStack.hasItemMeta() && itemStack.getItemMeta() instanceof KnowledgeBookMeta) {
for (NamespacedKey key : ((KnowledgeBookMeta) itemStack.getItemMeta()).getRecipes()) {
output.add(key.toString());
}
}
return output;
}
ItemTag item;
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.knowledge_book_recipes>
// @returns ListTag
// @mechanism ItemTag.knowledge_book_recipes
// @group properties
// @description
// Returns a recipes unlocked by this knowledge book. Recipes are in the Namespace:Key format, for example "minecraft:gold_nugget".
// These keys are not necessarily 1:1 with material names, as seen in the example "minecraft:gold_ingot_from_nuggets".
// -->
if (attribute.startsWith("knowledge_book_recipes")) {
return recipeList().getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
ListTag recipes = recipeList();
if (recipes.size() > 0) {
return recipes.identify();
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "knowledge_book_recipes";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name knowledge_book_recipes
// @input ListTag
// @description
// Sets the item's knowledge book recipe list, in the Namespace:Key format.
// @tags
// <ItemTag.knowledge_book_recipes>
// -->
if (mechanism.matches("knowledge_book_recipes")) {
KnowledgeBookMeta meta = (KnowledgeBookMeta) item.getItemMeta();
List<NamespacedKey> recipes = new ArrayList<>();
ListTag newRecipes = mechanism.valueAsType(ListTag.class);
for (String str : newRecipes) {
recipes.add(Utilities.parseNamespacedKey(str));
}
meta.setRecipes(recipes);
item.getItemStack().setItemMeta(meta);
}
}
}