Jump to content

[1.11] Block with two properties?


Recommended Posts

Posted

A while ago I was having trouble with a block always facing north. This is fixed now, however now I'm on to a new block which requires two properties: Facing and Active. Facing is an EnumFacing and Active is a boolean. I am stuck with what to do with Block#getMetaFromState and Block#getStateFromMeta.

 

My block code so far:

public class BlockPurifier extends BlockTileEntity<TileEntityPurifier> {

    public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
    public static final PropertyBool ACTIVE = PropertyBool.create("active");

    public BlockPurifier() {
        super(Material.ROCK, "purifier");

        IBlockState defaultState = this.blockState.getBaseState();
        defaultState.withProperty(FACING, EnumFacing.NORTH);
        defaultState.withProperty(ACTIVE, false);

        this.setDefaultState(defaultState);

        setHardness(3f);
        setResistance(5f);
        setHarvestLevel("pickaxe", 0);
    }

    @Override
    public Class<TileEntityPurifier> getTileEntityClass() {
        return TileEntityPurifier.class;
    }

    @Nullable
    @Override
    public TileEntityPurifier createTileEntity(World world, IBlockState state) {
        return new TileEntityPurifier();
    }

    @Override
    public BlockStateContainer createBlockState() {
        return new BlockStateContainer(this, FACING);
    }

    @Override
    public int getMetaFromState(IBlockState state) {
        return ((EnumFacing)state.getValue(FACING)).getHorizontalIndex(); //How do I add a boolean to this?
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); //How do I add a boolean to this?
    }

    @Override
    public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack) {
        EnumFacing entityFacing = entity.getHorizontalFacing();

        if(!world.isRemote) {
            if(entityFacing == EnumFacing.NORTH) {
                entityFacing = EnumFacing.SOUTH;
            } else if(entityFacing == EnumFacing.EAST) {
                entityFacing = EnumFacing.WEST;
            } else if(entityFacing == EnumFacing.SOUTH) {
                entityFacing = EnumFacing.NORTH;
            } else if(entityFacing == EnumFacing.WEST) {
                entityFacing = EnumFacing.EAST;
            }

            world.setBlockState(pos, state.withProperty(FACING, entityFacing), 2);
        }
    }
}

Posted

If your block has 6 facings, you need to add 8 (or better yet, boolean-or with 8 ) when getting the meta.

If it has 4 (BlockHorizontal) then you can get away with adding 4.

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

What those worthies are saying is that meta has 4 bits, and facing is going to use up 2 or 3 of them. That leaves you at least the 1 bit needed for a boolean value. With that much Forge knowledge, you should be able to figure out a way to pack both values into the 4-bit meta and then unpack them again (that's fundamental programming technique, sometimes taught here, but you should get into the habit of finding external references for standard techniques).

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

Thanks for the advice everyone. I don't have access to my code right now, so I'll have an update for you when I try that.

Posted (edited)
On 2/7/2017 at 8:17 PM, Daeruin said:

See definition #2: https://www.merriam-webster.com/dictionary/worthies

"a worthy or prominent person"

I looked at that guess i didn't scroll down far enough; thanks.

Edited by Animefan8888
Typo

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

So I have finally gotten some time away from my course load to do some modding again. I am having trouble with decoding the meta. Would subtract 4 and check if the facing value is valid? I'm not sure how to do it.

Posted

Does your block have 4 facings (like the furnace) or 6 (like the piston)? If you have 4 facings, you can add/subtract 4 to get the other value, if you need 6 you'd have to add 8. Or better, use bitwise operators (look at BlockOldLeaves for an example in Block#getStateFromMeta and Block#getMetaFromState).

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Don't subtract. Wrong. Bad.

 

int eightBit = (meta & 7);

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)

Ok. So would this work? I can't test it because the models/textures aren't finished.

 

@Override
    public int getMetaFromState(IBlockState state) {
        int meta = 0;
        
        meta = meta | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();

        if (!state.getValue(ACTIVE)) {
            meta |= 4;
        }

        return meta;
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(ACTIVE, (meta & 4) == 0);
    }

 

Edited by Awesome_Spider
Posted
29 minutes ago, larsgerrits said:

Does your block have 4 facings (like the furnace) or 6 (like the piston)?

Oh, and my block has four facings.

Posted

Man, if only the forums worked and I could view spoilers...

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

You can't? Here:

@Override
    public int getMetaFromState(IBlockState state) {
        int meta = 0;

        meta = meta | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();

        if (!state.getValue(ACTIVE)) {
            meta |= 4;
        }

        return meta;
    }

    @Override
    public IBlockState getStateFromMeta(int meta) {
        return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(ACTIVE, (meta & 4) == 0);
    }

 

Posted

Won't work when ACTIVE is false, because your meta -> facing will pass in a value greater than 4, which the enum won't know what to do with.  You need to &3 that bit to strip off the 4th bit.

 

