From 2b2c81aa45db0de410a8f9714095cecb069662fd Mon Sep 17 00:00:00 2001 From: Markil3 <75867393+Markil3@users.noreply.github.com> Date: Mon, 30 Aug 2021 22:10:22 -0600 Subject: [PATCH] Allows some items to not trigger a hand/crosshair update. This is helpful if one is using Firework rockets to fly around the landscape. --- .../markil3/immersive_hud/ConfigManager.java | 49 +++++ .../java/markil3/immersive_hud/ModMenu.java | 193 +++++++++++------- .../markil3/immersive_hud/TimerUtils.java | 77 ++++--- .../assets/immersive_hud/lang/en_us.json | 6 +- 4 files changed, 208 insertions(+), 117 deletions(-) diff --git a/src/main/java/markil3/immersive_hud/ConfigManager.java b/src/main/java/markil3/immersive_hud/ConfigManager.java index cbc4e62..e259403 100644 --- a/src/main/java/markil3/immersive_hud/ConfigManager.java +++ b/src/main/java/markil3/immersive_hud/ConfigManager.java @@ -1,11 +1,19 @@ package markil3.immersive_hud; +import net.minecraft.item.Item; +import net.minecraft.item.Items; +import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.registry.Registry; import me.shedaniel.autoconfig.AutoConfig; import static markil3.immersive_hud.Main.TICKS_PER_SECOND; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + /** * Configuration manager of this mod, which reads from and writes to this mod's * configuration file. @@ -88,6 +96,7 @@ public class ConfigManager private boolean showArmor = true; private double minHealth = 0.5; private int minHunger = 17; + private Identifier[] ignoredItems = new Identifier[]{Registry.ITEM.getId(Items.FIREWORK_ROCKET)}; /** * Returns the instance of this class. @@ -247,6 +256,26 @@ public class ConfigManager return this.minHunger; } + /** + * Obtains a collection of items that won't cause hands and crosshairs to show when used. + * + * @return A set of ignored items. + */ + public Set getIgnoredItems() + { + return Arrays.stream(ignoredItems).map(Registry.ITEM::get).collect(Collectors.toSet()); + } + + /** + * Obtains a collection of items that won't cause hands and crosshairs to show when used. + * + * @return An array of identifiers. + */ + public Identifier[] getIgnoredIdentifiers() + { + return this.ignoredItems; + } + /** * Sets the time that the hands are allowed to display. * @@ -318,6 +347,26 @@ public class ConfigManager { this.minHunger = MathHelper.clamp(boundary, 0, 20); } + + /** + * Sets which items won't cause the hands and crosshairs to show when used. + * + * @param items - A set of ignored items. + */ + public void setIgnoredItems(Item... items) + { + this.ignoredItems = Arrays.stream(items).map(Registry.ITEM::getId).toArray(Identifier[]::new); + } + + /** + * Sets which items won't cause the hands and crosshairs to show when used. + * + * @param items - A set of ignored items. + */ + public void setIgnoredItems(String... items) + { + this.ignoredItems = Arrays.stream(items).map(Identifier::new).filter(i -> Registry.ITEM.getOrEmpty(i).isPresent()).toArray(Identifier[]::new); + } /** * Saves changes to this mod's configuration. */ diff --git a/src/main/java/markil3/immersive_hud/ModMenu.java b/src/main/java/markil3/immersive_hud/ModMenu.java index 7ec4c34..c653b3f 100644 --- a/src/main/java/markil3/immersive_hud/ModMenu.java +++ b/src/main/java/markil3/immersive_hud/ModMenu.java @@ -3,7 +3,11 @@ package markil3.immersive_hud; import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; +import net.minecraft.item.Items; import net.minecraft.text.TranslatableText; +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; +import net.minecraft.util.registry.Registry; import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.clothconfig2.api.ConfigBuilder; @@ -13,6 +17,11 @@ import me.shedaniel.clothconfig2.gui.entries.DoubleListEntry; import static markil3.immersive_hud.Main.TICKS_PER_SECOND; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Optional; +import java.util.stream.Collectors; + public class ModMenu implements ModMenuApi { @@ -29,42 +38,45 @@ public class ModMenu implements ModMenuApi "option.immersive_hud." + name + "MaxTime"), (float) value.getMaxTime() / TICKS_PER_SECOND) - .setDefaultValue(SHOW_TIME) - .setMin(0) - .setMax(10 * 60 * TICKS_PER_SECOND) // 10 Minutes - .setTooltip(new TranslatableText( - "tooltip.immersive_hud." + name + - "MaxTime")) - .setSaveConsumer(val -> value.setMaxTime((int) (val * TICKS_PER_SECOND))) - .build(); + .setDefaultValue(SHOW_TIME) + .setMin(0) + .setMax(10 * 60 * TICKS_PER_SECOND) // 10 Minutes + .setTooltip(new TranslatableText( + "tooltip.immersive_hud." + name + + "MaxTime")) + .setSaveConsumer(val -> value + .setMaxTime((int) (val * TICKS_PER_SECOND))) + .build(); DoubleListEntry fadeIn = entryBuilder.startDoubleField(new TranslatableText( "option.immersive_hud." + name + "FadeIn"), (float) value.getFadeInTime() / TICKS_PER_SECOND) - .setDefaultValue(FADE_IN) - .setMin(0) - .setMax(10 * 60 * TICKS_PER_SECOND) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud." + name + - "FadeIn")) - .setSaveConsumer(val -> value.setFadeInTime((int) (val * TICKS_PER_SECOND))) - .build(); + .setDefaultValue(FADE_IN) + .setMin(0) + .setMax(10 * 60 * TICKS_PER_SECOND) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud." + name + + "FadeIn")) + .setSaveConsumer(val -> value + .setFadeInTime((int) (val * TICKS_PER_SECOND))) + .build(); DoubleListEntry fadeOut = entryBuilder.startDoubleField(new TranslatableText( "option.immersive_hud." + name + "FadeOut"), (float) value.getFadeOutTime() / TICKS_PER_SECOND) - .setDefaultValue(FADE_OUT) - .setMin(0) - .setMax(10 * 60 * TICKS_PER_SECOND) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud." + name + - "FadeOut")) - .setSaveConsumer(val -> value.setFadeOutTime((int) (val * TICKS_PER_SECOND))) - .build(); + .setDefaultValue(FADE_OUT) + .setMin(0) + .setMax(10 * 60 * TICKS_PER_SECOND) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud." + name + + "FadeOut")) + .setSaveConsumer(val -> value + .setFadeOutTime((int) (val * TICKS_PER_SECOND))) + .build(); cat.addEntry(maxTime); cat.addEntry(fadeIn); @@ -79,23 +91,25 @@ public class ModMenu implements ModMenuApi ConfigEntryBuilder entryBuilder; ConfigManager instance = ConfigManager.getInstance(); final ConfigBuilder builder = ConfigBuilder.create() - .setParentScreen(screen) - .setTitle(new TranslatableText( - "immersive_hud.configGui.title")) - .setSavingRunnable(() -> { - try - { - if (Class.forName("me.shedaniel.autoconfig.AutoConfig") != null) - { - AutoConfig.getConfigHolder(ConfigManagerCloth.class) - .save(); - } - } - catch (ClassNotFoundException e) - { - // Do nothing - } - }); + .setParentScreen(screen) + .setTitle(new TranslatableText( + "immersive_hud.configGui.title")) + .setSavingRunnable(() -> { + try + { + if (Class + .forName("me.shedaniel.autoconfig.AutoConfig") != null) + { + AutoConfig + .getConfigHolder(ConfigManagerCloth.class) + .save(); + } + } + catch (ClassNotFoundException e) + { + // Do nothing + } + }); general = builder.getOrCreateCategory(new TranslatableText( "category.immersive_hud.general")); @@ -124,65 +138,92 @@ public class ModMenu implements ModMenuApi "option.immersive_hud" + ".crosshairTime"), (float) instance.getCrosshairTime() / TICKS_PER_SECOND) - .setDefaultValue(6) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud" + - ".crosshairTime")) - .setSaveConsumer(val -> instance.setCrosshairTime((int) (val * TICKS_PER_SECOND))) - .build()); + .setDefaultValue(6) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud" + + ".crosshairTime")) + .setSaveConsumer(val -> instance + .setCrosshairTime((int) (val * TICKS_PER_SECOND))) + .build()); general.addEntry(entryBuilder.startBooleanToggle(new TranslatableText( "option.immersive_hud.hideCrosshair"), instance.hideCrosshair()) - .setDefaultValue(true) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.hideCrosshair")) - .setSaveConsumer(val -> instance.hideCrosshair(val)) - .build()); + .setDefaultValue(true) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.hideCrosshair")) + .setSaveConsumer(val -> instance.hideCrosshair(val)) + .build()); general.addEntry(entryBuilder.startDoubleField(new TranslatableText( "option.immersive_hud.handTime"), (float) instance.getHandTime() / TICKS_PER_SECOND) - .setDefaultValue(30) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.handTime")) - .setSaveConsumer(val -> instance.setHandTime((int) (val * TICKS_PER_SECOND))) - .build()); + .setDefaultValue(30) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.handTime")) + .setSaveConsumer(val -> instance + .setHandTime((int) (val * TICKS_PER_SECOND))) + .build()); general.addEntry(entryBuilder.startBooleanToggle(new TranslatableText( "option.immersive_hud.hideHands"), instance.hideHands()) - .setDefaultValue(true) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.hideHands")) - .setSaveConsumer(val -> instance.hideHands(val)) - .build()); + .setDefaultValue(true) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.hideHands")) + .setSaveConsumer(val -> instance.hideHands(val)) + .build()); general.addEntry(entryBuilder.startDoubleField(new TranslatableText( "option.immersive_hud.minHealth"), instance.getMinHealth()) - .setDefaultValue(0.5) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.minHealth")) - .setSaveConsumer(val -> instance.setMinHealth(val)) - .build()); + .setDefaultValue(0.5) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.minHealth")) + .setSaveConsumer(val -> instance.setMinHealth(val)) + .build()); general.addEntry(entryBuilder.startIntField(new TranslatableText( "option.immersive_hud.minHunger"), instance.getMinHunger()) - .setDefaultValue(17) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.minHunger")) - .setSaveConsumer(val -> instance.setMinHunger(val)) - .build()); + .setDefaultValue(17) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.minHunger")) + .setSaveConsumer(val -> instance.setMinHunger(val)) + .build()); general.addEntry(entryBuilder.startBooleanToggle(new TranslatableText( "option.immersive_hud.showArmor"), instance.shouldShowArmor()) - .setDefaultValue(true) - .setTooltip(new TranslatableText( - "tooltip.immersive_hud.showArmor")) - .setSaveConsumer(val -> instance.shouldShowArmor(val)) + .setDefaultValue(true) + .setTooltip(new TranslatableText( + "tooltip.immersive_hud.showArmor")) + .setSaveConsumer(val -> instance.shouldShowArmor(val)) + .build()); + + general.addEntry(entryBuilder + .startStrList(new TranslatableText("option.immersive_hud.ignoreList"), Arrays + .asList(instance + .getIgnoredIdentifiers()).stream().map(Identifier::toString) + .collect(Collectors.toList())) + .setDefaultValue(Arrays + .asList(Registry.ITEM.getId(Items.FIREWORK_ROCKET).toString())) + .setTooltip(new TranslatableText("tooltip.immersive_hud.ignoreList")) + .setSaveConsumer(val -> instance.setIgnoredItems(val.toArray(new String[0]))).setErrorSupplier(val -> { + try + { + Optional item = val.stream().filter(i -> !Registry.ITEM.getOrEmpty(new Identifier(i)).isPresent()).findFirst(); + if (item.isPresent()) + { + return Optional.of(new TranslatableText("error.immersive_hud.ignoreList.unknownItem")); + } + } + catch (InvalidIdentifierException e) + { + return Optional.of(new TranslatableText("error.immersive_hud.ignoreList.illegalId")); + } + return Optional.empty(); + }) .build()); return builder.build(); diff --git a/src/main/java/markil3/immersive_hud/TimerUtils.java b/src/main/java/markil3/immersive_hud/TimerUtils.java index 36b600f..ed8d1c7 100644 --- a/src/main/java/markil3/immersive_hud/TimerUtils.java +++ b/src/main/java/markil3/immersive_hud/TimerUtils.java @@ -49,6 +49,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Optional; +import java.util.Set; /** * Contains the logic for changing the HUD. @@ -287,9 +288,8 @@ public class TimerUtils * view, and briefly brings the health and hunger into view if the held item * is food. * - * @param hand - The hand being triggered + * @param hand - The hand being triggered * @param clear - Whether the hand it being enabled or disabled. - * * @since 0.1-1.16.4-fabric */ public static void onClick(Hand hand, boolean clear) @@ -304,6 +304,7 @@ public class TimerUtils LivingEntity entity; Item item; Iterator hands; + Set ignored; if (mc.getCameraEntity() instanceof LivingEntity) { entity = (LivingEntity) mc.getCameraEntity(); @@ -329,6 +330,14 @@ public class TimerUtils .map(ItemStack::getItem) .orElse(null); } + if (item != null) + { + ignored = ConfigManager.getInstance().getIgnoredItems(); + if (ignored.contains(item)) + { + return; + } + } crosshairTime = ConfigManager.getInstance().getCrosshairTime(); if (hand == Hand.MAIN_HAND) { @@ -345,11 +354,11 @@ public class TimerUtils if (item.isFood()) { healthTime = health.getMaxTime() - (healthTime > 0 ? - health.getFadeInTime() : - 0); + health.getFadeInTime() : + 0); hungerTime = hunger.getMaxTime() - (hungerTime > 0 ? - hunger.getFadeInTime() : - 0); + hunger.getFadeInTime() : + 0); } } else @@ -362,11 +371,9 @@ public class TimerUtils * Run whenever a hand is rendered in 1st person. This brings the player * hands into and out of view as needed. * - * @param hand - The hand being rendered. + * @param hand - The hand being rendered. * @param matrixStack - The rendering stack. - * * @return If true, then don't render this hand at all. - * * @since 0.1-1.16.4-fabric */ public static boolean onRenderHand(Hand hand, @@ -432,7 +439,6 @@ public class TimerUtils * needed. * * @return If true, then cancel drawing the crosshair. - * * @since 0.1-1.16.4-fabric */ public static boolean drawCrosshair(float ticks) @@ -469,8 +475,8 @@ public class TimerUtils > 0) { setAlpha(Main.getAlpha(crosshairTime > 0 ? - crosshairTime : - CROSSHAIR_TIME, CROSSHAIR_TIME, 0, 0)); + crosshairTime : + CROSSHAIR_TIME, CROSSHAIR_TIME, 0, 0)); } else { @@ -489,7 +495,6 @@ public class TimerUtils * as needed. * * @return If true, then cancel drawing the mount health bar. - * * @since 0.1-1.16.4-fabric */ public static boolean drawMountHealth(MatrixStack stack, float ticks) @@ -533,8 +538,8 @@ public class TimerUtils if (changed) { mountTime = healthTimes.getMaxTime() - (mountTime > 0 ? - healthTimes.getFadeInTime() : - 0); + healthTimes.getFadeInTime() : + 0); } else if (mountTime > 0) { @@ -621,7 +626,6 @@ public class TimerUtils * Obtains how transparent * * @param effectinstance - * * @return */ public static float getPotionAlpha(StatusEffectInstance effectinstance) @@ -633,7 +637,8 @@ public class TimerUtils if (effectinstance.getDuration() <= BLINK_TIME) { effectAlpha = - MathHelper.sin(7000F / (effectinstance.getDuration() + 16F * (float) Math.PI)) * 50F / (effectinstance + MathHelper.sin(7000F / (effectinstance + .getDuration() + 16F * (float) Math.PI)) * 50F / (effectinstance .getDuration() + 100F) + 0.5F; } else if (effectinstance.getDuration() <= BLINK_TIME + potion.getFadeInTime()) @@ -658,7 +663,6 @@ public class TimerUtils * needed. * * @return If true, then cancel drawing the armor bar. - * * @since 0.1-1.16.4-fabric */ public static boolean drawArmor() @@ -686,7 +690,6 @@ public class TimerUtils * needed. * * @return If true, then cancel drawing the health. - * * @since 0.1-1.16.4-fabric */ public static boolean drawHealth(float ticks) @@ -730,8 +733,8 @@ public class TimerUtils if (changed) { healthTime = healthTimes.getMaxTime() - (healthTime > 0 ? - healthTimes.getFadeInTime() : - 0); + healthTimes.getFadeInTime() : + 0); } else if (healthTime > 0) { @@ -762,7 +765,6 @@ public class TimerUtils * needed. * * @return If true, then cancel drawing the hunger bar. - * * @since 0.1-1.16.4-fabric */ public static boolean drawHunger(float ticks) @@ -809,8 +811,8 @@ public class TimerUtils if (changed) { hungerTime = hungerTimes.getMaxTime() - (hungerTime > 0 ? - hungerTimes.getFadeInTime() : - 0); + hungerTimes.getFadeInTime() : + 0); } else if (hungerTime > 0) { @@ -841,9 +843,7 @@ public class TimerUtils * as needed. * * @param stack - * * @return If true, then cancel drawing the jump bar. - * * @since 0.1-1.16.4-fabric */ public static boolean drawJumpbar(MatrixStack stack, float ticks) @@ -865,8 +865,8 @@ public class TimerUtils if (entity.method_3151() > 0) { jumpTime = jump.getMaxTime() - (jumpTime > 0 ? - jump.getFadeInTime() : - 0); + jump.getFadeInTime() : + 0); } else if (jumpTime > 0) { @@ -890,7 +890,6 @@ public class TimerUtils * as needed. * * @return If true, then cancel drawing the experience bar. - * * @since 0.1-1.16.4-fabric */ public static boolean drawExperience(MatrixStack stack, float ticks) @@ -919,8 +918,8 @@ public class TimerUtils if (changed) { experienceTime = experience.getMaxTime() - (experienceTime > 0 ? - experience.getFadeInTime() : - 0); + experience.getFadeInTime() : + 0); } else if (experienceTime > 0) { @@ -945,7 +944,6 @@ public class TimerUtils * or not to draw it. * * @return If true, then cancel drawing the hotbar. - * * @see #recolorHotbar(MatrixStack) * @since 0.1-1.16.4-fabric */ @@ -1054,7 +1052,7 @@ public class TimerUtils { if (entity.isUsingItem() && entity .getActiveHand() != null && entity.getActiveHand() - .ordinal() == i) + .ordinal() == i) { switch (i) { @@ -1117,11 +1115,11 @@ public class TimerUtils if (item.isFood()) { healthTime = health.getMaxTime() - (healthTime > 0 ? - health.getFadeInTime() : - 0); + health.getFadeInTime() : + 0); hungerTime = hunger.getMaxTime() - (hungerTime > 0 ? - hunger.getFadeInTime() : - 0); + hunger.getFadeInTime() : + 0); } if (i == 0) { @@ -1150,8 +1148,8 @@ public class TimerUtils if (changed) { hotbarTime = hotbar.getMaxTime() - (hotbarTime > 0 ? - hotbar.getFadeInTime() : - 0); + hotbar.getFadeInTime() : + 0); } else if (hotbarTime > 0) { @@ -1166,7 +1164,6 @@ public class TimerUtils * needed. * * @return If true, then cancel drawing the crosshair. - * * @since 0.1-1.16.4-fabric */ public static void recolorHotbar(MatrixStack stack) diff --git a/src/main/resources/assets/immersive_hud/lang/en_us.json b/src/main/resources/assets/immersive_hud/lang/en_us.json index 238c055..775863a 100644 --- a/src/main/resources/assets/immersive_hud/lang/en_us.json +++ b/src/main/resources/assets/immersive_hud/lang/en_us.json @@ -25,6 +25,7 @@ "option.immersive_hud.minHealth": "Minimum Health Fade", "option.immersive_hud.minHunger": "Minimum Hunger Fade", "option.immersive_hud.showArmor": "Show Armor", + "option.immersive_hud.ignoreList": "Ignored Items", "tooltip.immersive_hud.hotbarMaxTime": "How long the hotbar can be displayed on screen in seconds", "tooltip.immersive_hud.hotbarFadeIn": "How many seconds it takes for the hotbar to fade in", "tooltip.immersive_hud.hotbarFadeOut": "How many seconds it takes for the hotbar to move out", @@ -49,5 +50,8 @@ "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.minHunger": "When the hunger level goes below this level, the hunger 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", + "error.immersive_hud.ignoreList.unknownItem": "Unknown item %s", + "error.immersive_hud.ignoreList.illegalId": "%s is not a legal item identifier" } \ No newline at end of file