aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2024-06-14 20:01:38 +0800
committerAaron <51387595+AzureAaron@users.noreply.github.com>2024-06-18 16:34:37 -0400
commit70a7e2528cc4757e324c99dc8b51f082a457d1db (patch)
tree8fad17559cd5ff029678f9e7553ad92682cb1d3a /src/main/java/de/hysky/skyblocker/utils
parent3a53e51494523871870491617ae6add9b3fe87fe (diff)
downloadSkyblocker-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')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ColorUtils.java7
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/datafixer/ItemStackComponentizationFixer.java15
2 files changed, 14 insertions, 8 deletions
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<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;
+ }
}