SearchArgument.java 5.0 KB

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