Jump to content

[1.20.1] [SOLVED] Checking if a block has a property


Recommended Posts

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
Link to comment
Share on other sites

  • 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;
}
	

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.