-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemScriptContainer.java
191 lines (162 loc) · 6.96 KB
/
ItemScriptContainer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package net.aufdemrand.denizen.scripts.containers.core;
import java.util.ArrayList;
import java.util.List;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.LeatherColorer;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemScriptContainer extends ScriptContainer {
// <--[language]
// @name Item Script Containers
// @description
// Item script containers are an easy way to pre-define custom items for use within scripts. Item
// scripts work with the dItem object, and can be fetched with the Object Fetcher by using the
// dItem constructor i@item_script_name. Example:
// - drop <player.location> i@super_dooper_diamond
//
// The following is the format for the container. All keys are optional.
// <code>
// Item Script Name:
// type: item
//
// display name: custom name
// lore:
// - item
// - ...
// enchantments:
// - enchantment_name:level
// - ...
// recipe:
// - m@material|m@material|m@material
// - m@material|m@material|m@material
// - m@material|m@material|m@material
// bound: true # Bound items cannot be dropped
// color: c@color # Only colorable items (such as leather)
// book: book_script_name # Only i@written_book types
// </code>
//
// -->
dNPC npc = null;
dPlayer player = null;
public boolean bound = false;
List<dMaterial> recipe = null;
public ItemScriptContainer(ConfigurationSection configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
ItemScriptHelper.item_scripts.put(getName(), this);
// Set Recipe
if (contains("RECIPE")) {
recipe = new ArrayList<dMaterial>();
for (String recipeRow : getStringList("RECIPE")) {
recipeRow = TagManager.tag(player, npc, recipeRow);
String[] row = recipeRow.split("\\|", 3);
for (String material : row) {
recipe.add(recipe.size(), dMaterial.valueOf(material));
}
}
ShapedRecipe shapedRecipe = new ShapedRecipe(getItemFrom().getItemStack());
shapedRecipe.shape("abc", "def", "ghi");
char x = 'a';
for (dMaterial material : recipe) {
if (!material.getMaterial().name().equals("AIR"))
shapedRecipe.setIngredient(x, material.getMaterialData());
x++;
}
Bukkit.getServer().addRecipe(shapedRecipe);
}
}
public dItem getItemFrom() {
return getItemFrom(null, null);
}
public List<dMaterial> getRecipe() {
return recipe;
}
public dItem getItemFrom(dPlayer player, dNPC npc) {
// Try to use this script to make an item.
dItem stack = null;
try {
// Check validity of material
if (contains("MATERIAL")){
String material = TagManager.tag(player, npc, getString("MATERIAL"));
stack = dItem.valueOf(material);
}
// Make sure we're working with a valid base ItemStack
if (stack == null) return null;
ItemMeta meta = stack.getItemStack().getItemMeta();
List<String> lore = new ArrayList<String>();
// Set Id of the first, invisible lore
lore.add(dItem.itemscriptIdentifier + getName());
// Set Display Name
if (contains("DISPLAY NAME")){
String displayName = TagManager.tag(player, npc, getString("DISPLAY NAME"));
meta.setDisplayName(displayName);
}
// Set if the object is bound to the player
if (contains("BOUND")) {
bound = Boolean.valueOf(TagManager.tag(player, npc, getString("BOUND")));
}
// Set Lore
if (contains("LORE")) {
for (String l : getStringList("LORE")){
l = TagManager.tag(player, npc, l);
lore.add(l);
}
}
meta.setLore(lore);
stack.getItemStack().setItemMeta(meta);
// Set Enchantments
if (contains("ENCHANTMENTS")) {
for (String enchantment : getStringList("ENCHANTMENTS")) {
enchantment = TagManager.tag(player, npc, enchantment);
try {
// Build enchantment context
int level = 1;
if (enchantment.split(":").length > 1) {
level = Integer.valueOf(enchantment.split(":")[1]);
enchantment = enchantment.split(":")[0];
}
// Add enchantment
Enchantment ench = Enchantment.getByName(enchantment.toUpperCase());
stack.getItemStack().addUnsafeEnchantment(ench, level);
} catch (Exception e) {
dB.echoError("While constructing '" + getName() + "', there has been a problem. '" + enchantment + "' is an invalid Enchantment!");
}
}
}
// Set Color
if (contains("COLOR"))
{
String color = TagManager.tag(player, npc, getString("COLOR"));
LeatherColorer.colorArmor(stack, color);
}
// Set Book
if (contains("BOOK")) {
BookScriptContainer book = ScriptRegistry
.getScriptContainerAs(getString("BOOK").replace("s@", ""), BookScriptContainer.class);
stack = book.writeBookTo(stack, player, npc);
}
} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this item script!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
stack = null;
}
return stack;
}
public void setNPC(dNPC npc) {
this.npc = npc;
}
public void setPlayer(dPlayer player) {
this.player = player;
}
}