package de.hype.bbsentials.constants.enviromentShared; import de.hype.bbsentials.constants.BBDisplayNameProvider; import de.hype.bbsentials.constants.BBDisplayNameProviderWithCustom; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class EnumUtils { public static List getAllDisplayNames(Class enumClass) { List displayNames = new ArrayList<>(); for (BBDisplayNameProvider item : enumClass.getEnumConstants()) { displayNames.add(item.getDisplayName()); } return displayNames; } public static & BBDisplayNameProvider> List getEnumsAsList(Class enumClass) { List enumList = new ArrayList<>(); for (T item : enumClass.getEnumConstants()) { enumList.add(item); } return enumList; } public static List getDisplayNames(Collection itemList) { List displayNames = new ArrayList<>(); for (BBDisplayNameProvider item : itemList) { displayNames.add(item.getDisplayName()); } return displayNames; } public static List getEnumsAsList(List itemList) { List enumList = new ArrayList<>(itemList); return enumList; } public static List getAllEnumNames(Class> enumClass) { List enumNames = new ArrayList<>(); Enum[] enumConstants = enumClass.getEnumConstants(); for (Enum enumConstant : enumConstants) { enumNames.add(enumConstant.name()); } return enumNames; } // public interface BBDisplayNameProvider { // String getDisplayName(); // default public String serialize() { // return name() + ":" + displayName; // } // // default public ChChestItems deserialize(String serializedValue) { // String[] parts = serializedValue.split(":"); // if (parts.length != 2) { // throw new IllegalArgumentException("Invalid serialized value format"); // } // String enumName = parts[0]; // String displayName = parts[1]; // // return ChChestItems.valueOf(enumName).setDisplayName(displayName); // } // } // Methods for BBDisplayNameProviderWithCustom public static & BBDisplayNameProviderWithCustom> T getEnumByNameWithCustom(Class enumClass, String enumName) { boolean found = false; for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.name().equals(enumName)) { return (enumValue); } } return createCustomEnum(enumClass, enumName); } public static & BBDisplayNameProviderWithCustom> T getEnumByValueWithCustom(Class enumClass, String value) { for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.getDisplayName().equals(value)) { return enumValue; } } return (createCustomEnum(enumClass, value)); } public static & BBDisplayNameProviderWithCustom> T[] getEnumsByNameWithCustom(Class enumClass, String[] names) { List matchingEnums = new ArrayList<>(); for (String name : names) { boolean found = false; for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.name().equals(name)) { matchingEnums.add(enumValue); found = true; break; } } if (!found) { matchingEnums.add(createCustomEnum(enumClass, name)); } } return matchingEnums.toArray((T[]) java.lang.reflect.Array.newInstance(enumClass, 0)); } public static & BBDisplayNameProviderWithCustom> T[] getEnumsByValueWithCustom(Class enumClass, String[] values) { List matchingEnums = new ArrayList<>(); for (String value : values) { boolean found = false; for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.toString().equals(value)) { matchingEnums.add(enumValue); found = true; break; } } if (!found) { matchingEnums.add(createCustomEnum(enumClass, value)); } } return matchingEnums.toArray((T[]) java.lang.reflect.Array.newInstance(enumClass, 0)); } // Methods for BBDisplayNameProvider public static & BBDisplayNameProvider> T getEnumByName(Class enumClass, String enumName) { try { return Enum.valueOf(enumClass, enumName); } catch (IllegalArgumentException e) { return null; // Enum value not found } } public static & BBDisplayNameProvider> T getEnumByValue(Class enumClass, String value) { for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.getDisplayName().equals(value)) { return enumValue; } } return null; } public static & BBDisplayNameProvider> T[] getEnumsByName(Class enumClass, String[] names) { List matchingEnums = new ArrayList<>(); for (String name : names) { boolean found = false; for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.name().equals(name)) { matchingEnums.add(enumValue); found = true; break; } } } return matchingEnums.toArray((T[]) java.lang.reflect.Array.newInstance(enumClass, 0)); } public static & BBDisplayNameProvider> T[] getEnumsByValue(Class enumClass, String[] values) { List matchingEnums = new ArrayList<>(); for (String value : values) { boolean found = false; for (T enumValue : enumClass.getEnumConstants()) { if (enumValue.getDisplayName().equals(value)) { matchingEnums.add(enumValue); found = true; break; } } } return matchingEnums.toArray((T[]) java.lang.reflect.Array.newInstance(enumClass, 0)); } private static & BBDisplayNameProviderWithCustom> T createCustomEnum(Class enumClass, String value) { T customEnum = null; try { customEnum = Enum.valueOf(enumClass, "Custom"); } catch (Exception ignored) { try { customEnum = Enum.valueOf(enumClass, "CUSTOM"); } catch (Exception ignored2) { } } if (customEnum == null) { } customEnum.setDisplayName(value); return customEnum; } }