001package org.mcaccess.minecraftaccess.api;
002
003import com.mojang.blaze3d.platform.InputConstants;
004import net.minecraft.resources.Identifier;
005import org.jetbrains.annotations.ApiStatus;
006import org.jetbrains.annotations.Contract;
007import org.jetbrains.annotations.NotNull;
008
009import org.mcaccess.minecraftaccess.MainClass;
010
011/**
012 * Registry for registering items with Minecraft Access.
013 *
014 * <p>Any usage outside of {@link MinecraftAccessAddon#init(AddonRegistry)} is unsupported and may break at any time.
015 *
016 * @see MinecraftAccessAddon#init(AddonRegistry)
017 * @since 1.12.0
018 */
019public final class AddonRegistry {
020    private final String modid;
021
022    /** @hidden */
023    @ApiStatus.Internal
024    public AddonRegistry(String modid) {
025        this.modid = modid;
026    }
027
028    /**
029     * Registers a {@link Status}.
030     *
031     * @param identifier A unique identifier for the {@link Status}. This is automatically namespaced by mod ID.
032     * @param status The {@link Status} to register.
033     * @throws IllegalArgumentException If a {@link Status} is already registered with the provided identifier and the same mod ID.
034     * @throws UnsupportedOperationException Registries are already frozen.
035     * @see Status
036     * @since 1.12.0
037     */
038    public void register(@NotNull String identifier, @NotNull Status status) {
039        MainClass.register(Status.class, Identifier.fromNamespaceAndPath(modid, identifier), status);
040    }
041
042    /**
043     * Registers an {@link AccessMenuFunction}.
044     *
045     * @param identifier A unique identifier for the {@link AccessMenuFunction}. This is automatically namespaced by mod ID.
046     * @param function The {@link AccessMenuFunction} to register.
047     * @return An {@link AccessMenuFunctionRegistration} to set additional optional properties.
048     * @throws IllegalArgumentException If an {@link AccessMenuFunction} is already registered with the provided identifier and the same mod ID.
049     * @throws UnsupportedOperationException Registries are already frozen.
050     * @see AccessMenuFunction
051     * @since 1.12.0
052     */
053    public AccessMenuFunctionRegistration register(@NotNull String identifier, @NotNull AccessMenuFunction function) {
054        MainClass.register(AccessMenuFunction.class, Identifier.fromNamespaceAndPath(modid, identifier), function);
055        return new AccessMenuFunctionRegistration(Identifier.fromNamespaceAndPath(modid, identifier));
056    }
057
058    /**
059     * Registers a {@link WorldNarrator}.
060     *
061     * @param identifier A unique identifier for the {@link WorldNarrator}. This is automatically namespaced by mod ID.
062     * @param worldNarrator The {@link WorldNarrator} to register.
063     * @throws IllegalArgumentException If a {@link WorldNarrator} is already registered with the provided identifier and the same mod ID.
064     * @throws UnsupportedOperationException Registries are already frozen.
065     * @see WorldNarrator
066     * @since 1.12.0
067     */
068    public void register(@NotNull String identifier, @NotNull WorldNarrator worldNarrator) {
069        MainClass.register(WorldNarrator.class, Identifier.fromNamespaceAndPath(modid, identifier), worldNarrator);
070    }
071
072    /**
073     * Allows setting additional optional properties on registered access menu functions.
074     *
075     * @since 1.12.0
076     */
077    public static final class AccessMenuFunctionRegistration {
078        private final Identifier identifier;
079
080        AccessMenuFunctionRegistration(Identifier identifier) {
081            this.identifier = identifier;
082        }
083
084        /**
085         * Sets the default keybind for this function.
086         *
087         * @param keycode The primary keycode for the binding from {@link InputConstants}.
088         * @param modifiers Additional modifiers which must also be held.
089         *                  Supports {@link InputConstants#MOD_SHIFT}, {@link InputConstants#MOD_CONTROL}, {@link InputConstants#MOD_ALT}
090         *                  in addition to keycodes from {@link InputConstants}.
091         * @return This
092         * @see InputConstants
093         * @since 1.12.0
094         */
095        @Contract("_, _ -> this")
096        public AccessMenuFunctionRegistration withDefaultKeybind(int keycode, int @NotNull ... modifiers) {
097            MainClass.register(DefaultKeybind.class, identifier, new DefaultKeybind(keycode, modifiers));
098            return this;
099        }
100
101        /** @hidden */
102        @ApiStatus.Internal
103        public static final class DefaultKeybind {
104            /** @hidden */
105            @ApiStatus.Internal
106            public final int keycode;
107            /** @hidden */
108            @ApiStatus.Internal
109            public final int[] modifiers;
110
111            private DefaultKeybind(int keycode, int[] modifiers) {
112                this.keycode = keycode;
113                this.modifiers = modifiers;
114            }
115        }
116    }
117}