Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to cast enum into other enum?

I'm trying to make a mod for 1.7.10 (I know, outdated, but I don't think it matters much in this context) merging a few mods with CustomNPC, basically adding a few weapons that would look and work like CustomNPC weapons but made of differen mods' materials. The shields the mod provides for some reason require a certain enum in their constructor:

public class ItemShield extends ItemNpcInterface {

  public EnumNpcToolMaterial material;
  
  public ItemShield(int par1, EnumNpcToolMaterial material) {
    super(par1);
    this.material = material;
    setMaxDamage(material.getMaxUses());
    setCreativeTab((CreativeTabs)CustomItems.tabWeapon);
  }

The enum stores material types and all their values. I don't wanna edit the code of that mod iself, but I want to add a few more materials to it. Is there a way to add something to that enum or maybe cast an identical enum with different materials into this type?

I've already found a way around it by making a custom shield class that simply takes the values from my custom enum, but I'm curious if there's a way to do it without adding new classes.

EDIT

I think I'll add the mod's EnumNpcToolMaterial so it's more clear what exactly that shield constructor wants

public enum EnumNpcToolMaterial {
  WOOD(0, 59, 2.0F, 0, 15),
  STONE(1, 131, 4.0F, 1, 5),
  BRONZE(2, 170, 5.0F, 2, 15),
  IRON(2, 250, 6.0F, 2, 14),
  DIA(3, 1561, 8.0F, 3, 10),
  GOLD(0, 32, 12.0F, 1, 22),
  EMERALD(3, 1000, 8.0F, 4, 10),
  DEMONIC(3, 100, 8.0F, 6, 10),
  FROST(2, 59, 6.0F, 3, 5),
  MITHRIL(3, 3000, 8.0F, 3, 10);
  
  private final int harvestLevel;
  
  private final int maxUses;
  
  private final float efficiencyOnProperMaterial;
  
  private final int damageVsEntity;
  
  private final int enchantability;
  
  EnumNpcToolMaterial(int par3, int par4, float par5, int par6, int par7) {
    this.harvestLevel = par3;
    this.maxUses = par4;
    this.efficiencyOnProperMaterial = par5;
    this.damageVsEntity = par6;
    this.enchantability = par7;
  }
  
  public int getMaxUses() {
    return this.maxUses;
  }
  
  public float getEfficiencyOnProperMaterial() {
    return this.efficiencyOnProperMaterial;
  }
  
  public int getDamageVsEntity() {
    return this.damageVsEntity;
  }
  
  public int getHarvestLevel() {
    return this.harvestLevel;
  }
  
  public int getEnchantability() {
    return this.enchantability;
  }
}
like image 965
AHuIci Avatar asked Oct 26 '22 10:10

AHuIci


1 Answers

You're looking for EnumHelper.addEnum. While there's no officially supported way to dynamically extend Enums in Java, there's enough need to do so in modded Minecraft that Forge created a class to automatically do all of the reflection and hackery needed to do so. You'd use it like this (untested):

EnumNpcToolMaterial ADAMANT = EnumHelper.addEnum(EnumNpcToolMaterial.class, "ADAMANT", new Class<?>[]{int.class, int.class, float.class, int.class, int.class}, new Object[]{3, 5000, 15.0F, 10, 30});
like image 150
Joseph Sible-Reinstate Monica Avatar answered Nov 15 '22 04:11

Joseph Sible-Reinstate Monica