oωo (owo-lib)

oωo (owo-lib)

17M Downloads

[Suggestion] Annotations To Control BlockItem Settings

Brittank88 opened this issue · 4 comments

commented

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.

commented

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

commented

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...

commented

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.

commented

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;
    }