Block, item and block item tints handling

This commit is contained in:
Emmanuel BENOîT 2016-07-09 10:05:06 +02:00
parent 8b1455c8fd
commit b4aa443c7b
5 changed files with 149 additions and 11 deletions

View file

@ -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 );
}
};
}
}

View file

@ -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( );
}
}

View file

@ -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( );
}

View file

@ -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( );
}

View file

@ -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,10 +108,14 @@ public class URegistry
public static void addItem( final Item item , final boolean registerModel )
{
if ( URegistry.ITEMS.put( item , registerModel ) == null ) {
GameRegistry.register( item );
URegistry.ITEMS.put( item , registerModel );
if ( item instanceof I_UTintedItem ) {
URegistry.TINTED_ITEMS.add( item );
}
URegistry.addRecipeRegistrar( item );
}
}
public static void addBlock( final Block block )
@ -131,8 +145,15 @@ public class URegistry
public static void addBlock( final Block block , final Item blockItem , final boolean registerItemModel )
{
if ( URegistry.BLOCKS.add( block ) ) {
GameRegistry.register( block );
URegistry.BLOCKS.add( block );
if ( block instanceof I_UTintedBlock ) {
URegistry.TINTED_BLOCKS.add( block );
}
if ( block instanceof I_UTintedItem ) {
URegistry.TINTED_BLOCK_ITEMS.add( block );
}
URegistry.addRecipeRegistrar( block );
URegistry.addOreGenerationRegistrar( block );
@ -140,6 +161,7 @@ public class URegistry
URegistry.addItem( blockItem , registerItemModel );
}
}
}
public static void setFuel( final Item item , final int value )
@ -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 );
}
}
}