diff options
| author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-06-14 20:01:38 +0800 |
|---|---|---|
| committer | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-06-18 16:34:37 -0400 |
| commit | 70a7e2528cc4757e324c99dc8b51f082a457d1db (patch) | |
| tree | 8fad17559cd5ff029678f9e7553ad92682cb1d3a /src/main/java/de/hysky/skyblocker/utils/datafixer | |
| parent | 3a53e51494523871870491617ae6add9b3fe87fe (diff) | |
| download | Skyblocker-70a7e2528cc4757e324c99dc8b51f082a457d1db.tar.gz Skyblocker-70a7e2528cc4757e324c99dc8b51f082a457d1db.tar.bz2 Skyblocker-70a7e2528cc4757e324c99dc8b51f082a457d1db.zip | |
Fix dynamic registry usage and related tests
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/datafixer')
| -rw-r--r-- | src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java | 15 |
1 files changed, 12 insertions, 3 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 959e6a5f..a9b227a1 100644 --- a/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java @@ -7,6 +7,7 @@ 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.ComponentType; @@ -33,7 +34,7 @@ public class ItemStackComponentizationFixer { private static final WrapperLookup LOOKUP = BuiltinRegistries.createWrapperLookup(); public static ItemStack fixUpItem(NbtCompound nbt) { - Dynamic<NbtElement> dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<>(LOOKUP.getOps(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,7 +45,7 @@ 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 = LOOKUP.getOps(NbtOps.INSTANCE); + RegistryOps<NbtElement> nbtRegistryOps = getRegistryLookup().getOps(NbtOps.INSTANCE); return Arrays.toString(stack.getComponentChanges().entrySet().stream().map(entry -> { @SuppressWarnings("unchecked") @@ -66,7 +67,7 @@ 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(LOOKUP); + ItemStringReader reader = new ItemStringReader(getRegistryLookup()); try { ItemResult result = reader.consume(new StringReader(itemId + componentsString)); @@ -80,4 +81,12 @@ public class ItemStackComponentizationFixer { 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; + } } |
