Argument.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.search;
  24. import me.shedaniel.rei.api.EntryStack;
  25. import me.shedaniel.rei.gui.config.SearchMode;
  26. import net.fabricmc.api.EnvType;
  27. import net.fabricmc.api.Environment;
  28. import net.minecraft.network.chat.Style;
  29. import org.apache.commons.lang3.mutable.Mutable;
  30. import org.jetbrains.annotations.ApiStatus;
  31. import org.jetbrains.annotations.NotNull;
  32. import org.jetbrains.annotations.Nullable;
  33. @ApiStatus.Internal
  34. @Environment(EnvType.CLIENT)
  35. public abstract class Argument<T, R> {
  36. public Argument() {
  37. }
  38. public abstract String getName();
  39. @Nullable
  40. public String getPrefix() {
  41. return null;
  42. }
  43. @NotNull
  44. public Style getHighlightedStyle() {
  45. return Style.EMPTY;
  46. }
  47. public SearchMode getSearchMode() {
  48. return SearchMode.PREFIX;
  49. }
  50. public MatchStatus matchesArgumentPrefix(String text) {
  51. MatchStatus status = checkMatchPrefix(text, getPrefix());
  52. if (status.isMatched()) {
  53. return status;
  54. } else if (getSearchMode() == SearchMode.ALWAYS) {
  55. status = checkMatchPrefix(text, "");
  56. if (status.isMatched()) {
  57. status.notUsingGrammar();
  58. }
  59. return status;
  60. }
  61. return MatchStatus.unmatched();
  62. }
  63. private MatchStatus checkMatchPrefix(String text, String prefix) {
  64. if (prefix == null) return MatchStatus.unmatched();
  65. if (text.startsWith("-" + prefix)) return MatchStatus.invertMatched(text.substring(1 + prefix.length())).grammar(0, prefix.length() + 1);
  66. if (text.startsWith(prefix + "-")) return MatchStatus.invertMatched(text.substring(1 + prefix.length())).grammar(0, prefix.length() + 1);
  67. if (text.startsWith(prefix)) return MatchStatus.matched(text.substring(prefix.length())).grammar(0, prefix.length());
  68. return MatchStatus.unmatched();
  69. }
  70. public abstract boolean matches(Mutable<R> data, EntryStack stack, String searchText, T filterData);
  71. public abstract T prepareSearchFilter(String searchText);
  72. }