Skip to content

Commit

Permalink
Optimise PotionTimersHud (MeteorDevelopment#4211)
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored and tyrannus00 committed Apr 25, 2024
1 parent 30bcf34 commit c1ecaee
Showing 1 changed file with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

package meteordevelopment.meteorclient.systems.hud.elements;

import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.ObjectObjectImmutablePair;
import meteordevelopment.meteorclient.settings.*;
import meteordevelopment.meteorclient.systems.hud.*;
import meteordevelopment.meteorclient.utils.misc.Names;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffectUtil;
import net.minecraft.util.StringHelper;

import java.util.ArrayList;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;
Expand Down Expand Up @@ -152,9 +155,8 @@ public class PotionTimersHud extends HudElement {
.build()
);

private final Color rainbow = new Color(255, 255, 255);
private double rainbowHue1;
private double rainbowHue2;
private final List<Pair<StatusEffectInstance, String>> texts = new ArrayList<>();
private double rainbowHue;

public PotionTimersHud() {
super(INFO);
Expand All @@ -180,10 +182,14 @@ public void tick(HudRenderer renderer) {
double width = 0;
double height = 0;

texts.clear();

for (StatusEffectInstance statusEffectInstance : mc.player.getStatusEffects()) {
if (hiddenEffects.get().contains(statusEffectInstance.getEffectType())) continue;
if (!showAmbient.get() && statusEffectInstance.isAmbient()) continue;
width = Math.max(width, renderer.textWidth(getString(statusEffectInstance), shadow.get(), getScale()));
String text = getString(statusEffectInstance);
texts.add(new ObjectObjectImmutablePair<>(statusEffectInstance, text));
width = Math.max(width, renderer.textWidth(text, shadow.get(), getScale()));
height += renderer.textHeight(shadow.get(), getScale());
}

Expand All @@ -204,47 +210,38 @@ public void render(HudRenderer renderer) {
return;
}

rainbowHue1 += rainbowSpeed.get() * renderer.delta;
if (rainbowHue1 > 1) rainbowHue1 -= 1;
else if (rainbowHue1 < -1) rainbowHue1 += 1;
rainbowHue += rainbowSpeed.get() * renderer.delta;
if (rainbowHue > 1) rainbowHue -= 1;
else if (rainbowHue < -1) rainbowHue += 1;

rainbowHue2 = rainbowHue1;
double localRainbowHue = rainbowHue;

for (StatusEffectInstance statusEffectInstance : mc.player.getStatusEffects()) {
if (hiddenEffects.get().contains(statusEffectInstance.getEffectType())) continue;
if (!showAmbient.get() && statusEffectInstance.isAmbient()) continue;
Color color = new Color(255, 255, 255);

switch (colorMode.get()) {
for (Pair<StatusEffectInstance, String> potionEffectEntry : texts) {
Color color = switch (colorMode.get()) {
case Effect -> {
int c = statusEffectInstance.getEffectType().getColor();
color.r = Color.toRGBAR(c);
color.g = Color.toRGBAG(c);
color.b = Color.toRGBAB(c);
int c = potionEffectEntry.left().getEffectType().getColor();
yield new Color(c).a(255);
}
case Flat -> {
color = flatColor.get();
flatColor.get().update();
yield flatColor.get();
}
case Rainbow -> {
rainbowHue2 += rainbowSpread.get();
int c = java.awt.Color.HSBtoRGB((float) rainbowHue2, rainbowSaturation.get().floatValue(), rainbowBrightness.get().floatValue());
rainbow.r = Color.toRGBAR(c);
rainbow.g = Color.toRGBAG(c);
rainbow.b = Color.toRGBAB(c);
color = rainbow;
localRainbowHue += rainbowSpread.get();
int c = java.awt.Color.HSBtoRGB((float) localRainbowHue, rainbowSaturation.get().floatValue(), rainbowBrightness.get().floatValue());
yield new Color(c);
}
}
};

String text = getString(statusEffectInstance);
String text = potionEffectEntry.right();
renderer.text(text, x + alignX(renderer.textWidth(text, shadow.get(), getScale()), alignment.get()), y, color, shadow.get(), getScale());

y += renderer.textHeight(shadow.get(), getScale());
}
}

private String getString(StatusEffectInstance statusEffectInstance) {
return String.format("%s %d (%s)", Names.get(statusEffectInstance.getEffectType()), statusEffectInstance.getAmplifier() + 1, statusEffectInstance.isInfinite() ? "inf" : StatusEffectUtil.getDurationText(statusEffectInstance, 1).getString()); //todo remove "inf" when font rendering can use symbols
return String.format("%s %d (%s)", Names.get(statusEffectInstance.getEffectType()), statusEffectInstance.getAmplifier() + 1, statusEffectInstance.isInfinite() ? "inf" : StringHelper.formatTicks(statusEffectInstance.getDuration())); //todo remove "inf" when font rendering can use symbols
}

private double getScale() {
Expand Down

0 comments on commit c1ecaee

Please sign in to comment.