From ff9f9d4d7effee589e01b4bca5862a079b00c862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C4=B1rat=20Kaya?= <48600092+benfiratkaya@users.noreply.github.com> Date: Wed, 28 Feb 2024 19:34:05 +0300 Subject: [PATCH] fixed stock cache --- .../webstore/commands/WebStoreCommand.java | 15 +++-- .../webstore/gui/ConfirmPurchaseGui.java | 2 +- .../webstore/helpers/WebStoreHelper.java | 55 ++++++++++++++++++- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/commands/WebStoreCommand.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/commands/WebStoreCommand.java index de5d162e1..0dfb80905 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/commands/WebStoreCommand.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/commands/WebStoreCommand.java @@ -7,13 +7,12 @@ import dev.triumphteam.cmd.core.annotation.SubCommand; import lombok.RequiredArgsConstructor; import net.leaderos.plugin.Bukkit; +import net.leaderos.plugin.api.LeaderOSAPI; import net.leaderos.plugin.api.managers.ModuleManager; import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.modules.webstore.gui.MainWebStoreGui; -import net.leaderos.plugin.api.LeaderOSAPI; import net.leaderos.plugin.modules.webstore.helpers.WebStoreHelper; -import net.leaderos.shared.Shared; -import net.leaderos.shared.helpers.UrlUtil; +import net.leaderos.plugin.modules.webstore.model.Product; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -50,6 +49,14 @@ public void buyCommand(CommandSender sender, String productId) { if (!ModuleManager.getModule("WebStore").isEnabled()) return; Player player = (Player) sender; - WebStoreHelper.buyItem(player, productId); + + Product product = WebStoreHelper.findProductById(productId); + + if (product == null) { + player.sendMessage(ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreError())); + return; + } + + WebStoreHelper.buyItem(player, product); } } diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/ConfirmPurchaseGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/ConfirmPurchaseGui.java index 84d442e5b..9cbacf31c 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/ConfirmPurchaseGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/ConfirmPurchaseGui.java @@ -86,7 +86,7 @@ public static StaticGuiElement confirmIcon(Player player, Product product, Inven 1, click -> { gui.close(); - WebStoreHelper.buyItem(player, product.getProductId()); + WebStoreHelper.buyItem(player, product); return true; } ); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/helpers/WebStoreHelper.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/helpers/WebStoreHelper.java index 2cca60d9d..231137949 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/helpers/WebStoreHelper.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/helpers/WebStoreHelper.java @@ -6,19 +6,24 @@ import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.helpers.MDChat.MDChatAPI; import net.leaderos.plugin.modules.cache.model.User; +import net.leaderos.plugin.modules.webstore.model.Product; import net.leaderos.shared.error.Error; import net.leaderos.shared.helpers.RequestUtil; import net.leaderos.shared.model.Response; import net.leaderos.shared.model.request.impl.store.BuyRequest; import net.leaderos.shared.modules.auth.AuthHelper; import net.leaderos.shared.modules.credit.enums.UpdateType; +import net.leaderos.plugin.modules.webstore.model.Category; import org.bukkit.entity.Player; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.List; public class WebStoreHelper { - public static void buyItem(Player player, String productId) { + public static void buyItem(Player player, Product product) { if (RequestUtil.canRequest(player.getUniqueId())) { RequestUtil.addRequest(player.getUniqueId()); @@ -35,10 +40,22 @@ public static void buyItem(Player player, String productId) { org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { // Buy progress try { - Response buyRequest = new BuyRequest(user.getId(), productId).getResponse(); + Response buyRequest = new BuyRequest(user.getId(), product.getProductId()).getResponse(); if (buyRequest.getResponseCode() == HttpURLConnection.HTTP_OK) { + JSONObject responseData = buyRequest.getResponseMessage().getJSONObject("data"); + + // Update Stock + JSONArray products = responseData.getJSONArray("products"); + for (int i = 0; i < products.length(); i++) { + JSONObject productData = products.getJSONObject(i); + if (productData.getString("id").equals(product.getProductId())) { + product.setStock(productData.getInt("stock")); + break; + } + } + // Calls UpdateCache event for update player's cache - double credits = buyRequest.getResponseMessage().getJSONObject("data").getDouble("credits"); + double credits = responseData.getDouble("credits"); org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), credits, UpdateType.SET))); player.sendTitle(title, subtitleSuccess); } @@ -80,4 +97,36 @@ else if (buyRequest.getError() == Error.PRODUCT_NOT_FOUND) ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); } } + + public static Product findProductById(String productId) { + List categories = Category.getCategories(); + + for (Category category : categories) { + Product foundProduct = findProductByIdRecursive(productId, category); + if (foundProduct != null) { + return foundProduct; + } + } + return null; + } + + private static Product findProductByIdRecursive(String productId, Category category) { + // Check if the product is in this category + for (Product product : category.getProductList()) { + if (product.getProductId().equalsIgnoreCase(productId)) { + return product; + } + } + + // Check if the product is in any sub-category + for (Category subCategory : category.getSubCategories()) { + Product foundProduct = findProductByIdRecursive(productId, subCategory); + if (foundProduct != null) { + return foundProduct; + } + } + + // Product not found + return null; + } }