ItemStackDisplayGui.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package me.km.networking;
  2. import com.mojang.blaze3d.matrix.MatrixStack;
  3. import com.mojang.blaze3d.systems.RenderSystem;
  4. import me.km.utils.ClientReflectionUtils;
  5. import me.km.utils.Mapper;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.gui.AbstractGui;
  8. import net.minecraft.client.renderer.ItemRenderer;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.util.ResourceLocation;
  12. import net.minecraftforge.api.distmarker.Dist;
  13. import net.minecraftforge.api.distmarker.OnlyIn;
  14. @OnlyIn(Dist.CLIENT)
  15. public class ItemStackDisplayGui extends AbstractGui {
  16. private static final ResourceLocation WIDGETS_TEX_PATH =
  17. new ResourceLocation("textures/gui/widgets.png");
  18. public final static ItemStackDisplayGui INSTANCE =
  19. new ItemStackDisplayGui(Minecraft.getInstance());
  20. private final Minecraft mc;
  21. private final ItemStack[] icons = new ItemStack[9];
  22. private boolean inactive = true;
  23. public ItemStackDisplayGui(Minecraft mc) {
  24. this.mc = mc;
  25. for(int i = 0; i < icons.length; i++) {
  26. icons[i] = ItemStack.EMPTY;
  27. }
  28. }
  29. public void setActive(boolean active) {
  30. this.inactive = !active;
  31. }
  32. public void setIcon(int index, String name) {
  33. if(index >= 0 && index < 9) {
  34. Item item = Mapper.getItem(name);
  35. if(item != null) {
  36. icons[index] = new ItemStack(item);
  37. }
  38. }
  39. }
  40. public final void clear() {
  41. for(int i = 0; i < icons.length; i++) {
  42. icons[i] = ItemStack.EMPTY;
  43. }
  44. }
  45. @SuppressWarnings("deprecation")
  46. public void paint(MatrixStack matrixStack, KeyManager km) {
  47. if(inactive) {
  48. return;
  49. }
  50. int screenWidth = (mc.getWindow().getGuiScaledWidth() >> 1) - 91;
  51. int y = 0;
  52. if(ClientReflectionUtils.isRenderingBossBar()) {
  53. y += 20;
  54. }
  55. RenderSystem.enableBlend();
  56. RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
  57. mc.getTextureManager().bind(WIDGETS_TEX_PATH);
  58. int oldBlitOffset = this.getBlitOffset();
  59. setBlitOffset(-90);
  60. blit(matrixStack, screenWidth, y, 0, 0, 182, 22);
  61. setBlitOffset(oldBlitOffset);
  62. y += 3;
  63. ItemRenderer ir = mc.getItemRenderer();
  64. int i = 0;
  65. for(int x = 3; x < 164; x += 20) {
  66. if(!icons[i].isEmpty()) {
  67. ir.renderAndDecorateItem(mc.player, icons[i], screenWidth + x, y);
  68. ir.renderGuiItemDecorations(this.mc.font, icons[i], screenWidth + x, y,
  69. km.getKeyDescription(i));
  70. }
  71. i++;
  72. }
  73. RenderSystem.disableBlend();
  74. }
  75. }