Skip to content

Commit

Permalink
Fix #3723 Some Items in creative menu are not being added to JEI
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 12, 2024
1 parent c844479 commit ba55d49
Showing 1 changed file with 90 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -64,106 +65,67 @@ public static List<ItemStack> create(StackHelper stackHelper) {
final CreativeModeTab.ItemDisplayParameters displayParameters =
new CreativeModeTab.ItemDisplayParameters(features, hasOperatorItemsTabPermissions, registryAccess);

for (CreativeModeTab itemGroup : CreativeModeTabs.allTabs()) {
if (itemGroup.getType() != CreativeModeTab.Type.CATEGORY) {
for (CreativeModeTab tab : CreativeModeTabs.allTabs()) {
if (tab.getType() != CreativeModeTab.Type.CATEGORY) {
LOGGER.debug(
"Skipping creative tab: '{}' because it is type: {}",
itemGroup.getDisplayName().getString(),
itemGroup.getType()
tab.getDisplayName().getString(),
tab.getType()
);
continue;
}
try {
itemGroup.buildContents(displayParameters);
tab.buildContents(displayParameters);
} catch (RuntimeException | LinkageError e) {
LOGGER.error(
"Item Group crashed while building contents." +
"Items from this group will be missing from the JEI ingredient list: {}",
itemGroup.getDisplayName().getString(),
tab.getDisplayName().getString(),
e
);
continue;
}

@Unmodifiable Collection<ItemStack> creativeTabItemStacks;
@Unmodifiable Collection<ItemStack> displayItems;
@Unmodifiable Collection<ItemStack> searchTabDisplayItems;
try {
creativeTabItemStacks = itemGroup.getSearchTabDisplayItems();
displayItems = tab.getDisplayItems();
searchTabDisplayItems = tab.getSearchTabDisplayItems();
} catch (RuntimeException | LinkageError e) {
LOGGER.error(
"Item Group crashed while getting search tab display items." +
"Some items from this group will be missing from the JEI ingredient list: {}",
itemGroup.getDisplayName().getString(),
tab.getDisplayName().getString(),
e
);
continue;
}

if (creativeTabItemStacks.isEmpty()) {
try {
Collection<ItemStack> displayItems = itemGroup.getDisplayItems();
if (displayItems.isEmpty()) {
LOGGER.warn(
"Item Group has no display items and no search tab display items. " +
"Items from this group will be missing from the JEI ingredient list. {}",
itemGroup.getDisplayName().getString()
);
continue;
} else {
LOGGER.warn(
"Item Group has no search tab display items. " +
"Falling back on getting the regular display items: {}",
itemGroup.getDisplayName().getString()
);
creativeTabItemStacks = displayItems;
}
} catch (RuntimeException | LinkageError e) {
LOGGER.error(
"Item Group has no search tab display items and crashed while getting display items. " +
"Items from this group will be missing from the JEI ingredient list. {}",
itemGroup.getDisplayName().getString(),
e
);
continue;
}
if (displayItems.isEmpty() && searchTabDisplayItems.isEmpty()) {
LOGGER.warn(
"Item Group has no display items and no search tab display items. " +
"Items from this group will be missing from the JEI ingredient list. {}",
tab.getDisplayName().getString()
);
continue;
}

Set<Object> tabUidSet = new HashSet<>();
int added = 0;
Set<Object> duplicateInTab = new HashSet<>();
int duplicateInTabCount = 0;
for (ItemStack itemStack : creativeTabItemStacks) {
if (itemStack.isEmpty()) {
LOGGER.error("Found an empty itemStack from creative tab: {}", itemGroup);
} else {
Object itemKey = getItemKey(stackHelper, itemStack);
if (itemKey != null) {
if (tabUidSet.contains(itemKey)) {
duplicateInTab.add(itemKey);
duplicateInTabCount++;
}
if (itemUidSet.add(itemKey)) {
tabUidSet.add(itemKey);
itemList.add(itemStack);
added++;
}
}
}
}
LOGGER.debug(
"Added {}/{} new items from creative tab: {}",
added,
creativeTabItemStacks.size(),
itemGroup.getDisplayName().getString()
addFromTab(
displayItems,
"displayItems",
tab,
stackHelper,
itemList,
itemUidSet
);
if (duplicateInTabCount > 0) {
LOGGER.warn(
"""
{} duplicate items were found in creative tab: {}
This may indicate that these types of item need a subtype interpreter added to JEI:
{}""",
duplicateInTabCount,
itemGroup.getDisplayName().getString(),
duplicateInTab.stream().map(Object::toString).collect(Collectors.joining(", ", "[", "]"))
if (!displayItems.equals(searchTabDisplayItems)) {
addFromTab(
searchTabDisplayItems,
"searchTabDisplayItems",
tab,
stackHelper,
itemList,
itemUidSet
);
}
}
Expand All @@ -175,6 +137,59 @@ public static List<ItemStack> create(StackHelper stackHelper) {
return itemList;
}

private static void addFromTab(
Collection<ItemStack> tabDisplayItems,
String displayType,
CreativeModeTab tab,
StackHelper stackHelper,
List<ItemStack> itemList,
Set<Object> itemUidSet
) {
Set<Object> tabUidSet = new HashSet<>();
int added = 0;
Set<Object> duplicateInTab = new HashSet<>();
int duplicateInTabCount = 0;
for (ItemStack itemStack : tabDisplayItems) {
if (itemStack.isEmpty()) {
LOGGER.error("Found an empty itemStack in '{}' creative tab's {}", tab, displayType);
} else {
Object itemKey = safeGetUid(stackHelper, itemStack);
if (itemKey != null) {
if (tabUidSet.contains(itemKey)) {
duplicateInTab.add(itemKey);
duplicateInTabCount++;
}
if (itemUidSet.add(itemKey)) {
tabUidSet.add(itemKey);
itemList.add(itemStack);
added++;
}
}
}
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"Added {}/{} new items from '{}' creative tab's {}",
StringUtils.leftPad(Integer.toString(added), 4, ' '),
StringUtils.leftPad(Integer.toString(tabDisplayItems.size()), 4, ' '),
tab.getDisplayName().getString(),
displayType
);
}
if (duplicateInTabCount > 0) {
LOGGER.warn(
"""
{} duplicate items were found in '{}' creative tab's: {}
This may indicate that these types of item need a subtype interpreter added to JEI:
{}""",
duplicateInTabCount,
tab.getDisplayName().getString(),
displayType,
duplicateInTab.stream().map(Object::toString).collect(Collectors.joining(", ", "[", "]"))
);
}
}

private static void addItemsFromRegistries(
StackHelper stackHelper,
List<ItemStack> itemList,
Expand All @@ -192,7 +207,7 @@ private static void addItemsFromRegistries(

int added = 0;
for (ItemStack itemStack : itemStacks) {
Object itemKey = getItemKey(stackHelper, itemStack);
Object itemKey = safeGetUid(stackHelper, itemStack);
if (itemKey != null && itemUidSet.add(itemKey)) {
itemList.add(itemStack);
added++;
Expand All @@ -218,7 +233,7 @@ private static void addItemsFromRegistries(

int added = 0;
for (ItemStack itemStack : itemStacks) {
Object itemKey = getItemKey(stackHelper, itemStack);
Object itemKey = safeGetUid(stackHelper, itemStack);
if (itemKey != null && itemUidSet.add(itemKey)) {
itemList.add(itemStack);
added++;
Expand All @@ -234,7 +249,7 @@ private static void addItemsFromRegistries(
}

@Nullable
private static Object getItemKey(StackHelper stackHelper, ItemStack stack) {
private static Object safeGetUid(StackHelper stackHelper, ItemStack stack) {
if (stackHelper.hasSubtypes(stack)) {
try {
return stackHelper.getUniqueIdentifierForStack(stack, UidContext.Ingredient);
Expand Down

0 comments on commit ba55d49

Please sign in to comment.