diff --git a/src/main/java/codechicken/nei/CollapsibleItems.java b/src/main/java/codechicken/nei/CollapsibleItems.java index 9d67caab..bc6d788f 100644 --- a/src/main/java/codechicken/nei/CollapsibleItems.java +++ b/src/main/java/codechicken/nei/CollapsibleItems.java @@ -67,6 +67,16 @@ public class CollapsibleItems { } } + public static class ApiGroup { + + private String unlocalizedName; + + public ApiGroup setName(String unlocalizedName) { + this.unlocalizedName = unlocalizedName; + return this; + } + } + private static final String STATE_KEY = "collapsibleitems"; protected static File statesFile; @@ -107,36 +117,42 @@ public class CollapsibleItems { loadStates(); } - public static void registerGroup(String unlocalizedName, String filter) { + public static ApiGroup registerGroup(String filter) { + final ApiGroup apiGroup = new ApiGroup(); + CollapsibleItems.apiGroups.add(() -> { GroupItem group = new GroupItem(); group.setFilter(filter); - group.unlocalizedName = unlocalizedName; + group.unlocalizedName = apiGroup.unlocalizedName; return group; }); + + return apiGroup; } - public static void registerGroup(String unlocalizedName, Iterable items) { + public static ApiGroup registerGroup(Iterable items) { + final ApiGroup apiGroup = new ApiGroup(); final ItemStackSet filter = new ItemStackSet(); for (ItemStack stack : items) { if (stack != null) filter.add(stack.copy()); } - if (filter.isEmpty()) return; + if (filter.isEmpty()) return apiGroup; - final String key = unlocalizedName + ";" - + filter.keys().stream() - .map(stack -> Item.itemRegistry.getNameForObject(stack.getItem()) + ":" + stack.getItemDamage()) - .sorted().collect(Collectors.joining(",")); + final String key = filter.keys().stream() + .map(stack -> Item.itemRegistry.getNameForObject(stack.getItem()) + ":" + stack.getItemDamage()) + .sorted().collect(Collectors.joining(",")); final String guid = UUID.nameUUIDFromBytes(key.getBytes()).toString(); CollapsibleItems.apiGroups.add(() -> { GroupItem group = new GroupItem(); group.setFilter(filter, guid); - group.unlocalizedName = unlocalizedName; + group.unlocalizedName = apiGroup.unlocalizedName; return group; }); + + return apiGroup; } private static void parseFile(List itemStrings) { diff --git a/src/main/java/codechicken/nei/api/API.java b/src/main/java/codechicken/nei/api/API.java index 26d1da19..bc9d2b32 100644 --- a/src/main/java/codechicken/nei/api/API.java +++ b/src/main/java/codechicken/nei/api/API.java @@ -347,23 +347,23 @@ public class API { * the lowest priority: an item that matches a preset or a collapsibleitems.cfg group stays there. They are active * only while the Collapsible Items option is enabled. Must be called while plugins are loading. * - * @param unlocalizedName Translation key of the name shown for the group - * @param filter A search string for matching items that belong to this group. Also identifies the group - * when its expanded state is saved, so changing it resets the saved state. + * @param filter A search string for matching items that belong to this group. Also identifies the group when its + * expanded state is saved, so changing it resets the saved state. + * @return A handle to optionally set the name shown for the group */ - public static void addCollapsibleGroup(String unlocalizedName, String filter) { - CollapsibleItems.registerGroup(unlocalizedName, filter); + public static CollapsibleItems.ApiGroup addCollapsibleGroup(String filter) { + return CollapsibleItems.registerGroup(filter); } /** - * Same as {@link #addCollapsibleGroup(String, String)}, but the group is given as a list of item stacks. The stacks + * Same as {@link #addCollapsibleGroup(String)}, but the group is given as a list of item stacks. The stacks * identify the group when its expanded state is saved, so changing them resets the saved state. * - * @param unlocalizedName Translation key of the name shown for the group - * @param items Item stacks that belong to this group + * @param items Item stacks that belong to this group + * @return A handle to optionally set the name shown for the group */ - public static void addCollapsibleGroup(String unlocalizedName, Iterable items) { - CollapsibleItems.registerGroup(unlocalizedName, items); + public static CollapsibleItems.ApiGroup addCollapsibleGroup(Iterable items) { + return CollapsibleItems.registerGroup(items); } @Deprecated