From b50419cd6b7e9fffe6c7b12ccff22e0c862d8892 Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Tue, 29 Sep 2015 18:15:32 +0200 Subject: [PATCH] Add property-support to ItemBlockMeta for the unlocalized names. Also add a utility function to register blocks models based on that. --- .../mantle/item/ItemBlockMeta.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/slimeknights/mantle/item/ItemBlockMeta.java b/src/main/java/slimeknights/mantle/item/ItemBlockMeta.java index bdcdeb6..cebb8ff 100644 --- a/src/main/java/slimeknights/mantle/item/ItemBlockMeta.java +++ b/src/main/java/slimeknights/mantle/item/ItemBlockMeta.java @@ -1,11 +1,54 @@ package slimeknights.mantle.item; import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.resources.model.ModelResourceLocation; +import net.minecraft.item.Item; import net.minecraft.item.ItemColored; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.model.ModelLoader; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.registry.GameData; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import java.util.Collection; +import java.util.Locale; public class ItemBlockMeta extends ItemColored { + protected IProperty mappingProperty; + public ItemBlockMeta(Block block) { super(block, true); } + + @Override + public String getUnlocalizedName(ItemStack stack) { + if(mappingProperty == null) return super.getUnlocalizedName(stack); + + IBlockState state = block.getStateFromMeta(stack.getMetadata()); + String name = state.getValue(mappingProperty).toString().toLowerCase(Locale.US); + return super.getUnlocalizedName(stack) + "." + name; + } + + public static void setMappingProperty(Block block, IProperty property) { + ((ItemBlockMeta)Item.getItemFromBlock(block)).mappingProperty = property; + } + + @SideOnly(Side.CLIENT) + public void registerItemModels() { + final Item item = this; + final ResourceLocation loc = (ResourceLocation) GameData.getBlockRegistry().getNameForObject(block); + + + for(Comparable o : (Collection)mappingProperty.getAllowedValues()) { + int meta = block.getMetaFromState(block.getDefaultState().withProperty(mappingProperty, o)); + String name = mappingProperty.getName(o); + + ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(loc, mappingProperty.getName() + "=" + name)); + } + } }