Skip to content

Commit

Permalink
Add scaled FOV modifier for any FOV changes that should respect FOV s…
Browse files Browse the repository at this point in the history
…caling

Fixes bow zooming in too much when you turn FOV effects scale to 0
  • Loading branch information
KnightMiner committed Feb 11, 2023
1 parent 8da65fd commit cf3195e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface TinkerDataKeys {

/** Float value for the FOV modifier, will be 1.0 if no change */
ComputableDataKey<FloatMultiplier> FOV_MODIFIER = TConstruct.createKey("zoom_multiplier", FloatMultiplier::new);

/** FOV modifier that only applies when not disabled in the settings menu */
ComputableDataKey<FloatMultiplier> SCALED_FOV_MODIFIER = TConstruct.createKey("scaled_fov_multiplier", FloatMultiplier::new);
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public static void checkFastUsingItem(IToolStackView tool, LivingEntity living)
instance.addTransientModifier(FAST_USE_ITEM);
}
if (living.level.isClientSide) {
living.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).set(IModifiable.FAST_USE_ITEM, 0.4f));
living.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.SCALED_FOV_MODIFIER).set(IModifiable.FAST_USE_ITEM, 0.4f));
}
}
}
Expand All @@ -382,7 +382,7 @@ public static void finishUsingItem(LivingEntity living) {
instance.removeModifier(FAST_USE_ITEM);
}
if (living.level.isClientSide) {
living.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.FOV_MODIFIER).remove(IModifiable.FAST_USE_ITEM));
living.getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> data.computeIfAbsent(TinkerDataKeys.SCALED_FOV_MODIFIER).remove(IModifiable.FAST_USE_ITEM));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.HumanoidArm;
Expand Down Expand Up @@ -112,10 +113,31 @@ static void renderHand(RenderHandEvent event) {
@SubscribeEvent
static void handleZoom(FOVModifierEvent event) {
event.getEntity().getCapability(TinkerDataCapability.CAPABILITY).ifPresent(data -> {
FloatMultiplier zoom = data.get(TinkerDataKeys.FOV_MODIFIER);
if (zoom != null) {
event.setNewfov(event.getNewfov() * zoom.getValue());
float newFov = event.getNewfov();

// scaled effects only apply if we have FOV scaling, nothing to do if 0
float effectScale = Minecraft.getInstance().options.fovEffectScale;
if (effectScale > 0) {
FloatMultiplier scaledZoom = data.get(TinkerDataKeys.SCALED_FOV_MODIFIER);
if (scaledZoom != null) {
// much easier when 1, save some effort
if (effectScale == 1) {
newFov *= scaledZoom.getValue();
} else {
// unlerp the fov before multiplitying to make sure we apply the proper amount
// we could use the original FOV, but someone else may have modified it
float original = event.getFov();
newFov *= Mth.lerp(effectScale, 1.0F, scaledZoom.getValue() * original) / original;
}
}
}

// non-scaled effects are much easier to deal with
FloatMultiplier constZoom = data.get(TinkerDataKeys.FOV_MODIFIER);
if (constZoom != null) {
newFov *= constZoom.getValue();
}
event.setNewfov(newFov);
});
}

Expand Down

0 comments on commit cf3195e

Please sign in to comment.