FluidEntryStack.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.impl;
  24. import com.google.common.collect.Lists;
  25. import com.mojang.blaze3d.vertex.BufferBuilder;
  26. import com.mojang.blaze3d.vertex.DefaultVertexFormat;
  27. import com.mojang.blaze3d.vertex.PoseStack;
  28. import com.mojang.blaze3d.vertex.Tesselator;
  29. import com.mojang.math.Matrix4f;
  30. import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
  31. import me.shedaniel.math.Point;
  32. import me.shedaniel.math.Rectangle;
  33. import me.shedaniel.rei.api.ClientHelper;
  34. import me.shedaniel.rei.api.ConfigObject;
  35. import me.shedaniel.rei.api.EntryStack;
  36. import me.shedaniel.rei.api.fractions.Fraction;
  37. import me.shedaniel.rei.api.widgets.Tooltip;
  38. import me.shedaniel.rei.utils.CollectionUtils;
  39. import me.shedaniel.rei.utils.ImmutableLiteralText;
  40. import net.minecraft.ChatFormatting;
  41. import net.minecraft.client.Minecraft;
  42. import net.minecraft.client.renderer.texture.TextureAtlas;
  43. import net.minecraft.client.renderer.texture.TextureAtlasSprite;
  44. import net.minecraft.client.resources.language.I18n;
  45. import net.minecraft.core.Registry;
  46. import net.minecraft.network.chat.Component;
  47. import net.minecraft.network.chat.TextComponent;
  48. import net.minecraft.resources.ResourceLocation;
  49. import net.minecraft.world.level.material.Fluid;
  50. import net.minecraft.world.level.material.Fluids;
  51. import org.apache.commons.lang3.StringUtils;
  52. import org.jetbrains.annotations.ApiStatus;
  53. import org.jetbrains.annotations.NotNull;
  54. import org.jetbrains.annotations.Nullable;
  55. import java.util.List;
  56. import java.util.Optional;
  57. import java.util.concurrent.ThreadLocalRandom;
  58. import java.util.stream.Collectors;
  59. import java.util.stream.Stream;
  60. @ApiStatus.Internal
  61. public class FluidEntryStack extends AbstractEntryStack {
  62. private static final Fraction IGNORE_AMOUNT = Fraction.of(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong(Long.MAX_VALUE)).simplify();
  63. private Fluid fluid;
  64. private Fraction amount;
  65. private int hashIgnoreAmount;
  66. private int hash;
  67. public FluidEntryStack(Fluid fluid) {
  68. this(fluid, IGNORE_AMOUNT);
  69. }
  70. public FluidEntryStack(Fluid fluid, Fraction amount) {
  71. this.fluid = fluid;
  72. this.amount = amount;
  73. rehash();
  74. }
  75. private void rehash() {
  76. hashIgnoreAmount = 31 + getType().ordinal();
  77. hashIgnoreAmount = 31 * hashIgnoreAmount + fluid.hashCode();
  78. hash = 31 * hashIgnoreAmount + amount.hashCode();
  79. }
  80. @Override
  81. public Optional<ResourceLocation> getIdentifier() {
  82. return Optional.ofNullable(Registry.FLUID.getKey(getFluid()));
  83. }
  84. @Override
  85. public Type getType() {
  86. return Type.FLUID;
  87. }
  88. @Override
  89. public Fraction getAccurateAmount() {
  90. return amount;
  91. }
  92. @Override
  93. public void setAmount(Fraction amount) {
  94. this.amount = amount.equals(IGNORE_AMOUNT) ? IGNORE_AMOUNT : max(amount, Fraction.empty());
  95. if (isEmpty()) {
  96. fluid = Fluids.EMPTY;
  97. }
  98. rehash();
  99. }
  100. private <T extends Comparable<T>> T max(T o1, T o2) {
  101. return o1.compareTo(o2) > 0 ? o1 : o2;
  102. }
  103. @Override
  104. public boolean isEmpty() {
  105. return (!amount.equals(IGNORE_AMOUNT) && !amount.isGreaterThan(Fraction.empty())) || fluid == Fluids.EMPTY;
  106. }
  107. @Override
  108. public EntryStack copy() {
  109. EntryStack stack = EntryStack.create(fluid, amount);
  110. for (Short2ObjectMap.Entry<Object> entry : getSettings().short2ObjectEntrySet()) {
  111. stack.setting(EntryStack.Settings.getById(entry.getShortKey()), entry.getValue());
  112. }
  113. return stack;
  114. }
  115. @Override
  116. public Object getObject() {
  117. return fluid;
  118. }
  119. @Override
  120. public boolean equalsIgnoreTagsAndAmount(EntryStack stack) {
  121. if (stack.getType() == Type.ITEM)
  122. return EntryStack.copyItemToFluids(stack).anyMatch(this::equalsIgnoreTagsAndAmount);
  123. if (stack.getType() != Type.FLUID)
  124. return false;
  125. return fluid == stack.getFluid();
  126. }
  127. @Override
  128. public boolean equalsIgnoreTags(EntryStack stack) {
  129. if (stack.getType() == Type.ITEM)
  130. return EntryStack.copyItemToFluids(stack).anyMatch(this::equalsIgnoreTags);
  131. if (stack.getType() != Type.FLUID)
  132. return false;
  133. return fluid == stack.getFluid() && (amount.equals(IGNORE_AMOUNT) || stack.getAccurateAmount().equals(IGNORE_AMOUNT) || amount.equals(stack.getAccurateAmount()));
  134. }
  135. @Override
  136. public boolean equalsIgnoreAmount(EntryStack stack) {
  137. if (stack.getType() == Type.ITEM)
  138. return EntryStack.copyItemToFluids(stack).anyMatch(this::equalsIgnoreAmount);
  139. if (stack.getType() != Type.FLUID)
  140. return false;
  141. return fluid == stack.getFluid();
  142. }
  143. @Override
  144. public boolean equalsAll(EntryStack stack) {
  145. if (stack.getType() != Type.FLUID)
  146. return false;
  147. return fluid == stack.getFluid() && (amount.equals(IGNORE_AMOUNT) || stack.getAccurateAmount().equals(IGNORE_AMOUNT) || amount.equals(stack.getAccurateAmount()));
  148. }
  149. @Override
  150. public int hashOfAll() {
  151. return hash;
  152. }
  153. @Override
  154. public int hashIgnoreAmountAndTags() {
  155. return hashIgnoreAmount;
  156. }
  157. @Override
  158. public int hashIgnoreTags() {
  159. return hash;
  160. }
  161. @Override
  162. public int hashIgnoreAmount() {
  163. return hashIgnoreAmount;
  164. }
  165. @Nullable
  166. @Override
  167. public Tooltip getTooltip(Point point) {
  168. if (!get(Settings.TOOLTIP_ENABLED).get() || isEmpty())
  169. return null;
  170. List<Component> toolTip = Lists.newArrayList(asFormattedText());
  171. if (!amount.isLessThan(Fraction.empty()) && !amount.equals(IGNORE_AMOUNT)) {
  172. String amountTooltip = get(Settings.Fluid.AMOUNT_TOOLTIP).apply(this);
  173. if (amountTooltip != null)
  174. toolTip.addAll(Stream.of(amountTooltip.split("\n")).map(TextComponent::new).collect(Collectors.toList()));
  175. }
  176. if (Minecraft.getInstance().options.advancedItemTooltips) {
  177. toolTip.add((new TextComponent(Registry.FLUID.getKey(this.getFluid()).toString())).withStyle(ChatFormatting.DARK_GRAY));
  178. }
  179. toolTip.addAll(get(Settings.TOOLTIP_APPEND_EXTRA).apply(this));
  180. if (get(Settings.TOOLTIP_APPEND_MOD).get() && ConfigObject.getInstance().shouldAppendModNames()) {
  181. ClientHelper.getInstance().appendModIdToTooltips(toolTip, Registry.FLUID.getKey(getFluid()).getNamespace());
  182. }
  183. return Tooltip.create(toolTip);
  184. }
  185. @SuppressWarnings("deprecation")
  186. @Override
  187. public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) {
  188. if (get(Settings.RENDER).get()) {
  189. SimpleFluidRenderer.FluidRenderingData renderingData = SimpleFluidRenderer.fromFluid(fluid);
  190. if (renderingData != null) {
  191. TextureAtlasSprite sprite = renderingData.getSprite();
  192. int color = renderingData.getColor();
  193. int a = 255;
  194. int r = (color >> 16 & 255);
  195. int g = (color >> 8 & 255);
  196. int b = (color & 255);
  197. Minecraft.getInstance().getTextureManager().bind(TextureAtlas.LOCATION_BLOCKS);
  198. Tesselator tess = Tesselator.getInstance();
  199. BufferBuilder bb = tess.getBuilder();
  200. Matrix4f matrix = matrices.last().pose();
  201. bb.begin(7, DefaultVertexFormat.POSITION_TEX_COLOR);
  202. bb.vertex(matrix, bounds.getMaxX(), bounds.y, getZ()).uv(sprite.getU1(), sprite.getV0()).color(r, g, b, a).endVertex();
  203. bb.vertex(matrix, bounds.x, bounds.y, getZ()).uv(sprite.getU0(), sprite.getV0()).color(r, g, b, a).endVertex();
  204. bb.vertex(matrix, bounds.x, bounds.getMaxY(), getZ()).uv(sprite.getU0(), sprite.getV1()).color(r, g, b, a).endVertex();
  205. bb.vertex(matrix, bounds.getMaxX(), bounds.getMaxY(), getZ()).uv(sprite.getU1(), sprite.getV1()).color(r, g, b, a).endVertex();
  206. tess.end();
  207. }
  208. }
  209. }
  210. @NotNull
  211. @Override
  212. public Component asFormattedText() {
  213. ResourceLocation id = Registry.FLUID.getKey(fluid);
  214. if (I18n.exists("block." + id.toString().replaceFirst(":", ".")))
  215. return new ImmutableLiteralText(I18n.get("block." + id.toString().replaceFirst(":", ".")));
  216. return new ImmutableLiteralText(CollectionUtils.mapAndJoinToString(id.getPath().split("_"), StringUtils::capitalize, " "));
  217. }
  218. }