Skip to content

Commit

Permalink
Fix #999 Improve mod name tooltip detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 25, 2017
1 parent 8a8c254 commit ccecfae
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mezz/jei/config/Constants.java
Expand Up @@ -5,7 +5,7 @@
import java.util.Locale;

public final class Constants {
public static final String minecraftModName = "Minecraft";
public static final String MINECRAFT_NAME = "Minecraft";

// Mod info
public static final String MOD_ID = "jei";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mezz/jei/gui/recipes/RecipeCategoryTab.java
Expand Up @@ -90,8 +90,8 @@ public List<String> getTooltip() {

String modName = LegacyUtil.getModName(category);
if (modName != null) {
modName = ForgeModIdHelper.getInstance().getModNameForModId(modName);
tooltip.add(Config.getModNameFormat() + modName);
modName = ForgeModIdHelper.getInstance().getFormattedModNameForModId(modName);
tooltip.add(modName);
}
return tooltip;
}
Expand Down
Expand Up @@ -30,9 +30,9 @@ public int compare(IIngredientListElement o1, IIngredientListElement o2) {
final int orderIndex1 = o1.getOrderIndex();
final int orderIndex2 = o2.getOrderIndex();
return Integer.compare(orderIndex1, orderIndex2);
} else if (modName1.equals(Constants.minecraftModName)) {
} else if (modName1.equals(Constants.MINECRAFT_NAME)) {
return -1;
} else if (modName2.equals(Constants.minecraftModName)) {
} else if (modName2.equals(Constants.MINECRAFT_NAME)) {
return 1;
} else {
return modName1.compareTo(modName2);
Expand Down
Expand Up @@ -30,7 +30,7 @@ public String getTitle() {

@Override
public String getModName() {
return Constants.minecraftModName;
return Constants.MINECRAFT_NAME;
}

@Override
Expand Down
Expand Up @@ -60,7 +60,7 @@ public String getTitle() {

@Override
public String getModName() {
return Constants.minecraftModName;
return Constants.MINECRAFT_NAME;
}

@Override
Expand Down
Expand Up @@ -55,7 +55,7 @@ public String getTitle() {

@Override
public String getModName() {
return Constants.minecraftModName;
return Constants.MINECRAFT_NAME;
}

@Override
Expand Down Expand Up @@ -110,8 +110,7 @@ public void setRecipe(IRecipeLayout recipeLayout, IRecipeWrapper recipeWrapper,
}

if (modIdDifferent) {
String modNameFormat = Config.getModNameFormat();
String modName = modNameFormat + ForgeModIdHelper.getInstance().getModNameForModId(recipeModId);
String modName = ForgeModIdHelper.getInstance().getFormattedModNameForModId(recipeModId);
tooltip.add(TextFormatting.GRAY + Translator.translateToLocalFormatted("jei.tooltip.recipe.by", modName));
}

Expand Down
Expand Up @@ -42,7 +42,7 @@ public String getTitle() {

@Override
public String getModName() {
return Constants.minecraftModName;
return Constants.MINECRAFT_NAME;
}

@Nullable
Expand Down
Expand Up @@ -38,7 +38,7 @@ public String getTitle() {

@Override
public String getModName() {
return Constants.minecraftModName;
return Constants.MINECRAFT_NAME;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/mezz/jei/startup/AbstractModIdHelper.java
Expand Up @@ -24,9 +24,10 @@ public <T> List<String> addModNameToIngredientTooltip(List<String> tooltip, T in
return tooltip;
}

String modName = getModNameForIngredient(ingredient, ingredientHelper);
String modId = ingredientHelper.getModId(ingredient);
String modName = getFormattedModNameForModId(modId);
List<String> tooltipCopy = new ArrayList<>(tooltip);
tooltipCopy.add(modNameFormat + modName);
tooltipCopy.add(modName);
return tooltipCopy;
}
}
27 changes: 23 additions & 4 deletions src/main/java/mezz/jei/startup/ForgeModIdHelper.java
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Map;

import mezz.jei.config.Config;
import mezz.jei.config.Constants;
import mezz.jei.util.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
Expand All @@ -16,8 +18,10 @@
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import org.apache.commons.lang3.StringUtils;

public class ForgeModIdHelper extends AbstractModIdHelper {
private static final String MOD_NAME_FORMAT_CODE = "%MODNAME%";
@Nullable
private static ForgeModIdHelper INSTANCE;

Expand All @@ -43,6 +47,22 @@ public String getModNameForModId(String modId) {
return modContainer.getName();
}

@Override
public String getFormattedModNameForModId(String modId) {
String modNameFormat = Config.getModNameFormat();
String modName = getModNameForModId(modId);
modName = removeChatFormatting(modName); // some crazy mod has formatting in the name
if (modNameFormat.contains(MOD_NAME_FORMAT_CODE)) {
return StringUtils.replaceOnce(modNameFormat, MOD_NAME_FORMAT_CODE, modName);
}
return modNameFormat + modName;
}

private static String removeChatFormatting(String string) {
String withoutFormattingCodes = TextFormatting.getTextWithoutFormattingCodes(string);
return (withoutFormattingCodes == null) ? "" : withoutFormattingCodes;
}

@Nullable
@Override
public String getModNameTooltipFormatting() {
Expand All @@ -56,14 +76,13 @@ public String getModNameTooltipFormatting() {

if (tooltip.size() > 1) {
String lastLine = tooltip.get(tooltip.size() - 1);
if (lastLine.contains("Minecraft")) {
if (lastLine.contains(Constants.MINECRAFT_NAME)) {
String withoutFormatting = TextFormatting.getTextWithoutFormattingCodes(lastLine);
if (withoutFormatting != null) {
if (lastLine.equals(withoutFormatting)) {
return "";
} else if (lastLine.endsWith(withoutFormatting)) {
int i = lastLine.length() - withoutFormatting.length();
return lastLine.substring(0, i);
} else if (lastLine.contains(withoutFormatting)) {
return StringUtils.replaceOnce(lastLine, Constants.MINECRAFT_NAME, MOD_NAME_FORMAT_CODE);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mezz/jei/startup/IModIdHelper.java
Expand Up @@ -9,6 +9,8 @@
public interface IModIdHelper {
String getModNameForModId(String modId);

String getFormattedModNameForModId(String modId);

<T> String getModNameForIngredient(T ingredient, IIngredientHelper<T> ingredientHelper);

<T> List<String> addModNameToIngredientTooltip(List<String> tooltip, T ingredient, IIngredientHelper<T> ingredientHelper);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/mezz/jei/test/lib/TestModIdHelper.java
Expand Up @@ -10,6 +10,11 @@ public String getModNameForModId(String modId) {
return "ModName(" + modId + ")";
}

@Override
public String getFormattedModNameForModId(String modId) {
return getModNameForModId(modId);
}

@Nullable
@Override
public String getModNameTooltipFormatting() {
Expand Down

0 comments on commit ccecfae

Please sign in to comment.