From e2ee54a27de47554a01c1aeeb634f0825f8d3a79 Mon Sep 17 00:00:00 2001 From: liach Date: Mon, 13 Jul 2015 18:15:24 +0800 Subject: [PATCH] Add an EnumHelper hook and fixed an issue --- .../minecraftforge/common/BiomeManager.java | 2 +- .../minecraftforge/common/EnumPlantType.java | 28 ++++++++++- .../debug/EnumPlantTypeTest.java | 49 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/EnumPlantTypeTest.java diff --git a/src/main/java/net/minecraftforge/common/BiomeManager.java b/src/main/java/net/minecraftforge/common/BiomeManager.java index ee584e28b5..1b4517603e 100644 --- a/src/main/java/net/minecraftforge/common/BiomeManager.java +++ b/src/main/java/net/minecraftforge/common/BiomeManager.java @@ -175,7 +175,7 @@ public class BiomeManager if (t.name().equals(name)) return t; } - BiomeType ret = EnumHelper.addEnum(BiomeType.class, name, BiomeType.class); + BiomeType ret = EnumHelper.addEnum(BiomeType.class, name, new Class[0], new Object[0]); if (ret.ordinal() >= biomes.length) { diff --git a/src/main/java/net/minecraftforge/common/EnumPlantType.java b/src/main/java/net/minecraftforge/common/EnumPlantType.java index 3622e6c743..648d3dcffe 100644 --- a/src/main/java/net/minecraftforge/common/EnumPlantType.java +++ b/src/main/java/net/minecraftforge/common/EnumPlantType.java @@ -1,5 +1,7 @@ package net.minecraftforge.common; +import net.minecraftforge.common.util.EnumHelper; + public enum EnumPlantType { Plains, @@ -8,5 +10,27 @@ public enum EnumPlantType Cave, Water, Nether, - Crop -} \ No newline at end of file + Crop; + + /** + * Getting a custom {@link EnumPlantType}, or an existing one if it has the same name as that one. Your plant should implement {@link IPlantable} + * and return this custom type in {@link IPlantable#getPlantType(net.minecraft.world.IBlockAccess, net.minecraft.util.BlockPos)}. + * + *

If your new plant grows on blocks like any one of them above, never create a new {@link EnumPlantType}. + * This enumeration is only functioning in + * {@link net.minecraft.block.Block#canSustainPlant(net.minecraft.world.IBlockAccess, net.minecraft.util.BlockPos, net.minecraft.util.EnumFacing, IPlantable)}, + * which you are supposed to override this function in your new block and create a new plant type to grow on that block. + * + *

You can create an instance of your plant type in your API and let your/others mods access it. It will be faster than calling this method. + * @param name the name of the type of plant, you had better follow the style above + * @return the acquired {@EnumPlantType}, a new one if not found. + */ + public static EnumPlantType getPlantType(String name) + { + for (EnumPlantType t : values()) + { + if (t.name().equalsIgnoreCase(name)) return t; + } + return EnumHelper.addEnum(EnumPlantType.class, name, new Class[0], new Object[0]); + } +} diff --git a/src/test/java/net/minecraftforge/debug/EnumPlantTypeTest.java b/src/test/java/net/minecraftforge/debug/EnumPlantTypeTest.java new file mode 100644 index 0000000000..2f7e7aa869 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/EnumPlantTypeTest.java @@ -0,0 +1,49 @@ +package net.minecraftforge.debug; + +import net.minecraftforge.common.BiomeManager.BiomeType; +import net.minecraftforge.common.EnumPlantType; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +@Mod(modid = "enumplanttypetest") +public class EnumPlantTypeTest +{ + private static Logger LOGGER = LogManager.getLogger(); + + @Mod.EventHandler + public void onInit(FMLInitializationEvent event) + { + BiomeType biomeType = null; + try + { + biomeType = BiomeType.getType("FAKE"); + } + catch (NullPointerException npe) + { + LOGGER.warn("EnumHelper in BiomeType is working incorrectly!", npe); + } + finally + { + if (biomeType == null || !biomeType.name().equals("FAKE")) + LOGGER.warn("EnumHelper in BiomeType is working incorrectly!"); + } + EnumPlantType plantType = null; + if (plantType == null || !plantType.name().equals("FAKE")) + ; + try + { + plantType = EnumPlantType.getPlantType("FAKE"); + } + catch (NullPointerException npe) + { + LOGGER.warn("EnumHelper in EnumPlantType is working incorrectly!", npe); + } + finally + { + if (plantType == null || !plantType.name().equals("FAKE")) + LOGGER.warn("EnumHelper in EnumPlantType is working incorrectly!"); + } + } +}