Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Dependency Injection for service initialization #707

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

oh-noey
Copy link
Collaborator

@oh-noey oh-noey commented Sep 1, 2024

Describe in detail what your pull request accomplishes

  • Use Dirk as a Dependency Injection (DI) framework to allow for simpler usage of the inversion of control pattern.
  • Adds several building block life cycle management systems to abstract away common patterns
    • Service for startup/stop patterns
    • Worker for an async/sync background processors
  • Simplifies event handler registration by automatically doing so once added to the DI container.
  • Abstract version management concepts via inversion of control
    • Dependents can use VersionInfo to access version related data
  • Abstract settings lifecycle management via a Service
    • Eventually it would be good to migrate Settings to be injected, but that is out of scope for now
  • Migrate several small startup routines to Service singletons
  • Migrate all startup BukkitRunnable implementations to Worker
  • TODO: Add unit test

Checklist

  • Unit tests
  • Proper internationalization
  • Tested
  • Performance tested

Comment on lines +114 to +127
public @NotNull WorldHandler getWorldHandler(){
return application.container().getService(WorldHandler.class);
}

public SmoothTeleport getSmoothTeleport() {
return smoothTeleport;
public @NotNull SmoothTeleport getSmoothTeleport() {
return application.container().getService(SmoothTeleport.class);
}

public AsyncManager getAsyncManager() {
return asyncManager;
public @NotNull AsyncManager getAsyncManager() {
return application.container().getService(AsyncManager.class);
}

public @NotNull WreckManager getWreckManager(){
return wreckManager;
return application.container().getService(WreckManager.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate these and instead expose getService?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah but it might be good to expose an API class that holds this stuff or something - exposing the DI container is an anti-pattern typically

Comment on lines +8 to +10
boolean isAsync();

int getPeriod();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we default these to false and 1 second?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the usages internally, they mostly are 1 tick services. However, for users of the API I think it makes more sense to maintain a 1 second default so as to not provide a possible foot-gun.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really intended for external users/addons - if we do see value here I would probably move this stuff to its own package and build it out a bit more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of items in the addons that could utilize this type of thing. I'd like to see us build it in such a way it can be used internally and externally.


import org.jetbrains.annotations.NotNull;

public record VersionInfo(String version) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call this MinecraftVersionInfo to reduce confusion with Movecraft versioning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point


import java.util.logging.Logger;

public class VersionProvider implements Provider<VersionInfo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here

Comment on lines +23 to 26
@Deprecated
public static @NotNull String getPackageName(@NotNull String minecraftVersion) {
String[] parts = minecraftVersion.split("\\.");
if (parts.length < 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is no longer used we should remove it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its still used for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be merged into Settings as one class? I think it'd be far easier to maintain if they are together.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent is to change Settings to eventually be non-static. seperating them helps for this purpose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants