[Suggestion] Move @Unique methods out of ItemStackMixin and into a helper class
CammiePone opened this issue ยท 5 comments
So, this one's more for modders like myself, but could you move the methods you have marked as @Unique
out of your ItemStackMixin and into some helper class? I kinda need to mixin into one of the methods (addAttributes()
) but you can't mixin into a mixin, even if the method you're trying to inject into is marked as @Unique
.
Why? So I can invert the colours of the positive and negative attributes, because +Mana Lock is a bad thing, not a good thing.
A little off-topic to Trinket development but I think you should be able to mix into a unique method just fine. Just keep in mind that:
- Mixin classes don't exist at runtime
- Everything in a mixin class is applied to their target class(es) at runtime
This is why it makes sense that you can't mix into a mixin, your target simply won't exist at runtime. But it does mean you should be able to mix into the method applied (in this case added) to the target class.
Something like this should work:
@Mixin(ItemStack.class)
public abstract ItemStackMixin {
@Inject(method="addAttributes", at=@At("HEAD"))
private void doSomething([...], CallbackInfo info) {...}
}
It's probably a good idea to change your mixin's priority too, to make sure Trinkets' method has been added (not sure that's really how this works). Keep in mind however that because the method is annotated with @Unique
and is private, it will be renamed if another method with the same signature is applied to the target class before this one.
Because of all this shoddiness:
- Make sure you set require=0 on your injector, so it doesn't fail if the method can't be found (you should really do this for any non-essential injector)
- We should consider the possibility of writing an actual API for this in Trinkets
Except here's the problem: it's not called addAttributes()
at runtime if it's marked with @Unique
. @Unique
will randomly change the method's name if there's another one that's similar.
That's exactly what I said?
Keep in mind however that because the method is annotated with @unique and is private, it will be renamed if another method with the same signature is applied to the target class before this one.
It shouldn't be a problem if you have your injector marked as require=0 for the very rare case that another mixin adds a method with both the same method name and parameters.
Yea, but then things break if said mod is installed which means I get loaded with bug reports for something I have 0 control over. Players go after what is in their face with the issue, which means they only sometimes get the mod right. You'd think closing the issue once would solve that forever, but no. Players also don't read or search if their issue has been reported before.
It'd be better if I could guarantee that it never breaks, or an API be made for it as you suggested. Otherwise it's going to be an annoyance in the future.
Also you probably wanna surround the annotation with ` because you just pinged someone accidentally
+1 on this.
You should keep to a minimal amount of code in your mixins as a general practice anyway, both as a consideration to other modders as it reduces the chance of conflicts, and because you can't easily debug code that's injected by the mixin since setting break points in the mixin class will never be hit by the IDE.