Jump to content

Recommended Posts

Posted

It looks like something has changed to make a block that change color based on biome (like grass does). Infact i was trying to make a grass slab, by using this class

package com.mwvanilla.blocks;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.biome.BiomeColorHelper;

public abstract class BlockGrassSlab extends BlockVanillaSlab implements IBlockColor
{
    public BlockGrassSlab()
    {
        super(Blocks.grass);
    }

    @Override
    public int colorMultiplier(IBlockState state, IBlockAccess access, BlockPos pos, int tintIndex) {
    	return BiomeColorHelper.getGrassColorAtPos(access, pos);
    }
}

 

But in game the slab looks like this

kuIuRY4.png

 

So it seems the colorMultiplier function is never called. The Json file for the slab (the lower one) is this

{
    "parent": "block/block",
    "textures": {
        "bottom": "blocks/dirt",
        "top": "blocks/grass_top",
        "side": "blocks/grass_side",
	"overlay" : "blocks/grass_side_overlay",
	"particle": "blocks/dirt"
    },
    "elements": [
        {   "from": [ 0, 0, 0 ],
            "to": [ 16, 8, 16 ],
            "faces": {
                "down":  { "uv": [ 0, 0, 16, 16 ], "texture": "#bottom", "cullface": "down" },
                "up":    { "uv": [ 0, 0, 16, 16 ], "texture": "#top" , "tintindex": 0},
                "north": { "uv": [ 0, 8, 16, 16 ], "texture": "#side", "cullface": "north" },
                "south": { "uv": [ 0, 8, 16, 16 ], "texture": "#side", "cullface": "south" },
                "west":  { "uv": [ 0, 8, 16, 16 ], "texture": "#side", "cullface": "west" },
                "east":  { "uv": [ 0, 8, 16, 16 ], "texture": "#side", "cullface": "east" }
            }
        },
	{   "from": [ 0, 0, 0 ],
            "to": [ 16, 8, 16 ],
            "faces": {
                "north": { "uv": [ 0, 8, 16, 16 ], "texture": "#overlay", "cullface": "north" , "tintindex": 0},
                "south": { "uv": [ 0, 8, 16, 16 ], "texture": "#overlay", "cullface": "south", "tintindex": 0 },
                "west":  { "uv": [ 0, 8, 16, 16 ], "texture": "#overlay", "cullface": "west" , "tintindex": 0},
                "east":  { "uv": [ 0, 8, 16, 16 ], "texture": "#overlay", "cullface": "east" , "tintindex": 0}
            }
        }
    ]
}

 

And this is the super class BlockVanillaSlab

package com.mwvanilla.blocks;

import java.util.List;
import java.util.Random;

import com.mineworld.blocks.ores.BlockOreSlab;
import com.mwvanilla.core.MWVanillaSlabs;
import com.mwvanilla.core.MWVanillaTabs;

import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockSlab;
import net.minecraft.block.BlockSlab.EnumBlockHalf;
import net.minecraft.block.material.EnumPushReaction;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.client.resources.FoliageColorReloadListener;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Enchantments;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.EnumSkyBlock;
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;

public abstract class BlockVanillaSlab extends BlockSlab
{
    public static final PropertyEnum<BlockVanillaSlab.Variant> VARIANT = PropertyEnum.<BlockVanillaSlab.Variant>create("variant", BlockVanillaSlab.Variant.class);
    private Block block;
    public BlockVanillaSlab(Block block)
    {
        super(block.getDefaultState().getMaterial());
        this.block = block;
        IBlockState iblockstate = this.blockState.getBaseState();

        if (!this.isDouble())
        {
            iblockstate = iblockstate.withProperty(HALF, BlockSlab.EnumBlockHalf.BOTTOM);
            this.setCreativeTab(MWVanillaTabs.tabVanillaSlabs);
        }

        this.setDefaultState(iblockstate.withProperty(VARIANT, BlockVanillaSlab.Variant.DEFAULT));
        this.setHardness(block.getBlockHardness(block.getDefaultState(), null, null));
        this.setResistance(block.getExplosionResistance(null));
        this.setStepSound(this.block.getStepSound());
        if(this.block.equals(Blocks.glowstone))
        	this.setLightLevel(0.75F);
        if(this.block.equals(Blocks.sea_lantern))
        	this.setLightLevel(1.0F);
	this.useNeighborBrightness = !this.isDouble();
	if(this.block.equals(Blocks.ice) || this.block.equals(Blocks.packed_ice))
		this.slipperiness = 0.98F;
	this.setTickRandomly(this.block.equals(Blocks.ice) || this.block.equals(Blocks.mycelium));
    }
    
