-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement generic conditional stat hook
In the future, will implement on some pre-existing stats where practical, though I suspect melee will want more context Use for ranged version of dwarven, momentum, conducting, raging, and airborne
- Loading branch information
1 parent
c563aca
commit c6fffed
Showing
12 changed files
with
242 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...main/java/slimeknights/tconstruct/library/modifiers/hook/ConditionalStatModifierHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package slimeknights.tconstruct.library.modifiers.hook; | ||
|
||
import net.minecraft.world.entity.LivingEntity; | ||
import slimeknights.tconstruct.library.modifiers.ModifierEntry; | ||
import slimeknights.tconstruct.library.modifiers.TinkerHooks; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.stat.FloatToolStat; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
/** Hook for modifying a stat conditioned on the holder */ | ||
public interface ConditionalStatModifierHook { | ||
/** Default behavior */ | ||
ConditionalStatModifierHook EMPTY = (tool, modifier, living, stat, baseValue, multiplier) -> baseValue; | ||
|
||
/** Constructor for a merger that runs all hooks from the children */ | ||
Function<Collection<ConditionalStatModifierHook>, ConditionalStatModifierHook> ALL_MERGER = AllMerger::new; | ||
|
||
/** | ||
* Method to modify a stat as the tool is being used | ||
* @param tool Tool instance | ||
* @param modifier Modifier instance | ||
* @param living Entity holding the tool | ||
* @param stat Stat to be modified, safe to do instance equality | ||
* @param baseValue Value before this hook modified the stat | ||
* @param multiplier Global multiplier, same value contained in the tool, but fetched for convenience as it's commonly needed for stat bonuses | ||
* @return New value of the stat, or baseValue if you choose not to modify this stat | ||
*/ | ||
float modifyStat(IToolStackView tool, ModifierEntry modifier, LivingEntity living, FloatToolStat stat, float baseValue, float multiplier); | ||
|
||
/** Gets the given stat from the tool, as modified by this hook */ | ||
static float getModifiedStat(IToolStackView tool, LivingEntity living, FloatToolStat stat, float value) { | ||
float multiplier = tool.getMultiplier(stat); | ||
for (ModifierEntry entry : tool.getModifierList()) { | ||
value = entry.getHook(TinkerHooks.CONDITIONAL_STAT).modifyStat(tool, entry, living, stat, value, multiplier); | ||
} | ||
return stat.clamp(value); | ||
} | ||
|
||
/** Gets the given stat from the tool, as modified by this hook */ | ||
static float getModifiedStat(IToolStackView tool, LivingEntity living, FloatToolStat stat) { | ||
return getModifiedStat(tool, living, stat, tool.getStats().get(stat)); | ||
} | ||
|
||
/** All hook merger: runs hooks of all children */ | ||
record AllMerger(Collection<ConditionalStatModifierHook> modules) implements ConditionalStatModifierHook { | ||
@Override | ||
public float modifyStat(IToolStackView tool, ModifierEntry modifier, LivingEntity living, FloatToolStat stat, float baseValue, float multiplier) { | ||
for (ConditionalStatModifierHook hook : modules) { | ||
baseValue = hook.modifyStat(tool, modifier, living, stat, baseValue, multiplier); | ||
} | ||
return baseValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 21 additions & 1 deletion
22
src/main/java/slimeknights/tconstruct/tools/modifiers/traits/harvest/AirborneModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,41 @@ | ||
package slimeknights.tconstruct.tools.modifiers.traits.harvest; | ||
|
||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed; | ||
import slimeknights.tconstruct.library.modifiers.ModifierEntry; | ||
import slimeknights.tconstruct.library.modifiers.TinkerHooks; | ||
import slimeknights.tconstruct.library.modifiers.hook.ConditionalStatModifierHook; | ||
import slimeknights.tconstruct.library.modifiers.impl.NoLevelsModifier; | ||
import slimeknights.tconstruct.library.modifiers.util.ModifierHookMap.Builder; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.stat.FloatToolStat; | ||
import slimeknights.tconstruct.library.tools.stat.ToolStats; | ||
|
||
public class AirborneModifier extends NoLevelsModifier { | ||
public class AirborneModifier extends NoLevelsModifier implements ConditionalStatModifierHook { | ||
@Override | ||
public int getPriority() { | ||
return 75; // runs after other modifiers | ||
} | ||
|
||
@Override | ||
protected void registerHooks(Builder hookBuilder) { | ||
hookBuilder.addHook(this, TinkerHooks.CONDITIONAL_STAT); | ||
} | ||
|
||
@Override | ||
public void onBreakSpeed(IToolStackView tool, int level, BreakSpeed event, Direction sideHit, boolean isEffective, float miningSpeedModifier) { | ||
// the speed is reduced when not on the ground, cancel out | ||
if (!event.getEntity().isOnGround()) { | ||
event.setNewSpeed(event.getNewSpeed() * 5); | ||
} | ||
} | ||
|
||
@Override | ||
public float modifyStat(IToolStackView tool, ModifierEntry modifier, LivingEntity living, FloatToolStat stat, float baseValue, float multiplier) { | ||
if (stat == ToolStats.ACCURACY && !living.isOnGround() && !living.onClimbable()) { | ||
return baseValue + 0.5f; | ||
} | ||
return baseValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.