Skip to content

Commit

Permalink
💎 Search now checks for category permission to show results. Closes #45
Browse files Browse the repository at this point in the history
Took 13 minutes
  • Loading branch information
kiranhart committed Oct 3, 2024
1 parent fcfcdb8 commit 57154fe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ca/tweetzy/skulls/api/SkullsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public interface SkullsAPI {
* @param phrase The phrase to search for.
* @return A list of Skulls that match the search phrase.
*/
List<Skull> getSkullsBySearch(@NonNull final String phrase);
List<Skull> getSkullsBySearch(@NonNull Player player, @NonNull final String phrase);

/**
* This returns a list of skulls, given a list of IDs.
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/ca/tweetzy/skulls/guis/SkullsViewGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;

/**
* Date Created: April 21 2022
* Time Created: 11:59 a.m.
Expand All @@ -54,7 +56,7 @@ public SkullsViewGUI(final Gui parent, final SkullUser skullPlayer, final String
Bukkit.getPlayer(skullPlayer.getUUID()),
viewMode == ViewMode.SEARCH ? TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_SEARCH, "search_phrase", category) : viewMode == ViewMode.FAVOURITE ? TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_FAVOURITES) : TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_CATEGORY, "category_name", category),
6,
viewMode == ViewMode.SEARCH ? Skulls.getSkullManager().getSkullsBySearch(category) : viewMode == ViewMode.FAVOURITE ? Skulls.getSkullManager().getSkulls(skullPlayer.getFavourites()) : Skulls.getSkullManager().getSkulls(category)
viewMode == ViewMode.SEARCH ? new ArrayList<>() : viewMode == ViewMode.FAVOURITE ? Skulls.getSkullManager().getSkulls(skullPlayer.getFavourites()) : Skulls.getSkullManager().getSkulls(category)
);

this.category = category;
Expand All @@ -64,6 +66,11 @@ public SkullsViewGUI(final Gui parent, final SkullUser skullPlayer, final String
draw();
}

@Override
protected void prePopulate() {
this.items = Skulls.getSkullManager().getSkullsBySearch(this.player, category);
}

@Override
protected ItemStack makeDisplayItem(Skull skull) {
final QuickItem item = QuickItem.of(skull.getItemStack()).name(TranslationManager.string(Translations.GUI_SKULLS_LIST_ITEMS_SKULL_NAME, "skull_name", skull.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public List<Skull> getSkulls(@NonNull String category) {
}

@Override
public List<Skull> getSkullsBySearch(@NonNull String phrase) {
return Skulls.getSkullManager().getSkullsBySearch(phrase);
public List<Skull> getSkullsBySearch(@NonNull Player player, @NonNull String phrase) {
return Skulls.getSkullManager().getSkullsBySearch(player, phrase);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ca/tweetzy/skulls/manager/SkullManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.BufferedReader;
Expand Down Expand Up @@ -113,7 +114,7 @@ public Skull getRandomSkull() {
return enabledSkulls.get(random.nextInt(enabledSkulls.size()));
}

public List<Skull> getSkullsBySearch(String phrase) {
public List<Skull> getSkullsBySearch(Player player, String phrase) {
synchronized (this.skulls) {
int id = -1;
if (phrase.startsWith("id:")) {
Expand All @@ -125,7 +126,7 @@ public List<Skull> getSkullsBySearch(String phrase) {
if (id != -1)
return Collections.singletonList(getSkull(id));

return this.skulls.stream().filter(skull -> BaseCategory.getById(skull.getCategory()).isEnabled() && (Common.match(phrase, skull.getName()) || Common.match(phrase, skull.getCategory()) || skull.getTags().stream().anyMatch(tag -> Common.match(phrase, tag)))).collect(Collectors.toList());
return this.skulls.stream().filter(skull -> player.hasPermission("skulls.category." + BaseCategory.getById(skull.getCategory()).getId().toLowerCase().replace(" ", "").replace("&", "")) && BaseCategory.getById(skull.getCategory()).isEnabled() && (Common.match(phrase, skull.getName()) || Common.match(phrase, skull.getCategory()) || skull.getTags().stream().anyMatch(tag -> Common.match(phrase, tag)))).collect(Collectors.toList());
}
}

Expand Down

0 comments on commit 57154fe

Please sign in to comment.