5

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;
  }
}
AHuIci
  • 51
  • 2
  • You want to cast from which to which material class/enum ? – Elikill58 Oct 28 '21 at 09:57
  • @Elikill58 I want to use my custom identical enum, or at least my own custom values in place of the one coded in the shield's constructor when creating my custom shields – AHuIci Oct 28 '21 at 10:20
  • 2
    `YourOtherEnum.valueOf(otherEnum.name())` or something – Elikill58 Oct 28 '21 at 10:24
  • Still can't get it to work, there's no error in eclipse, but the game won't start and says that there's "No enum constant". I'm not exactly sure if I did everything right tho cause I'm pretty new to java – AHuIci Oct 28 '21 at 10:52
  • 2
    @AHuIci: you can't really add to an existing enum without modifying the code and recompiling it. Enums are proper types in Java and not just glorified integer values (like they are in some other languages). That means you also can't just "cast one enum to another" like this. If you *don't* need existing code to work with your new values, you can maybe do some workarounds, but it seems like that's not sufficient for you. – Joachim Sauer Oct 28 '21 at 10:55
  • Java enums are not extensible except by editing the definition of the enum and adding new values. Check out the accepted answer to this question: https://stackoverflow.com/questions/9614282/how-to-create-an-instance-of-enum-using-reflection-in-java – DwB Oct 28 '21 at 14:53

1 Answers1

2

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});
  • 5
    That's fascinating and disgusting at the same time ;-) It's probably also one of the reasons why I wouldn't enjoy getting into modding. – Joachim Sauer Oct 28 '21 at 19:11