Question

Ok I was trying to figure this out on my own but I am kinda stumped. I am trying to fix some code errors in a mod for minecraft. I got a lot of the fixed except for this one.

Basically this guys mod wants to gain access to an array in the superclass which it extends called field_94586_c. When the code was written that variable was public scoped. Now in the later versions it has been changed to a private scope. I was playing around with Java reflection to try and gain access to the variable so I can write the needed data to it.

Here the code snippet that applies...

@Override
@SideOnly(Side.CLIENT)
/**
 * Gets an icon index based on an item's damage value
 */
public Icon getIconFromDamage(int par1)
{

    if (par1 < 0 || par1 >= skullTypes.length) {
        par1 = 0;
    }

    return field_94586_c[par1];
}

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister)
{

    field_94586_c = new Icon[field_94587_a.length];

    for (int i = 0; i < field_94587_a.length; ++i)
        if (i >= 5)
            field_94586_c[i] = par1IconRegister.registerIcon("iguanatweakstconstruct:" + field_94587_a[i]);
        else
            field_94586_c[i] = par1IconRegister.registerIcon(getIconString() + "_" + field_94587_a[i]);
}

Basically I need to write a modified version of the array to the superclass. He adds a few new items to the array.

My original attempt was to use a "reflector" class to return an object to me. But the issue I cannot figure out how to turn the Object into a Icon[]. I tried casting it but Java complains that is not possible. I don't have the original error.

Here is my reflector class...

package iguanaman.iguanatweakstconstruct.util;

import java.lang.reflect.Field;

/**
* Created by Ian on 4/10/2014.
*/
public class IguanaReflector {

public static Object modifyAccess(final String className, final String filedName) throws SecurityException, NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException {

    final Field field = Class.forName(className).getDeclaredField(filedName);
    field.setAccessible(true);

    return field.get(Class.forName(className));

}

}

Its not much and to be honest its based on some looking around on the net.

The full package name I am trying to access is net.minecraft.item.ItemSkull and the field is field_94586_c.

Any help would be very appreciated. Thanks!

Was it helpful?

Solution

An instance field is attached to an instance. It doesn't exist on its own.

The Field#get(Object) method javadoc states

Returns the value of the field represented by this Field, on the specified object. The value is automatically wrapped in an object if it has a primitive type.

In other words, you need to call get by passing the object (the instance) on which to get the field value (or null if the field is static). You're passing the Class object for the class that the field is declared in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top