(And no, I can't view spoilers, I don't know why. The JS on this forum is broken as all getout for me: I can't start new threads, I can't use post preview, and I can't open spoilers)

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
9 hours ago, Draco18s said:

(And no, I can't view spoilers, I don't know why. The JS on this forum is broken as all getout for me: I can't start new threads, I can't use post preview, and I can't open spoilers)

 

I had this issue on another site once, and it resolved once I removed a Chrome extension related to pop-ups. Do you maybe have a Firefox (?) extension installed blocking it? Or try disabling each extension one-by-one and see if that fixes it.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted (edited)
1 hour ago, larsgerrits said:

I had this issue on another site once, and it resolved once I removed a Chrome extension related to pop-ups. Do you maybe have a Firefox (?) extension installed blocking it? Or try disabling each extension one-by-one and see if that fixes it.

That gives me a result that a Greasemonkey script that I had to strip out the image in someone's (annoyingly large) signature is at fault.

 

By which I mean, an empty Greasemonkey script breaks everything (as in, only comments, and disabling it makes spoilers work). Which makes no fucking sense.  At all.  And tells me, once again, that this forum's javascript is shitty.  The infinite recursion trying to start a new thread was the first red flag.

 

Huh, turns out the infinite recursion is also because of the greasemonkey script.

 

That's fucking idiotic.

Edited by Draco18s

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)

I went over to Invision Power Services to complain/file a bug report, only to not know the password to my existing account (I can't make an account because my username AND email are in use by an existing account) but I can't reset my password because there are no user accounts using that email address.  What.

 

Oh, and despite my language and apparent distaste for this forum software, it's not the worst forum software I've had the displeasure of using.  Nope, that'd be the wreck that is the Dungeon Defenders 2 forums, built by Duxter. Everything uses AJAX, which means navigating pages skips over your browser history.  It also means they had to hack together a way to link to specific pages/posts, using redirects (further fucking over the browser history).  Page numbers above 10 display as the number 10.  There's no "report to moderator" feature.  At one point removing the flag that marks someone as a troll (moderator feature, muting that person's posts) also made that person a moderator.  I'm not making that up.

Edited by Draco18s

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

What are you using the active boolean for? My custom furnace has a boolean isBurning that is not encoded into the metadata but still works.

Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps.

Posted
Just now, Leomelonseeds said:

What are you using the active boolean for? My custom furnace has a boolean isBurning that is not encoded into the metadata but still works.

"And still works" covers a lot of ways it could be handled. Vanilla's furnace uses two whole blocks for it, whereas a TE could store the value and it could be retrieved using getActualState, so that statement by itself isn't actually helpful.  Or even necessarily related.  There's lots of reasons one might want to have two properties on their block.

 

I've got one with three.

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

Ok. Thanks. Now how would I change my blockstate json file to incorporate this extra property? I have an animated texture for when "active" is true and a normal one when "active" is false.

 

{
  "forge_marker": 1,
  "defaults": {
    "textures": {
      "front": "roboticraft:blocks/purifier_front_inactive",
      "top": "roboticraft:blocks/purifier_side",
      "side": "roboticraft:blocks/steam_engine_side",
      "particle": "roboticraft:blocks/steam_engine_side"
    }
  },
  "variants": {
    "normal": {
      "model": "orientable"
    },
    "inventory": {
      "model": "orientable"
    },
    "facing=north": {
      "model": "orientable"
    },
    "facing=south": {
      "model": "orientable",
      "y": 180
    },
    "facing=west": {
      "model": "orientable",
      "y": 270
    },
    "facing=east": {
      "model": "orientable",
      "y": 90
    }
  }
}

 

Btw, I understand you not liking this new software. I have this issue when visiting the site on IPad, for a while we couldn't edit posts, the BBCode capabilities are lacking, putting any text after spoilers puts the text in the spoiler instead of on the outside, the forum doesn't notify through email anymore, etc. They are working it out I'm sure, it is a new software after all.

Posted
1 minute ago, Awesome_Spider said:

the forum doesn't notify through email anymore

 
 

You can change every notification setting here: http://www.minecraftforge.net/forum/notifications/options/

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted (edited)

Now that I look at my email, it does. My email isn't notifying me of new emails. Sorry, that one is not on Forge's end.

Edited by Awesome_Spider
Typo
Posted (edited)

Oops. I had copied my other machine's blockstate file and didn't change every texture path over. That's embarrassing. Here is the updated json file:

 "defaults": {
    "textures": {
      "front": "roboticraft:blocks/purifier_front_inactive",
      "top": "roboticraft:blocks/purifier_side",
      "side": "roboticraft:blocks/purifier_side",
      "particle": "roboticraft:blocks/purifier_side"
    }
  },
  "variants": {
    "normal": {
      "model": "orientable"
    },
    "inventory": {
      "model": "orientable"
    },
    "facing=north": {
      "model": "orientable"
    },
    "facing=south": {
      "model": "orientable",
      "y": 180
    },
    "facing=west": {
      "model": "orientable",
      "y": 270
    },
    "facing=east": {
      "model": "orientable",
      "y": 90
    }
  }
}

 

Does anyone know how I add another property to this?

Edited by Awesome_Spider

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.