Jump to content

Recommended Posts

Posted (edited)

I want to have it that when the block state changes it changes the block model but It won't do that. Code explains it a bit at the onBlockActivated method

package com.tyrellplayz.tcm.blocks;

import java.util.Locale;

import com.tyrellplayz.tcm.EnumModBlocks;
import com.tyrellplayz.tcm.init.ModCreativeTabs;

import net.minecraft.block.Block;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.BlockLog;
import net.minecraft.block.BlockRailBase;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import scala.swing.TextComponent;
 
public class BlockTrafficLight extends ModBlock{
	
    private static AxisAlignedBB AABB = new AxisAlignedBB(0.3D, 0.0D, 0.3D, 0.7D, 1.0D, 0.7D);;
	
    public static final PropertyEnum TYPE = PropertyEnum.create("type", BlockTrafficLight.Type.class);
	
	public BlockTrafficLight() {
		super(Material.IRON, EnumModBlocks.TRAFFICLIGHT);
		setLightLevel(0.0F);
		setCreativeTab(ModCreativeTabs.roadsTab );
		this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(TYPE, Type.OFF));
	}

	@Override
	public boolean isFullBlock(IBlockState state) {return false;}
	
	@Override
	public boolean isFullCube(IBlockState state) {return false;}
	
	@Override
	public boolean isOpaqueCube(IBlockState state) {return false;}
	
	@Override
	public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
		return AABB;
	}
	
	@Override
	public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) {
		return AABB;
	}

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    @Override
    public IBlockState getStateFromMeta(int meta){
    	return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(TYPE, Type.byMetadata(meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    @Override
    public int getMetaFromState(IBlockState state){
    	return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
    }
    
    @Override
    protected BlockStateContainer createBlockState() {
    	return new BlockStateContainer(this, FACING, TYPE);
    }
    
    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
    	return state.withProperty(TYPE, Type.OFF);
    }
    
    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    	 
    	IBlockState s = state.getActualState(worldIn, pos);
    	
    	System.out.println("Value of TYPE: " + s.getValue(TYPE));
    	
    	// This should change the value of TYPE to YELLOW
    	// But it doesn't.
    	s.withProperty(TYPE, Type.YELLOW);
    	
    	// The output of this should not be the same as the first
    	// output, but it is.
    	System.out.println("Value of TYPE: " + s.getValue(TYPE));
    	
    	// This should set the block state to the block
    	// Changing the block model
    	worldIn.setBlockState(pos, s);
    	
    	return true;
    }
	
    @Override
    public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
    	IBlockState state = super.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, meta, placer, hand);
    	return state.withProperty(FACING, placer.getHorizontalFacing());
    }
	
    public static enum Type implements IStringSerializable{
        RED(0, "red"),
        YELLOW(1, "yellow"),
        GREEN(2, "green"),
        OFF(3, "off"); 

        private static BlockTrafficLight.Type[] META_LOOKUP = new BlockTrafficLight.Type[values().length];
        private int meta;
        private String name;
        
        private Type(int meta, String name){
            this.meta = meta;
            this.name = name;
        }

        public int getMetadata(){return this.meta;}

        @Override
        public String toString(){return this.name;}

        public static BlockTrafficLight.Type byMetadata(int meta){
            if (meta < 0 || meta >= META_LOOKUP.length){
                meta = 0;
            }

            return META_LOOKUP[meta];
        }

        public String getName(){return this.name;}
        
        public Type getNext() {
        	if(name().toLowerCase() == "off") {
        		return Type.RED;
        	}else if(name().toLowerCase() == "red") {
        		return Type.YELLOW;
        	}else if(name().toLowerCase() == "yellow") {
        		return Type.GREEN;
        	}else{
        		return Type.OFF;
        	}
        }

        static{
            for (BlockTrafficLight.Type blocktrafficlight$type : values()){
                META_LOOKUP[blocktrafficlight$type.getMetadata()] = blocktrafficlight$type;
            }
        }
    }
	
}

 

And if you are looking for where I register the names of the block

package com.tyrellplayz.tcm.blocks;

import com.tyrellplayz.tcm.EnumModBlocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.util.EnumFacing;

public class ModBlock extends Block {
	
    public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

	public ModBlock(Material materialIn, EnumModBlocks name) {
		super(materialIn);
		setUnlocalizedName(name.getUnlocalizedName());
		setRegistryName(name.getRegistryName());
	}

	public ModBlock(Material blockMaterialIn, MapColor blockMapColorIn, EnumModBlocks name) {
		super(blockMaterialIn, blockMapColorIn);
		setUnlocalizedName(name.getUnlocalizedName());
		setRegistryName(name.getRegistryName());
	}

}

 

Here is the  blockstate file (you don't need the model files because the models do work)

{
    "variants": {
        "facing=north,type=red":  {"model": "tcm:traffic_light_red"},
        "facing=east,type=red":  {"model": "tcm:traffic_light_red", "y": 90},
        "facing=south,type=red":  {"model": "tcm:traffic_light_red", "y": 180},
        "facing=west,type=red":  {"model": "tcm:traffic_light_red", "y": 270},
		
		"facing=north,type=yellow":  {"model": "tcm:traffic_light_yellow"},
        "facing=east,type=yellow":  {"model": "tcm:traffic_light_yellow", "y": 90},
        "facing=south,type=yellow":  {"model": "tcm:traffic_light_yellow", "y": 180},
        "facing=west,type=yellow":  {"model": "tcm:traffic_light_yellow", "y": 270}, 
		
		"facing=north,type=green":  {"model": "tcm:traffic_light_green"},
        "facing=east,type=green":  {"model": "tcm:traffic_light_green", "y": 90},
        "facing=south,type=green":  {"model": "tcm:traffic_light_green", "y": 180},
        "facing=west,type=green":  {"model": "tcm:traffic_light_green", "y": 270},
		
		"facing=north,type=off":  {"model": "tcm:traffic_light_off"},
        "facing=east,type=off":  {"model": "tcm:traffic_light_off", "y": 90},
        "facing=south,type=off":  {"model": "tcm:traffic_light_off", "y": 180},
        "facing=west,type=off":  {"model": "tcm:traffic_light_off", "y": 270}   
    }
}

 

Edited by TyrellPlayz
Posted

You say you want to change the model based on the block state, and that's not working; yet you haven't shown us your model or blockstate files?

Whatever Minecraft needs, it is most likely not yet another tool tier.

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.