From 21d109338350e1e1e641a1278feee44b5b21b746 Mon Sep 17 00:00:00 2001 From: mezz Date: Fri, 11 Oct 2024 21:02:49 +0900 Subject: [PATCH] Simplify EnumCodec --- Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java | 6 ++---- .../main/java/mezz/jei/gui/config/BookmarkJsonConfig.java | 2 +- .../main/java/mezz/jei/library/config/EditModeConfig.java | 2 +- .../src/main/java/mezz/jei/library/helpers/CodecHelper.java | 2 +- .../library/plugins/debug/ingredients/ErrorIngredient.java | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java b/Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java index 2785eeef5..f72e9eb59 100644 --- a/Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java +++ b/Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java @@ -3,14 +3,12 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; -import java.util.function.Function; - public class EnumCodec { - public static > Codec create(Class enumClass, Function valueOf) { + public static > Codec create(Class enumClass) { return Codec.STRING.flatXmap( name -> { try { - T e = valueOf.apply(name); + T e = Enum.valueOf(enumClass, name); return DataResult.success(e); } catch (IllegalArgumentException ignored) { return DataResult.error(() -> "Unknown enum name: '" + name + "' for enum class: " + enumClass); diff --git a/Gui/src/main/java/mezz/jei/gui/config/BookmarkJsonConfig.java b/Gui/src/main/java/mezz/jei/gui/config/BookmarkJsonConfig.java index dd70c821b..be32617cb 100644 --- a/Gui/src/main/java/mezz/jei/gui/config/BookmarkJsonConfig.java +++ b/Gui/src/main/java/mezz/jei/gui/config/BookmarkJsonConfig.java @@ -36,7 +36,7 @@ public class BookmarkJsonConfig implements IBookmarkConfig { private static final Logger LOGGER = LogManager.getLogger(); private static final int VERSION = 2; - private static final Codec TYPE_CODEC = EnumCodec.create(BookmarkType.class, BookmarkType::valueOf); + private static final Codec TYPE_CODEC = EnumCodec.create(BookmarkType.class); private static @Nullable MapCodec BOOKMARK_CODEC; @SuppressWarnings("deprecation") diff --git a/Library/src/main/java/mezz/jei/library/config/EditModeConfig.java b/Library/src/main/java/mezz/jei/library/config/EditModeConfig.java index 64930c748..3e5573939 100644 --- a/Library/src/main/java/mezz/jei/library/config/EditModeConfig.java +++ b/Library/src/main/java/mezz/jei/library/config/EditModeConfig.java @@ -188,7 +188,7 @@ public FileSerializer(Path path, RegistryAccess registryAccess, ICodecHelper cod this.path = path; this.codec = RecordCodecBuilder.create(builder -> { return builder.group( - EnumCodec.create(HideMode.class, HideMode::valueOf) + EnumCodec.create(HideMode.class) .fieldOf("hide_mode") .forGetter(Pair::getFirst), codecHelper.getTypedIngredientCodec().codec() diff --git a/Library/src/main/java/mezz/jei/library/helpers/CodecHelper.java b/Library/src/main/java/mezz/jei/library/helpers/CodecHelper.java index 6edcb26d8..17891b81d 100644 --- a/Library/src/main/java/mezz/jei/library/helpers/CodecHelper.java +++ b/Library/src/main/java/mezz/jei/library/helpers/CodecHelper.java @@ -129,7 +129,7 @@ private Codec createDefaultRecipeCategoryCodec(IRecipeManager recipeManag .forGetter(Data::registryName), getTypedIngredientCodec().codec().fieldOf("ingredient") .forGetter(Data::ingredient), - EnumCodec.create(RecipeIngredientRole.class, RecipeIngredientRole::valueOf).fieldOf("ingredient_role") + EnumCodec.create(RecipeIngredientRole.class).fieldOf("ingredient_role") .forGetter(Data::ingredientRole) ).apply(builder, Data::new); }); diff --git a/Library/src/main/java/mezz/jei/library/plugins/debug/ingredients/ErrorIngredient.java b/Library/src/main/java/mezz/jei/library/plugins/debug/ingredients/ErrorIngredient.java index 180edcf08..64aba3c43 100644 --- a/Library/src/main/java/mezz/jei/library/plugins/debug/ingredients/ErrorIngredient.java +++ b/Library/src/main/java/mezz/jei/library/plugins/debug/ingredients/ErrorIngredient.java @@ -7,7 +7,7 @@ public record ErrorIngredient(CrashType crashType) { public static final IIngredientType TYPE = () -> ErrorIngredient.class; - public static final Codec CODEC = EnumCodec.create(CrashType.class, CrashType::valueOf) + public static final Codec CODEC = EnumCodec.create(CrashType.class) .xmap(ErrorIngredient::new, ErrorIngredient::crashType); public enum CrashType {