From 68dcd8bb357c9a6dabd0c5e4a399d9ba0807df91 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Mon, 11 Jun 2018 23:33:56 -0700 Subject: [PATCH] Better errors for FoodStats runtime problems Add errors for: - A common mod incompatibility case (FoodStats overload) - AppleCore explosion (IAppleCoreFoodStats not implemented by FoodStats) --- java/squeek/applecore/asm/Hooks.java | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/java/squeek/applecore/asm/Hooks.java b/java/squeek/applecore/asm/Hooks.java index 5f170de..a296405 100644 --- a/java/squeek/applecore/asm/Hooks.java +++ b/java/squeek/applecore/asm/Hooks.java @@ -31,10 +31,25 @@ public class Hooks { - public static boolean onAppleCoreFoodStatsUpdate(FoodStats foodStats, EntityPlayer player) + private static void verifyFoodStats(FoodStats foodStats, EntityPlayer player) { if (!(foodStats instanceof IAppleCoreFoodStats)) - return false; + { + String playerName = player != null ? player.getName() : ""; + String className = foodStats.getClass().getName(); + throw new RuntimeException("FoodStats does not implement IAppleCoreFoodStats on player '"+playerName+"' (class = "+className+"). This likely means that AppleCore has failed catastrophically in some way."); + } + if (((IAppleCoreFoodStats)foodStats).getPlayer() == null) + { + String playerName = player != null ? player.getName() : ""; + String className = foodStats.getClass().getName(); + throw new RuntimeException("FoodStats has a null player field (this field is added by AppleCore at runtime) on player '"+playerName+"' (class = "+className+"). This likely means that some mod has overloaded FoodStats, which is incompatible with AppleCore."); + } + } + + public static boolean onAppleCoreFoodStatsUpdate(FoodStats foodStats, EntityPlayer player) + { + verifyFoodStats(foodStats, player); IAppleCoreFoodStats appleCoreFoodStats = (IAppleCoreFoodStats) foodStats; @@ -120,11 +135,13 @@ else if (shouldDoRegen) public static FoodValues onFoodStatsAdded(FoodStats foodStats, ItemFood itemFood, ItemStack itemStack, EntityPlayer player) { + verifyFoodStats(foodStats, player); return AppleCoreAPI.accessor.getFoodValuesForPlayer(itemStack, player); } public static void onPostFoodStatsAdded(FoodStats foodStats, ItemFood itemFood, ItemStack itemStack, FoodValues foodValues, int hungerAdded, float saturationAdded, EntityPlayer player) { + verifyFoodStats(foodStats, player); MinecraftForge.EVENT_BUS.post(new FoodEvent.FoodEaten(player, itemStack, foodValues, hungerAdded, saturationAdded)); } @@ -258,13 +275,13 @@ public static void fireOnGrowthEvent(Block block, World world, BlockPos pos, IBl public static boolean needFood(FoodStats foodStats) { + verifyFoodStats(foodStats, null); return foodStats.getFoodLevel() < getMaxHunger(foodStats); } public static int getMaxHunger(FoodStats foodStats) { - if (!(foodStats instanceof IAppleCoreFoodStats)) - return 20; + verifyFoodStats(foodStats, null); EntityPlayer player = ((IAppleCoreFoodStats) foodStats).getPlayer(); return AppleCoreAPI.accessor.getMaxHunger(player); @@ -297,8 +314,7 @@ public static int getHungerForDisplay(FoodStats foodStats) public static float onExhaustionAdded(FoodStats foodStats, float deltaExhaustion) { - if (!(foodStats instanceof IAppleCoreFoodStats)) - return deltaExhaustion; + verifyFoodStats(foodStats, null); EntityPlayer player = ((IAppleCoreFoodStats) foodStats).getPlayer(); ExhaustionEvent.ExhaustionAddition event = new ExhaustionEvent.ExhaustionAddition(player, deltaExhaustion);