Add Inter Mod Communication to API
mcenderdragon opened this issue ยท 4 comments
it would be nice to use InterModComms so no hard dependency is needed.
e.g.
public static final String REGISTER_ITEM = "register_item";
public static final String REGISTER_ARMOR = "register_armor";
public void collectIMC(InterModProcessEvent event)
{
Stream<IMCMessage> msg = InterModComms.getMessages(Constants.MOD_ID, REGISTER_ITEM ::equals);
msg.map(m -> (Object[])m.messageSupplier().get()).forEach(this::registerItem);
msg = InterModComms.getMessages(Constants.MOD_ID, REGISTER_ARMOR ::equals);
msg.map(m -> (Object[])m.messageSupplier().get()).forEach(this::registerArmor);
}
public void registerArmor(Object[] params)
{
int paramNum = -1;
try
{
if(params.length != 3 && params.length != 4)
{
(new IllegalArgumentException("Expected params to be Object[] with length 3 or 4")).printStackTrace();
return;
}
ArmorItem[] items = (ArmorItem[]) params[paramNum=0];
ResourceLocation texture = (ResourceLocation) params[paramNum=1];
//import java.awt.Dimension;
Dimension[] textureSizes = (Dimension[]) params[paramNum=2];
if(textureSizes.length!=5)
{
(new IllegalArgumentException("Expected 3rd param to be Dimension[] with length 5")).printStackTrace();
return;
}
if(params.length==3)
{
//TODO: imo remove TextureOffset since there are tons of other classes that do this or make a record out of this (java 17 feature) and add a consrtructor for Dimension
DetailArmorBarAPI.customArmorBarBuilder()
.armor(items)
.render((ItemStack itemStack) -> new ArmorBarRenderManager(texture, textureSizes[0].width, textureSizes[0].height, new TextureOffset(textureSizes[1]), new TextureOffset(textureSizes[2]), new TextureOffset(textureSizes[3]), new TextureOffset(textureSizes[4]));)
.register()
}
else
{
Color color = = (Color) params[paramNum=3];
DetailArmorBarAPI.customArmorBarBuilder()
.armor(items)
.render((ItemStack itemStack) -> new ArmorBarRenderManager(texture, textureSizes[0].width, textureSizes[0].height, new TextureOffset(textureSizes[1]), new TextureOffset(textureSizes[2]), new TextureOffset(textureSizes[3]), new TextureOffset(textureSizes[4]), color);)
.register()
}
}
catch(ClassCastException e)
{
(new IllegalArgumentException("Exception with Param " + paramNum, e)).printStackTrace();
}
}
public void registerItem(Object[] params)
{
int paramNum = -1;
try
{
if(params.length != 3 && params.length != 4)
{
(new IllegalArgumentException("Expected params to be Object[] with length 3 or 4")).printStackTrace();
return;
}
Item item = (ArmorItem[]) params[paramNum=0];
ResourceLocation texture = (ResourceLocation) params[paramNum=1];
//import java.awt.Dimension;
Dimension[] textureSizes = (Dimension[]) params[paramNum=2];
if(textureSizes.length!=3)
{
(new IllegalArgumentException("Expected 3rd param to be Dimension[] with length 3")).printStackTrace();
return;
}
if(params.length==3)
{
//TODO: imo remove TextureOffset since there are tons of other classes that do this or make a record out of this (java 17 feature) and add a consrtructor for Dimension
DetailArmorBarAPI.customArmorBarBuilder()
.item(item)
.render((ItemStack itemStack) -> new ItemBarRenderManager(texture, textureSizes[0].width, textureSizes[0].height, new TextureOffset(textureSizes[1]), new TextureOffset(textureSizes[2]), true);)
.register()
}
else
{
Color color = = (Color) params[paramNum=3];
DetailArmorBarAPI.customArmorBarBuilder()
.item(item)
.render((ItemStack itemStack) -> new ItemBarRenderManager(texture, textureSizes[0].width, textureSizes[0].height, new TextureOffset(textureSizes[1]), new TextureOffset(textureSizes[2]), true, color);)
.register()
}
}
catch(ClassCastException e)
{
(new IllegalArgumentException("Exception with Param " + paramNum, e)).printStackTrace();
}
}
I don't think inter mod communication is really needed for this.
You could just do the following during FMLClientSetupEvent:
if (ModList.get().isLoaded("detailab")) {
DetailArmorBarSupport.register();
}
(which I also do in one of my mods btw)
The only problem I currently have with Detail Armor Bar is that I need to comment out the code inside the register()
method whenever I'm updating my mod to a newer MC version.
Personally, I would prefer having the API itself split from the mod for developers, similar to how Just Enough Items (JEI) this does. This would remove the need to have DAB (the mod) in a development environment.
I don't think inter mod communication is really needed for this. You could just do the following during FMLClientSetupEvent:
if (ModList.get().isLoaded("detailab")) { DetailArmorBarSupport.register(); }(which I also do in one of my mods btw)
The only problem I currently have with Detail Armor Bar is that I need to comment out the code inside the
register()
method whenever I'm updating my mod to a newer MC version. Personally, I would prefer having the API itself split from the mod for developers, similar to how Just Enough Items (JEI) this does. This would remove the need to have DAB (the mod) in a development environment.
IMC would solve this as you would have a soft dependency (or no depedency at all) and not a hard depedency like now.
IMC would solve this as you would have a soft dependency (or no depedency at all) and not a hard depedency like now.
Perhaps, but I personally find the idea of there being an API "abstraction layer" more appealing and cleaner to implement. It's possible I don't understand IMC correctly, but from what I see you still need DAB for compilation in your dev env.
IMC would solve this as you would have a soft dependency (or no depedency at all) and not a hard depedency like now.
Perhaps, but I personally find the idea of there being an API "abstraction layer" more appealing and cleaner to implement. It's possible I don't understand IMC correctly, but from what I see you still need DAB for compilation in your dev env.
IMC can be implemented to be without any dependencies (like thinkers did it before datapacks)