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 --- .../datafixer/ItemStackComponentizationFixer.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer') diff --git a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java new file mode 100644 index 00000000..f1306ad5 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -0,0 +1,26 @@ +package de.hysky.skyblocker.utils.datafixer; + +import com.mojang.serialization.Dynamic; + +import net.minecraft.datafixer.Schemas; +import net.minecraft.datafixer.TypeReferences; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtOps; + +/** + * Contains a data fixer to convert legacy item NBT to the new components system. + * + * @see {@link net.minecraft.datafixer.fix.ItemStackComponentizationFix} + */ +public class ItemStackComponentizationFixer { + private static final int ITEM_NBT_DATA_VERSION = 3817; + private static final int ITEM_COMPONENTS_DATA_VERSION = 3818; + + public static ItemStack fixUpItem(NbtCompound nbt) { + Dynamic 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).result().orElseThrow(); + } +} -- 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 --- .../datafixer/ItemStackComponentizationFixer.java | 62 +++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer') 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 f1306ad5..0fa909bf 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -1,26 +1,84 @@ package de.hysky.skyblocker.utils.datafixer; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.mojang.brigadier.StringReader; import com.mojang.serialization.Dynamic; +import net.minecraft.command.argument.ItemStringReader; +import net.minecraft.command.argument.ItemStringReader.ItemResult; +import net.minecraft.component.DataComponentType; import net.minecraft.datafixer.Schemas; import net.minecraft.datafixer.TypeReferences; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.DynamicRegistryManager; +import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryOps; +import net.minecraft.util.Identifier; /** - * Contains a data fixer to convert legacy item NBT to the new components system. + * 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} */ public class ItemStackComponentizationFixer { private static final int ITEM_NBT_DATA_VERSION = 3817; - private static final int ITEM_COMPONENTS_DATA_VERSION = 3818; + private static final int ITEM_COMPONENTS_DATA_VERSION = 3820; + private static final DynamicRegistryManager REGISTRY_MANAGER = new DynamicRegistryManager.ImmutableImpl(List.of(Registries.ITEM, Registries.DATA_COMPONENT_TYPE)); public static ItemStack fixUpItem(NbtCompound nbt) { Dynamic 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).result().orElseThrow(); } + + /** + * Modified version of {@link net.minecraft.command.argument.ItemStackArgument#asString(net.minecraft.registry.RegistryWrapper.WrapperLookup)} to only care about changed components. + * + * @return The {@link ItemStack}'s components as a string which is in the format that the {@code /give} command accepts. + */ + public static String componentsAsString(ItemStack stack) { + RegistryOps nbtRegistryOps = REGISTRY_MANAGER.getOps(NbtOps.INSTANCE); + + String componentsString = stack.getComponentChanges().entrySet().stream().flatMap(entry -> { + @SuppressWarnings("unchecked") + DataComponentType dataComponentType = (DataComponentType) entry.getKey(); + Identifier componentId = Registries.DATA_COMPONENT_TYPE.getId(dataComponentType); + Optional encodedComponent = dataComponentType.getCodec().encodeStart(nbtRegistryOps, entry.getValue().orElseThrow()).result(); + + if (componentId == null || encodedComponent.isEmpty()) { + return Stream.empty(); + } + + return Stream.of(componentId.toString() + "=" + encodedComponent.orElseThrow()); + }).collect(Collectors.joining(String.valueOf(','))); + + return "[" + componentsString + "]"; + } + + /** + * 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) { + ItemStringReader reader = new ItemStringReader(REGISTRY_MANAGER); + + try { + ItemResult result = reader.consume(new StringReader(itemId + componentsString)); + ItemStack stack = new ItemStack(result.item(), count); + + stack.applyComponentsFrom(result.components()); + + return stack; + } catch (Exception ignored) {} + + return ItemStack.EMPTY; + } } -- 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/ItemStackComponentizationFixer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer') 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 0fa909bf..e77954b0 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -35,7 +35,7 @@ public class ItemStackComponentizationFixer { public static ItemStack fixUpItem(NbtCompound nbt) { Dynamic 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).result().orElseThrow(); + return ItemStack.CODEC.parse(dynamic).getOrThrow(); } /** -- cgit From a0a7f4b09a5f14970b13d06612ab60b7a3991fa7 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Wed, 24 Apr 2024 08:55:05 -0400 Subject: Update item components data version --- .../skyblocker/utils/datafixer/ItemStackComponentizationFixer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer') 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 e77954b0..fa873b84 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -29,7 +29,7 @@ import net.minecraft.util.Identifier; */ public class ItemStackComponentizationFixer { private static final int ITEM_NBT_DATA_VERSION = 3817; - private static final int ITEM_COMPONENTS_DATA_VERSION = 3820; + private static final int ITEM_COMPONENTS_DATA_VERSION = 3825; private static final DynamicRegistryManager REGISTRY_MANAGER = new DynamicRegistryManager.ImmutableImpl(List.of(Registries.ITEM, Registries.DATA_COMPONENT_TYPE)); public static ItemStack fixUpItem(NbtCompound nbt) { -- 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/ItemStackComponentizationFixer.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer') 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 dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic(NbtOps.INSTANCE, nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); + Dynamic 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 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 dataComponentType = (DataComponentType) entry.getKey(); Identifier componentId = Registries.DATA_COMPONENT_TYPE.getId(dataComponentType); Optional 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) { -- cgit