diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-04-28 15:07:28 -0400 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-04-28 15:07:46 -0400 |
commit | d36e1ce0c7fbb0c2e7f04dc070c2a6a0ed764976 (patch) | |
tree | 4845c4d22fd5cc1b230d363f073be01db430bc04 /src | |
parent | 45bcbe967ac58a2dc5ef606381e1653003ac17e3 (diff) | |
download | Skyblocker-d36e1ce0c7fbb0c2e7f04dc070c2a6a0ed764976.tar.gz Skyblocker-d36e1ce0c7fbb0c2e7f04dc070c2a6a0ed764976.tar.bz2 Skyblocker-d36e1ce0c7fbb0c2e7f04dc070c2a6a0ed764976.zip |
Refactor ItemStackComponentizationFixer
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java | 22 | ||||
-rw-r--r-- | src/test/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixerTest.java | 18 |
2 files changed, 19 insertions, 21 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java index fa873b84..3543a2f1 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -1,9 +1,9 @@ package de.hysky.skyblocker.utils.datafixer; +import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; import com.mojang.brigadier.StringReader; import com.mojang.serialization.Dynamic; @@ -24,8 +24,8 @@ import net.minecraft.util.Identifier; /** * Contains a data fixer to convert legacy item NBT to the new components system, among other fixers related to the item components system. - * - * @see {@link net.minecraft.datafixer.fix.ItemStackComponentizationFix} + * + * @see net.minecraft.datafixer.fix.ItemStackComponentizationFix */ public class ItemStackComponentizationFixer { private static final int ITEM_NBT_DATA_VERSION = 3817; @@ -33,7 +33,7 @@ public class ItemStackComponentizationFixer { private static final DynamicRegistryManager REGISTRY_MANAGER = new DynamicRegistryManager.ImmutableImpl(List.of(Registries.ITEM, Registries.DATA_COMPONENT_TYPE)); public static ItemStack fixUpItem(NbtCompound nbt) { - Dynamic<NbtElement> dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<NbtElement>(NbtOps.INSTANCE, nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); + Dynamic<NbtElement> dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<>(NbtOps.INSTANCE, nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); return ItemStack.CODEC.parse(dynamic).getOrThrow(); } @@ -46,25 +46,23 @@ public class ItemStackComponentizationFixer { public static String componentsAsString(ItemStack stack) { RegistryOps<NbtElement> nbtRegistryOps = REGISTRY_MANAGER.getOps(NbtOps.INSTANCE); - String componentsString = stack.getComponentChanges().entrySet().stream().flatMap(entry -> { + return Arrays.toString(stack.getComponentChanges().entrySet().stream().map(entry -> { @SuppressWarnings("unchecked") DataComponentType<Object> dataComponentType = (DataComponentType<Object>) entry.getKey(); Identifier componentId = Registries.DATA_COMPONENT_TYPE.getId(dataComponentType); Optional<NbtElement> encodedComponent = dataComponentType.getCodec().encodeStart(nbtRegistryOps, entry.getValue().orElseThrow()).result(); if (componentId == null || encodedComponent.isEmpty()) { - return Stream.empty(); + return null; } - return Stream.of(componentId.toString() + "=" + encodedComponent.orElseThrow()); - }).collect(Collectors.joining(String.valueOf(','))); - - return "[" + componentsString + "]"; + return componentId + "=" + encodedComponent.orElseThrow(); + }).filter(Objects::nonNull).toArray()); } /** * Constructs an {@link ItemStack} from an {@code itemId}, with item components in string format as returned by {@link #componentsAsString(ItemStack)}, and with a specified stack count. - * + * * @return an {@link ItemStack} or {@link ItemStack#EMPTY} if there was an exception thrown. */ public static ItemStack fromComponentsString(String itemId, int count, String componentsString) { 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); } |