Skip to content

Commit

Permalink
Use new Forge item.getCreatorModId feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Oct 3, 2017
1 parent 4f82726 commit 0f4c4a5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 23 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Expand Up @@ -92,8 +92,6 @@ curseforge {
changelog = file('changelog.html')
changelogType = 'html'
releaseType = 'beta'
addGameVersion '1.12'
addGameVersion '1.12.1'
}
}

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
@@ -1,8 +1,8 @@
mcversion=1.12.2
forgeversion=14.23.0.2486
forgeversion=14.23.0.2501
mcp_mappings=snapshot_20170918
curse_project_id=238222

version_major=4
version_minor=7
version_patch=11
version_minor=8
version_patch=0
8 changes: 8 additions & 0 deletions src/api/java/mezz/jei/api/ingredients/IIngredientHelper.java
Expand Up @@ -53,6 +53,14 @@ public interface IIngredientHelper<V> {
*/
String getModId(V ingredient);

/**
* Return the modId of the mod that should be displayed.
* @since JEI 4.8.0
*/
default String getDisplayModId(V ingredient) {
return getModId(ingredient);
}

/**
* Get the main colors of this ingredient. Used for the color search.
* If this is too difficult to implement for your ingredient, just return an empty collection.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mezz/jei/JustEnoughItems.java
Expand Up @@ -17,8 +17,8 @@
name = Constants.NAME,
version = Constants.VERSION,
guiFactory = "mezz.jei.config.JEIModGuiFactory",
acceptedMinecraftVersions = "[1.12,1.13)",
dependencies = "required-after:forge@[14.21.0.2348,);")
acceptedMinecraftVersions = "[1.12.2,1.13)",
dependencies = "required-after:forge@[14.23.0.2500,);")
public class JustEnoughItems {

@SuppressWarnings("NullableProblems")
Expand Down
Expand Up @@ -18,7 +18,7 @@ public interface IIngredientListElement<V> {

String getDisplayName();

String getModName();
String getModNameForSorting();

Set<String> getModNameStrings();

Expand Down
Expand Up @@ -26,7 +26,7 @@ public static <T> String getDisplayName(T ingredient, IIngredientHelper<T> ingre
return removeChatFormatting(displayName);
}

public static <T> List<String> getTooltipStrings(T ingredient, IIngredientHelper<T> ingredientHelper, IIngredientRenderer<T> ingredientRenderer, Set<String> toRemove) {
public static <T> List<String> getTooltipStrings(T ingredient, IIngredientRenderer<T> ingredientRenderer, Set<String> toRemove) {
ITooltipFlag.TooltipFlags tooltipFlag = Config.getSearchAdvancedTooltips() ? ITooltipFlag.TooltipFlags.ADVANCED : ITooltipFlag.TooltipFlags.NORMAL;
Minecraft minecraft = Minecraft.getMinecraft();
List<String> tooltip = ingredientRenderer.getTooltip(minecraft, ingredient, tooltipFlag);
Expand Down
43 changes: 32 additions & 11 deletions src/main/java/mezz/jei/ingredients/IngredientListElement.java
Expand Up @@ -4,11 +4,13 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableSet;
import mezz.jei.api.ingredients.IIngredientHelper;
Expand All @@ -34,8 +36,8 @@ public class IngredientListElement<V> implements IIngredientListElement<V> {
private final IIngredientHelper<V> ingredientHelper;
private final IIngredientRenderer<V> ingredientRenderer;
private final String displayName;
private final String modName;
private final String modId;
private final List<String> modIds;
private final List<String> modNames;
private final String resourceId;
private boolean hidden = false;

Expand Down Expand Up @@ -69,9 +71,14 @@ protected IngredientListElement(V ingredient, int orderIndex, IIngredientHelper<
this.orderIndex = orderIndex;
this.ingredientHelper = ingredientHelper;
this.ingredientRenderer = ingredientRenderer;

this.modId = ingredientHelper.getModId(ingredient);
this.modName = modIdHelper.getModNameForModId(modId);
String displayModId = ingredientHelper.getDisplayModId(ingredient);
String modId = ingredientHelper.getModId(ingredient);
this.modIds = new ArrayList<>();
this.modIds.add(displayModId);
if (!modId.equals(displayModId)) {
this.modIds.add(modId);
}
this.modNames = this.modIds.stream().map(modIdHelper::getModNameForModId).collect(Collectors.toList());
this.displayName = IngredientInformation.getDisplayName(ingredient, ingredientHelper);
this.resourceId = LegacyUtil.getResourceId(ingredient, ingredientHelper);
}
Expand Down Expand Up @@ -102,23 +109,37 @@ public final String getDisplayName() {
}

@Override
public String getModName() {
return modName;
public String getModNameForSorting() {
return modNames.get(0);
}

@Override
public Set<String> getModNameStrings() {
String modNameLowercase = this.modName.toLowerCase(Locale.ENGLISH);
Set<String> modNameStrings = new HashSet<>();
for (int i = 0; i < modIds.size(); i++) {
String modId = modIds.get(i);
String modName = modNames.get(i);
addModNameStrings(modNameStrings, modId, modName);
}
return modNameStrings;
}

private static void addModNameStrings(Set<String> modNames, String modId, String modName) {
String modNameLowercase = modName.toLowerCase(Locale.ENGLISH);
String modNameNoSpaces = SPACE_PATTERN.matcher(modNameLowercase).replaceAll("");
String modIdNoSpaces = SPACE_PATTERN.matcher(modId).replaceAll("");
return ImmutableSet.of(modNameLowercase, modId, modNameNoSpaces, modIdNoSpaces);
modNames.add(modId);
modNames.add(modNameNoSpaces);
modNames.add(modIdNoSpaces);
}

@Override
public final List<String> getTooltipStrings() {
String modNameLowercase = this.modName.toLowerCase(Locale.ENGLISH);
String modName = this.modNames.get(0);
String modId = this.modIds.get(0);
String modNameLowercase = modName.toLowerCase(Locale.ENGLISH);
String displayNameLowercase = Translator.toLowercaseWithLocale(this.displayName);
return IngredientInformation.getTooltipStrings(ingredient, ingredientHelper, ingredientRenderer, ImmutableSet.of(modId, modNameLowercase, displayNameLowercase, resourceId));
return IngredientInformation.getTooltipStrings(ingredient, ingredientRenderer, ImmutableSet.of(modId, modNameLowercase, displayNameLowercase, resourceId));
}

@Override
Expand Down
Expand Up @@ -15,8 +15,8 @@ private IngredientListElementComparator() {

@Override
public int compare(IIngredientListElement o1, IIngredientListElement o2) {
final String modName1 = o1.getModName();
final String modName2 = o2.getModName();
final String modName1 = o1.getModNameForSorting();
final String modName2 = o2.getModNameForSorting();

if (modName1.equals(modName2)) {
boolean isItemStack1 = (o1.getIngredient() instanceof ItemStack);
Expand Down
Expand Up @@ -61,6 +61,19 @@ public String getModId(ItemStack ingredient) {
return itemName.getResourceDomain();
}

@Override
public String getDisplayModId(ItemStack ingredient) {
ErrorUtil.checkNotEmpty(ingredient);

Item item = ingredient.getItem();
String modId = item.getCreatorModId(ingredient);
if (modId == null) {
String stackInfo = getErrorInfo(ingredient);
throw new IllegalStateException("item.getCreatorModId() returned null for: " + stackInfo);
}
return modId;
}

@Override
public Iterable<Color> getColors(ItemStack ingredient) {
return ColorGetter.getColors(ingredient, 2);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mezz/jei/startup/AbstractModIdHelper.java
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.config.Config;
Expand All @@ -24,7 +25,7 @@ public <T> List<String> addModNameToIngredientTooltip(List<String> tooltip, T in
return tooltip;
}

String modId = ingredientHelper.getModId(ingredient);
String modId = ingredientHelper.getDisplayModId(ingredient);
String modName = getFormattedModNameForModId(modId);
List<String> tooltipCopy = new ArrayList<>(tooltip);
tooltipCopy.add(modName);
Expand Down

0 comments on commit 0f4c4a5

Please sign in to comment.