PlayerDisplayGui.java 4.6 KB

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