-1

The output I am trying to access in bold.

Key: PropertyInteger{name=age, clazz=class java.lang.Integer, values=[0, 1, 2, 3, 4, 5, 6, 7]}, Value: 4

I need to access the PropertyInteger values

The code that give me the above output is

private List blockInfo() {
    ArrayList arraylist = Lists.newArrayList();
    arraylist.add("");

    BlockPos pos = this.mc.objectMouseOver.getBlockPos();
    IBlockState state = this.mc.theWorld.getBlockState(pos);           

    Iterator entries = state.getProperties().entrySet().iterator();

    while(entries.hasNext()) {
        Entry entry = (Entry) entries.next();
        Object key = entry.getKey();
        Object value = entry.getValue();            

        System.out.println("Key: " + key + ", Value: " + value);

    }     

    return arraylist;
}

How do I go about accessing the data held in the key?

Thanks.

  • `Iterator entries = ... ` etc. perhaps? – Anders R. Bystrup May 18 '15 at 10:17
  • First of all ... don't use [raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). And since `PropertyInteger` looks like one of your own classes, you should know how to access one of its properties. – Tom May 18 '15 at 10:22

2 Answers2

0

What probably helps is a good share of generics.

What types are the key and value in the entries? By now I'll use KeyType and ValueType stubs.

Are classes BlockPos and IBlockState your own of from some library? Please provide a link or code fragments.

private List<String> blockInfo() {

//if you're going to add Strings, parametrize it
List<String> arraylist = Lists.newArrayList();
arraylist.add("");

BlockPos pos = this.mc.objectMouseOver.getBlockPos();
IBlockState state = this.mc.theWorld.getBlockState(pos);           

//to achieve this, the state.getProperties().entrySet() should be parametrized too
//Entry<KeyType, ValueType> presupposes that there's a Map<KeyType, ValueType> 
//somewhere in state.getProperties()
Iterator<Entry<KeyType, ValueType>> entries = state.getProperties().entrySet().iterator();

while(entries.hasNext()) {
    //no cast needed now
    Entry<KeyType, ValueType> entry =  entries.next();
    KeyType key = entry.getKey();
    ValueType value = entry.getValue();            

    //Here you have the exact types and can retrieve any information.

}     

return arraylist;

}

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
0

You should use HashMap instead of List, is faster than List and return value by the key.

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54