PlayerDisplayGui.java 4.4 KB

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