Skip to content

Commit

Permalink
add friendly error message if non-deobf and the MekanismApi class is …
Browse files Browse the repository at this point in the history
…loaded from the api jar
  • Loading branch information
thiakil committed Apr 14, 2018
1 parent 9d981cc commit f523000
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/mekanism/client/ApiJarPresentException.java
@@ -0,0 +1,43 @@
package mekanism.client;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiErrorScreen;
import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Copied from EnderCore
*/
public class ApiJarPresentException extends CustomModLoadingErrorDisplayException
{
private static final long serialVersionUID = 1L;

private final @Nonnull String[] msgs;

public ApiJarPresentException(@Nonnull String[] msgs) {
super(msgs[0], new RuntimeException());
this.msgs = msgs;
}

@Override
public void initGui(GuiErrorScreen errorScreen, FontRenderer fontRenderer) {
}

@Override
public void drawScreen(@Nullable GuiErrorScreen errorScreen, @Nullable FontRenderer fontRenderer, int mouseRelX, int mouseRelY, float tickTime) {
if (errorScreen == null || fontRenderer == null) {
return;
}
int y = errorScreen.height / 2 - msgs.length * 5;
for (String msg : msgs) {
if (msg != null) {
errorScreen.drawCenteredString(fontRenderer, msg, errorScreen.width / 2, y, 0xFFFFFF);
y += 10;
} else {
y += 5;
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/mekanism/client/ClientProxy.java
Expand Up @@ -1150,4 +1150,10 @@ public FontRenderer getFontRenderer()
{
return Minecraft.getMinecraft().fontRenderer;
}

@Override
public void throwApiPresentException()
{
throw new ApiJarPresentException(API_PRESENT_MESSAGE);
}
}
6 changes: 6 additions & 0 deletions src/main/java/mekanism/common/CommonProxy.java
Expand Up @@ -708,4 +708,10 @@ public Object getFontRenderer()
{
return null;
}

protected final String[] API_PRESENT_MESSAGE = {"Mekanism API jar detected (Mekanism-<version>-api.jar),", "please delete it from your mods folder and restart the game."};

public void throwApiPresentException(){
throw new RuntimeException(String.join(" ", API_PRESENT_MESSAGE));
}
}
10 changes: 10 additions & 0 deletions src/main/java/mekanism/common/Mekanism.java
Expand Up @@ -5,6 +5,7 @@
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.UUID;
Expand Down Expand Up @@ -106,6 +107,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -701,6 +703,14 @@ public void loadComplete(FMLInterModComms.IMCEvent event)
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
//sanity check the api location if not deobf
if (!((Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment"))){
String apiLocation = MekanismAPI.class.getProtectionDomain().getCodeSource().getLocation().toString();
if (apiLocation.toLowerCase(Locale.ROOT).contains("-api.jar")){
proxy.throwApiPresentException();
}
}

File config = event.getSuggestedConfigurationFile();

//Set the mod's configuration
Expand Down

1 comment on commit f523000

@tomelfring
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this. Saves a bunch of issues!

Please sign in to comment.