diff --git a/pom.xml b/pom.xml index ff45991..5d866a0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 4.2.4 + 4.3.0 jar diff --git a/src/main/java/tsp/headdb/implementation/Head.java b/src/main/java/tsp/headdb/implementation/Head.java index a915576..6a0315e 100644 --- a/src/main/java/tsp/headdb/implementation/Head.java +++ b/src/main/java/tsp/headdb/implementation/Head.java @@ -13,6 +13,7 @@ import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; import java.util.UUID; +import java.util.regex.Pattern; /** * Represents a Head that a player can obtain via the database @@ -21,6 +22,7 @@ import java.util.UUID; */ public class Head { + public static final Pattern SPLIT = Pattern.compile(","); private String name; private UUID uuid; private String value; @@ -121,7 +123,7 @@ public class Head { } public Head tags(String tags) { - this.tags = Arrays.asList(tags.split(",")); + this.tags = Arrays.asList(SPLIT.split(tags)); return this; } diff --git a/src/main/java/tsp/headdb/implementation/HeadDatabase.java b/src/main/java/tsp/headdb/implementation/HeadDatabase.java index 834a1a3..c06dc8b 100644 --- a/src/main/java/tsp/headdb/implementation/HeadDatabase.java +++ b/src/main/java/tsp/headdb/implementation/HeadDatabase.java @@ -183,7 +183,7 @@ public class HeadDatabase { * @throws IOException error * @throws ParseException error */ - private List gather(String url, Category category) throws IOException, ParseException { + protected List gather(String url, Category category) throws IOException, ParseException { long start = System.currentTimeMillis(); List headList = new ArrayList<>(); JSONParser parser = new JSONParser(); @@ -199,14 +199,13 @@ public class HeadDatabase { uuid = UUID.randomUUID(); } - Head head = new Head(nextId) + Head head = new Head(nextId++) .name(obj.get("name").toString()) .uniqueId(uuid) .value(obj.get("value").toString()) .tags(obj.get("tags") != null ? obj.get("tags").toString() : "None") .category(category); - nextId++; headList.add(head); } @@ -223,7 +222,7 @@ public class HeadDatabase { * @return JSON-string response * @throws IOException error */ - private String fetch(String url) throws IOException { + protected String fetch(String url) throws IOException { String line; StringBuilder response = new StringBuilder(); diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 8830e8d..5dfa261 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -2,6 +2,7 @@ package tsp.headdb.inventory; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; @@ -24,6 +25,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; +import java.util.stream.Collectors; /** * Class for handling the "dirty" work @@ -32,6 +34,7 @@ import java.util.function.Consumer; public class InventoryUtils { private InventoryUtils() {} + private static final FileConfiguration config = HeadDB.getInstance().getConfig(); private static final Localization localization = HeadDB.getInstance().getLocalization(); private static final Map uiLocation = new HashMap<>(); @@ -62,7 +65,7 @@ public class InventoryUtils { } public static void openFavoritesMenu(Player player) { - List heads = HeadAPI.getFavoriteHeads(player.getUniqueId()); + List heads = config.getBoolean("hidden.hideFavorites") ? filter(HeadAPI.getFavoriteHeads(player.getUniqueId())) : HeadAPI.getFavoriteHeads(player.getUniqueId()); PagedPane pane = new PagedPane(4, 6, replace(localization.getMessage("menu.favorites"), heads.size(), "Favorites", "None", player)); @@ -87,7 +90,7 @@ public class InventoryUtils { } public static PagedPane openSearchDatabase(Player player, String search) { - List heads = HeadAPI.getHeadsByName(search); + List heads = filter(HeadAPI.getHeadsByName(search)); PagedPane pane = new PagedPane(4, 6, replace(localization.getMessage("menu.search"), heads.size(), "None", search, player)); @@ -118,7 +121,7 @@ public class InventoryUtils { } public static void openTagSearchDatabase(Player player, String tag) { - List heads = HeadAPI.getHeadsByTag(tag); + List heads = filter(HeadAPI.getHeadsByTag(tag)); PagedPane pane = new PagedPane(4, 6, replace(localization.getMessage("menu.tagSearch"), heads.size(), "None", tag, player)); @@ -148,7 +151,7 @@ public class InventoryUtils { } public static void openCategoryDatabase(Player player, Category category) { - List heads = HeadAPI.getHeads(category); + List heads = filter(HeadAPI.getHeads(category)); PagedPane pane = new PagedPane(4, 6, replace(localization.getMessage("menu.category"), heads.size(), category.getName(), "None", player)); @@ -375,4 +378,27 @@ public class InventoryUtils { .replace("%player%", player.getName()); } + private static List filter(List source) { + if (!config.getBoolean("hidden.enabled")) { + return source; + } + + List result = new ArrayList<>(); + List tags = config.getStringList("hidden.tags"); + List names = config.getStringList("hidden.names"); + + for (Head head : source) { + if (!names.contains(head.getName()) && !contains(head.getTags(), tags)) { + result.add(head); + } + } + + + return result; + } + + private static boolean contains(List list1, List list2) { + return list1.stream().anyMatch(list2.stream().collect(Collectors.toSet())::contains); + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a97e5cd..fcbc274 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,6 +8,18 @@ localHeads: true # Permission: headdb.category. requireCategoryPermission: false +# Hidden heads from the menu +hidden: + enabled: false + # If enabled it will also hide any heads matching these tags in the favorites menu + hideFavorites: true + # If the head name matches any of the listen words it will be hidden (case-sensitive) + names: + - "" + # If the head has one of the listed tags it will be hidden + tags: + - "" + # Economy options economy: enable: false @@ -16,7 +28,7 @@ economy: # Providers like treasury support multiple currencies # Set the ID of the one used for head purchasing below. # Leave empty to use the primary currency or if the provider does not support multiple currencies - currency: '' + currency: "" cost: alphabet: 100 animals: 100