Jump to content

Recommended Posts

Posted

I've searched through videos and websites, as well as the forge docs, but I can't find how to properly add a custom blockstate to my custom-modeled block. I'm trying to change the texture of the block whenever I right click it, but nothing I add in seems to work correctly. I keep getting the correct hitbox, but no model or texture is recognized. What is the proper way to do this for the newest version?

Posted (edited)

Luckily there's a vanilla block that has this functionality already: the daylight detector. The blockstate for that looks like:

{
  "variants": {
    "inverted=false": {
      "model": "minecraft:block/daylight_detector"
    },
    "inverted=true": {
      "model": "minecraft:block/daylight_detector_inverted"
    }
  }
}

So you can adapt your blockstate to use true/false flags like that. Now we need to look at the class file for the daylight detector to see how the tags are implemented. I've added the relevant parts here.

public static final BooleanProperty INVERTED = BlockStateProperties.INVERTED;

public DaylightDetectorBlock(AbstractBlock.Properties properties) {
        super(properties);
        this.setDefaultState(this.stateContainer.getBaseState().with(INVERTED, Boolean.FALSE));
    }

public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (player.isAllowEdit()) {
            if (worldIn.isRemote) {
                return ActionResultType.SUCCESS;
            } else {
                BlockState blockstate = state.func_235896_a_(INVERTED);
                worldIn.setBlockState(pos, blockstate, 4);
                return ActionResultType.CONSUME;
            }
        } else {
            return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
        }
    }

 

Edited by urbanxx001
  • Thanks 1
Posted

Thanks for the help! I just realized my mistake: for some reason, in the block.json file, if I insert a space between two attributes, it doesn't recognize the second one. I fixed it, and now my block works perfectly

Posted (edited)

Do you mean so that when its toggled, it just stays always in that state? just use the value of your boolean property then...the first time the block gets toggled your boolean property will become true (or false, depends on the default value you set before)..you have to make it so that the next time it won't toggle if the boolean property has already changed

Edited by Beethoven92

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Posted
1 hour ago, Beethoven92 said:

Do you mean so that when its toggled, it just stays always in that state? just use the value of your boolean property then...the first time the block gets toggled your boolean property will become true (or false, depends on the default value you set before)..you have to make it so that the next time it won't toggle if the boolean property has already changed

The if statement won't let me compare a BooleanProperty to a Boolean because apparently they're two separate things, and I have no idea how to fix this.

Posted (edited)

A BooleanProperty is an object that says "a blockstate is allowed to have two values: true and false."

It is not, itself, the state of the block in the world. Getting a bool from the blockstate is simple: you ask the blockstate for its value.

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

Nevermind, I had a different name specified in my arguments, so for me it was just state.get(). All works find now, thanks for the help.

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.