From 50bcccce2b82c72d3c233649a6233304b665d3ca Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sat, 17 Feb 2024 23:56:50 +0100 Subject: [PATCH] fix: enchanted book recipes not working on fabric RegistryFixedCodec doesn't work on Fabric for some reason. --- .../UpgradeWithEnchantedBookRecipe.java | 53 ++++++++++--------- ...radeWithEnchantedBookRecipeSerializer.java | 25 +++------ 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipe.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipe.java index 6121ada6f..e6a5d69e1 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipe.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipe.java @@ -4,21 +4,16 @@ import java.util.Objects; import java.util.Optional; -import javax.annotation.Nullable; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; -import net.minecraft.core.Holder; import net.minecraft.core.NonNullList; -import net.minecraft.core.registries.Registries; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; -import net.minecraft.resources.RegistryFixedCodec; -import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.item.EnchantedBookItem; -import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.CraftingBookCategory; import net.minecraft.world.item.crafting.Ingredient; @@ -30,29 +25,36 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Blocks; +import static java.util.Objects.requireNonNull; + public class UpgradeWithEnchantedBookRecipe extends ShapedRecipe { public static final Codec CODEC = RecordCodecBuilder.create( instance -> instance.group( - RegistryFixedCodec.create(Registries.ENCHANTMENT).fieldOf("enchantment") - .forGetter(UpgradeWithEnchantedBookRecipe::getEnchantment), + Codec.STRING.fieldOf("enchantment") + .xmap(ResourceLocation::new, ResourceLocation::toString) + .forGetter(UpgradeWithEnchantedBookRecipe::getEnchantmentId), Codec.INT.fieldOf("level").orElse(1) .forGetter(UpgradeWithEnchantedBookRecipe::getEnchantmentLevel), - RegistryFixedCodec.create(Registries.ITEM).fieldOf("result") - .forGetter(UpgradeWithEnchantedBookRecipe::getResultItem) + Codec.STRING.fieldOf("result") + .xmap(ResourceLocation::new, ResourceLocation::toString) + .forGetter(UpgradeWithEnchantedBookRecipe::getResultItemId) ).apply(instance, UpgradeWithEnchantedBookRecipe::new) ); - private final Holder enchantment; + private final ResourceLocation enchantmentId; private final int level; - private final Holder resultItem; + private final ResourceLocation resultItemId; - UpgradeWithEnchantedBookRecipe(final Holder enchantment, + UpgradeWithEnchantedBookRecipe(final ResourceLocation enchantmentId, final int level, - final Holder resultItem) { + final ResourceLocation resultItemId) { super("", CraftingBookCategory.MISC, new ShapedRecipePattern(3, 3, NonNullList.of( Ingredient.EMPTY, Ingredient.of(new ItemStack(Items.INSTANCE.getQuartzEnrichedIron())), - Ingredient.of(EnchantedBookItem.createForEnchantment(new EnchantmentInstance(enchantment.value(), level))), + Ingredient.of(EnchantedBookItem.createForEnchantment(new EnchantmentInstance( + getEnchantment(enchantmentId), + level + ))), Ingredient.of(new ItemStack(Items.INSTANCE.getQuartzEnrichedIron())), Ingredient.of(new ItemStack(Blocks.BOOKSHELF)), Ingredient.of(new ItemStack(Items.INSTANCE.getUpgrade())), @@ -60,26 +62,25 @@ public class UpgradeWithEnchantedBookRecipe extends ShapedRecipe { Ingredient.of(new ItemStack(Items.INSTANCE.getQuartzEnrichedIron())), Ingredient.of(new ItemStack(Items.INSTANCE.getQuartzEnrichedIron())), Ingredient.of(new ItemStack(Items.INSTANCE.getQuartzEnrichedIron())) - ), Optional.empty()), new ItemStack(resultItem.value())); - this.enchantment = enchantment; + ), Optional.empty()), new ItemStack(BuiltInRegistries.ITEM.get(resultItemId))); + this.enchantmentId = enchantmentId; this.level = level; - this.resultItem = resultItem; + this.resultItemId = resultItemId; } - public Holder getResultItem() { - return resultItem; + private static Enchantment getEnchantment(final ResourceLocation enchantmentId) { + return requireNonNull(BuiltInRegistries.ENCHANTMENT.get(enchantmentId)); } - public Holder getEnchantment() { - return enchantment; + ResourceLocation getResultItemId() { + return resultItemId; } - @Nullable - public ResourceLocation getEnchantmentId() { - return enchantment.unwrapKey().map(ResourceKey::location).orElse(null); + ResourceLocation getEnchantmentId() { + return enchantmentId; } - public int getEnchantmentLevel() { + int getEnchantmentLevel() { return level; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipeSerializer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipeSerializer.java index 8fcc1e445..30cd78fbd 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipeSerializer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/upgrade/UpgradeWithEnchantedBookRecipeSerializer.java @@ -1,16 +1,9 @@ package com.refinedmods.refinedstorage2.platform.common.upgrade; -import java.util.Objects; - import com.mojang.serialization.Codec; -import net.minecraft.core.Holder; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.core.registries.Registries; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceKey; -import net.minecraft.world.item.Item; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.crafting.RecipeSerializer; -import net.minecraft.world.item.enchantment.Enchantment; public class UpgradeWithEnchantedBookRecipeSerializer implements RecipeSerializer { @Override @@ -20,22 +13,16 @@ public Codec codec() { @Override public UpgradeWithEnchantedBookRecipe fromNetwork(final FriendlyByteBuf buf) { - final Holder result = BuiltInRegistries.ITEM.getHolder(ResourceKey.create( - Registries.ITEM, - buf.readResourceLocation() - )).orElseThrow(); - final Holder enchantment = BuiltInRegistries.ENCHANTMENT.getHolder(ResourceKey.create( - Registries.ENCHANTMENT, - buf.readResourceLocation() - )).orElseThrow(); + final ResourceLocation resultItemId = buf.readResourceLocation(); + final ResourceLocation enchantmentId = buf.readResourceLocation(); final int level = buf.readInt(); - return new UpgradeWithEnchantedBookRecipe(enchantment, level, result); + return new UpgradeWithEnchantedBookRecipe(enchantmentId, level, resultItemId); } @Override public void toNetwork(final FriendlyByteBuf buf, final UpgradeWithEnchantedBookRecipe recipe) { - buf.writeResourceLocation(recipe.getResultItem().unwrapKey().orElseThrow().location()); - buf.writeResourceLocation(Objects.requireNonNull(recipe.getEnchantmentId())); + buf.writeResourceLocation(recipe.getResultItemId()); + buf.writeResourceLocation(recipe.getEnchantmentId()); buf.writeInt(recipe.getEnchantmentLevel()); } }