/*
 * This file is part of LuckPerms, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package me.lucko.luckperms.common.util;

import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

/**
 * Small utility to cache custom name lookups for enum values.
 *
 * @param <E> the enum type
 */
public class EnumNamer<E extends Enum<E>> {
    public static final Function<Enum<?>, String> LOWER_CASE_NAME = value -> value.name().toLowerCase();

    private final String[] names;

    public EnumNamer(Class<E> enumClass, Map<? super E, String> definedNames, Function<? super E, String> namingFunction) {
        E[] values = enumClass.getEnumConstants();
        this.names = new String[values.length];
        for (E value : values) {
            String name = definedNames.get(value);
            if (name == null) {
                name = namingFunction.apply(value);
            }
            this.names[value.ordinal()] = name;
        }
    }

    public EnumNamer(Class<E> enumClass, Function<? super E, String> namingFunction) {
        this(enumClass, Collections.emptyMap(), namingFunction);
    }

    public String name(E value) {
        return this.names[value.ordinal()];
    }

}

@@ -1654,16 +1654,78 @@
 %5D names;
+%0A    private final Function%3C? super E, String%3E namingFunction;
 %0A%0A    pu
@@ -2177,24 +2177,70 @@
 ;%0A        %7D%0A
+        this.namingFunction = namingFunction;%0A
     %7D%0A%0A    p
@@ -2435,16 +2435,248 @@
         
+int ordinal = value.ordinal();%0A        // support the Bukkit-Forge hack where enum constants are added at runtime...%0A        if (ordinal %3E= this.names.length) %7B%0A            return this.namingFunction.apply(value);%0A        %7D%0A        
 return t
@@ -2685,31 +2685,23 @@
 s.names%5B
-value.
 ordinal
-()
 %5D;%0A    %7D

Support Bukkit-Forge hacks which add enum constants at runtime (#2779)