aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java
blob: 2aca8f8e4e9342a5be1e7ec2e72b3e734948e48f (plain)
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
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.registry.RegistryKeys;
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(ItemStackComponentizationFixer.getRegistryLookup().getWrapperOrThrow(RegistryKeys.ENCHANTMENT).getOrThrow(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(ItemStackComponentizationFixer.getRegistryLookup().getOps(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();
		}
	}
}