From 4e2924407645b04c30d4a2823a1d9d0983c2c790 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Sat, 2 Mar 2024 15:16:27 -0500 Subject: 24w09a --- .../ItemStackComponentizationFixerTest.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer') 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..11951c44 --- /dev/null +++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java @@ -0,0 +1,47 @@ +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.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.StringNbtReader; + +public class ItemStackComponentizationFixerTest { + private final NbtCompound NBT = convertToNbt("{id:\"minecraft:diamond_sword\",Count:1,tag:{ExtraAttributes:{id:\"TEST\"}}}"); + private final Gson GSON = new Gson(); + + @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).result().orElseThrow(); + + Assertions.assertEquals("{\"id\":\"minecraft:diamond_sword\",\"components\":{\"minecraft:custom_data\":{\"ExtraAttributes\":{\"id\":\"TEST\"}}}}", GSON.toJson(stackJson)); + } + + private static NbtCompound convertToNbt(String nbt) { + try { + return StringNbtReader.parse(nbt); + } catch (Exception e) { + return new NbtCompound(); + } + } +} -- cgit From 02ed1d456c7924753f4b1447818ae9986539a950 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:20:33 -0400 Subject: 24w12a --- .../skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer') diff --git a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java index 11951c44..82f33e6d 100644 --- a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java @@ -34,7 +34,7 @@ public class ItemStackComponentizationFixerTest { ItemStack fixedStack = ItemStackComponentizationFixer.fixUpItem(NBT); JsonElement stackJson = ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, fixedStack).result().orElseThrow(); - Assertions.assertEquals("{\"id\":\"minecraft:diamond_sword\",\"components\":{\"minecraft:custom_data\":{\"ExtraAttributes\":{\"id\":\"TEST\"}}}}", GSON.toJson(stackJson)); + Assertions.assertEquals("{\"id\":\"minecraft:diamond_sword\",\"count\":1,\"components\":{\"minecraft:custom_data\":{\"ExtraAttributes\":{\"id\":\"TEST\"}}}}", GSON.toJson(stackJson)); } private static NbtCompound convertToNbt(String nbt) { -- cgit From 44f1f0829a683217daba452faf01566abbd94a3e Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Fri, 22 Mar 2024 03:19:38 -0400 Subject: Add more item component data fixers --- .../ItemStackComponentizationFixerTest.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer') diff --git a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java index 82f33e6d..774f1735 100644 --- a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java @@ -10,13 +10,24 @@ 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() { @@ -36,6 +47,37 @@ public class ItemStackComponentizationFixerTest { 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 { -- cgit From df9c1b29f0ca35f97e1c74910f6d0e01c2ca6ccb Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Tue, 23 Apr 2024 20:18:43 -0400 Subject: Refactor usages of Optional's orElseThrow to getOrThrow Mojang's method is more concise and provides far superior error messages incase the value isn't present (like why it happened) whereas with Optionals its just the standard value not present message. --- .../skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer') diff --git a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java index 774f1735..99c6a744 100644 --- a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java @@ -43,7 +43,7 @@ public class ItemStackComponentizationFixerTest { @Test void testDataFixer() { ItemStack fixedStack = ItemStackComponentizationFixer.fixUpItem(NBT); - JsonElement stackJson = ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, fixedStack).result().orElseThrow(); + 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)); } -- cgit From d36e1ce0c7fbb0c2e7f04dc070c2a6a0ed764976 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 28 Apr 2024 15:07:28 -0400 Subject: Refactor ItemStackComponentizationFixer --- .../datafixer/ItemStackComponentizationFixerTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/test/java/de/hysky/skyblocker/utils/datafixer') diff --git a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java index 99c6a744..d791fd72 100644 --- a/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java @@ -24,7 +24,7 @@ public class ItemStackComponentizationFixerTest { 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()); }); @@ -47,35 +47,35 @@ public class ItemStackComponentizationFixerTest { 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); } -- cgit