Renderer.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.api;
  6. import me.shedaniel.rei.gui.renderers.EmptyRenderer;
  7. import me.shedaniel.rei.gui.renderers.FluidRenderer;
  8. import me.shedaniel.rei.gui.renderers.ItemStackRenderer;
  9. import me.shedaniel.rei.gui.renderers.SimpleRecipeRenderer;
  10. import me.shedaniel.rei.gui.widget.QueuedTooltip;
  11. import net.minecraft.client.gui.DrawableHelper;
  12. import net.minecraft.fluid.Fluid;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.util.math.MathHelper;
  15. import javax.annotation.Nullable;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import java.util.function.Function;
  19. import java.util.function.Supplier;
  20. public abstract class Renderer extends DrawableHelper {
  21. /**
  22. * Gets an item stack renderer by an item stack supplier
  23. *
  24. * @param supplier the supplier for getting the item stack
  25. * @return the item stack renderer
  26. */
  27. public static ItemStackRenderer fromItemStackSupplier(Supplier<ItemStack> supplier) {
  28. return fromItemStacks(() -> Collections.singletonList(supplier.get()), true, null);
  29. }
  30. /**
  31. * Gets an item stack renderer by an item stack supplier
  32. *
  33. * @param supplier the supplier for getting the item stack
  34. * @return the item stack renderer
  35. */
  36. public static ItemStackRenderer fromItemStackSupplierNoCounts(Supplier<ItemStack> supplier) {
  37. return fromItemStacks(() -> Collections.singletonList(supplier.get()), false, null);
  38. }
  39. /**
  40. * Gets an item stack renderer by an item stack
  41. *
  42. * @param stack the item stack to be displayed
  43. * @return the item stack renderer
  44. */
  45. public static ItemStackRenderer fromItemStack(ItemStack stack) {
  46. return fromItemStacks(() -> Collections.singletonList(stack), true, null);
  47. }
  48. public static FluidRenderer fromFluid(Fluid fluid) {
  49. return fromFluid(() -> fluid, null);
  50. }
  51. public static FluidRenderer fromFluid(Supplier<Fluid> fluidSupplier, @Nullable Function<Fluid, List<String>> extraTooltipSupplier) {
  52. return new FluidRenderer() {
  53. @Override
  54. public Fluid getFluid() {
  55. return fluidSupplier.get();
  56. }
  57. @Override
  58. protected List<String> getExtraToolTips(Fluid fluid) {
  59. if (extraTooltipSupplier == null)
  60. return super.getExtraToolTips(fluid);
  61. List<String> apply = extraTooltipSupplier.apply(fluid);
  62. if (apply == null)
  63. return super.getExtraToolTips(fluid);
  64. return apply;
  65. }
  66. };
  67. }
  68. /**
  69. * Gets an item stack renderer by an item stack
  70. *
  71. * @param stack the item stack to be displayed
  72. * @return the item stack renderer
  73. */
  74. public static ItemStackRenderer fromItemStackNoCounts(ItemStack stack) {
  75. return fromItemStacks(() -> Collections.singletonList(stack), false, null);
  76. }
  77. /**
  78. * Gets an empty renderer
  79. *
  80. * @return an empty renderer
  81. */
  82. public static EmptyRenderer empty() {
  83. return EmptyRenderer.INSTANCE;
  84. }
  85. /**
  86. * Gets a simple recipe renderer from inputs and outputs
  87. *
  88. * @param input the list of input items
  89. * @param output the list of output items
  90. * @return the recipe renderer
  91. */
  92. public static SimpleRecipeRenderer fromRecipe(Supplier<List<List<ItemStack>>> input, Supplier<List<ItemStack>> output) {
  93. return new SimpleRecipeRenderer(input, output);
  94. }
  95. public static ItemStackRenderer fromItemStacks(List<ItemStack> stacks) {
  96. return fromItemStacks(() -> stacks, true, null);
  97. }
  98. public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, boolean renderCounts, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier) {
  99. return new ItemStackRenderer() {
  100. @Override
  101. public ItemStack getItemStack() {
  102. if (stacksSupplier.get().isEmpty())
  103. return ItemStack.EMPTY;
  104. return stacksSupplier.get().get(MathHelper.floor((System.currentTimeMillis() / 500 % (double) stacksSupplier.get().size()) / 1f));
  105. }
  106. @Override
  107. protected boolean renderCounts() {
  108. return renderCounts;
  109. }
  110. @Override
  111. protected List<String> getExtraToolTips(ItemStack stack) {
  112. if (extraTooltipSupplier == null)
  113. return super.getExtraToolTips(stack);
  114. List<String> apply = extraTooltipSupplier.apply(stack);
  115. if (apply == null)
  116. return super.getExtraToolTips(stack);
  117. return apply;
  118. }
  119. };
  120. }
  121. public static ItemStackRenderer fromItemStacksNoCounts(List<ItemStack> stacks) {
  122. return fromItemStacks(() -> stacks, false, null);
  123. }
  124. /**
  125. * Gets the current blit offset
  126. *
  127. * @return the blit offset
  128. */
  129. public int getBlitOffset() {
  130. return this.blitOffset;
  131. }
  132. /**
  133. * Sets the current blit offset
  134. *
  135. * @param offset the new blit offset
  136. */
  137. public void setBlitOffset(int offset) {
  138. this.blitOffset = offset;
  139. }
  140. /**
  141. * Renders of the renderable
  142. *
  143. * @param x the x coordinate of the renderable
  144. * @param y the y coordinate of the renderable
  145. * @param mouseX the x coordinate of the mouse
  146. * @param mouseY the y coordinate of the mouse
  147. * @param delta the delta
  148. */
  149. public abstract void render(int x, int y, double mouseX, double mouseY, float delta);
  150. @Nullable
  151. public QueuedTooltip getQueuedTooltip(float delta) {
  152. return null;
  153. }
  154. }