Config Errors no longer crash the game but log the error

This commit is contained in:
Tschipp 2017-08-15 11:56:52 +02:00
parent 5c4dc41662
commit 47f87d6df4
5 changed files with 125 additions and 65 deletions

View File

@ -1,21 +1,17 @@
package tschipp.carryon;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fluids.FluidRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLConstructionEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import tschipp.carryon.common.CommonProxy;
@EventBusSubscriber
@ -30,10 +26,11 @@ public class CarryOn {
public static CarryOn instance;
public static final String MODID = "carryon";
public static final String VERSION = "1.1";
public static final String VERSION = "1.1.1";
public static final String NAME = "Carry On";
public static final String UPDATE_JSON = "https://gist.githubusercontent.com/Tschipp/dccadee7c90d7a34e6e76a35d9d6fa2e/raw/bf7fb60d5e59f73eee65b271d5c01585e26a0352/update.json";
public static final Logger LOGGER = LogManager.getFormatterLogger("CarryOn");
//public static SimpleNetworkWrapper network;
@EventHandler

View File

@ -224,7 +224,9 @@ public class RenderEvents
GlStateManager.scale(1, 1, 1);
GlStateManager.popMatrix();
} else {
}
else
{
modelPlayer.bipedLeftArm.isHidden = false;
modelPlayer.bipedRightArm.isHidden = false;
}
@ -273,8 +275,16 @@ public class RenderEvents
fakeRightArm.addBox(model.bipedRightArm.offsetX - 7.2F, model.bipedRightArm.offsetY, model.bipedRightArm.offsetZ, 3, 12, 4, .08F);
}
fakeRightArm.rotateAngleX = -.9F;
fakeLeftArm.rotateAngleX = -.9F;
if (!player.isSneaking())
{
fakeRightArm.rotateAngleX = -.9F;
fakeLeftArm.rotateAngleX = -.9F;
}
else
{
fakeRightArm.rotateAngleX = -1.3F;
fakeLeftArm.rotateAngleX = -1.3F;
}
model.bipedBody.addChild(fakeLeftArm);
model.bipedBody.addChild(fakeRightArm);

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
@ -22,6 +23,7 @@ import net.minecraftforge.fml.common.registry.IForgeRegistryEntry;
import net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import tschipp.carryon.CarryOn;
import tschipp.carryon.common.config.CarryOnConfig;
import tschipp.carryon.common.helper.InvalidConfigException;
import tschipp.carryon.common.helper.StringParser;
@ -40,13 +42,15 @@ public class ModelOverridesHandler
for (int i = 0; i < overrides.length; i++)
{
boolean errored = false;
Object toOverrideObject;
Object overrideObject;
NBTTagCompound tag = new NBTTagCompound();
String currentline = overrides[i];
if (StringUtils.isEmpty(currentline) || !StringUtils.contains(currentline, "->"))
throw new InvalidConfigException("Missing Override Model at line " + i + " : " + currentline);
new InvalidConfigException("Missing Override Model at line " + i + " : " + currentline).printException();
String[] sa = currentline.split("->");
String toOverride = "";
@ -58,13 +62,17 @@ public class ModelOverridesHandler
}
catch (ArrayIndexOutOfBoundsException e)
{
throw new InvalidConfigException("Missing Override Model at line " + i + " : " + currentline);
errored = true;
new InvalidConfigException("Missing Override Model at line " + i + " : " + currentline).printException();
}
if (toOverride.contains("{"))
{
if (!toOverride.contains("}"))
throw new InvalidConfigException("Missing } at line " + i + " : " + currentline);
{
errored = true;
new InvalidConfigException("Missing } at line " + i + " : " + currentline).printException();
}
String nbt = toOverride.substring(toOverride.indexOf("{"));
toOverride = toOverride.replace(nbt, "");
@ -74,12 +82,16 @@ public class ModelOverridesHandler
}
catch (NBTException e)
{
throw new InvalidConfigException("Error while parsing NBT at line " + i + " : " + e.getMessage());
errored = true;
new InvalidConfigException("Error while parsing NBT at line " + i + " : " + e.getMessage()).printException();
}
}
else if (toOverride.contains("}"))
throw new InvalidConfigException("Missing { at line " + i + " : " + currentline);
{
errored = true;
new InvalidConfigException("Missing { at line " + i + " : " + currentline).printException();
}
String modidToOverride = "minecraft";
String modidOverride = "minecraft";
@ -90,7 +102,7 @@ public class ModelOverridesHandler
if (override.contains(":"))
modidOverride = override.replace(override.substring(override.indexOf(":")), "");
if (Loader.isModLoaded(modidOverride) && Loader.isModLoaded(modidToOverride))
if (Loader.isModLoaded(modidOverride) && Loader.isModLoaded(modidToOverride) && !errored)
{
int meta = StringParser.getMeta(toOverride);
@ -99,24 +111,31 @@ public class ModelOverridesHandler
else
toOverrideObject = StringParser.getBlockState(toOverride);
overrideObject = StringParser.getItem(override);
if (Block.getBlockFromItem((Item) overrideObject) != Blocks.AIR)
overrideObject = StringParser.getItemStack(override);
else
overrideObject = StringParser.getBlockState(override);
if (toOverrideObject != null)
{
overrideObject = StringParser.getItem(override);
NBTTagCompound keyComp = new NBTTagCompound();
keyComp.setTag("nbttag", tag);
if (toOverrideObject instanceof Block)
{
keyComp.setString("block", ((Block) toOverrideObject).getRegistryName().toString());
if (Block.getBlockFromItem((Item) overrideObject) != Blocks.AIR)
overrideObject = StringParser.getItemStack(override);
else
overrideObject = StringParser.getBlockState(override);
if (overrideObject != null)
{
NBTTagCompound keyComp = new NBTTagCompound();
keyComp.setTag("nbttag", tag);
if (toOverrideObject instanceof Block)
{
keyComp.setString("block", ((Block) toOverrideObject).getRegistryName().toString());
}
else
{
keyComp.setInteger("stateid", Block.getStateId((IBlockState) toOverrideObject));
keyComp.setString("block", ((IBlockState) toOverrideObject).getBlock().getRegistryName().toString());
}
OVERRIDE_OBJECTS.put(keyComp, overrideObject);
}
}
else
{
keyComp.setInteger("stateid", Block.getStateId((IBlockState) toOverrideObject));
keyComp.setString("block", ((IBlockState) toOverrideObject).getBlock().getRegistryName().toString());
}
OVERRIDE_OBJECTS.put(keyComp, overrideObject);
}
}
}

