Jump to content

[1.8.9] Block states


TheTrollguy_

Recommended Posts

So recently I've been kinda playing with what Minecraft 1.8 has to offer and I've encountered some problems. The thing is I'm trying to make a block that has two properties: facing and colour. Now the only property that needs to be saved is the facing one - I save it using metadata, the colour property is determined by the tick in the world. I don't really have time to lose, so can anyone tell me what I need to do to: 1) save the first property ; 2) to determine depending on the world tick what the second property is?

Link to comment
Share on other sites

Welp, I don't know what to do anymore, I tried my best, but I can't get this to work. What I'm trying to do is switch my block's property "colour" if the tick is under specified conditions as seen in the code, but all I get is the block that does change the "facing" property, but the "colour" property is always "green". :(

Here's the code, what am I doing wrong?

 

package tt.trafficStuffMod.trafficLights;

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

import net.minecraft.block.Block;
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.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockTrafficLights extends Block
{
public static final PropertyEnum COLOUR = PropertyEnum.create("colour", BlockTrafficLights.EnumColour.class);
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

public enum EnumColour implements IStringSerializable
{
	ON(0, "off"), OFF(1, "on"), RED(2, "red"), YELLOW(3, "yellow"), GREEN(4, "green");
    private int colour_value; private String colour_name;
    
    private EnumColour(int value, String name)
    {
        this.colour_value = value;
        this.colour_name = name;
    } 
    
    public String getName() { return colour_name; }
    public int getValue() { return colour_value; }
}
private EnumColour[] colours = {EnumColour.OFF, EnumColour.ON, EnumColour.RED, EnumColour.YELLOW, EnumColour.GREEN};

public BlockTrafficLights()
{
	super(Material.iron);
	this.setUnlocalizedName("trafficLights");
	this.setCreativeTab(CreativeTabs.tabBlock);
	this.setLightLevel(0.9F);
	this.setTickRandomly(true);
	this.setDefaultState(blockState.getBaseState().withProperty(COLOUR, EnumColour.ON).withProperty(FACING, EnumFacing.NORTH));
}

@Override
public boolean isOpaqueCube()
{
	return false;
}

@Override
public boolean isFullCube()
{
	return false;
}

@SideOnly(Side.CLIENT)
public EnumWorldBlockLayer getBlockLayer()
{
	return EnumWorldBlockLayer.SOLID;
}

@Override
protected BlockState createBlockState()
{
	return new BlockState(this, new IProperty[] {COLOUR, FACING});
}

@Override
public int getMetaFromState(IBlockState blockState)
{
	return ((EnumFacing) blockState.getValue(FACING)).getHorizontalIndex();
}

public IBlockState getActualState(IBlockState blockState, IBlockAccess blockAccess, BlockPos blockPosition)
{
	return blockState.withProperty(FACING, blockState.getValue(FACING)).withProperty(COLOUR, blockState.getValue(COLOUR));
}

@Override
public int damageDropped(IBlockState blockState)
{
	return getMetaFromState(blockState);
}

@Override
public void getSubBlocks(Item item, CreativeTabs creativeTab, List list)
{
    for (int i = 0; i < 4; i++)
    	list.add(new ItemStack(item, 1, i));
}

@Override
public ItemStack getPickBlock(MovingObjectPosition movingObjectPosition, World world, BlockPos blockPosition)
{
	return new ItemStack(this, 1, this.getMetaFromState(world.getBlockState(blockPosition)));
}

@Override
public boolean getTickRandomly()
{
	return true;
}

public void updateTick(World world, BlockPos blockPosition, IBlockState blockState, Random random)
{
	super.updateTick(world, blockPosition, blockState, random);
	long tick = world.getWorldTime()%1000;
	System.out.println(tick);
	if (tick >= 0 && tick < 350 ) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[4]), 4); }
	if (tick >= 350 && tick < 370) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 370 && tick < 390) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[4]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 390 && tick < 410) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 410 && tick < 430) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[4]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 430 && tick < 450) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 450 && tick < 500) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[3]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 500 && tick < 850) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[2]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 850 && tick < 870) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 870 && tick < 890) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[2]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 890 && tick < 910) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 910 && tick < 930) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[2]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 930 && tick < 950) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[1]).withProperty(FACING, blockState.getValue(FACING)), 4); }
	if (tick >= 950 && tick < 1000) { world.setBlockState(blockPosition, blockState.withProperty(COLOUR, colours[3]), 4); }
}

public IBlockState onBlockPlaced(World world, BlockPos blockPos, EnumFacing facing, float X, float Y, float Z, int integer, EntityLivingBase entityLivingBase)
{
	if (entityLivingBase.isSneaking()) { return getDefaultState().withProperty(FACING, entityLivingBase.getHorizontalFacing().getOpposite()); }
	else { return getDefaultState().withProperty(FACING, entityLivingBase.getHorizontalFacing()); }
}
}

Link to comment
Share on other sites

I think they meant "changing" with a G.  But the question still doesn't make any sense.

 

A property is a wrapper around some other value.

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.

Link to comment
Share on other sites

I'm sorry for the misunderstanding. I was walking and talking at the same time while I was writing that. What I meant was: how do I change the "colour" state/property/whateverItIs called and if there is a method to change it since I'm not home ATM and I can't get to MC's code. I'm still not home though

 

EDIT: Looks like I'm not that good at expressing my thoughts...

Link to comment
Share on other sites

Properties are a wrapper around metadata.  To change the property you need to change the metadata.

 

You would need to get the current IBlockState, change the color (

.withProperty

I think), and set it back with world.setBlockState

 

The problem is your current block setup both:

a) ignores the fuck out of the color (

getMetaFromState

discards the color information)

b) has too many total states to encode in metadata (5 colors * 4 facings = 20 states)

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.

Link to comment
Share on other sites

I kinda feel like an idiot right now. Thank you, but here's the thing: the colour doesnt have to be saved since it is determined by the time in the world. I'm really confused now since this should kinda work like redstone dust, one block and hundreds of states. Now that I'm writing this, maybe I should look its code....

Link to comment
Share on other sites

Most of Redstone dust's "state" is based on "what other blocks around me connect to redstone dust?"

 

Your block can't use "world time" as part of its extended state because:

1) World time changes constantly and your block is not asked for its extended state constantly. The game assumes that state is fixed unless the worldstate changes (world time is not part of the world state).

2) You don't encode your color state in metadata, so changing the color state doesn't update the world state: it sees the block and metadata values being unchanged so it does nothing.

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.

Link to comment
Share on other sites

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.

×
×
  • Create New...

Important Information

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