From 70a7e2528cc4757e324c99dc8b51f082a457d1db Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:01:38 +0800 Subject: Fix dynamic registry usage and related tests --- src/main/java/de/hysky/skyblocker/utils/ColorUtils.java | 7 ++----- .../utils/datafixer/ItemStackComponentizationFixer.java | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/utils') diff --git a/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java b/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java index 4b17ef1a..2c8f5e4a 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ColorUtils.java @@ -5,6 +5,7 @@ import net.minecraft.util.DyeColor; public class ColorUtils { /** * Takes an RGB color as an integer and returns an array of the color's components as floats, in RGB format. + * * @param color The color to get the components of. * @return An array of the color's components as floats. */ @@ -20,10 +21,6 @@ public class ColorUtils { * @param dye The dye from which the entity color will be used for the components. */ public static float[] getFloatComponents(DyeColor dye) { - return new float[] { - ((dye.getEntityColor() >> 16) & 0xFF) / 255f, - ((dye.getEntityColor() >> 8) & 0xFF) / 255f, - (dye.getEntityColor() & 0xFF) / 255f - }; + return getFloatComponents(dye.getEntityColor()); } } 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 dynamic = Schemas.getFixer().update(TypeReferences.ITEM_STACK, new Dynamic<>(LOOKUP.getOps(NbtOps.INSTANCE), nbt), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION); + Dynamic 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 nbtRegistryOps = LOOKUP.getOps(NbtOps.INSTANCE); + RegistryOps 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; + } } -- cgit