Skip to content

Commit 2cba052

Browse files
committed
Adds custom data.
1 parent f0ab985 commit 2cba052

7 files changed

Lines changed: 61 additions & 31 deletions

File tree

api/src/main/java/kr/toxicity/libraries/datacomponent/api/DataComponentType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@SuppressWarnings("unused")
1515
public interface DataComponentType<T> {
1616

17+
Supplier<DataComponentType<CustomData>> CUSTOM_DATA = () -> nms().customData();
1718
Supplier<DataComponentType<Integer>> MAX_STACK_SIZE = () -> nms().maxStackSize();
1819
Supplier<DataComponentType<Integer>> MAX_DAMAGE = () -> nms().maxDamage();
1920
Supplier<DataComponentType<Integer>> DAMAGE = () -> nms().damage();

api/src/main/java/kr/toxicity/libraries/datacomponent/api/NMS.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface NMS {
1717
@NotNull
1818
Registry<? extends DataComponentType<?>> componentRegistry();
1919

20+
@NotNull
21+
DataComponentType<CustomData> customData();
2022
@NotNull
2123
DataComponentType<Integer> maxStackSize();
2224
@NotNull

api/src/main/java/kr/toxicity/libraries/datacomponent/api/wrapper/CustomData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
public record CustomData(@NotNull CompoundTag tag) implements ComponentData<CustomData> {
88
@Override
99
public Codec<CustomData> codec() {
10-
return NMS.nms().entityData().codec();
10+
return NMS.nms().customData().codec();
1111
}
1212
}

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ allprojects {
1111
apply(plugin = "java")
1212

1313
group = "kr.toxicity.libraries.datacomponent"
14-
version = "1.0.10"
14+
version = "1.0.11"
1515

1616
repositories {
1717
mavenCentral()

nms/v1_20_R4/src/main/java/kr/toxicity/libraries/datacomponent/nms/v1_20_R4/DataComponentTypeImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ final class DataComponentTypeImpl<T, R> implements kr.toxicity.libraries.datacom
3838
}
3939
};
4040

41+
static final DataComponentTypeImpl<CustomData, net.minecraft.world.item.component.CustomData> CUSTOM_DATA = register(
42+
"custom_data",
43+
DataComponents.CUSTOM_DATA,
44+
Converters.CUSTOM_DATA,
45+
CodecImpl.CUSTOM_DATA
46+
);
4147
static final DataComponentTypeImpl<Integer, Integer> MAX_STACK_SIZE = register(
4248
"max_stack_size",
4349
DataComponents.MAX_STACK_SIZE,

nms/v1_20_R4/src/main/java/kr/toxicity/libraries/datacomponent/nms/v1_20_R4/NMSImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public final class NMSImpl implements NMS {
1818
return new ItemAdapterImpl(itemStack);
1919
}
2020

21+
@Override
22+
public @NotNull DataComponentType<CustomData> customData() {
23+
return DataComponentTypeImpl.CUSTOM_DATA;
24+
}
25+
2126
@Override
2227
public @NotNull Registry<? extends DataComponentType<?>> componentRegistry() {
2328
return DataComponentTypeImpl.REGISTRY;
Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
11
package kr.toxicity.test;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
35
import com.google.gson.JsonParser;
6+
import com.google.gson.stream.JsonWriter;
47
import kr.toxicity.libraries.datacomponent.DataComponentAPIBukkit;
58
import kr.toxicity.libraries.datacomponent.api.DataComponentAPI;
69
import kr.toxicity.libraries.datacomponent.api.DataComponentType;
710
import kr.toxicity.libraries.datacomponent.api.NMS;
8-
import kr.toxicity.libraries.datacomponent.api.wrapper.Rarity;
11+
import kr.toxicity.libraries.datacomponent.api.wrapper.*;
912
import net.kyori.adventure.text.Component;
1013
import org.bukkit.Material;
14+
import org.bukkit.configuration.file.YamlConfiguration;
1115
import org.bukkit.inventory.ItemStack;
1216
import org.bukkit.plugin.java.JavaPlugin;
1317

18+
import java.io.BufferedWriter;
19+
import java.io.File;
20+
import java.io.FileWriter;
21+
import java.util.HashMap;
22+
1423
public class TestPlugin extends JavaPlugin {
1524
@Override
1625
public void onEnable() {
1726
DataComponentAPIBukkit.load();
1827

19-
var apply = DataComponentAPI.api().adapter(new ItemStack(Material.DIAMOND_SWORD));
28+
var apply = DataComponentAPI.api().adapter(new ItemStack(Material.NETHERITE_SWORD));
2029
apply.set(DataComponentType.DAMAGE, 3);
21-
apply.set(DataComponentType.REPAIR_COST, 20);
22-
apply.set(DataComponentType.RARITY, Rarity.EPIC);
23-
getLogger().info(apply.serialize().toString());
24-
25-
// Serialization.
26-
var data = DataComponentAPI.api().deserializer().deserialize(
27-
JsonParser.parseString("{\"damage\":3,\"max_stack_size\":1,\"repair_cost\":20,\"tool\":{\"rules\":[{\"blocks\":\"minecraft:cobweb\",\"speed\":15.0,\"correct_for_drops\":true},{\"blocks\":\"#minecraft:sword_efficient\",\"speed\":1.5}],\"damage_per_block\":2},\"max_damage\":1561,\"item_lore\":[],\"rarity\":\"epic\"}").getAsJsonObject()
28-
);
29-
var diamond = DataComponentAPI.api().adapter(new ItemStack(Material.DIAMOND));
30-
getLogger().info(diamond.serialize().toString());
31-
data.set(diamond);
32-
getLogger().info(diamond.serialize().toString());
33-
34-
var value = data.get(NMS.nms().rarity());
35-
if (value != null) getLogger().info(value.name());
36-
37-
for (Material material : Material.values()) {
38-
if (material.isItem()) {
39-
try {
40-
DataComponentAPI.api().adapter(new ItemStack(material)).serialize();
41-
} catch (Exception e) {
42-
getLogger().info(material.name());
43-
}
44-
}
30+
apply.set(DataComponentType.REPAIR_COST, 10);
31+
apply.set(DataComponentType.ENCHANTMENT_GLINT_OVERRIDE, false);
32+
apply.set(DataComponentType.ITEM_NAME, Component.text("Hello"));
33+
apply.set(DataComponentType.CUSTOM_NAME, Component.text("World"));
34+
apply.set(DataComponentType.CUSTOM_MODEL_DATA, new CustomModelData(1));
35+
36+
var map = new HashMap<String, Tag<?>>();
37+
map.put("a", new Tag<>(Tag.INT, 1));
38+
map.put("b", new Tag<>(Tag.STRING, "ddd"));
39+
map.put("c", new Tag<>(Tag.INT_ARRAY, new int[] {1, 2, 3}));
40+
apply.set(DataComponentType.CUSTOM_DATA, new CustomData(new CompoundTag(map)));
41+
42+
var get = apply.build();
43+
44+
var data = getDataFolder();
45+
if (!data.exists()) data.mkdirs();
46+
47+
var result = new File(data, "result.json");
48+
var compare = new File(data, "compare.yml");
49+
try (JsonWriter writer = new JsonWriter(new BufferedWriter(new FileWriter(result)))) {
50+
writer.setIndent(" ");
51+
var itemJson = new JsonObject();
52+
itemJson.addProperty("type", Material.NETHERITE_SWORD.name());
53+
itemJson.add("components", apply.serialize());
54+
new Gson().toJson(itemJson, writer);
55+
} catch (Exception e) {
56+
57+
}
58+
try {
59+
var yaml = new YamlConfiguration();
60+
yaml.set("item" , get);
61+
yaml.save(compare);
62+
} catch (Exception e) {
63+
4564
}
46-
var test = DataComponentAPI.api().adapter(new ItemStack(Material.DIAMOND));
47-
test.set(NMS.nms().customName(), Component.empty());
48-
System.out.println(test.serialize());
4965
}
5066
}

0 commit comments

Comments
 (0)