Question

i'm trying to execute a similar block of code for each class that extents my main class(BlockOre.java). The code i'm asking about is for a minecraft mod, but the question is just about Java, not minecraft. In my main class i create three new "Block" objects.

public static Block AluminumOre = new BlockAluminumOre();
public static Block VibraniumOre = new BlockVibraniumOre();
public static Block PoloniumOre = new BlockPoloniumOre();

The Classes for each of these blocks extend my class, BlockOre. In my class BlockOre, i would like to have all of the similar repetitive functions of these blocks.

@SideOnly(Side.CLIENT)
public IIcon iconOre;
@SideOnly(Side.CLIENT)
public IIcon iconNetherOre;
@SideOnly(Side.CLIENT)
public IIcon iconEndOre;
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister){
    iconOre = iconRegister.registerIcon(Reference.MODID + ":EndAluminumOre");
    iconNetherOre = iconRegister.registerIcon(Reference.MODID + ":NetherAluminumOre");
    iconEndOre = iconRegister.registerIcon(Reference.MODID + ":AluminumOre");
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(int side, int metadata ){
    int dim = Minecraft.getMinecraft().theWorld.provider.dimensionId;
    if (dim == -1)
        return iconNetherOre;
    else if(dim == 0)
        return iconOre;
    else if(dim == 1)
        return iconEndOre;
    else
        return iconOre;

}

Is what i would like them all to do. I need to create these new Icons, register them, and then return them to the function that will grab the textures for the blocks. I only have pretty basic knowledge of java and cannot seem to figure out how to change all the objects depending on what the inheriting the class is.

So to put it more simply, i need for iconOre = icon(x)Ore, where x = the class extending this one.

Ps. I know i can use "this" to get the object. I need to change the string too, How can i properly format this.toString() properly for that. It returns [Object]@XXXXXXXX depending on where is in memory. Correct?

Was it helpful?

Solution

The simplest way to do this would be to create a generic class which extends BlockOre and contains the name you need to retrieve, such as:

public class GenericBlockOre extends BlockOre
{
    final String name;

    @SideOnly(Side.CLIENT)
    public IIcon iconOre, iconNetherOre, iconEndOre;

    public GenericBlockOre(final int id, final String name)
    {
        super(id);
        this.name = name;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconRegister)
    {
        iconOre = iconRegister.registerIcon(Reference.MODID + ":End" + this.name + "Ore");
        iconNetherOre = iconRegister.registerIcon(Reference.MODID + ":Nether" + this.name + "Ore");
        iconEndOre = iconRegister.registerIcon(Reference.MODID + ":" + this.name + "Ore");
    }
}

And your items would become:

public static Block aluminumOre = new GenericBlockOre(1, "Aluminum");
public static Block vibraniumOre = new GenericBlockOre(2, "Vibranium");
public static Block poloniumOre = new GenericBlockOre(3, "Polonium");

Or if you really need to create a new class as well, the extending classes could be as simple as:

class AluminumOre extends GenericBlockOre
{
    public AluminumOre()
    {
        super(1, "Aluminum");
    }
}

N.B. Obviously you can't use 1, 2, 3 as your block IDs and need to be changed.

However, if you are absolutely certain it has to be generic and you're willing to sacrifice the efficiency of your mod for it, you could do this to maniuplate the ore name out of the class:

final AluminumOre ore = new AluminumOre();

final String className = ore.getClass().getSimpleName();
final String oreName = className.substring(0, className.indexOf("Ore"));
System.out.println(oreName); //Prints: Aluminum
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top