    @Override
    public boolean isOpaqueCube(IBlockState state) {
    	return state.getMaterial().equals(Material.glass) || state.getMaterial().equals(Material.ice) ? false : super.isOpaqueCube(state);
    }
    
    @Override
    public EnumPushReaction getMobilityFlag(IBlockState state) {
    	return this.block.equals(Blocks.obsidian) ? EnumPushReaction.BLOCK : EnumPushReaction.NORMAL; 
    }
    
    @Override
    public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face)
    {
        if(this.block.getDefaultState().getMaterial().equals(Material.glass) || this.block.getDefaultState().getMaterial().equals(Material.ice))
        	return Blocks.glass.doesSideBlockRendering(state, world, pos, face);
        else
        	return super.doesSideBlockRendering(state, world, pos, face);
    }
    
    @Override
    public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos,
    		EnumFacing side) {
    	if(this.block.getDefaultState().getMaterial().equals(Material.glass) || this.block.getDefaultState().getMaterial().equals(Material.ice)) {
    		IBlockState iblockstate = blockAccess.getBlockState(pos.offset(side));
            Block block = iblockstate.getBlock();
            if (blockState != iblockstate)
            {
                return true;
            }

            if (block == this)
            {
                return false;
            }
            
            return block == this ? false : super.shouldSideBeRendered(blockState, blockAccess, pos, side);
    	}
    	return super.shouldSideBeRendered(blockState, blockAccess, pos, side);
    }
    
    @SideOnly(Side.CLIENT)
    public BlockRenderLayer getBlockLayer()
    {
        return this.block.equals(Blocks.glass) || this.block.equals(Blocks.grass) ? BlockRenderLayer.CUTOUT : this.block.equals(Blocks.stained_glass) || this.block.equals(Blocks.ice) ? BlockRenderLayer.TRANSLUCENT : BlockRenderLayer.SOLID;
    }
    
    public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
    {
    	if(this.block.equals(Blocks.ice)) {
    		player.addStat(StatList.func_188055_a(this));
            player.addExhaustion(0.025F);

            if (this.canSilkHarvest(worldIn, pos, state, player) && EnchantmentHelper.getEnchantmentLevel(Enchantments.silkTouch, stack) > 0)
            {
                java.util.List<ItemStack> items = new java.util.ArrayList<ItemStack>();
                ItemStack itemstack = this.createStackedBlock(state);

                if (itemstack != null)
                {
                    items.add(itemstack);
                }

                net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(items, worldIn, pos, state, 0, 1.0f, true, player);
                for (ItemStack is : items)
                    spawnAsEntity(worldIn, pos, is);
            }
            else
            {
                if (worldIn.provider.doesWaterVaporize())
                {
                    worldIn.setBlockToAir(pos);
                    return;
                }

                int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.fortune, stack);
                harvesters.set(player);
                this.dropBlockAsItem(worldIn, pos, state, i);
                harvesters.set(null);
                Material material = worldIn.getBlockState(pos.down()).getMaterial();

                if (material.blocksMovement() || material.isLiquid())
                {
                    worldIn.setBlockState(pos, Blocks.flowing_water.getDefaultState());
                }
            }
    	}
    }
    
    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        if (this.block.equals(Blocks.ice) && worldIn.getLightFor(EnumSkyBlock.BLOCK, pos) > 11 - this.getDefaultState().getLightOpacity())
        {
            this.func_185679_b(worldIn, pos);
        }
    }

    private void func_185679_b(World p_185679_1_, BlockPos p_185679_2_)
    {
        if (p_185679_1_.provider.doesWaterVaporize())
        {
            p_185679_1_.setBlockToAir(p_185679_2_);
        }
        else
        {
            this.dropBlockAsItem(p_185679_1_, p_185679_2_, p_185679_1_.getBlockState(p_185679_2_), 0);
            p_185679_1_.setBlockState(p_185679_2_, Blocks.water.getDefaultState());
            p_185679_1_.notifyBlockOfStateChange(p_185679_2_, Blocks.water);
        }
    }
            
    protected boolean canSilkHarvest()
    {
        return (this.block.getDefaultState().getMaterial().equals(Material.glass) || this.block.getDefaultState().getMaterial().equals(Material.ice)) && !this.isDouble() ? true : super.canSilkHarvest();
    }
    
    @Override
