PlayerDisplayGui.java 4.6 KB

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