View File

@ -1,11 +1,31 @@
package tschipp.carryon.common.helper;
public class InvalidConfigException extends RuntimeException
import tschipp.carryon.CarryOn;
public class InvalidConfigException extends Exception
{
public InvalidConfigException(String cause)
{
super(cause);
}
public void printException()
{
CarryOn.LOGGER.error(this.getMessage());
for (int i = 0; i < this.getStackTrace().length; i++)
{
StackTraceElement element = this.getStackTrace()[i];
CarryOn.LOGGER.error(element.toString());
if(i >= 10)
{
CarryOn.LOGGER.error((this.getStackTrace().length - 10) + " more...");
break;
}
}
CarryOn.LOGGER.info("");
}
}

View File

@ -2,6 +2,8 @@ package tschipp.carryon.common.helper;
import javax.annotation.Nullable;
import org.apache.logging.log4j.Level;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
@ -9,22 +11,24 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import tschipp.carryon.CarryOn;
public class StringParser
{
@Nullable
public static Block getBlock(String string)
{
NBTTagCompound tag = getTagCompound(string);
if(tag != null)
if (tag != null)
string = string.replace(tag.toString(), "");
if (string.contains(";"))
string = string.replace(string.substring(string.indexOf(";")), "");
Block block = Block.getBlockFromName(string);
if(block == null)
throw new InvalidConfigException("Block Parsing Error. Invalid Name: " + string);
if (block == null)
new InvalidConfigException("Block Parsing Error. Invalid Name: " + string).printException();
return block;
}
@ -32,10 +36,9 @@ public class StringParser
public static int getMeta(String string)
{
NBTTagCompound tag = getTagCompound(string);
if(tag != null)
if (tag != null)
string = string.replace(tag.toString(), "");
if (string.contains(";"))
{
int meta = 0;
@ -45,7 +48,7 @@ public class StringParser
}
catch (Exception e)
{
throw new InvalidConfigException("Meta Parsing Error at: " + string + " : " + e.getMessage());
new InvalidConfigException("Meta Parsing Error at: " + string + " : " + e.getMessage()).printException();
}
return meta;
@ -53,47 +56,59 @@ public class StringParser
return 0;
}
@Nullable
public static IBlockState getBlockState(String string)
{
NBTTagCompound tag = getTagCompound(string);
if(tag != null)
if (tag != null)
string = string.replace(tag.toString(), "");
int meta = getMeta(string);
if(meta == 0)
return getBlock(string).getDefaultState();
if (meta == 0)
{
Block block = getBlock(string);
if(block != null)
return block.getDefaultState();
}
try
{
return getBlock(string).getStateFromMeta(meta);
}
catch (Exception e)
{
throw new InvalidConfigException("Blockstate parsing Exception at: " + string + " : " + e.getMessage());
new InvalidConfigException("Blockstate parsing Exception at: " + string + " : " + e.getMessage()).printException();
return null;
}
}
@Nullable
public static Item getItem(String string)
{
NBTTagCompound tag = getTagCompound(string);
if(tag != null)
if (tag != null)
string = string.replace(tag.toString(), "");
if(string.contains(";"))
if (string.contains(";"))
string = string.replace(string.substring(string.indexOf(";")), "");
return Item.getByNameOrId(string);
}
public static ItemStack getItemStack(String string)
{
ItemStack stack = new ItemStack(getItem(string), 1, getMeta(string));
NBTTagCompound tag = getTagCompound(string);
if(tag != null)
stack.setTagCompound(tag);
Item item = getItem(string);
if(item == null)
return ItemStack.EMPTY;
ItemStack stack = new ItemStack(item, 1, getMeta(string));
NBTTagCompound tag = getTagCompound(string);
if (tag != null)
stack.setTagCompound(tag);
return stack;
}
@Nullable
public static NBTTagCompound getTagCompound(String string)
{
@ -101,7 +116,7 @@ public class StringParser
if (string.contains("{"))
{
if (!string.contains("}"))
throw new InvalidConfigException("Missing } at : " + string);
new InvalidConfigException("Missing } at : " + string).printException();
String nbt = string.substring(string.indexOf("{"));
string = string.replace(nbt, "");
@ -111,16 +126,15 @@ public class StringParser
}
catch (NBTException e)
{
throw new InvalidConfigException("Error while parsing NBT: " + e.getMessage());
new InvalidConfigException("Error while parsing NBT: " + e.getMessage()).printException();
return null;
}
}
else if (string.contains("}"))
throw new InvalidConfigException("Missing { at : " + string);
new InvalidConfigException("Missing { at : " + string).printException();
return tag;
}
}