SearchArgument.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2018, 2019, 2020 shedaniel
  3. * Licensed under the MIT License (the "License").
  4. */
  5. package me.shedaniel.rei.impl;
  6. import com.google.common.collect.Lists;
  7. import me.shedaniel.rei.api.EntryStack;
  8. import me.shedaniel.rei.gui.widget.QueuedTooltip;
  9. import me.shedaniel.rei.utils.CollectionUtils;
  10. import net.minecraft.client.MinecraftClient;
  11. import net.minecraft.client.item.TooltipContext;
  12. import net.minecraft.client.resource.language.I18n;
  13. import net.minecraft.fluid.Fluid;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.text.Text;
  17. import net.minecraft.util.Identifier;
  18. import net.minecraft.util.registry.Registry;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.jetbrains.annotations.ApiStatus;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import java.util.function.Function;
  25. @ApiStatus.Internal
  26. public class SearchArgument {
  27. public static final SearchArgument ALWAYS = new SearchArgument(ArgumentType.ALWAYS, "", true);
  28. @Deprecated private static List<Item> searchBlacklisted = Lists.newArrayList();
  29. private ArgumentType argumentType;
  30. private String text;
  31. public final Function<String, Boolean> INCLUDE = s -> s.contains(text);
  32. public final Function<String, Boolean> NOT_INCLUDE = s -> !s.contains(text);
  33. private boolean include;
  34. public SearchArgument(ArgumentType argumentType, String text, boolean include) {
  35. this(argumentType, text, include, true);
  36. }
  37. public SearchArgument(ArgumentType argumentType, String text, boolean include, boolean autoLowerCase) {
  38. this.argumentType = argumentType;
  39. this.text = autoLowerCase ? text.toLowerCase(Locale.ROOT) : text;
  40. this.include = include;
  41. }
  42. @ApiStatus.Internal
  43. public static String tryGetEntryStackName(EntryStack stack) {
  44. if (stack.getType() == EntryStack.Type.ITEM)
  45. return tryGetItemStackName(stack.getItemStack());
  46. else if (stack.getType() == EntryStack.Type.FLUID)
  47. return tryGetFluidName(stack.getFluid());
  48. return "";
  49. }
  50. @ApiStatus.Internal
  51. public static String tryGetEntryStackTooltip(EntryStack stack) {
  52. QueuedTooltip tooltip = stack.getTooltip(0, 0);
  53. if (tooltip != null)
  54. return CollectionUtils.joinToString(tooltip.getText(), "\n");
  55. return "";
  56. }
  57. @ApiStatus.Internal
  58. public static String tryGetFluidName(Fluid fluid) {
  59. Identifier id = Registry.FLUID.getId(fluid);
  60. if (I18n.hasTranslation("block." + id.toString().replaceFirst(":", ".")))
  61. return I18n.translate("block." + id.toString().replaceFirst(":", "."));
  62. return CollectionUtils.mapAndJoinToString(id.getPath().split("_"), StringUtils::capitalize, " ");
  63. }
  64. @ApiStatus.Internal
  65. public static List<String> tryGetItemStackToolTip(ItemStack itemStack, boolean careAboutAdvanced) {
  66. if (!searchBlacklisted.contains(itemStack.getItem()))
  67. try {
  68. return CollectionUtils.map(itemStack.getTooltip(MinecraftClient.getInstance().player, MinecraftClient.getInstance().options.advancedItemTooltips && careAboutAdvanced ? TooltipContext.Default.ADVANCED : TooltipContext.Default.NORMAL), Text::asFormattedString);
  69. } catch (Throwable e) {
  70. e.printStackTrace();
  71. searchBlacklisted.add(itemStack.getItem());
  72. }
  73. return Collections.singletonList(tryGetItemStackName(itemStack));
  74. }
  75. @ApiStatus.Internal
  76. public static String tryGetItemStackName(ItemStack stack) {
  77. if (!searchBlacklisted.contains(stack.getItem()))
  78. try {
  79. return stack.getName().asFormattedString();
  80. } catch (Throwable e) {
  81. e.printStackTrace();
  82. searchBlacklisted.add(stack.getItem());
  83. }
  84. try {
  85. return I18n.translate("item." + Registry.ITEM.getId(stack.getItem()).toString().replace(":", "."));
  86. } catch (Throwable e) {
  87. e.printStackTrace();
  88. }
  89. return "ERROR";
  90. }
  91. public Function<String, Boolean> getFunction(boolean include) {
  92. return include ? INCLUDE : NOT_INCLUDE;
  93. }
  94. public ArgumentType getArgumentType() {
  95. return argumentType;
  96. }
  97. public String getText() {
  98. return text;
  99. }
  100. public boolean isInclude() {
  101. return include;
  102. }
  103. @Override
  104. public String toString() {
  105. return String.format("Argument[%s]: name = %s, include = %b", argumentType.name(), text, include);
  106. }
  107. public enum ArgumentType {
  108. TEXT,
  109. MOD,
  110. TOOLTIP,
  111. TAG,
  112. ALWAYS
  113. }
  114. @ApiStatus.Internal
  115. public static class SearchArguments {
  116. public static final SearchArguments ALWAYS = new SearchArguments(new SearchArgument[]{SearchArgument.ALWAYS});
  117. private SearchArgument[] arguments;
  118. public SearchArguments(SearchArgument[] arguments) {
  119. this.arguments = arguments;
  120. }
  121. public SearchArgument[] getArguments() {
  122. return arguments;
  123. }
  124. }
  125. }