ItemStackDisplayGui.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package me.km.networking;
  2. import com.mojang.blaze3d.systems.RenderSystem;
  3. import me.km.KajetansMod;
  4. import me.km.utils.ClientReflectionUtils;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.client.gui.AbstractGui;
  7. import net.minecraft.client.gui.FontRenderer;
  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 ItemStackDisplayGui extends AbstractGui {
  13. private static final ResourceLocation CUSTOM_ICONS = new ResourceLocation(KajetansMod.MODID, "textures/gui/itemstacks.png");
  14. public final static ItemStackDisplayGui INSTANCE = new ItemStackDisplayGui(Minecraft.getInstance());
  15. private static class Stack {
  16. private int x = -1;
  17. private int y = 0;
  18. private int width = 0;
  19. private int height = 0;
  20. private String text = "";
  21. public void setIcon(int index, int c) {
  22. if(index < 0) {
  23. clear();
  24. } else if(index >= 0 && index < 104) {
  25. x = (index % 52) * 16 + 192;
  26. y = (index / 52) * 16;
  27. width = 16;
  28. height = 16;
  29. text = (c == 0) ? "" : String.valueOf(c);
  30. } else {
  31. index -= 104;
  32. x = (index % 16) * 64;
  33. y = (index / 16) * 64 + 32;
  34. width = 64;
  35. height = 64;
  36. text = null;
  37. }
  38. }
  39. public void clear() {
  40. x = -1;
  41. }
  42. }
  43. private final Minecraft mc;
  44. private final Stack[] icons = new Stack[9];
  45. private boolean inactive = true;
  46. public ItemStackDisplayGui(Minecraft mc) {
  47. this.mc = mc;
  48. for(int i = 0; i < icons.length; i++) {
  49. icons[i] = new Stack();
  50. }
  51. }
  52. public void setActive(boolean active) {
  53. this.inactive = !active;
  54. }
  55. public void setIcon(int index, int i, int count) {
  56. if(index >= 0 && index < 9) {
  57. icons[index].setIcon(i, count);
  58. }
  59. }
  60. public final void clear() {
  61. for(Stack stack : icons) {
  62. stack.clear();
  63. }
  64. }
  65. public void paint(KeyManager km) {
  66. if(inactive) {
  67. return;
  68. }
  69. RenderSystem.enableBlend();
  70. RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  71. mc.getTextureManager().bindTexture(CUSTOM_ICONS);
  72. int screenWidth = (mc.getMainWindow().getScaledWidth() >> 1) - 91;
  73. int y = 3;
  74. if(ClientReflectionUtils.isRenderingBossBar()) {
  75. y += 20;
  76. }
  77. blit(screenWidth, y - 3, 0, 0, 182, 22, 1024, 1024);
  78. int i = 0;
  79. for(int x = 3; x < 164; x += 20) {
  80. Stack s = icons[i];
  81. if(s.x != -1) {
  82. blit(screenWidth + x, y, 16, 16, s.x, s.y, s.width, s.height, 1024, 1024);
  83. }
  84. i++;
  85. }
  86. RenderSystem.disableBlend();
  87. FontRenderer fr = mc.fontRenderer;
  88. i = 0;
  89. for(int x = 20; x < 181; x += 20) {
  90. if(icons[i].x != -1) {
  91. if(icons[i].text == null) {
  92. String s = km.getKeyDescription(i);
  93. fr.drawStringWithShadow(s, (float) (screenWidth + x - fr.getStringWidth(s)), y + 9, 16777215);
  94. } else if(!icons[i].text.isEmpty()) {
  95. fr.drawStringWithShadow(icons[i].text, (float) (screenWidth + x - fr.getStringWidth(icons[i].text)), y + 9, 16777215);
  96. }
  97. }
  98. i++;
  99. }
  100. }
  101. }