PlayerDisplayGui.java 4.9 KB

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