From a90023a26af9348b8568a155809d51003649915e Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 22 Apr 2022 23:46:44 +0200 Subject: [PATCH] close option and commands on purchase --- pom.xml | 2 +- src/main/java/tsp/headdb/api/HeadAPI.java | 2 ++ .../headdb/implementation/HeadDatabase.java | 5 ++++- .../tsp/headdb/inventory/InventoryUtils.java | 19 +++++++++++++++++++ src/main/resources/config.yml | 9 +++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 37894bf..e58aa22 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 4.3.1 + 4.4.0 jar diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index c6112eb..a728f00 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -23,6 +23,8 @@ import java.util.UUID; * * @author TheSilentPro */ +// TODO: Optional instead of null. +// TODO: Remove stream, use loop. public final class HeadAPI { private HeadAPI() {} diff --git a/src/main/java/tsp/headdb/implementation/HeadDatabase.java b/src/main/java/tsp/headdb/implementation/HeadDatabase.java index c06dc8b..8a72208 100644 --- a/src/main/java/tsp/headdb/implementation/HeadDatabase.java +++ b/src/main/java/tsp/headdb/implementation/HeadDatabase.java @@ -33,6 +33,7 @@ import javax.annotation.Nonnull; * * @author TheSilentPro */ +// TODO: Optionals instead of null. public class HeadDatabase { private final JavaPlugin plugin; @@ -120,7 +121,8 @@ public class HeadDatabase { @Nonnull public List getHeads(Category category) { - return heads.get(category) != null ? Collections.unmodifiableList(heads.get(category)) : new ArrayList<>(); + List result = heads.get(category); + return result != null ? Collections.unmodifiableList(result) : Collections.emptyList(); } /** @@ -186,6 +188,7 @@ public class HeadDatabase { protected List gather(String url, Category category) throws IOException, ParseException { long start = System.currentTimeMillis(); List headList = new ArrayList<>(); + // TODO: gson JSONParser parser = new JSONParser(); JSONArray array = (JSONArray) parser.parse(fetch(url)); for (Object o : array) { diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 5dfa261..2473a7c 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -31,6 +31,7 @@ import java.util.stream.Collectors; * Class for handling the "dirty" work * such as inventories and economy. */ +// TODO: Rewrite public class InventoryUtils { private InventoryUtils() {} @@ -356,6 +357,10 @@ public class InventoryUtils { } public static void purchaseHead(Player player, Head head, int amount, String category, String description) { + if (config.getBoolean("closeOnPurchase", false)) { + player.closeInventory(); + } + Utils.sendMessage(player, String.format(localization.getMessage("processPayment"), amount, head.getName())); processPayment(player, amount, category, description, result -> { if (Boolean.TRUE.equals(result)) { @@ -365,11 +370,25 @@ public class InventoryUtils { ItemStack item = head.getMenuItem(); item.setAmount(amount); player.getInventory().addItem(item); + + // Commands can only be ran on main thread + Bukkit.getScheduler().runTask(HeadDB.getInstance(), InventoryUtils::runPurchaseCommands); } } }); } + private static void runPurchaseCommands() { + // Backwards compatability + if (!config.contains("commands.purchase")) { + return; + } + + for (String cmd : config.getStringList("commands.purchase")) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd); + } + } + private static String replace(String message, int size, String category, String search, Player player) { return message .replace("%size%", String.valueOf(size)) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fcbc274..2332840 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,6 +8,9 @@ localHeads: true # Permission: headdb.category. requireCategoryPermission: false +# If enabled, the menu will close after purchasing a head (even if the purchase fails) +closeOnPurchase: false + # Hidden heads from the menu hidden: enabled: false @@ -136,6 +139,12 @@ ui: volume: 1 pitch: 1 +# Command Configuration +commands: + # Commands to run ONLY if the purchase is successful. + purchase: + - "" + # If the original fetching fails and this is enabled, # the plugin will attempt to fetch the heads from an archive. fallback: true