StatusDisplayGui.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package me.km.networking;
  2. import com.mojang.blaze3d.matrix.MatrixStack;
  3. import java.util.Collection;
  4. import java.util.LinkedList;
  5. import java.util.TreeMap;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.gui.AbstractGui;
  8. import net.minecraft.potion.EffectInstance;
  9. import net.minecraft.util.text.TextFormatting;
  10. import net.minecraft.util.text.LanguageMap;
  11. import net.minecraftforge.api.distmarker.Dist;
  12. import net.minecraftforge.api.distmarker.OnlyIn;
  13. @OnlyIn(Dist.CLIENT)
  14. public class StatusDisplayGui extends AbstractGui {
  15. public final static StatusDisplayGui INSTANCE = new StatusDisplayGui(Minecraft.getInstance());
  16. private class LineData implements Comparable<LineData> {
  17. private int time;
  18. private final String text;
  19. public LineData(int time, String text) {
  20. this.time = time;
  21. int index = text.indexOf('#');
  22. if(index != -1) {
  23. this.text = text.substring(0, index)
  24. + LanguageMap.getInstance().getOrDefault(text.substring(index + 1));
  25. } else {
  26. this.text = text;
  27. }
  28. }
  29. public String getText() {
  30. return text;
  31. }
  32. public String getTimeText() {
  33. if(time == Integer.MAX_VALUE) {
  34. return "";
  35. }
  36. return (time / 20) + "s ";
  37. }
  38. public int getTimeWidth() {
  39. return time == Integer.MAX_VALUE ? 0 : mc.font.width(getTimeText());
  40. }
  41. public int getTextWidth() {
  42. return mc.font.width(getText());
  43. }
  44. @Override
  45. public int compareTo(LineData o) {
  46. return -Integer.compare(time, o.time);
  47. }
  48. }
  49. private final TreeMap<Integer, LineData> strings;
  50. private final Minecraft mc;
  51. public StatusDisplayGui(Minecraft mc) {
  52. this.mc = mc;
  53. this.strings = new TreeMap<>();
  54. }
  55. public void tick() {
  56. strings.entrySet().removeIf(e -> {
  57. if(e.getValue().time < Integer.MAX_VALUE) {
  58. e.getValue().time--;
  59. if(e.getValue().time < 0) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. });
  65. }
  66. public void add(int i, int time, String s) {
  67. strings.put(i, new LineData(time, s));
  68. }
  69. public void remove(int i) {
  70. strings.remove(i);
  71. }
  72. public void clear() {
  73. strings.clear();
  74. }
  75. public void paint(MatrixStack matrixStack) {
  76. Collection<EffectInstance> collection = this.mc.player.getActiveEffects();
  77. if(strings.isEmpty() && collection.isEmpty()) {
  78. return;
  79. }
  80. // getting data
  81. LinkedList<LineData> list = new LinkedList<>(strings.values());
  82. collection.forEach(effect -> {
  83. StringBuilder sb = new StringBuilder();
  84. if(effect.getEffect().isBeneficial()) {
  85. sb.append(TextFormatting.GREEN);
  86. } else {
  87. sb.append(TextFormatting.RED);
  88. }
  89. sb.append(LanguageMap.getInstance().getOrDefault(effect.getDescriptionId()));
  90. list.add(new LineData(effect.getDuration(), sb.toString()));
  91. });
  92. // sorting by time
  93. list.sort(null);
  94. int lineHeight = mc.font.lineHeight;
  95. int height = 4 + lineHeight * (strings.size() + collection.size());
  96. int maxTextWidth = 0;
  97. int maxTimeWidth = 0;
  98. for(LineData data : list) {
  99. maxTextWidth = Math.max(data.getTextWidth(), maxTextWidth);
  100. maxTimeWidth = Math.max(data.getTimeWidth(), maxTimeWidth);
  101. }
  102. int screenWidth = mc.getWindow().getGuiScaledWidth();
  103. int x = screenWidth - maxTextWidth - maxTimeWidth - 4;
  104. int y = 2;
  105. fill(matrixStack, x, 0, screenWidth, height, 1342177280);
  106. x += 2;
  107. String time;
  108. for(LineData data : list) {
  109. time = data.getTimeText();
  110. mc.font.draw(matrixStack, time + data.getText(), x + maxTimeWidth - mc.font.width(time),
  111. y + 1, 0xFFFFFF);
  112. y += lineHeight;
  113. }
  114. }
  115. }