aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/de/hysky/skyblocker/utils/datafixer
diff options
context:
space:
mode:
authorAaron <51387595+AzureAaron@users.noreply.github.com>2024-05-04 19:14:06 -0400
committerGitHub <noreply@github.com>2024-05-04 19:14:06 -0400
commit4a4234d7c9d4f038d4fa418fb15ef24ce3fcc501 (patch)
tree6e87c6b67aabeb82dbe075d68c16b5492ff92c9b /src/test/java/de/hysky/skyblocker/utils/datafixer
parented0489539902d77595625aaa3bca4e328e1f7e88 (diff)
parentf7b13895a4605e1d22e2c00e7b62c7365902d1aa (diff)
downloadSkyblocker-4a4234d7c9d4f038d4fa418fb15ef24ce3fcc501.tar.gz
Skyblocker-4a4234d7c9d4f038d4fa418fb15ef24ce3fcc501.tar.bz2
Skyblocker-4a4234d7c9d4f038d4fa418fb15ef24ce3fcc501.zip
Merge pull request #669 from SkyblockerMod/1.20.5
1.20.5 & 1.20.6
Diffstat (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer')
-rw-r--r--src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java
new file mode 100644
index 00000000..d791fd72
--- /dev/null
+++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java
@@ -0,0 +1,89 @@
+package de.hysky.skyblocker.utils.datafixer;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.mojang.serialization.JsonOps;
+
+import net.minecraft.Bootstrap;
+import net.minecraft.SharedConstants;
+import net.minecraft.component.DataComponentTypes;
+import net.minecraft.component.type.ItemEnchantmentsComponent;
+import net.minecraft.enchantment.Enchantments;
+import net.minecraft.item.ItemStack;
+import net.minecraft.item.Items;
+import net.minecraft.nbt.NbtCompound;
+import net.minecraft.nbt.StringNbtReader;
+import net.minecraft.util.Util;
+
+public class ItemStackComponentizationFixerTest {
+ private final NbtCompound NBT = convertToNbt("{id:\"minecraft:diamond_sword\",Count:1,tag:{ExtraAttributes:{id:\"TEST\"}}}");
+ private final Gson GSON = new Gson();
+ private final ItemStack TEST_STACK = Util.make(new ItemStack(Items.DIAMOND_SWORD, 1), item -> {
+ ItemEnchantmentsComponent.Builder builder = new ItemEnchantmentsComponent.Builder(ItemEnchantmentsComponent.DEFAULT);
+
+ builder.add(Enchantments.SHARPNESS, 1);
+ item.set(DataComponentTypes.ENCHANTMENTS, builder.build());
+ });
+
+ @BeforeAll
+ public static void setup() {
+ SharedConstants.createGameVersion();
+ Bootstrap.initialize();
+ }
+
+ @Test
+ void testNbtConversion() {
+ Assertions.assertNotEquals(NBT, new NbtCompound());
+ }
+
+ @Test
+ void testDataFixer() {
+ ItemStack fixedStack = ItemStackComponentizationFixer.fixUpItem(NBT);
+ JsonElement stackJson = ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, fixedStack).getOrThrow();
+
+ Assertions.assertEquals("{\"id\":\"minecraft:diamond_sword\",\"count\":1,\"components\":{\"minecraft:custom_data\":{\"ExtraAttributes\":{\"id\":\"TEST\"}}}}", GSON.toJson(stackJson));
+ }
+
+ @Test
+ void testComponentsAsString() {
+ String componentString = ItemStackComponentizationFixer.componentsAsString(TEST_STACK);
+
+ Assertions.assertEquals("[minecraft:enchantments={levels:{\"minecraft:sharpness\":1}}]", componentString);
+ }
+
+ @Test
+ void testFromComponentsString() {
+ String componentString = "[minecraft:enchantments={levels:{\"minecraft:sharpness\":1}}]";
+ ItemStack stack = ItemStackComponentizationFixer.fromComponentsString("minecraft:diamond_sword", 1, componentString);
+
+ Assertions.assertTrue(ItemStack.areItemsAndComponentsEqual(stack, TEST_STACK));
+ }
+
+ @Test
+ void testFromComponentsStringWithInvalidItem() {
+ String componentString = "[minecraft:enchantments={levels:{\"minecraft:sharpness\":1}}]";
+ ItemStack stack = ItemStackComponentizationFixer.fromComponentsString("minecraft:does_not_exist", 1, componentString);
+
+ Assertions.assertEquals(stack, ItemStack.EMPTY);
+ }
+
+ @Test
+ void testNbtToComponentsString() {
+ ItemStack fixedStack = ItemStackComponentizationFixer.fixUpItem(NBT);
+ String componentsString = ItemStackComponentizationFixer.componentsAsString(fixedStack);
+
+ Assertions.assertEquals("[minecraft:custom_data={ExtraAttributes:{id:\"TEST\"}}]", componentsString);
+ }
+
+ private static NbtCompound convertToNbt(String nbt) {
+ try {
+ return StringNbtReader.parse(nbt);
+ } catch (Exception e) {
+ return new NbtCompound();
+ }
+ }
+}