-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ItemHelperImpl.java
338 lines (307 loc) · 15.2 KB
/
ItemHelperImpl.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.denizenscript.denizen.nms.v1_16.helpers;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizen.nms.util.jnbt.*;
import com.denizenscript.denizen.nms.util.jnbt.Tag;
import com.denizenscript.denizen.nms.v1_16.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.interfaces.ItemHelper;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.server.v1_16_R3.*;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryPlayer;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.*;
public class ItemHelperImpl extends ItemHelper {
public static IRecipe<?> getNMSRecipe(NamespacedKey key) {
MinecraftKey nmsKey = CraftNamespacedKey.toMinecraft(key);
for (Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>> recipeMap : ((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().recipes.values()) {
IRecipe<?> recipe = recipeMap.get(nmsKey);
if (recipe != null) {
return recipe;
}
}
return null;
}
public void setMaxStackSize(Material material, int size) {
try {
ReflectionHelper.getFinalSetter(Material.class, "maxStack").invoke(material, size);
ReflectionHelper.getFinalSetter(Item.class, "maxStackSize").invoke(IRegistry.ITEM.get(MinecraftKey.a(material.getKey().getKey())), size);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
@Override
public Integer burnTime(Material material) {
return TileEntityFurnace.f().get(CraftMagicNumbers.getItem(material));
}
@Override
public Recipe getRecipeById(NamespacedKey key) {
IRecipe<?> recipe = getNMSRecipe(key);
if (recipe == null) {
return null;
}
return recipe.toBukkitRecipe();
}
@Override
public void removeRecipe(NamespacedKey key) {
MinecraftKey nmsKey = CraftNamespacedKey.toMinecraft(key);
for (Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>> recipeMap : ((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().recipes.values()) {
recipeMap.remove(nmsKey);
}
}
@Override
public void clearDenizenRecipes() {
for (Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>> recipeMap : ((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().recipes.values()) {
for (MinecraftKey key : new ArrayList<>(recipeMap.keySet())) {
if (key.getNamespace().equalsIgnoreCase("denizen")) {
recipeMap.remove(key);
}
}
}
}
@Override
public void setShapedRecipeIngredient(ShapedRecipe recipe, char c, ItemStack[] item, boolean exact) {
if (item.length == 1 && item[0].getType() == Material.AIR) {
recipe.setIngredient(c, new RecipeChoice.MaterialChoice(Material.AIR));
}
else if (exact) {
recipe.setIngredient(c, new RecipeChoice.ExactChoice(item));
}
else {
Material[] mats = new Material[item.length];
for (int i = 0; i < item.length; i++) {
mats[i] = item[i].getType();
}
recipe.setIngredient(c, new RecipeChoice.MaterialChoice(mats));
}
}
public static RecipeItemStack itemArrayToRecipe(ItemStack[] items, boolean exact) {
RecipeItemStack.StackProvider[] stacks = new RecipeItemStack.StackProvider[items.length];
for (int i = 0; i < items.length; i++) {
stacks[i] = new RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(items[i]));
}
RecipeItemStack itemRecipe = new RecipeItemStack(Arrays.stream(stacks));
itemRecipe.exact = exact;
return itemRecipe;
}
@Override
public void registerFurnaceRecipe(String keyName, String group, ItemStack result, ItemStack[] ingredient, float exp, int time, String type, boolean exact) {
MinecraftKey key = new MinecraftKey("denizen", keyName);
RecipeItemStack itemRecipe = itemArrayToRecipe(ingredient, exact);
RecipeCooking recipe;
if (type.equalsIgnoreCase("smoker")) {
recipe = new RecipeSmoking(key, group, itemRecipe, CraftItemStack.asNMSCopy(result), exp, time);
}
else if (type.equalsIgnoreCase("blast")) {
recipe = new RecipeBlasting(key, group, itemRecipe, CraftItemStack.asNMSCopy(result), exp, time);
}
else if (type.equalsIgnoreCase("campfire")) {
recipe = new RecipeCampfire(key, group, itemRecipe, CraftItemStack.asNMSCopy(result), exp, time);
}
else {
recipe = new FurnaceRecipe(key, group, itemRecipe, CraftItemStack.asNMSCopy(result), exp, time);
}
((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().addRecipe(recipe);
}
@Override
public void registerStonecuttingRecipe(String keyName, String group, ItemStack result, ItemStack[] ingredient, boolean exact) {
MinecraftKey key = new MinecraftKey("denizen", keyName);
RecipeItemStack itemRecipe = itemArrayToRecipe(ingredient, exact);
RecipeStonecutting recipe = new RecipeStonecutting(key, group, itemRecipe, CraftItemStack.asNMSCopy(result));
((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().addRecipe(recipe);
}
@Override
public void registerShapelessRecipe(String keyName, String group, ItemStack result, List<ItemStack[]> ingredients, boolean[] exact) {
MinecraftKey key = new MinecraftKey("denizen", keyName);
ArrayList<RecipeItemStack> ingredientList = new ArrayList<>();
for (int i = 0; i < ingredients.size(); i++) {
ingredientList.add(itemArrayToRecipe(ingredients.get(i), exact[i]));
}
ShapelessRecipes recipe = new ShapelessRecipes(key, group, CraftItemStack.asNMSCopy(result), NonNullList.a(null, ingredientList.toArray(new RecipeItemStack[0])));
((CraftServer) Bukkit.getServer()).getServer().getCraftingManager().addRecipe(recipe);
}
@Override
public String getInternalNameFromMaterial(Material material) {
// In 1.13+ Material names match their internal name
return "minecraft:" + CoreUtilities.toLowerCase(material.name());
}
@Override
public Material getMaterialFromInternalName(String internalName) {
return Material.matchMaterial(internalName);
}
@Override
public String getJsonString(ItemStack itemStack) {
String json = CraftItemStack.asNMSCopy(itemStack).C().getChatModifier().toString().replace("\\", "\\\\").replace("\"", "\\\"");
return json.substring(176, json.length() - 185);
}
@Override
public String getRawHoverText(ItemStack itemStack) {
NBTTagCompound tag = CraftItemStack.asNMSCopy(itemStack).getTag();
if (tag == null) {
return null;
}
return tag.toString();
}
@Override
public PlayerProfile getSkullSkin(ItemStack is) {
net.minecraft.server.v1_16_R3.ItemStack itemStack = CraftItemStack.asNMSCopy(is);
if (itemStack.hasTag()) {
NBTTagCompound tag = itemStack.getTag();
if (tag.hasKeyOfType("SkullOwner", 10)) {
GameProfile profile = GameProfileSerializer.deserialize(tag.getCompound("SkullOwner"));
if (profile != null) {
Property property = Iterables.getFirst(profile.getProperties().get("textures"), null);
return new PlayerProfile(profile.getName(), profile.getId(),
property != null ? property.getValue() : null,
property != null ? property.getSignature() : null);
}
}
}
return null;
}
@Override
public ItemStack setSkullSkin(ItemStack itemStack, PlayerProfile playerProfile) {
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
if (playerProfile.hasTexture()) {
gameProfile.getProperties().get("textures").clear();
if (playerProfile.getTextureSignature() != null) {
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
}
else {
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture()));
}
}
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagCompound tag = nmsItemStack.hasTag() ? nmsItemStack.getTag() : new NBTTagCompound();
tag.set("SkullOwner", GameProfileSerializer.serialize(new NBTTagCompound(), gameProfile));
nmsItemStack.setTag(tag);
return CraftItemStack.asBukkitCopy(nmsItemStack);
}
@Override
public ItemStack addNbtData(ItemStack itemStack, String key, Tag value) {
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagCompound tag = nmsItemStack.hasTag() ? nmsItemStack.getTag() : new NBTTagCompound();
CompoundTag compound = CompoundTagImpl.fromNMSTag(tag).createBuilder().put(key, value).build();
nmsItemStack.setTag(((CompoundTagImpl) compound).toNMSTag());
return CraftItemStack.asBukkitCopy(nmsItemStack);
}
@Override
public CompoundTag getNbtData(ItemStack itemStack) {
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
if (nmsItemStack != null && nmsItemStack.hasTag()) {
return CompoundTagImpl.fromNMSTag(nmsItemStack.getTag());
}
return new CompoundTagImpl(new HashMap<>());
}
@Override
public ItemStack setNbtData(ItemStack itemStack, CompoundTag compoundTag) {
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
nmsItemStack.setTag(((CompoundTagImpl) compoundTag).toNMSTag());
return CraftItemStack.asBukkitCopy(nmsItemStack);
}
@Override
public PotionEffect getPotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, Color color, boolean icon) {
return new PotionEffect(type, duration, amplifier, ambient, particles, icon);
}
@Override
public void setInventoryItem(Inventory inventory, ItemStack item, int slot) {
if (inventory instanceof CraftInventoryPlayer && ((CraftInventoryPlayer) inventory).getInventory().player == null) {
((CraftInventoryPlayer) inventory).getInventory().setItem(slot, CraftItemStack.asNMSCopy(item));
}
else {
inventory.setItem(slot, item);
}
}
@Override
public IntArrayTag convertUuidToNbt(UUID id) {
return new IntArrayTag(GameProfileSerializer.a(id).getInts());
}
@Override
public UUID convertNbtToUuid(IntArrayTag id) {
return GameProfileSerializer.a(new NBTTagIntArray(id.getValue()));
}
@Override
public String getDisplayName(ItemTag item) {
if (!item.getItemMeta().hasDisplayName()) {
return null;
}
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
String jsonText = ((NBTTagCompound) nmsItemStack.getTag().get("display")).getString("Name");
BaseComponent[] nameComponent = ComponentSerializer.parse(jsonText);
return FormattedTextHelper.stringify(nameComponent, ChatColor.WHITE);
}
@Override
public List<String> getLore(ItemTag item) {
if (!item.getItemMeta().hasLore()) {
return null;
}
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
NBTTagList list = ((NBTTagCompound) nmsItemStack.getTag().get("display")).getList("Lore", 8);
List<String> outList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
BaseComponent[] lineComponent = ComponentSerializer.parse(list.getString(i));
outList.add(FormattedTextHelper.stringify(lineComponent, ChatColor.WHITE));
}
return outList;
}
@Override
public void setDisplayName(ItemTag item, String name) {
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
NBTTagCompound tag = nmsItemStack.getOrCreateTag();
NBTTagCompound display = tag.getCompound("display");
if (!tag.hasKey("display")) {
tag.set("display", display);
}
if (name == null || name.isEmpty()) {
display.set("Name", null);
return;
}
BaseComponent[] components = FormattedTextHelper.parse(name, ChatColor.WHITE);
display.set("Name", NBTTagString.a(ComponentSerializer.toString(components)));
item.setItemStack(CraftItemStack.asBukkitCopy(nmsItemStack));
}
@Override
public void setLore(ItemTag item, List<String> lore) {
net.minecraft.server.v1_16_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
NBTTagCompound tag = nmsItemStack.getOrCreateTag();
NBTTagCompound display = tag.getCompound("display");
if (!tag.hasKey("display")) {
tag.set("display", display);
}
if (lore == null || lore.isEmpty()) {
display.set("Lore", null);
}
else {
NBTTagList tagList = new NBTTagList();
for (String line : lore) {
tagList.add(NBTTagString.a(ComponentSerializer.toString(FormattedTextHelper.parse(line, ChatColor.WHITE))));
}
display.set("Lore", tagList);
}
item.setItemStack(CraftItemStack.asBukkitCopy(nmsItemStack));
}
}