AcademyCraft

AcademyCraft

1M Downloads

Improvement: Use Dependency Injection for GUI resources

WeAthFoLD opened this issue ยท 0 comments

commented

Abstract

I would like to avoid writing the following kind of initialization code

@Registrant
public class SomeGui {

    private static WidgetContainer document;

    private static IFont myFont;

    @RegInitCallback
    public static void init() {
        document = CGUIDocument.panicRead(new ResourceLocation("academy:guis/somegui.xml"));
        myFont = Resources.font();
    }

}

and use the following

@Registrant
public class SomeGui {

    @Resources.InjectDocument("somegui")
    private static WidgetContainer document;

    @Resources.InjectFont(FontType.REGULAR)
    private static IFont myFont;

}

The DI annotation will document themselves about when the instance will be injected, and users should guarantee themselves to use the instance after injection.

Reason

  • Less code, better readability
  • Those resources require a specific stage and order to load (e.g. font must be before CGUI documents). There is a implicit inconsistency introduced if we load all by ourselves. For example, out of laziness we might be tempted to get the font in static initializers of some classes, which results in loading error.