From b4aa443c7bccc63fcb9337eb53385206b5cc0d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 9 Jul 2016 10:05:06 +0200 Subject: [PATCH] Block, item and block item tints handling --- src/java/mmm/materials/MLeaves.java | 45 +++++++++++++++++ src/java/mmm/proxy/PClient.java | 11 ++++- src/java/mmm/utils/I_UTintedBlock.java | 17 +++++++ src/java/mmm/utils/I_UTintedItem.java | 20 ++++++++ src/java/mmm/utils/URegistry.java | 67 ++++++++++++++++++++++---- 5 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 src/java/mmm/utils/I_UTintedBlock.java create mode 100644 src/java/mmm/utils/I_UTintedItem.java diff --git a/src/java/mmm/materials/MLeaves.java b/src/java/mmm/materials/MLeaves.java index e039854..85eb16e 100644 --- a/src/java/mmm/materials/MLeaves.java +++ b/src/java/mmm/materials/MLeaves.java @@ -5,6 +5,8 @@ import java.util.Arrays; import java.util.List; import java.util.Random; +import mmm.utils.I_UTintedBlock; +import mmm.utils.I_UTintedItem; import mmm.utils.URegistry; import net.minecraft.block.Block; import net.minecraft.block.BlockLeaves; @@ -12,6 +14,8 @@ import net.minecraft.block.BlockPlanks; import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.renderer.color.IBlockColor; +import net.minecraft.client.renderer.color.IItemColor; import net.minecraft.entity.EntityLivingBase; import net.minecraft.init.Blocks; import net.minecraft.item.Item; @@ -19,8 +23,10 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.ColorizerFoliage; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import net.minecraft.world.biome.BiomeColorHelper; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -28,6 +34,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; public class MLeaves extends BlockLeaves + implements I_UTintedBlock , I_UTintedItem { private final MWood wood; @@ -153,4 +160,42 @@ public class MLeaves return Blocks.LEAVES.shouldSideBeRendered( state , world , pos , side ); } + + @Override + @SideOnly( Side.CLIENT ) + public IBlockColor getBlockTint( ) + { + return new IBlockColor( ) { + + @Override + public int colorMultiplier( final IBlockState state , final IBlockAccess worldIn , final BlockPos pos , + final int tintIndex ) + { + final boolean inWorld = worldIn != null && pos != null; + final int baseTint = inWorld + ? BiomeColorHelper.getFoliageColorAtPos( worldIn , pos ) + : ColorizerFoliage.getFoliageColorBasic( ); + return baseTint; + } + }; + } + + + @Override + @SideOnly( Side.CLIENT ) + public IItemColor getItemTint( ) + { + final IBlockColor bTint = this.getBlockTint( ); + final IBlockState ds = this.getDefaultState( ); + return new IItemColor( ) { + + @Override + public int getColorFromItemstack( final ItemStack stack , final int tintIndex ) + { + return bTint.colorMultiplier( ds , null , null , tintIndex ); + } + + }; + } + } diff --git a/src/java/mmm/proxy/PClient.java b/src/java/mmm/proxy/PClient.java index 3b13bc2..47364ed 100644 --- a/src/java/mmm/proxy/PClient.java +++ b/src/java/mmm/proxy/PClient.java @@ -2,6 +2,7 @@ package mmm.proxy; import mmm.utils.URegistry; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @@ -14,7 +15,15 @@ public class PClient public void preInit( final FMLPreInitializationEvent event ) { super.preInit( event ); - URegistry.setupItemModels( ); + URegistry.preInitClient( ); + } + + + @Override + public void init( FMLInitializationEvent event ) + { + super.init( event ); + URegistry.initClient( ); } } \ No newline at end of file diff --git a/src/java/mmm/utils/I_UTintedBlock.java b/src/java/mmm/utils/I_UTintedBlock.java new file mode 100644 index 0000000..7d1c254 --- /dev/null +++ b/src/java/mmm/utils/I_UTintedBlock.java @@ -0,0 +1,17 @@ +package mmm.utils; + + +import net.minecraft.client.renderer.color.IBlockColor; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + + + +/** For blocks that need to register a tint */ +public interface I_UTintedBlock +{ + + @SideOnly( Side.CLIENT ) + public IBlockColor getBlockTint( ); + +} diff --git a/src/java/mmm/utils/I_UTintedItem.java b/src/java/mmm/utils/I_UTintedItem.java new file mode 100644 index 0000000..10473be --- /dev/null +++ b/src/java/mmm/utils/I_UTintedItem.java @@ -0,0 +1,20 @@ +package mmm.utils; + + +import net.minecraft.client.renderer.color.IItemColor; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + + + +/** + * For items that need to register a tint, or blocks with automatically registered items that need + * to be tinted. + */ +public interface I_UTintedItem +{ + + @SideOnly( Side.CLIENT ) + public IItemColor getItemTint( ); + +} diff --git a/src/java/mmm/utils/URegistry.java b/src/java/mmm/utils/URegistry.java index 61b47f1..02a1c47 100644 --- a/src/java/mmm/utils/URegistry.java +++ b/src/java/mmm/utils/URegistry.java @@ -1,6 +1,7 @@ package mmm.utils; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -9,7 +10,10 @@ import java.util.Map; import mmm.Mmm; import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.client.renderer.color.BlockColors; +import net.minecraft.client.renderer.color.ItemColors; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemCloth; @@ -19,6 +23,8 @@ import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.fml.common.IFuelHandler; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.IForgeRegistryEntry; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; import scala.actors.threadpool.Arrays; @@ -51,6 +57,10 @@ public class URegistry private static final HashMap< Item , Boolean > ITEMS = new HashMap< Item , Boolean >( ); private static final HashSet< Block > BLOCKS = new HashSet< Block >( ); + private static final ArrayList< Block > TINTED_BLOCKS = new ArrayList<>( ); + private static final ArrayList< Block > TINTED_BLOCK_ITEMS = new ArrayList<>( ); + private static final ArrayList< Item > TINTED_ITEMS = new ArrayList<>( ); + private static final HashMap< Item , Object > FUELS = new HashMap<>( ); private static final FuelHandler FUEL_HANDLER = new FuelHandler( ); @@ -98,9 +108,13 @@ public class URegistry public static void addItem( final Item item , final boolean registerModel ) { - GameRegistry.register( item ); - URegistry.ITEMS.put( item , registerModel ); - URegistry.addRecipeRegistrar( item ); + if ( URegistry.ITEMS.put( item , registerModel ) == null ) { + GameRegistry.register( item ); + if ( item instanceof I_UTintedItem ) { + URegistry.TINTED_ITEMS.add( item ); + } + URegistry.addRecipeRegistrar( item ); + } } @@ -131,13 +145,21 @@ public class URegistry public static void addBlock( final Block block , final Item blockItem , final boolean registerItemModel ) { - GameRegistry.register( block ); - URegistry.BLOCKS.add( block ); - URegistry.addRecipeRegistrar( block ); - URegistry.addOreGenerationRegistrar( block ); + if ( URegistry.BLOCKS.add( block ) ) { + GameRegistry.register( block ); + if ( block instanceof I_UTintedBlock ) { + URegistry.TINTED_BLOCKS.add( block ); + } + if ( block instanceof I_UTintedItem ) { + URegistry.TINTED_BLOCK_ITEMS.add( block ); + } - if ( blockItem != null ) { - URegistry.addItem( blockItem , registerItemModel ); + URegistry.addRecipeRegistrar( block ); + URegistry.addOreGenerationRegistrar( block ); + + if ( blockItem != null ) { + URegistry.addItem( blockItem , registerItemModel ); + } } } @@ -202,8 +224,10 @@ public class URegistry } - public static void setupItemModels( ) + @SideOnly( Side.CLIENT ) + public static void preInitClient( ) { + // Register item models for ( final Map.Entry< Item , Boolean > entry : URegistry.ITEMS.entrySet( ) ) { if ( !entry.getValue( ) ) { continue; @@ -218,4 +242,27 @@ public class URegistry ModelLoader.setCustomModelResourceLocation( item , 0 , location ); } } + + + @SideOnly( Side.CLIENT ) + public static void initClient( ) + { + final BlockColors blockColors = Minecraft.getMinecraft( ).getBlockColors( ); + final ItemColors itemColors = Minecraft.getMinecraft( ).getItemColors( ); + + // Register tinted blocks + for ( final Block tintedBlock : URegistry.TINTED_BLOCKS ) { + blockColors.registerBlockColorHandler( ( (I_UTintedBlock) tintedBlock ).getBlockTint( ) , tintedBlock ); + } + + // Register tinted block items + for ( final Block tintedBlock : URegistry.TINTED_BLOCK_ITEMS ) { + itemColors.registerItemColorHandler( ( (I_UTintedItem) tintedBlock ).getItemTint( ) , tintedBlock ); + } + + // Register tinted items + for ( final Item tintedItem : URegistry.TINTED_ITEMS ) { + itemColors.registerItemColorHandler( ( (I_UTintedItem) tintedItem ).getItemTint( ) , tintedItem ); + } + } }