Adds fading for the oxygen bar.

This should fix #2. Thanks, @Reddy64!
This commit is contained in:
Markil3
2022-02-03 09:59:22 -07:00
parent a3523a274b
commit b799830e41
7 changed files with 171 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
plugins { plugins {
id 'fabric-loom' version '0.10-SNAPSHOT' id 'fabric-loom' version '0.11-SNAPSHOT'
id 'maven-publish' id 'maven-publish'
} }

View File

@@ -4,12 +4,12 @@ org.gradle.jvmargs=-Xmx4G
# Fabric Properties # Fabric Properties
# check these on https://fabricmc.net/use # check these on https://fabricmc.net/use
minecraft_version=1.18.1 minecraft_version=1.18.1
yarn_mappings=1.18.1+build.1 yarn_mappings=1.18.1+build.22
loader_version=0.12.12 loader_version=0.12.12
cloth_version=6.1.48 cloth_version=6.1.48
modmenu_version=3.0.1 modmenu_version=3.0.1
fabric_version=0.44.0+1.18 fabric_version=0.46.2+1.18
# Mod Properties # Mod Properties
mod_version = 1.3 mod_version = 1.3

View File

@@ -90,6 +90,7 @@ public class ConfigManager
private TimeValues jumpTime = new TimeValues(); private TimeValues jumpTime = new TimeValues();
private TimeValues healthTime = new TimeValues(); private TimeValues healthTime = new TimeValues();
private TimeValues hungerTime = new TimeValues(); private TimeValues hungerTime = new TimeValues();
private TimeValues airTime = new TimeValues();
private TimeValues effectTime = new TimeValues(); private TimeValues effectTime = new TimeValues();
private int crosshairTime = 6 * TICKS_PER_SECOND; private int crosshairTime = 6 * TICKS_PER_SECOND;
private boolean hideCrosshair = true; private boolean hideCrosshair = true;
@@ -98,6 +99,7 @@ public class ConfigManager
private boolean showArmor = true; private boolean showArmor = true;
private double minHealth = 0.5; private double minHealth = 0.5;
private int minHunger = 17; private int minHunger = 17;
private int minAir = 300;
private Identifier[] ignoredItems = new Identifier[]{Registry.ITEM.getId(Items.FIREWORK_ROCKET)}; private Identifier[] ignoredItems = new Identifier[]{Registry.ITEM.getId(Items.FIREWORK_ROCKET)};
private Identifier[] enchantingBlocks = new Identifier[]{Registry.BLOCK.getId(Blocks.ENCHANTING_TABLE), Registry.BLOCK.getId(Blocks.ANVIL), Registry.BLOCK.getId(Blocks.BOOKSHELF), Registry.BLOCK.getId(Blocks.FURNACE), Registry.BLOCK.getId(Blocks.BLAST_FURNACE), Registry.BLOCK.getId(Blocks.SMOKER), Registry.BLOCK.getId(Blocks.GRINDSTONE)}; private Identifier[] enchantingBlocks = new Identifier[]{Registry.BLOCK.getId(Blocks.ENCHANTING_TABLE), Registry.BLOCK.getId(Blocks.ANVIL), Registry.BLOCK.getId(Blocks.BOOKSHELF), Registry.BLOCK.getId(Blocks.FURNACE), Registry.BLOCK.getId(Blocks.BLAST_FURNACE), Registry.BLOCK.getId(Blocks.SMOKER), Registry.BLOCK.getId(Blocks.GRINDSTONE)};
@@ -177,6 +179,11 @@ public class ConfigManager
{ {
return this.hungerTime; return this.hungerTime;
} }
public TimeValues getAirTime()
{
return this.airTime;
}
/** /**
* Obtains time values related to potion effects. * Obtains time values related to potion effects.
@@ -258,6 +265,17 @@ public class ConfigManager
{ {
return this.minHunger; return this.minHunger;
} }
/**
* Obtains the minimum oxygen for fading. Anything below that and the oxygen
* always displays.
*
* @return The oxygen boundary, from 0 to 20.
*/
public int getMinAir()
{
return this.minAir;
}
/** /**
* Obtains a collection of items that won't cause hands and crosshairs to show when used. * Obtains a collection of items that won't cause hands and crosshairs to show when used.
@@ -370,6 +388,17 @@ public class ConfigManager
{ {
this.minHunger = MathHelper.clamp(boundary, 0, 20); this.minHunger = MathHelper.clamp(boundary, 0, 20);
} }
/**
* Sets the minimum oxygen value for fading. Anything below that and the
* air bar always displays.
*
* @param boundary - The air boundary, from 0 to 20.
*/
public void setMinAir(int boundary)
{
this.minAir = MathHelper.clamp(boundary, 0, 20);
}
/** /**
* Sets which items won't cause the hands and crosshairs to show when used. * Sets which items won't cause the hands and crosshairs to show when used.

View File

@@ -125,6 +125,9 @@ public class ModMenu implements ModMenuApi
startTimeField("hunger", entryBuilder, startTimeField("hunger", entryBuilder,
general, general,
instance.getHungerTime()); instance.getHungerTime());
startTimeField("air", entryBuilder,
general,
instance.getAirTime());
startTimeField("effect", entryBuilder, startTimeField("effect", entryBuilder,
general, general,
instance.getPotionTime()); instance.getPotionTime());
@@ -184,6 +187,15 @@ public class ModMenu implements ModMenuApi
"tooltip.immersive_hud.minHunger")) "tooltip.immersive_hud.minHunger"))
.setSaveConsumer(val -> instance.setMinHunger(val)) .setSaveConsumer(val -> instance.setMinHunger(val))
.build()); .build());
general.addEntry(entryBuilder.startIntField(new TranslatableText(
"option.immersive_hud.minAir"),
instance.getMinAir())
.setDefaultValue(300)
.setTooltip(new TranslatableText(
"tooltip.immersive_hud.minAir"))
.setSaveConsumer(val -> instance.setMinAir(val))
.build());
general.addEntry(entryBuilder.startBooleanToggle(new TranslatableText( general.addEntry(entryBuilder.startBooleanToggle(new TranslatableText(
"option.immersive_hud.showArmor"), "option.immersive_hud.showArmor"),

View File

@@ -131,11 +131,21 @@ public class TimerUtils
* in hunger. * in hunger.
*/ */
private static int hunger = -1; private static int hunger = -1;
/**
* Records what the oxygen level was previously. Used to check for a change
* in oxygen.
*/
private static int air = -1;
/** /**
* Records whether or not there was food poisoning. Used to check for a * Records whether or not there was food poisoning. Used to check for a
* change in hunger. * change in hunger.
*/ */
private static boolean isFoodPoisoned = false; private static boolean isFoodPoisoned = false;
/**
* How many more ticks the oxygen bar will be onscreen. Every tick is 1/20th
* of a second.
*/
private static double airTime;
/** /**
* How many more ticks the mount's health bar will be onscreen. Every tick * How many more ticks the mount's health bar will be onscreen. Every tick
@@ -256,6 +266,8 @@ public class TimerUtils
ConfigManager.getInstance().getJumpTime(); ConfigManager.getInstance().getJumpTime();
ConfigManager.TimeValues healthTimes = ConfigManager.getInstance().getHealthTime(); ConfigManager.TimeValues healthTimes = ConfigManager.getInstance().getHealthTime();
ConfigManager.TimeValues hungerTimes = ConfigManager.getInstance().getHungerTime(); ConfigManager.TimeValues hungerTimes = ConfigManager.getInstance().getHungerTime();
ConfigManager.TimeValues airTimes =
ConfigManager.getInstance().getAirTime();
float hotbarAlpha = Main.getAlpha(hotbarTime, float hotbarAlpha = Main.getAlpha(hotbarTime,
hotbar.getMaxTime(), hotbar.getMaxTime(),
@@ -277,13 +289,24 @@ public class TimerUtils
hungerTimes.getMaxTime(), hungerTimes.getMaxTime(),
hungerTimes.getFadeInTime(), hungerTimes.getFadeInTime(),
hungerTimes.getFadeOutTime())); hungerTimes.getFadeOutTime()));
statusAlpha = Math.max(statusAlpha, Main.getAlpha(airTime,
airTimes.getMaxTime(),
airTimes.getFadeInTime(),
airTimes.getFadeOutTime()));
/* /*
* Draw it in its standard location until it is completely transparent, then move it * Draw it in its standard location until it is completely transparent, then move it
* instantly. * instantly.
*/ */
statusAlpha = (float) Math.ceil(statusAlpha); statusAlpha = (float) Math.ceil(statusAlpha);
return 7 * (1 - Math.max(experienceAlpha, float trans = 7 * (1 - Math.max(experienceAlpha,
jumpAlpha)) + 22 * (1 - hotbarAlpha) + 42 * (1 - statusAlpha); jumpAlpha)) + 22 * (1 - hotbarAlpha) + 42 * (1 - statusAlpha) + 9 * (1 - Math.max(Main.getAlpha(hungerTime,
hungerTimes.getMaxTime(),
hungerTimes.getFadeInTime(),
hungerTimes.getFadeOutTime()), Main.getAlpha(healthTime,
healthTimes.getMaxTime(),
healthTimes.getFadeInTime(),
healthTimes.getFadeOutTime())));
return trans;
} }
/** /**
@@ -848,6 +871,93 @@ public class TimerUtils
return hungerTime == 0 && hunger > HUNGER_BOUNDARY && !entity return hungerTime == 0 && hunger > HUNGER_BOUNDARY && !entity
.hasStatusEffect(StatusEffects.HUNGER); .hasStatusEffect(StatusEffects.HUNGER);
} }
/**
* Determines whether or not to draw the oxygen bar, adjusting the alpha as
* needed.
*
* @return If true, then cancel drawing the oxygen bar.
*
* @since 1.3.1-1.18-fabric
*/
public static boolean drawAir(float ticks)
{
/*
* When air falls to this level or below, the oxygen bar won't
* disappear, allowing the player to be constantly reminded of their
* low oxygen.
*/
final int AIR_BOUNDARY = ConfigManager.getInstance().getMinAir();
ConfigManager.TimeValues airTimes =
ConfigManager.getInstance().getAirTime();
MinecraftClient mc = MinecraftClient.getInstance();
PlayerEntity entity;
boolean canceled;
boolean changed = false;
if (mc.getCameraEntity() instanceof PlayerEntity)
{
entity = (PlayerEntity) mc.getCameraEntity();
}
else
{
return true;
}
if (air != entity.getAir())
{
float dividor = entity.getMaxAir() / 10;
if (air / dividor != entity.getAir() / dividor)
{
changed = true;
}
air = entity.getAir();
}
if (air <= AIR_BOUNDARY)
{
changed = true;
}
if (entity.hasStatusEffect(StatusEffects
.CONDUIT_POWER) || entity.hasStatusEffect(StatusEffects
.WATER_BREATHING))
{
changed = false;
}
if (changed)
{
airTime = airTimes.getMaxTime() - (airTime > 0 ?
airTimes.getFadeInTime() :
0);
}
else if (airTime > 0)
{
airTime -= ticks;
}
if (airTime <= 0 && (air >= AIR_BOUNDARY || entity.hasStatusEffect(StatusEffects
.CONDUIT_POWER) || entity.hasStatusEffect(StatusEffects
.WATER_BREATHING)))
{
return true;
}
/*
* Only makes a change if the player is healthy. Otherwise,
* the bar is shown.
*/
if (entity.getAir() >= entity.getMaxAir() || entity.hasStatusEffect(StatusEffects
.CONDUIT_POWER) || entity.hasStatusEffect(StatusEffects
.WATER_BREATHING))
{
setAlpha(Main.getAlpha(airTime,
airTimes.getMaxTime(),
airTimes.getFadeInTime(),
airTimes.getFadeOutTime()));
}
return airTime == 0 && entity.getAir() >= entity.getMaxAir() && !entity.hasStatusEffect(StatusEffects
.CONDUIT_POWER) && !entity.hasStatusEffect(StatusEffects
.WATER_BREATHING);
}
/** /**
* Determines whether or not to draw the horse jump bar, adjusting the alpha * Determines whether or not to draw the horse jump bar, adjusting the alpha

View File

@@ -253,8 +253,9 @@ public class HudHook
{ {
try try
{ {
float trans = TimerUtils.getHealthTranslation();
stack.push(); stack.push();
stack.translate(0F, TimerUtils.getHealthTranslation(), 0F); stack.translate(0F, trans, 0F);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -381,6 +382,10 @@ public class HudHook
* Resets the color so that nothing else is bothered. * Resets the color so that nothing else is bothered.
*/ */
TimerUtils.setAlpha(1F); TimerUtils.setAlpha(1F);
if (TimerUtils.drawAir(MinecraftClient.getInstance().getTickDelta()))
{
TimerUtils.setAlpha(0F);
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -398,6 +403,7 @@ public class HudHook
{ {
try try
{ {
TimerUtils.setAlpha(1F);
stack.pop(); stack.pop();
} }
catch (Exception e) catch (Exception e)

View File

@@ -15,6 +15,9 @@
"option.immersive_hud.hungerMaxTime": "Hunger Bar Display Time", "option.immersive_hud.hungerMaxTime": "Hunger Bar Display Time",
"option.immersive_hud.hungerFadeIn": "Hunger Bar Fade In Time", "option.immersive_hud.hungerFadeIn": "Hunger Bar Fade In Time",
"option.immersive_hud.hungerFadeOut": "Hunger Bar Fade Out Time", "option.immersive_hud.hungerFadeOut": "Hunger Bar Fade Out Time",
"option.immersive_hud.airMaxTime": "Oxygen Bar Display Time",
"option.immersive_hud.airFadeIn": "Oxygen Bar Fade In Time",
"option.immersive_hud.airFadeOut": "Oxygen Bar Fade Out Time",
"option.immersive_hud.effectMaxTime": "Potion Display Time", "option.immersive_hud.effectMaxTime": "Potion Display Time",
"option.immersive_hud.effectFadeIn": "Potion Fade In Time", "option.immersive_hud.effectFadeIn": "Potion Fade In Time",
"option.immersive_hud.effectFadeOut": "Potion Fade Out Time", "option.immersive_hud.effectFadeOut": "Potion Fade Out Time",
@@ -24,6 +27,7 @@
"option.immersive_hud.hideHands": "Hide Hands", "option.immersive_hud.hideHands": "Hide Hands",
"option.immersive_hud.minHealth": "Minimum Health Fade", "option.immersive_hud.minHealth": "Minimum Health Fade",
"option.immersive_hud.minHunger": "Minimum Hunger Fade", "option.immersive_hud.minHunger": "Minimum Hunger Fade",
"option.immersive_hud.minAir": "Minimum Oxygen Fade",
"option.immersive_hud.showArmor": "Show Armor", "option.immersive_hud.showArmor": "Show Armor",
"option.immersive_hud.ignoreList": "Ignored Items", "option.immersive_hud.ignoreList": "Ignored Items",
"option.immersive_hud.enchantingList": "Experience Blocks", "option.immersive_hud.enchantingList": "Experience Blocks",
@@ -42,6 +46,9 @@
"tooltip.immersive_hud.hungerMaxTime": "How long the hunger bar can be displayed on screen in seconds", "tooltip.immersive_hud.hungerMaxTime": "How long the hunger bar can be displayed on screen in seconds",
"tooltip.immersive_hud.hungerFadeIn": "How many seconds it takes for the hunger bar to fade in", "tooltip.immersive_hud.hungerFadeIn": "How many seconds it takes for the hunger bar to fade in",
"tooltip.immersive_hud.hungerFadeOut": "How many seconds it takes for the hunger bar to fade out", "tooltip.immersive_hud.hungerFadeOut": "How many seconds it takes for the hunger bar to fade out",
"tooltip.immersive_hud.airMaxTime": "How long the hunger bar can be oxygen on screen in seconds",
"tooltip.immersive_hud.airFadeIn": "How many seconds it takes for the oxygen bar to fade in",
"tooltip.immersive_hud.airFadeOut": "How many seconds it takes for the oxygen bar to fade out",
"tooltip.immersive_hud.effectMaxTime": "How long potion icons can be displayed on screen in seconds", "tooltip.immersive_hud.effectMaxTime": "How long potion icons can be displayed on screen in seconds",
"tooltip.immersive_hud.effectFadeIn": "How many seconds it takes for potion icons to fade in", "tooltip.immersive_hud.effectFadeIn": "How many seconds it takes for potion icons to fade in",
"tooltip.immersive_hud.effectFadeOut": "How many seconds it takes for potion icons to fade out", "tooltip.immersive_hud.effectFadeOut": "How many seconds it takes for potion icons to fade out",
@@ -51,6 +58,7 @@
"tooltip.immersive_hud.hideHands": "Whether or not hands should be hidden after a period of time", "tooltip.immersive_hud.hideHands": "Whether or not hands should be hidden after a period of time",
"tooltip.immersive_hud.minHealth": "When the percentage of health goes below this level, the health bar won't fade away", "tooltip.immersive_hud.minHealth": "When the percentage of health goes below this level, the health bar won't fade away",
"tooltip.immersive_hud.minHunger": "When the hunger level goes below this level, the hunger bar won't fade away", "tooltip.immersive_hud.minHunger": "When the hunger level goes below this level, the hunger bar won't fade away",
"tooltip.immersive_hud.minAir": "When the oxygen level goes below this level, the oxygen bar won't fade away",
"tooltip.immersive_hud.showArmor": "Whether or not the armor bar should display on the HUD", "tooltip.immersive_hud.showArmor": "Whether or not the armor bar should display on the HUD",
"tooltip.immersive_hud.ignoreList": "Which items will not trigger the arm and crosshairs when used", "tooltip.immersive_hud.ignoreList": "Which items will not trigger the arm and crosshairs when used",
"tooltip.immersive_hud.enchantingList": "Which blocks will show the experience bar when looked at.", "tooltip.immersive_hud.enchantingList": "Which blocks will show the experience bar when looked at.",