Jump to content

[1.6.4] How to use metadata of a block?


AXELTOPOLINO

Recommended Posts

Hello guys,

I've got a custom block and I'm trying to use its metadata. I'd like to store in its metadata 2 things:

1) The direction of the block (4 values: from 0 to 3)

2) If the block is ON (2 values: 0 or 1).

 

How can I do that? I know about bitwise operations, but how can I use them in this case?

 

Thanks in advance,

Axel

Link to comment
Share on other sites

The fact that you know bitwise operations helps alot in explaining this problem. You'll have to assign bits in the metadata to represent something. Let's say we use the two least significant bits for the direction, and the 3rd bit from the right (the bit representing 4) for the on/off state. You can now use this to manipulate the states:

public void setDirection(World world, int x, int y, int z, int direction){
    int onOff = world.getBlockMetadata(x, y, z) & 4; //only get the on/off state, as we want to preserve it.
    world.setBlockMetadataWithNotify(x, y, z, onOff | direction, 3);
}

public int getDirection(World world, int x, int y, int z){
    return world.getBlockMetadata(x, y, z) & 3; //mask the on/off state
}

pubic void setActivated(World world, int x, int y, int z, boolean activated){
     int direction = getDirection(world, x, y, z);
    world.setBlockMetadataWithNotify(x, y, z, direction + (activated ? 4 : 0), 3);
}

public boolean getActivated(World world, int x, int y, int z){
    return world.getBlockMetadata(x, y, z) & 4 == 4;
} 

 

The number 3 used in the setBlockMetadataWithNotify() methods are flags that define whether a change of metadata should cause the client to rerender the block, and whether the change of metadata should cause a block update for neighbouring blocks. With the 3 it's doing both, but look in the setBlock() method in Block.java for a detailed explanation of the flags.

Author of PneumaticCraft, MineChess, Minesweeper Mod and Sokoban Mod. Visit www.minemaarten.com to take a look at them.

Link to comment
Share on other sites

Many thanks MineMaarten, just to know: how can I actually mask more than 1 value?

 

For example I have:

- the 4 directions

- on/off

- open/close

 

How can I get the 4 directions preserving the on/off and the open/close? Is it something like:

public int getDirection(World world, int x, int y, int z){
    return world.getBlockMetadata(x, y, z) & 3 & 4; //mask the on/off state and the open/close state
}

 

Thanks again!

Axel

Link to comment
Share on other sites

Many thanks MineMaarten, just to know: how can I actually mask more than 1 value?

 

For example I have:

- the 4 directions

- on/off

- open/close

 

How can I get the 4 directions preserving the on/off and the open/close? Is it something like:

public int getDirection(World world, int x, int y, int z){
    return world.getBlockMetadata(x, y, z) & 3 & 4; //mask the on/off state and the open/close state
}

 

Thanks again!

Axel

 

Er, no.  That won't work.

The bitwise AND you've got there will return 0 all the time, because this happens:

 

1101 (this is a metadata value expressed in binary.

0011 (3, binary)

0100 (4, binary)

 

metadata & 3 = (1101) & (0011) -> 1 & 0 = 0, 1 & 1 = 1 -> 0001

previous result & 4 = (0001) & (0100) -> 0000

 

In order to get the 4 directions preserving on/off and open/close you'd just return the metadata value without modifying 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.

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.

Announcements



×
×
  • Create New...

Important Information

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