Skip to content

Commit

Permalink
fixed stock cache
Browse files Browse the repository at this point in the history
  • Loading branch information
benfiratkaya committed Feb 28, 2024
1 parent f8d83f0 commit ff9f9d4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<Category> 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;
}
}

0 comments on commit ff9f9d4

Please sign in to comment.