Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Say if I want to check if a block has an AGE property, how would that be done with any block?
Not an AGE property of a singular block. Specifically if a block even has such a property.

Edited by A tired guy

BlockState has a .hasProperty() method, so I'd imagine this would work

public boolean blockHasAge(BlockState blockState) {
    return blockState.hasProperty(MyModBlockProperties.AGE);
}

 

Hm not sure what you're asking for then. Can you give a pseudocode example or explain differently what your intention is here?

  • Author

As I said in the first message, I want to check if a block even has such type of a property

nvm found a solution

Quote

 

if(state.getBlock() instanceof BushBlock )

{

 

}

 

 

  • A tired guy changed the title to [1.20.1] [SOLVED] Checking if a block has a property

may you are talking about catching all the AGE declaration even the ones from other mods custom properties ?

one would be to check only againts the minecraft BlockStateProperties class

Spoiler

				
			import net.minecraft.world.level.block.state.properties.BlockStateProperties;				
			// ########## ########## ########## ##########
		public int get_AGE() {
		if (this.level > -1) {
		return this.level;
		}
		
		BlockState blkstate = this.getBlockState();
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_1)) {
		return blkstate.getValue(BlockStateProperties.AGE_1);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_2)) {
		return blkstate.getValue(BlockStateProperties.AGE_2);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_3)) {
		return blkstate.getValue(BlockStateProperties.AGE_3);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_4)) {
		return blkstate.getValue(BlockStateProperties.AGE_4);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_5)) {
		return blkstate.getValue(BlockStateProperties.AGE_5);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_7)) {
		return blkstate.getValue(BlockStateProperties.AGE_7);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_15)) {
		return blkstate.getValue(BlockStateProperties.AGE_15);
		}
		
		if (blkstate.hasProperty(BlockStateProperties.AGE_25)) {
		return blkstate.getValue(BlockStateProperties.AGE_25);
		}
		
		return -1;
		}				
			

 

the other would be dirt your hands on the nbt data of the block and check if it have a string whit same name 

	// ##########
public String print() {
BlockState blkstate = this.getBlockState();

String json = "";

//int id = Block.getId(blkstate);

String propiedades = "";
for (Property<?> porp : blkstate.getProperties()) {

System.out.println(porp.getName() + " -> " + blkstate.getValue(porp));
propiedades += String.format( ",\"%1$s\":\"%2$s\"", porp.getName(), blkstate.getValue(porp) );
}

json = String.format("{\"blockname\":\"%1$s\",\"x\":\"%2$s\",\"y\":\"%3$s\",\"z\":\"%4$s\"%5$s}",
this.get_blkfullname(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), propiedades
);

//System.out.println("\n" + json + "\n" );
return json;
}
	

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.