2019-02-10 17:51:25 -05:00
/*
* Minecraft Forge
2020-07-02 19:49:11 +02:00
* Copyright (c) 2016-2020.
2019-02-10 17:51:25 -05:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2019-02-07 16:36:30 -05:00
package net.minecraftforge.common ;
2020-10-03 00:06:48 +02:00
import com.mojang.serialization.Codec ;
import net.minecraft.util.IStringSerializable ;
import java.util.function.Function ;
import java.util.function.Supplier ;
2019-02-07 16:36:30 -05:00
/**
* 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
* method called "create" which takes a String (enum name), and the rest of the
* parameters matching a constructor.
* <p>
* For example, an enum with the constructor {@code MyEnum(Object foo)} would
* require the method:
*
* <pre>
* public static MyEnum create(String name, Object foo)
* {
* throw new IllegalStateException("Enum not extended");
* }
* </pre>
*
* The method contents will be replaced with ASM at runtime. Multiple
* {@code create} methods <strong>can</strong> be defined as long as each
* matches a constructor.
*/
public interface IExtensibleEnum
{
2019-09-21 15:57:32 -04:00
/**
* Called by generated factory code to do any post-constructor setup required by
* the enum. Should not be called manually.
*/
@Deprecated
default void init () {}
2020-10-03 00:06:48 +02:00
/**
* Use this instead of {@link IStringSerializable#func_233023_a_(Supplier, Function)} for extensible enums because this not cache the enum values on construction
*/
static < E extends Enum < E > & IStringSerializable > Codec < E > createCodecForExtensibleEnum ( Supplier < E []> valuesSupplier , Function <? super String , ? extends E > enumValueFromNameFunction ) {
return IStringSerializable . func_233024_a_ ( Enum :: ordinal , ( id ) -> valuesSupplier . get () [ id ] , enumValueFromNameFunction );
}
2019-05-23 16:02:15 -07:00
}