From 9177ac1b2e326a3acffaf476eeff28a1cf9a637f Mon Sep 17 00:00:00 2001 From: Max Becker Date: Sat, 3 Oct 2020 00:06:48 +0200 Subject: [PATCH] Fix modded EntityClassifications not being useable in the codec. (#7337) --- .../entity/EntityClassification.java.patch | 17 ++++++++++++++++- .../gen/feature/jigsaw/JigsawPattern.java.patch | 7 ++++++- .../minecraftforge/common/IExtensibleEnum.java | 13 +++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/patches/minecraft/net/minecraft/entity/EntityClassification.java.patch b/patches/minecraft/net/minecraft/entity/EntityClassification.java.patch index f81882afb..41e1aa4e9 100644 --- a/patches/minecraft/net/minecraft/entity/EntityClassification.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityClassification.java.patch @@ -9,13 +9,28 @@ MONSTER("monster", 70, false, false, 128), CREATURE("creature", 10, true, true, 128), AMBIENT("ambient", 15, true, false, 128), -@@ -57,6 +57,10 @@ +@@ -14,7 +14,7 @@ + WATER_AMBIENT("water_ambient", 20, true, false, 64), + MISC("misc", -1, true, true, 128); + +- public static final Codec field_233667_g_ = IStringSerializable.func_233023_a_(EntityClassification::values, EntityClassification::func_233670_a_); ++ public static final Codec field_233667_g_ = net.minecraftforge.common.IExtensibleEnum.createCodecForExtensibleEnum(EntityClassification::values, EntityClassification::func_233670_a_); + private static final Map field_220364_f = Arrays.stream(values()).collect(Collectors.toMap(EntityClassification::func_220363_a, (p_220362_0_) -> { + return p_220362_0_; + })); +@@ -57,6 +57,16 @@ return this.field_82707_i; } + public static EntityClassification create(String name, String id, int maxNumberOfCreatureIn, boolean isPeacefulCreatureIn, boolean isAnimalIn, int despawnDistance) { + throw new IllegalStateException("Enum not extended"); + } ++ ++ @Override ++ @Deprecated ++ public void init() { ++ field_220364_f.put(this.func_220363_a(), this); ++ } + public int func_233671_f_() { return this.field_233669_n_; diff --git a/patches/minecraft/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java.patch b/patches/minecraft/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java.patch index 741324cde..ba5458837 100644 --- a/patches/minecraft/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java.patch +++ b/patches/minecraft/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java.patch @@ -1,6 +1,6 @@ --- a/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java +++ b/net/minecraft/world/gen/feature/jigsaw/JigsawPattern.java -@@ -104,7 +104,7 @@ +@@ -104,11 +104,11 @@ return this.field_214953_e.size(); } @@ -9,6 +9,11 @@ TERRAIN_MATCHING("terrain_matching", ImmutableList.of(new GravityStructureProcessor(Heightmap.Type.WORLD_SURFACE_WG, -1))), RIGID("rigid", ImmutableList.of()); +- public static final Codec field_236858_c_ = IStringSerializable.func_233023_a_(JigsawPattern.PlacementBehaviour::values, JigsawPattern.PlacementBehaviour::func_214938_a); ++ public static final Codec field_236858_c_ = net.minecraftforge.common.IExtensibleEnum.createCodecForExtensibleEnum(JigsawPattern.PlacementBehaviour::values, JigsawPattern.PlacementBehaviour::func_214938_a); + private static final Map field_214939_c = Arrays.stream(values()).collect(Collectors.toMap(JigsawPattern.PlacementBehaviour::func_214936_a, (p_214935_0_) -> { + return p_214935_0_; + })); @@ -135,5 +135,15 @@ public String func_176610_l() { return this.field_214940_d; diff --git a/src/main/java/net/minecraftforge/common/IExtensibleEnum.java b/src/main/java/net/minecraftforge/common/IExtensibleEnum.java index e0c9faf7b..8b8ebe787 100644 --- a/src/main/java/net/minecraftforge/common/IExtensibleEnum.java +++ b/src/main/java/net/minecraftforge/common/IExtensibleEnum.java @@ -19,6 +19,12 @@ package net.minecraftforge.common; +import com.mojang.serialization.Codec; +import net.minecraft.util.IStringSerializable; + +import java.util.function.Function; +import java.util.function.Supplier; + /** * To be implemented on vanilla enums that should be enhanced with ASM to be * extensible. If this is implemented on a class, the class must define a static @@ -47,4 +53,11 @@ public interface IExtensibleEnum */ @Deprecated default void init() {} + + /** + * Use this instead of {@link IStringSerializable#func_233023_a_(Supplier, Function)} for extensible enums because this not cache the enum values on construction + */ + static & IStringSerializable> Codec createCodecForExtensibleEnum(Supplier valuesSupplier, Function enumValueFromNameFunction) { + return IStringSerializable.func_233024_a_(Enum::ordinal, (id) -> valuesSupplier.get()[id], enumValueFromNameFunction); + } }