public boolean isFireSource(World world, BlockPos pos, EnumFacing face)
{
    	if(this.block.equals(Blocks.netherrack)) {
    		super.isFireSource(world, pos, face);    
    		if(face == EnumFacing.UP)
    		{
    			return true;
    		}
    		return false;
    	}
	return false;
}
    
    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
    	if(this.block.equals(Blocks.soul_sand)) {
    		 entityIn.motionX *= 0.4D;
    	        entityIn.motionZ *= 0.4D;
    	}
    }
    
    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(IBlockState worldIn, World pos, BlockPos state, Random rand)
    {
    	if(this.block.equals(Blocks.mycelium)) {
    		super.randomDisplayTick(worldIn, pos, state, rand);

            if (rand.nextInt(10) == 0)
            {
                pos.spawnParticle(EnumParticleTypes.TOWN_AURA, (double)((float)state.getX() + rand.nextFloat()), (double)((float)state.getY() + 1.1F), (double)((float)state.getZ() + rand.nextFloat()), 0.0D, 0.0D, 0.0D, new int[0]);
            }
    	}
    }
    
    @Override
    public int quantityDropped(IBlockState state, int fortune, Random random) {
    	return this.block.getDefaultState().getMaterial().equals(Material.glass) || this.block.getDefaultState().getMaterial().equals(Material.ice) || this.block.getDefaultState().getMaterial().equals(Material.packedIce) ? 0 : super.quantityDropped(state, fortune, random);
    }
    
    @Override
public boolean canProvidePower(IBlockState state) {
	return this.block.equals(Blocks.redstone_block);
}

@Override
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
	return  this.block.equals(Blocks.redstone_block) ? 15 : 0;
}

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(IBlockState state, Random rand, int fortune)
    {
        return this.block.getDefaultState().getMaterial().equals(Material.glass) ? null : Item.getItemFromBlock(this);
    }

    public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
    {
        return new ItemStack(Item.getItemFromBlock(this));
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public IBlockState getStateFromMeta(int meta)
    {
        IBlockState iblockstate = this.getDefaultState().withProperty(VARIANT, BlockVanillaSlab.Variant.DEFAULT);

        if (!this.isDouble())
        {
            iblockstate = iblockstate.withProperty(HALF, (meta &  == 0 ? BlockSlab.EnumBlockHalf.BOTTOM : BlockSlab.EnumBlockHalf.TOP);
        }

        return iblockstate;
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;

        if (!this.isDouble() && state.getValue(HALF) == BlockSlab.EnumBlockHalf.TOP)
        {
            i |= 8;
        }

        return i;
    }

    protected BlockStateContainer createBlockState()
    {
        return this.isDouble() ? new BlockStateContainer(this, new IProperty[] {VARIANT}): new BlockStateContainer(this, new IProperty[] {HALF, VARIANT});
    }

    /**
     * Returns the slab block name with the type associated with it
     */
    public String getUnlocalizedName(int meta)
    {
        return super.getUnlocalizedName();
    }

    public IProperty<?> getVariantProperty()
    {
        return VARIANT;
    }

    public Comparable<?> getTypeForItem(ItemStack stack)
    {
        return BlockVanillaSlab.Variant.DEFAULT;
    }
    
    public static enum Variant implements IStringSerializable
    {
        DEFAULT;

        public String getName()
        {
            return "default";
        }
    }
}

 

So how can i make this slab looks like grass? :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Posted

Don't implement

IBlockColor

/

IItemColor

on your

Block

s/

Item

s, create a separate implementation and register it with

BlockColors

/

ItemColors

from your client proxy in init. You can see how I do this here.

 

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • 4 weeks later...
Posted

Would there be a way to do this with entities?

[move]Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here Insert generic signature here[/move]

Posted

Would there be a way to do this with entities?

 

That would be a terrible idea.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Entities require more CPU, RAM, and GPU than blocks.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.