Renderer.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import java.util.stream.Collectors;
  21. public abstract class Renderer extends DrawableHelper {
  22. /**
  23. * Gets an item stack renderer by an item stack supplier
  24. *
  25. * @param supplier the supplier for getting the item stack
  26. * @return the item stack renderer
  27. */
  28. public static ItemStackRenderer fromItemStackSupplier(Supplier<ItemStack> supplier) {
  29. return fromItemStacks(() -> Collections.singletonList(supplier.get()), true, null);
  30. }
  31. /**
  32. * Gets an item stack renderer by an item stack supplier
  33. *
  34. * @param supplier the supplier for getting the item stack
  35. * @return the item stack renderer
  36. */
  37. public static ItemStackRenderer fromItemStackSupplierNoCounts(Supplier<ItemStack> supplier) {
  38. return fromItemStacks(() -> Collections.singletonList(supplier.get()), false, null);
  39. }
  40. /**
  41. * Gets an item stack renderer by an item stack
  42. *
  43. * @param stack the item stack to be displayed
  44. * @return the item stack renderer
  45. */
  46. public static ItemStackRenderer fromItemStack(ItemStack stack) {
  47. return fromItemStacks(() -> Collections.singletonList(stack), true, null);
  48. }
  49. public static FluidRenderer fromFluid(Fluid fluid) {
  50. return fromFluid(() -> fluid, null);
  51. }
  52. public static FluidRenderer fromFluid(Supplier<Fluid> fluidSupplier, @Nullable Function<Fluid, List<String>> extraTooltipSupplier) {
  53. return new FluidRenderer() {
  54. @Override
  55. public Fluid getFluid() {
  56. return fluidSupplier.get();
  57. }
  58. @Override
  59. protected List<String> getExtraToolTips(Fluid fluid) {
  60. if (extraTooltipSupplier == null)
  61. return super.getExtraToolTips(fluid);
  62. List<String> apply = extraTooltipSupplier.apply(fluid);
  63. if (apply == null)
  64. return super.getExtraToolTips(fluid);
  65. return apply;
  66. }
  67. };
  68. }
  69. /**
  70. * Gets an item stack renderer by an item stack
  71. *
  72. * @param stack the item stack to be displayed
  73. * @return the item stack renderer
  74. */
  75. public static ItemStackRenderer fromItemStackNoCounts(ItemStack stack) {
  76. return fromItemStacks(() -> Collections.singletonList(stack), false, null);
  77. }
  78. /**
  79. * Gets an empty renderer
  80. *
  81. * @return an empty renderer
  82. */
  83. public static EmptyRenderer empty() {
  84. return EmptyRenderer.INSTANCE;
  85. }
  86. /**
  87. * Gets a simple recipe renderer from inputs and outputs
  88. *
  89. * @param input the list of input items
  90. * @param output the list of output items
  91. * @return the recipe renderer
  92. */
  93. public static SimpleRecipeRenderer fromRecipe(Supplier<List<List<ItemStack>>> input, Supplier<List<ItemStack>> output) {
  94. return new SimpleRecipeRenderer(input, output);
  95. }
  96. public static ItemStackRenderer fromItemStacks(List<ItemStack> stacks) {
  97. return fromItemStacks(() -> stacks, true, null);
  98. }
  99. public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, boolean renderCounts, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier) {
  100. return fromItemStacks(stacksSupplier, stack -> renderCounts ? null : "", extraTooltipSupplier);
  101. }
  102. public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, @Nullable Function<ItemStack, String> countsFunction, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier) {
  103. return fromItemStacks(stacksSupplier, countsFunction, extraTooltipSupplier, true);
  104. }
  105. public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, @Nullable Function<ItemStack, String> countsFunction, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier, boolean renderOverlay) {
  106. List<ItemStack> stacks = stacksSupplier.get().stream().map(ItemStack::copy).collect(Collectors.toList());
  107. return new ItemStackRenderer() {
  108. @Override
  109. public ItemStack getItemStack() {
  110. if (stacks.isEmpty())
  111. return ItemStack.EMPTY;
  112. return stacks.get(MathHelper.floor((System.currentTimeMillis() / 500 % (double) stacks.size()) / 1f));
  113. }
  114. @Override
  115. protected String getCounts() {
  116. return countsFunction == null ? null : countsFunction.apply(getItemStack());
  117. }
  118. @Override
  119. protected boolean renderOverlay() {
  120. return renderOverlay;
  121. }
  122. @Override
  123. protected List<String> getExtraToolTips(ItemStack stack) {
  124. if (extraTooltipSupplier == null)
  125. return super.getExtraToolTips(stack);
  126. List<String> apply = extraTooltipSupplier.apply(stack);
  127. if (apply == null)
  128. return super.getExtraToolTips(stack);
  129. return apply;
  130. }
  131. };
  132. }
  133. public static ItemStackRenderer fromItemStacksNoCounts(List<ItemStack> stacks) {
  134. return fromItemStacks(() -> stacks, false, null);
  135. }
  136. /**
  137. * Gets the current blit offset
  138. *
  139. * @return the blit offset
  140. */
  141. public int getBlitOffset() {
  142. return this.blitOffset;
  143. }
  144. /**
  145. * Sets the current blit offset
  146. *
  147. * @param offset the new blit offset
  148. */
  149. public void setBlitOffset(int offset) {
  150. this.blitOffset = offset;
  151. }
  152. /**
  153. * Renders of the renderable
  154. *
  155. * @param x the x coordinate of the renderable
  156. * @param y the y coordinate of the renderable
  157. * @param mouseX the x coordinate of the mouse
  158. * @param mouseY the y coordinate of the mouse
  159. * @param delta the delta
  160. */
  161. public abstract void render(int x, int y, double mouseX, double mouseY, float delta);
  162. @Nullable
  163. public QueuedTooltip getQueuedTooltip(float delta) {
  164. return null;
  165. }
  166. }