Skip to content

Commit

Permalink
Simplify EnumCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Oct 11, 2024
1 parent f94d63c commit 21d1093
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Common/src/main/java/mezz/jei/common/codecs/EnumCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;

import java.util.function.Function;

public class EnumCodec {
public static <T extends Enum<T>> Codec<T> create(Class<T> enumClass, Function<String, T> valueOf) {
public static <T extends Enum<T>> Codec<T> create(Class<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookmarkType> TYPE_CODEC = EnumCodec.create(BookmarkType.class, BookmarkType::valueOf);
private static final Codec<BookmarkType> TYPE_CODEC = EnumCodec.create(BookmarkType.class);
private static @Nullable MapCodec<IBookmark> BOOKMARK_CODEC;

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private <T> Codec<T> 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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public record ErrorIngredient(CrashType crashType) {
public static final IIngredientType<ErrorIngredient> TYPE = () -> ErrorIngredient.class;

public static final Codec<ErrorIngredient> CODEC = EnumCodec.create(CrashType.class, CrashType::valueOf)
public static final Codec<ErrorIngredient> CODEC = EnumCodec.create(CrashType.class)
.xmap(ErrorIngredient::new, ErrorIngredient::crashType);

public enum CrashType {
Expand Down

0 comments on commit 21d1093

Please sign in to comment.