Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip colors from search term #4044

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public void openSearch(PlayerProfile profile, String input, boolean addToHistory
}

ChestMenu menu = new ChestMenu(Slimefun.getLocalization().getMessage(p, "guide.search.inventory").replace("%item%", ChatUtils.crop(ChatColor.WHITE, input)));
String searchTerm = input.toLowerCase(Locale.ROOT);
String searchTerm = ChatColor.stripColor(input.toLowerCase(Locale.ROOT));

if (addToHistory) {
profile.getGuideHistory().add(searchTerm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,14 +92,36 @@ void testOpenItemStack() throws InterruptedException {
Mockito.verify(guide).displayItem(profile, item, 1, false);
}

@Test
@DisplayName("Test if the Slimefun Search works with normal and colored terms")
void testOpenSearch_withColoredSearchTerm() throws InterruptedException {
String normalTerm = "iron";
String coloredTerm = ChatColor.DARK_PURPLE + "iron";

SlimefunItem testItem = TestUtilities.mockSlimefunItem(plugin, "IRON_ITEM", new CustomItemStack(Material.IRON_INGOT, "iron item"));
testItem.register(plugin);

Player player = server.addPlayer();
PlayerProfile profile = TestUtilities.awaitProfile(player);
SlimefunGuideImplementation guide = new SurvivalSlimefunGuide(false, false);

guide.openSearch(profile, normalTerm, false);
// Assert we can open with a non-coloured search term
Assertions.assertTrue(player.getOpenInventory().getTopInventory().contains(testItem.getItem()), "Failed on normal query");
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved

guide.openSearch(profile, coloredTerm, false);
// Assert we can open with a coloured search term
Assertions.assertTrue(player.getOpenInventory().getTopInventory().contains(testItem.getItem()), "Failed on colored query");
JustAHuman-xD marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Test if the Slimefun Search can be opened from the History")
void testOpenSearch() throws InterruptedException {
String query = "electric";
void testOpenSearchHistory() throws InterruptedException {
WalshyDev marked this conversation as resolved.
Show resolved Hide resolved
String term = "electric";

SlimefunGuideImplementation guide = Mockito.mock(SlimefunGuideImplementation.class);
PlayerProfile profile = prepare(guide, history -> history.add(query));
Mockito.verify(guide).openSearch(profile, query, false);
PlayerProfile profile = prepare(guide, history -> history.add(term));
Mockito.verify(guide).openSearch(profile, term, false);
}

@Test
Expand Down