PlayerDisplayGui.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package me.km.networking;
  2. import com.mojang.blaze3d.systems.RenderSystem;
  3. import java.util.ArrayList;
  4. import java.util.TreeMap;
  5. import me.km.KajetansMod;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.gui.AbstractGui;
  8. import net.minecraft.util.ResourceLocation;
  9. import net.minecraftforge.api.distmarker.Dist;
  10. import net.minecraftforge.api.distmarker.OnlyIn;
  11. @OnlyIn(Dist.CLIENT)
  12. public class PlayerDisplayGui extends AbstractGui
  13. {
  14. private static final ResourceLocation CUSTOM_ICONS = new ResourceLocation(KajetansMod.MODID, "textures/gui/icons.png");
  15. public final static PlayerDisplayGui INSTANCE = new PlayerDisplayGui(Minecraft.getInstance());
  16. private class IconStorage
  17. {
  18. private final Icon icon;
  19. private final int count;
  20. public IconStorage(int id, int count)
  21. {
  22. this.icon = Icon.getIcon(id);
  23. this.count = count;
  24. }
  25. public int getWidth()
  26. {
  27. return count * icon.getWidth() - count + 1;
  28. }
  29. public int getHeight()
  30. {
  31. return icon.getHeight();
  32. }
  33. }
  34. private class LineData
  35. {
  36. private ArrayList<String> list;
  37. private ArrayList<IconStorage> icons;
  38. public LineData(String s)
  39. {
  40. this.list = new ArrayList<>();
  41. this.icons = new ArrayList<>();
  42. int pos;
  43. int old = 0;
  44. while(true)
  45. {
  46. // icon format #X-Y- where X and Y are numbers
  47. pos = s.indexOf("#", old);
  48. if(pos == -1)
  49. {
  50. list.add(s.substring(old));
  51. break;
  52. }
  53. list.add(s.substring(old, pos));
  54. try
  55. {
  56. int first = s.indexOf("-", pos);
  57. int second = s.indexOf("-", first + 1);
  58. icons.add(new IconStorage(Integer.parseInt(s.substring(pos + 1, first)), Integer.parseInt(s.substring(first + 1, second))));
  59. old = second + 1;
  60. }
  61. catch(Exception ex)
  62. {
  63. list.add("ERROR");
  64. break;
  65. }
  66. }
  67. }
  68. public int getWidth()
  69. {
  70. return list.stream().mapToInt(s -> mc.fontRenderer.getStringWidth(s)).sum() + icons.stream().mapToInt(i -> i.getWidth()).sum();
  71. }
  72. public int getHeight()
  73. {
  74. return Math.max(mc.fontRenderer.FONT_HEIGHT, icons.stream().mapToInt(i -> i.getHeight()).max().orElse(0) + 1);
  75. }
  76. }
  77. private final TreeMap<Integer, LineData> strings;
  78. private final Minecraft mc;
  79. public PlayerDisplayGui(Minecraft mc)
  80. {
  81. this.mc = mc;
  82. this.strings = new TreeMap<>();
  83. }
  84. public void add(int i, String s)
  85. {
  86. strings.put(i, new LineData(s));
  87. }
  88. public void remove(int i)
  89. {
  90. strings.remove(i);
  91. }
  92. public void clear()
  93. {
  94. strings.clear();
  95. }
  96. public void paint()
  97. {
  98. if(strings.isEmpty())
  99. {
  100. return;
  101. }
  102. int x;
  103. int y = 2;
  104. int width = 0;
  105. int height = 4;
  106. for(LineData data : strings.values())
  107. {
  108. height += data.getHeight();
  109. width = Math.max(data.getWidth(), width);
  110. }
  111. fill(0, 0, width + 4, height, 1342177280);
  112. String s;
  113. for(LineData data : strings.values())
  114. {
  115. x = 2;
  116. for(int i = 0; i < data.list.size(); i++)
  117. {
  118. s = data.list.get(i);
  119. mc.fontRenderer.drawString(s, x, y + 1, 0xFFFFFF);
  120. if(!s.isEmpty())
  121. {
  122. x += mc.fontRenderer.getStringWidth(s);
  123. }
  124. else
  125. {
  126. x--;
  127. }
  128. if(i < data.icons.size())
  129. {
  130. x += paintIcons(data.icons.get(i), x, y);
  131. }
  132. }
  133. y += data.getHeight();
  134. }
  135. }
  136. private void bind(ResourceLocation res)
  137. {
  138. mc.getTextureManager().bindTexture(res);
  139. }
  140. private int paintIcons(IconStorage is, int x, int y)
  141. {
  142. RenderSystem.enableBlend();
  143. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  144. bind(CUSTOM_ICONS);
  145. Icon icon = is.icon;
  146. int width = icon.getWidth();
  147. int height = icon.getHeight();
  148. int offX = icon.getOffsetX();
  149. int offY = icon.getOffsetY();
  150. for(int i = 0; i < is.count; i++)
  151. {
  152. blit(x + width * i - i, y, offX, offY, width, height);
  153. }
  154. RenderSystem.disableBlend();
  155. return is.getWidth();
  156. }
  157. }