Jump to content

Recommended Posts

Posted

I have this code

	public IBlockState getStateFromMeta(int meta)
	{
		IBlockState iblockstate = this.getDefaultState();
		   /**
	     * Get the Index of this Facing (0-5). The order is D-U-N-S-W-E
	     */
		switch(meta) {
			case 0:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.DOWN);
				break;
			case 1:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.UP);
				break;
			case 2:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.NORTH);
				break;
			case 3:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.SOUTH);
				break;
			case 4:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.WEST);
				break;
			case 5:
				iblockstate = iblockstate.withProperty(FACING,EnumFacing.EAST);
				break;				
			
		}

		return iblockstate;
	}

 

Isn't there a better way to do this?  There is the  "EnumFacing.getHorizontal(meta))" but i need the UP and DOWN also

Posted (edited)

Have you tried a simple iteration over the enum's values, comparing to their ordinals?

 

for (EnumFacing face : EnumFacing.values()) {
	if (meta == face.ordinal()) {
		iblockstate = iblockstate.withProperty(FACING, face);
		break;
	}
}

Or, even smaller, just use the array index directly:

iblockstate = iblockstate.withProperty(FACING, EnumFacing.values()[meta]);

You might want to do some sanitizing on that version, but it's pretty concise as-is.

Edited by IceMetalPunk

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

Posted (edited)

Seriously?

Can't you people even look at the Enum? Or other blocks that use it?

EnumFacing.getFront(meta)

 


    /**
     * Get a Facing by it's index (0-5). The order is D-U-N-S-W-E. Named getFront for legacy reasons.
     */
    public static EnumFacing getFront(int index)
    {
        return VALUES[MathHelper.abs(index % VALUES.length)];
    }

 

Edited by Draco18s
  • Like 2

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 (edited)

The takeaway lesson here: Use your IDE to explore and walk through the vanilla classes to see:

 

  • A) What's on offer.
  • B) How vanilla uses things
Edited by jeffryfisher

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted

I solved it this way on block placing

 

Horizontal placing, like furnace.

	@Override
	public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing blockFaceClickedOn, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
	{
		  // find the quadrant the player is facing
		EnumFacing enumfacing = (placer == null) ? EnumFacing.NORTH : EnumFacing.fromAngle(placer.rotationYaw);

		  // Place allways same up and down direction
		return this.getDefaultState().withProperty(PROPERTYFACING, enumfacing);
	}		  		

 

 

And every direction placing, like piston

	@Override
	public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing blockFaceClickedOn, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
	{
		  return this.getDefaultState().withProperty(PROPERTYFACING, BlockPistonBase.getFacingFromEntity(pos, placer));
		  
	}		  		

 

Posted

PROPERTYFACING is set like so

public static final PropertyDirection PROPERTYFACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

 

And you need a json in blockstates, that has poperties facing and 6 models in block, one for each side.

 

Posted

Sigh.

 

public static final PropertyDirection PROPERTYFACING = BlockHorizontal.FACING;

 

Magic, you're done here. The property already exists, utilize it.

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.