Jump to content

[1.11]Problem getting the IProperty values from the BlockState


Recommended Posts

Posted

Hello! How do I get the value of the property? I have been experimenting with blockstates recently but I can't seem to pass the appropriate argument to the .getValue(IProperty<T> property) function.

if (args.length == 1 && args[0].equals("blockstate")) {
      World world = sender.getEntityWorld();
      cDroid.setPos2(cDroid.getBlockPosUnderMouse());
      sender.addChatMessage(new TextComponentString("Setting Block: " + cDroid.getPos2() + "\n"));
      System.out.println(world.getBlockState(cDroid.getPos2()));
      System.out.println(world.getBlockState(cDroid.getPos2()).getMaterial() + " THE MATERIAL");
      System.out.println(world.getBlockState(cDroid.getPos2()).getProperties() + " THE PROPERTIES");
      System.out.println(world.getBlockState(cDroid.getPos2()).getPropertyNames() + " THE PROPERTY NAMES.");
      System.out.println(world.getBlockState(cDroid.getPos2()).getBlock() + " THE BLOCK.");
    }

 

This is a bit that I have written to dive into what I can do with blockstates. All the above calls provide the expected output. My issue is with getting individual values of the properties so i can run comparisons against similar blocks. I can't seem to pass the IProperty<T> property argument to the .getValue function properly.

 

1) I have a Wool block that has the property color=lime.

2) How do I pull the value 'lime' out of the property?

 

The forge (https://mcforge.readthedocs.io/en/latest/blockstates/states/)documentation about blockstates tells me that I should pass .getValue(<PROPERTY>) to get the values of the properties. This clearly doesn't work and the compiler complains anyway I try and pass this argument. I have tried every way I can think of to pass the EnumDyeColor property to this function:

.getValue(<EnumDyeColor>) //doesn't work

.getValue(EnumDyeColor.LIME) //doesn't work

.getValue(IProperty<EnumDyeColor> EnumDyeColor.LIME) //doesn't work

For the sake of redundancy I won't post all my various attempts but none of them provide a property value (I have also tried passing PropertyEnum, the class used for the wool block colors as returned by blockstate.getProperties()). Sorry if I am missing something simple here...

 

The only thing I'm trying to do right now is (in the case of wool blocks):

If property color = X

      //do stuff

If property color = Y

    //do different stuff

 

Posted

I understand the bits of code that aren't valid java. I only posted them to illustrate that I have been attempting everything and Java is a fairly new language for me.

 

I also understand that the forge documentation is pseudocode, that doesn't mean it is clear or 'good' pseudocode.

 

This is where IProperty<?> comes in. Each Block has a set of zero or more of these objects, that describe, unsurprisingly, properties that the block have. Examples of this include color (IProperty<EnumDyeColor>), facing (IProperty<EnumFacing>), integer and boolean values, etc. Each property can have a value of the type parametrized by IProperty. For example, for the respective example properties, we can have values EnumDyeColor.WHITE, EnumFacing.EAST, 1, or false.

From the Forge documentation.

 

This confused me. If I can't use IProperty<EnumDyeColor> then how do I get the appropriate IProperty instance I want the value of?

 

Thanks.

 

 

Posted

IProperty<EnumDyeColor>

is a type, it's not an instance. A simple example:

String

is a type,

"hello"

is an instance of that type.

In the same way

IProperty<EnumDyeColor>

is the type of dye-color properties. The color property for blocks using

BlockColored

(like wool, etc.) is stored in a static field in

BlockColored

, you should be able to find it easily.

Usually you will find the properties for a block as static fields in it's implementation class or one of it's parent classes.

 

Thanks for clearing that up. I'll look into the instance. I only thought the function needed the property type, not the specific property instance.

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.