-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemStackSerializerHelper.java
More file actions
134 lines (122 loc) · 5.79 KB
/
ItemStackSerializerHelper.java
File metadata and controls
134 lines (122 loc) · 5.79 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
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
package net.theevilreaper.aves.file.gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minestom.server.MinecraftServer;
import net.minestom.server.component.DataComponents;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.component.EnchantmentList;
import net.minestom.server.item.enchant.Enchantment;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.registry.RegistryKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import static net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer.plainText;
/**
* The {@link ItemStackSerializerHelper} is a internal interface which contains some methods to serialize or deserialize data from or to an {@link ItemStack}.
*
* @author theEvilReaper
* @version 1.0.0
* @since 1.0.0
*/
sealed interface ItemStackSerializerHelper permits ItemStackGsonTypeAdapter {
String DISPLAY_NAME = "displayName";
String LORE_KEY = "lore";
String ENCHANTMENTS = "enchantments";
/**
* Serializes the custom name from the {@link ItemStack} into the given {@link JsonObject}.
*
* @param stack the stack to serialize
* @param object the object to serialize the data
*/
default void serializeCustomName(ItemStack stack, JsonObject object) {
if (!stack.has(DataComponents.CUSTOM_NAME)) return;
final Component itemName = stack.get(DataComponents.CUSTOM_NAME, Component.empty());
final String displayName = plainText().serialize(itemName);
object.addProperty(DISPLAY_NAME, displayName);
}
/**
* Deserializes the custom name from the {@link JsonObject} into the {@link ItemStack.Builder}.
*
* @param stack the builder to set the custom name
* @param jsonObject the object to deserialize the data
*/
default void serializeLore(ItemStack stack, JsonObject jsonObject) {
if (!stack.has(DataComponents.LORE)) return;
final List<Component> loreLines = stack.get(DataComponents.LORE);
if (loreLines != null && !loreLines.isEmpty()) {
JsonArray loreArray = new JsonArray();
for (Component loreLine : loreLines) {
loreArray.add(plainText().serialize(loreLine));
}
jsonObject.add(LORE_KEY, loreArray);
}
}
/**
* Deserializes the lore from the {@link JsonObject} into the {@link ItemStack.Builder}.
*
* @param builder the builder to set the lore
* @param object the object to deserialize the data
* @return the builder with the lore set
*/
default ItemStack.Builder deserializeLore(ItemStack.Builder builder, JsonObject object) {
if (!object.has(LORE_KEY)) return builder;
JsonArray loreArray = object.getAsJsonArray(LORE_KEY);
List<Component> lore = new ArrayList<>();
for (JsonElement element : loreArray) {
lore.add(LegacyComponentSerializer.legacyAmpersand().deserialize(element.getAsString()));
}
return builder.lore(lore);
}
/**
* Serializes the enchantments from the {@link ItemStack} into the given {@link JsonObject}.
*
* @param stack the stack to serialize
* @param object the object to serialize the data
*/
default void serializeEnchantments(ItemStack stack, JsonObject object) {
if (!stack.has(DataComponents.ENCHANTMENTS)) return;
JsonArray enchantsArray = new JsonArray();
final EnchantmentList enchantmentList = stack.get(DataComponents.ENCHANTMENTS);
if (enchantmentList != null && !enchantmentList.enchantments().isEmpty()) {
Set<Map.Entry<RegistryKey<Enchantment>, Integer>> entries = enchantmentList.enchantments().entrySet();
for (Map.Entry<RegistryKey<Enchantment>, Integer> entry : entries) {
JsonObject enchantmentObject = new JsonObject();
enchantmentObject.addProperty("enchantment", entry.getKey().name());
enchantmentObject.addProperty("level", entry.getValue());
enchantsArray.add(enchantmentObject);
}
object.add(ENCHANTMENTS, enchantsArray);
}
}
/**
* Deserializes the enchantments from the {@link JsonObject} into the {@link ItemStack.Builder}.
*
* @param builder the builder to set the enchantments
* @param object the object to deserialize the data
* @return the builder with the enchantments set
*/
default ItemStack.Builder deserializeEnchantments(ItemStack.Builder builder, JsonObject object) {
if (!object.has(ENCHANTMENTS)) return builder;
JsonArray enchantsArray = object.getAsJsonArray(ENCHANTMENTS);
Map<RegistryKey<Enchantment>, Integer> enchantments = new HashMap<>();
for (JsonElement enchantElement : enchantsArray) {
final JsonObject enchantObject = (JsonObject) enchantElement;
String nameSpace = enchantObject.get("enchantment").getAsString();
var level = enchantObject.get("level").getAsInt();
DynamicRegistry<Enchantment> enchantmentRegistry = MinecraftServer.getEnchantmentRegistry();
Enchantment rawEnchantment = enchantmentRegistry.get(Key.key(nameSpace));
if (rawEnchantment == null) continue;
RegistryKey<Enchantment> enchantment = enchantmentRegistry.getKey(rawEnchantment);
enchantments.putIfAbsent(enchantment, level);
}
EnchantmentList enchantmentList = new EnchantmentList(enchantments);
return builder.set(DataComponents.ENCHANTMENTS, enchantmentList);
}
}