StatusDisplayGui.java 4.4 KB

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