Skip to content

Commit

Permalink
Better errors for FoodStats runtime problems
Browse files Browse the repository at this point in the history
Add errors for:
- A common mod incompatibility case (FoodStats overload)
- AppleCore explosion (IAppleCoreFoodStats not implemented by FoodStats)
  • Loading branch information
squeek502 committed Jun 12, 2018
1 parent 3173d41 commit 68dcd8b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions java/squeek/applecore/asm/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() : "<unknown>";
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() : "<unknown>";
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;

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 68dcd8b

Please sign in to comment.