StatusDisplayGui.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) + LanguageMap.getInstance().func_230503_a_(text.substring(index + 1));
  24. } else {
  25. this.text = text;
  26. }
  27. }
  28. public String getText() {
  29. return text;
  30. }
  31. public String getTimeText() {
  32. if(time == Integer.MAX_VALUE) {
  33. return "";
  34. }
  35. return (time / 20) + "s ";
  36. }
  37. public int getTimeWidth() {
  38. return time == Integer.MAX_VALUE ? 0 : mc.fontRenderer.getStringWidth(getTimeText());
  39. }
  40. public int getTextWidth() {
  41. return mc.fontRenderer.getStringWidth(getText());
  42. }
  43. @Override
  44. public int compareTo(LineData o) {
  45. return -Integer.compare(time, o.time);
  46. }
  47. }
  48. private final TreeMap<Integer, LineData> strings;
  49. private final Minecraft mc;
  50. public StatusDisplayGui(Minecraft mc) {
  51. this.mc = mc;
  52. this.strings = new TreeMap<>();
  53. }
  54. public void tick() {
  55. strings.entrySet().removeIf(e
  56. -> {
  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.getActivePotionEffects();
  77. if(strings.isEmpty() && collection.isEmpty()) {
  78. return;
  79. }
  80. // getting data
  81. LinkedList<LineData> list = new LinkedList<>(strings.values());
  82. EffectInstance e;
  83. collection.forEach(effect
  84. -> {
  85. StringBuilder sb = new StringBuilder();
  86. if(effect.getPotion().isBeneficial()) {
  87. sb.append(TextFormatting.GREEN);
  88. } else {
  89. sb.append(TextFormatting.RED);
  90. }
  91. sb.append(LanguageMap.getInstance().func_230503_a_(effect.getEffectName()));
  92. list.add(new LineData(effect.getDuration(), sb.toString()));
  93. });
  94. // sorting by time
  95. list.sort(null);
  96. int lineHeight = mc.fontRenderer.FONT_HEIGHT;
  97. int height = 4 + lineHeight * (strings.size() + collection.size());
  98. int maxTextWidth = 0;
  99. int maxTimeWidth = 0;
  100. for(LineData data : list) {
  101. maxTextWidth = Math.max(data.getTextWidth(), maxTextWidth);
  102. maxTimeWidth = Math.max(data.getTimeWidth(), maxTimeWidth);
  103. }
  104. int screenWidth = mc.getMainWindow().getScaledWidth();
  105. int x = screenWidth - maxTextWidth - maxTimeWidth - 4;
  106. int y = 2;
  107. fill(matrixStack, x, 0, screenWidth, height, 1342177280);
  108. x += 2;
  109. String time;
  110. for(LineData data : list) {
  111. time = data.getTimeText();
  112. mc.fontRenderer.drawString(matrixStack, time + data.getText(), x + maxTimeWidth - mc.fontRenderer.getStringWidth(time), y + 1, 0xFFFFFF);
  113. y += lineHeight;
  114. }
  115. }
  116. }