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

[Suggestion] Annotations To Control BlockItem Settings #43

Open
Brittank88 opened this issue Jul 28, 2022 · 4 comments
Open

[Suggestion] Annotations To Control BlockItem Settings #43

Brittank88 opened this issue Jul 28, 2022 · 4 comments
Labels
type: enhancement this issue requests additions or enhancements to the codebase

Comments

@Brittank88
Copy link

Brittank88 commented Jul 28, 2022

Maybe there's a clean way to do this already, but I haven't yet found it.

It would be nice if when, for example, if you're defining blocks in a BlockRegistryContainer, you could use annotations to mark the OwoItemGroup and tab that they will be assigned to, etc.

It seems difficult to directly control the settings of the BlockItem that will be created when you are only able to define the Block settings.

I understand you can leverage postProcessField, but with the limits of annotations there's no clean way to, for example, point to the single existing instance of the OwoItemGroup that has been set up.

@Noaaan
Copy link
Member

Noaaan commented Jul 28, 2022

You are in luck, because this exact feature is already in owo. Here is a link to our documentation: https://docs.wispforest.io/owo/registration/#determining-block-item-settings-with-annotations

@Noaaan
Copy link
Member

Noaaan commented Jul 28, 2022

These are great suggestions, sorry for sending you to the docs when they clearly do not provide everything you asked for.
I should stop reading issues at 6am...

@Noaaan Noaaan added the type: enhancement this issue requests additions or enhancements to the codebase label Jul 28, 2022
@Brittank88
Copy link
Author

Brittank88 commented Jul 29, 2022

Haha that's totally fine - this is an awesome lib and I'm just happy that you're so open to enhancements. 😄

Just praying this will be present in the 1.18.2 version as well, as I am hoping to support 1.18.2-1.19 with my mod.

@Brittank88
Copy link
Author

Brittank88 commented Jul 29, 2022

Oh and just in case you (or anyone else) were curious, in the meantime I've found out a way to actually achieve this via reflection:

        // Assign group and tab based on annotation.
        Pair<AdAstraItemGroup, Integer> groupWithTab = ReflectionHelper.parseAssignedGroupAnnotation(field);
        if (groupWithTab != null) settings.group(groupWithTab.getLeft()).tab(groupWithTab.getRight());
    /**
     * Parses the {@link AssignedGroup} annotation of a given field.
     * <br><br>
     * Mainly used in conjunction with owo-lib's registry container system.
     *
     * @param field The field to parse.
     * @return A {@link Pair} of the {@link AdAstraItemGroup} and {@link Integer tab index},
     * or null if the field does not have the annotation or the values could not be retrieved from the annotation.
     *
     * @see AssignedGroup
     */
    public static @Nullable Pair<@NotNull AdAstraItemGroup, @NotNull Integer> parseAssignedGroupAnnotation(Field field) {

        AssignedGroup assignedGroup;
        if ((assignedGroup = field.getAnnotation(AssignedGroup.class)) == null) return null;
        else {
            AtomicReference<Pair<AdAstraItemGroup, Integer>> groupWithTab = new AtomicReference<>(null);

            AdAstraGroups.getGroups().stream()
                    .filter(g -> g.getClass().equals(assignedGroup.group()))
                    .findFirst()
                    .ifPresent(g -> groupWithTab.set(new Pair<>(g, assignedGroup.tab())));

            return groupWithTab.get();
        }
    public static ImmutableList<? extends AdAstraItemGroup> getGroups() {

        if (ALL_GROUPS == null) {
            ALL_GROUPS = ImmutableList.copyOf(
                    Arrays.stream(AdAstraGroups.class.getDeclaredFields())
                            .filter(f -> f.getType() == AdAstraItemGroup.class)
                            .map(f -> {
                                try { return (AdAstraItemGroup) f.get(AdAstraGroups.class); }
                                catch (IllegalAccessException e) { AdAstra.LOGGER.fatal("Failed to get group from field " + f.getName());  }
                                return null;
                            }).toList()
            );
        }

        return ALL_GROUPS;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement this issue requests additions or enhancements to the codebase
Projects
None yet
Development

No branches or pull requests

2 participants