Skip to content

Commit 5ae1385

Browse files
committed
Add an option to better tooltips to specify the format to display item byte sizes in
closes #3751
1 parent 56423b7 commit 5ae1385

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

src/main/java/meteordevelopment/meteorclient/systems/modules/render/BetterTooltips.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import net.minecraft.component.DataComponentTypes;
2626
import net.minecraft.component.type.*;
2727
import net.minecraft.component.type.SuspiciousStewEffectsComponent.StewEffect;
28-
import net.minecraft.entity.*;
28+
import net.minecraft.entity.Bucketable;
29+
import net.minecraft.entity.EntityType;
30+
import net.minecraft.entity.LivingEntity;
31+
import net.minecraft.entity.SpawnReason;
2932
import net.minecraft.entity.effect.StatusEffectInstance;
3033
import net.minecraft.entity.effect.StatusEffectUtil;
3134
import net.minecraft.item.*;
@@ -163,6 +166,14 @@ public class BetterTooltips extends Module {
163166
.build()
164167
);
165168

169+
private final Setting<SortSize> sizeType = sgOther.add(new EnumSetting.Builder<SortSize>()
170+
.name("byte-size-format")
171+
.description("The format by which to display the item's byte size.")
172+
.defaultValue(SortSize.Dynamic)
173+
.visible(byteSize::get)
174+
.build()
175+
);
176+
166177
private final Setting<Boolean> statusEffects = sgOther.add(new BoolSetting.Builder()
167178
.name("status-effects")
168179
.description("Adds list of status effects to tooltips of food items.")
@@ -233,19 +244,25 @@ private void appendTooltip(ItemStackTooltipEvent event) {
233244
success.value().write(ByteCountDataOutput.INSTANCE);
234245

235246
int byteCount = ByteCountDataOutput.INSTANCE.getCount();
236-
String count;
247+
String count = switch (sizeType.get()) {
248+
case Bytes -> String.format("%d bytes", byteCount);
249+
case Kilobytes -> String.format("%.2f kB", byteCount / 1024f);
250+
case Megabytes -> String.format("%.4f MB", byteCount / 1048576f);
251+
case Dynamic -> {
252+
if (byteCount >= 1048576) yield String.format("%.2f MB", byteCount / 1048576f);
253+
else if (byteCount >= 1024) yield String.format("%.2f kB", byteCount / 1024f);
254+
else yield String.format("%d bytes", byteCount);
255+
}
256+
};
237257

238258
ByteCountDataOutput.INSTANCE.reset();
239259

240-
if (byteCount >= 1024) count = String.format("%.2f kb", byteCount / (float) 1024);
241-
else count = String.format("%d bytes", byteCount);
242-
243-
event.appendEnd(Text.literal(count).formatted(Formatting.GRAY));
260+
event.appendEnd(Text.literal(count).formatted(Formatting.DARK_GRAY));
244261
} catch (Exception e) {
245262
event.appendEnd(Text.literal("Error getting bytes.").formatted(Formatting.RED));
246263
}
247264
}
248-
case DataResult.Error<NbtElement> error ->
265+
case DataResult.Error<NbtElement> ignored ->
249266
event.appendEnd(Text.literal("Error getting bytes.").formatted(Formatting.RED));
250267
default ->
251268
throw new MatchException(null, null);
@@ -441,4 +458,11 @@ public enum DisplayWhen {
441458
Keybind,
442459
Always
443460
}
461+
462+
public enum SortSize {
463+
Bytes,
464+
Kilobytes,
465+
Megabytes,
466+
Dynamic,
467+
}
444468
}

0 commit comments

Comments
 (0)