Skip to content

Commit

Permalink
Add ExhaustionEvent.GetExhaustionCap
Browse files Browse the repository at this point in the history
Closes #152
  • Loading branch information
squeek502 committed May 30, 2020
1 parent da38ef6 commit 5ee853c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
21 changes: 21 additions & 0 deletions java/squeek/applecore/api/hunger/ExhaustionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,25 @@ public Exhausted(EntityPlayer player, float exhaustionToRemove, float currentExh
deltaHunger = 0;
}
}

/**
* Fired every time the exhaustion level is capped to allow control over the cap.
*
* This event is fired in {@link FoodStats#addExhaustion}.<br>
* <br>
* {@link #exhaustionLevelCap} contains the exhaustion level that will be used to cap the exhaustion level.<br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult}<br>
*/
public static class GetExhaustionCap extends ExhaustionEvent
{
public float exhaustionLevelCap = 40f;

public GetExhaustionCap(EntityPlayer player)
{
super(player);
}
}
}
10 changes: 10 additions & 0 deletions java/squeek/applecore/asm/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ public static float onExhaustionAdded(FoodStats foodStats, float deltaExhaustion
return event.deltaExhaustion;
}

public static float getExhaustionCap(FoodStats foodStats)
{
verifyFoodStats(foodStats, null);

EntityPlayer player = ((IAppleCoreFoodStats) foodStats).getPlayer();
ExhaustionEvent.GetExhaustionCap event = new ExhaustionEvent.GetExhaustionCap(player);
MinecraftForge.EVENT_BUS.post(event);
return event.exhaustionLevelCap;
}

public static float fireExhaustingActionEvent(EntityPlayer player, ExhaustionEvent.ExhaustingActions source, float deltaExhaustion)
{
ExhaustionEvent.ExhaustingAction event = new ExhaustionEvent.ExhaustingAction(player, source, deltaExhaustion);
Expand Down
13 changes: 13 additions & 0 deletions java/squeek/applecore/asm/module/ModuleFoodStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,19 @@ private void hookAddExhaustion(ClassNode classNode, MethodNode addExhaustionMeth
toInject.add(new VarInsnNode(FSTORE, 1));

addExhaustionMethodNode.instructions.insertBefore(ASMHelper.findFirstInstruction(addExhaustionMethodNode), toInject);

// Replace the 40.0f constant with GetExhaustionCap call
InsnList replacement = new InsnList();
replacement.add(new VarInsnNode(ALOAD, 0));
replacement.add(new MethodInsnNode(INVOKESTATIC, ASMHelper.toInternalClassName(ASMConstants.HOOKS), "getExhaustionCap", ASMHelper.toMethodDescriptor("F", ASMConstants.FOOD_STATS), false));

InsnList needle = new InsnList();
needle.add(new LdcInsnNode(new Float("40.0")));

if (ASMHelper.findAndReplaceAll(addExhaustionMethodNode.instructions, needle, replacement) == 0)
{
throw new RuntimeException("Failed to inject GetExhaustionCap");
}
}

private boolean tryAddFieldGetter(ClassNode classNode, String methodName, String fieldName, String fieldDescriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void addExhaustion(float exhaustion)
{
exhaustion = Hooks.onExhaustionAdded(this, exhaustion);

// the body of the base function
this.foodExhaustionLevel = Math.min(this.foodExhaustionLevel + exhaustion, Hooks.getExhaustionCap(this));
}

// start unmodified
Expand Down
16 changes: 15 additions & 1 deletion java/squeek/applecore/example/ExhaustionModifier.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package squeek.applecore.example;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import squeek.applecore.api.AppleCoreAPI;
import squeek.applecore.api.hunger.ExhaustionEvent;

public class ExhaustionModifier
Expand All @@ -17,6 +18,8 @@ public void onExhausted(ExhaustionEvent.Exhausted event)
// this will enable hunger loss in peaceful difficulty
if (event.player.getFoodStats().getSaturationLevel() <= 0)
event.deltaHunger = -1;

AppleCoreExample.LOG.info("onExhausted exhaustion=" + AppleCoreAPI.accessor.getExhaustion(event.player));
}

@SubscribeEvent
Expand All @@ -29,10 +32,21 @@ public void onExhaustionAddition(ExhaustionEvent.ExhaustionAddition event)
@SubscribeEvent
public void onExhaustingAction(ExhaustionEvent.ExhaustingAction event)
{
if (event.source == ExhaustionEvent.ExhaustingActions.NORMAL_JUMP || event.source == ExhaustionEvent.ExhaustingActions.SPRINTING_JUMP)
if (event.source == ExhaustionEvent.ExhaustingActions.NORMAL_JUMP)
{
// random exhaustion each jump
event.deltaExhaustion *= Math.random();
}
else if (event.source == ExhaustionEvent.ExhaustingActions.SPRINTING_JUMP)
{
// note: this is over the default cap of 40, but also over the modified cap
event.deltaExhaustion = 100.0f;
}
}

@SubscribeEvent
public void getExhaustionCap(ExhaustionEvent.GetExhaustionCap event)
{
event.exhaustionLevelCap = 90.0f;
}
}

0 comments on commit 5ee853c

Please sign in to comment.