diff options
| author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-06-18 16:52:24 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-18 16:52:24 -0400 |
| commit | d6b220a8e42a1fc2dbc955779e86d199851b4674 (patch) | |
| tree | b17f740786785e3a5b23f00fb327f11e1230fd88 /src/main/java/de/hysky/skyblocker/utils/datafixer | |
| parent | 265c09b16b78b93a55745ce8dd008d405e735d6b (diff) | |
| parent | 9d7604c52e4d786032168b4ef46ac7d8af0a5891 (diff) | |
| download | Skyblocker-d6b220a8e42a1fc2dbc955779e86d199851b4674.tar.gz Skyblocker-d6b220a8e42a1fc2dbc955779e86d199851b4674.tar.bz2 Skyblocker-d6b220a8e42a1fc2dbc955779e86d199851b4674.zip | |
Merge pull request #769 from SkyblockerMod/1.21
1.21
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer')
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java | 28 |
1 files changed, 19 insertions, 9 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 3543a2f1..a9b227a1 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -1,25 +1,26 @@ package de.hysky.skyblocker.utils.datafixer; import java.util.Arrays; -import java.util.List; import java.util.Objects; import java.util.Optional; import com.mojang.brigadier.StringReader; import com.mojang.serialization.Dynamic; +import net.minecraft.client.MinecraftClient; import net.minecraft.command.argument.ItemStringReader; import net.minecraft.command.argument.ItemStringReader.ItemResult; -import net.minecraft.component.DataComponentType; +import net.minecraft.component.ComponentType; 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.BuiltinRegistries; import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryOps; +import net.minecraft.registry.RegistryWrapper.WrapperLookup; import net.minecraft.util.Identifier; /** @@ -30,10 +31,10 @@ 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 = 3825; - private static final DynamicRegistryManager REGISTRY_MANAGER = new DynamicRegistryManager.ImmutableImpl(List.of(Registries.ITEM, Registries.DATA_COMPONENT_TYPE)); + private static final WrapperLookup LOOKUP = BuiltinRegistries.createWrapperLookup(); public static ItemStack fixUpItem(NbtCompound nbt) { - Dynamic<NbtElement> dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<>(NbtOps.INSTANCE, nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); + Dynamic<NbtElement> dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<>(getRegistryLookup().getOps(NbtOps.INSTANCE), nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); return ItemStack.CODEC.parse(dynamic).getOrThrow(); } @@ -44,11 +45,11 @@ public class ItemStackComponentizationFixer { * @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<NbtElement> nbtRegistryOps = REGISTRY_MANAGER.getOps(NbtOps.INSTANCE); + RegistryOps<NbtElement> nbtRegistryOps = getRegistryLookup().getOps(NbtOps.INSTANCE); return Arrays.toString(stack.getComponentChanges().entrySet().stream().map(entry -> { @SuppressWarnings("unchecked") - DataComponentType<Object> dataComponentType = (DataComponentType<Object>) entry.getKey(); + ComponentType<Object> dataComponentType = (ComponentType<Object>) entry.getKey(); Identifier componentId = Registries.DATA_COMPONENT_TYPE.getId(dataComponentType); Optional<NbtElement> encodedComponent = dataComponentType.getCodec().encodeStart(nbtRegistryOps, entry.getValue().orElseThrow()).result(); @@ -66,17 +67,26 @@ public class ItemStackComponentizationFixer { * @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); + ItemStringReader reader = new ItemStringReader(getRegistryLookup()); try { ItemResult result = reader.consume(new StringReader(itemId + componentsString)); ItemStack stack = new ItemStack(result.item(), count); - stack.applyComponentsFrom(result.components()); + //Vanilla skips validation with /give so we will too + stack.applyUnvalidatedChanges(result.components()); return stack; } catch (Exception ignored) {} return ItemStack.EMPTY; } + + /** + * Tries to get the dynamic registry manager instance currently in use or else returns {@link #LOOKUP} + */ + public static WrapperLookup getRegistryLookup() { + MinecraftClient client = MinecraftClient.getInstance(); + return client != null && client.getNetworkHandler() != null && client.getNetworkHandler().getRegistryManager() != null ? client.getNetworkHandler().getRegistryManager() : LOOKUP; + } } |
