From 74ff69dad2fdd101705e71cc1cf3f5943508b8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C4=B1rat=20Kaya?= <48600092+benfiratkaya@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:19:30 +0300 Subject: [PATCH] 1.1.1: performance improvements, critical bug fixes --- .../plugin/modules/bazaar/gui/BazaarGui.java | 10 +- .../modules/bazaar/model/PlayerBazaar.java | 4 +- .../voucher/commands/VoucherCommand.java | 48 +++++--- .../webstore/commands/WebStoreCommand.java | 21 +++- .../modules/webstore/gui/WebStoreGui.java | 116 ++++++++++-------- .../impl/bazaar/RemoveBazaarItemRequest.java | 2 +- 6 files changed, 122 insertions(+), 79 deletions(-) diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java index 9fab265485..5b97f3b079 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java @@ -79,14 +79,20 @@ public static void showGui(Player player) { ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCannotCreateFull()); return false; } + if (!RequestUtil.canRequest(player.getUniqueId())) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); + return false; + } String title = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getWithdrawTitle()); String subtitleError = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getWithdrawErrorSubtitle()); String subtitleSuccess = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getWithdrawSuccessSubtitle()); String subtitleProgress = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getWithdrawProgressSubtitle()); player.sendTitle(title, subtitleProgress); + RequestUtil.addRequest(player.getUniqueId()); + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { - Error error = playerBazaarItem.withdrawItem(player); + Error error = playerBazaarItem.withdrawItem(player, playerBazaarItem.getId()); if (error == null) player.sendTitle(title, subtitleSuccess); else if (error == Error.DELETE_ERROR) @@ -108,6 +114,8 @@ else if (error == Error.DELETE_ERROR) org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> { gui.show(player); }); + + RequestUtil.invalidate(player.getUniqueId()); }); } } diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/model/PlayerBazaar.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/model/PlayerBazaar.java index 9562861138..bb4d524eea 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/model/PlayerBazaar.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/model/PlayerBazaar.java @@ -78,8 +78,8 @@ public ItemStack getItem() { * @return status of withdraw */ @SneakyThrows - public Error withdrawItem(Player player) { - Response deleteRequest = new RemoveBazaarItemRequest(getUserId(), getUserId()).getResponse(); + public Error withdrawItem(Player player, int itemId) { + Response deleteRequest = new RemoveBazaarItemRequest(getUserId(), itemId).getResponse(); if (deleteRequest.getResponseCode() == HttpURLConnection.HTTP_OK && deleteRequest.getResponseMessage().getBoolean("status")) { ItemStack item = ItemUtil.fromBase64(getBase64()); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/commands/VoucherCommand.java b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/commands/VoucherCommand.java index 3da3cf0111..c645fb3484 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/commands/VoucherCommand.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/commands/VoucherCommand.java @@ -9,12 +9,14 @@ 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.handlers.UpdateCacheEvent; import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.helpers.ItemUtil; import net.leaderos.plugin.modules.voucher.VoucherModule; import net.leaderos.shared.helpers.MoneyUtil; import net.leaderos.shared.helpers.Placeholder; +import net.leaderos.shared.helpers.RequestUtil; import net.leaderos.shared.model.Response; import net.leaderos.shared.modules.credit.CreditHelper; import net.leaderos.shared.modules.credit.enums.UpdateType; @@ -89,25 +91,35 @@ public void createCommand(Player player, Double amount) { return; } + if (!RequestUtil.canRequest(player.getUniqueId())) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); + return; + } + try { - Response removeCreditRequest = CreditHelper.removeCreditRequest(player.getName(), amount); - - if (Objects.requireNonNull(removeCreditRequest).getResponseCode() == HttpURLConnection.HTTP_OK - && removeCreditRequest.getResponseMessage().getBoolean("status")) { - // Calls UpdateCache event for update player's cache - org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), amount, UpdateType.REMOVE)); - ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders( - Bukkit.getInstance().getLangFile().getMessages().getVouchers().getSuccessfullyCreated(), - new Placeholder("{amount}", MoneyUtil.format(amount)) - )); - giveVoucher(player, amount); - } - // TODO if code not afforded or not ok - else - ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders( - Bukkit.getInstance().getLangFile().getMessages().getVouchers().getCannotCreateNotEnough(), - new Placeholder("{amount}", MoneyUtil.format(amount)) - )); + Double finalAmount = amount; + RequestUtil.addRequest(player.getUniqueId()); + + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { + boolean removeCredit = LeaderOSAPI.getCreditManager().remove(player.getName(), finalAmount); + if (removeCredit) { + // Calls UpdateCache event for update player's cache + org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), finalAmount, UpdateType.REMOVE)); + + ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders( + Bukkit.getInstance().getLangFile().getMessages().getVouchers().getSuccessfullyCreated(), + new Placeholder("{amount}", MoneyUtil.format(finalAmount)) + )); + giveVoucher(player, finalAmount); + } else { + ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders( + Bukkit.getInstance().getLangFile().getMessages().getVouchers().getCannotCreateNotEnough(), + new Placeholder("{amount}", MoneyUtil.format(finalAmount)) + )); + } + + RequestUtil.invalidate(player.getUniqueId()); + }); } catch (Exception ignored) { ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getPlayerNotAvailable()); 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 705368d98b..35b91ae78b 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 @@ -13,6 +13,7 @@ import net.leaderos.plugin.modules.webstore.gui.WebStoreGui; import net.leaderos.plugin.modules.webstore.helpers.WebStoreHelper; import net.leaderos.plugin.modules.webstore.model.Category; +import net.leaderos.shared.helpers.RequestUtil; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -56,14 +57,24 @@ public void categoryCommand(CommandSender sender, String categoryId) { Player player = (Player) sender; - Category category = WebStoreHelper.findCategoryById(player.getName(), categoryId); - - if (category == null) { - player.sendMessage(ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getWebStoreCategoryNotFound())); + if (!RequestUtil.canRequest(player.getUniqueId())) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); return; } - WebStoreGui.showGui(player, category); + RequestUtil.addRequest(player.getUniqueId()); + + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { + Category category = WebStoreHelper.findCategoryById(player.getName(), categoryId); + RequestUtil.invalidate(player.getUniqueId()); + + if (category == null) { + player.sendMessage(ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getWebStoreCategoryNotFound())); + return; + } + + WebStoreGui.showGui(player, category); + }); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java index 246fbdeb35..abc42cf651 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java @@ -11,6 +11,7 @@ import net.leaderos.plugin.Bukkit; import net.leaderos.plugin.modules.webstore.model.Product; import net.leaderos.plugin.helpers.GuiHelper; +import net.leaderos.shared.helpers.RequestUtil; import org.bukkit.entity.Player; import java.util.*; @@ -33,60 +34,71 @@ public WebStoreGui() {} * @param categoryObj category to open */ public static void showGui(Player player, Category categoryObj) { - // Gui template as array - String[] layout = Bukkit.getInstance().getModulesFile().getWebStore().getGui().getLayout().toArray(new String[0]); - // Inventory object - String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); - InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); - // Filler item for empty slots - gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); - - // List creator - List categoryList; - List productList; - if (categoryObj == null) { - categoryList = WebStoreHelper.getCategories(player.getName()); - productList = new ArrayList<>(); - } else { - categoryList = categoryObj.getSubCategories(); - productList = categoryObj.getProductList(); + if (!RequestUtil.canRequest(player.getUniqueId())) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); + return; } - GuiElementGroup elementGroup = new GuiElementGroup('e'); - // Category group creator - GuiElementGroup categoryGroup = new GuiElementGroup('c'); - if (!categoryList.isEmpty()) { - // Element group - addCategoriesToGroup(categoryList, elementGroup, player); - - // Category group - addCategoriesToGroup(categoryList, categoryGroup, player); - categoryGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); - gui.addElement(categoryGroup); - } - // Product group creator - GuiElementGroup productGroup = new GuiElementGroup('p'); - if (!productList.isEmpty()) { - // Element group - addProductsToGroup(productList, elementGroup, player, gui); - - // Product Group - addProductsToGroup(productList, productGroup, player, gui); - productGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); - gui.addElement(productGroup); - } - elementGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); - gui.addElement(elementGroup); - - // Credits icon - User user = User.getUser(player.getName()); - double credit = user == null ? 0.00 : user.getCredit(); - gui.addElement(GuiHelper.addCreditIcon(credit)); - - // Next and previous page icons - gui.addElement(GuiHelper.createNextPage(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getNextPage().getItem())); - gui.addElement(GuiHelper.createPreviousPage(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getPreviousPage().getItem())); - gui.show(player); + RequestUtil.addRequest(player.getUniqueId()); + + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { + // Gui template as array + String[] layout = Bukkit.getInstance().getModulesFile().getWebStore().getGui().getLayout().toArray(new String[0]); + // Inventory object + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); + InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); + // Filler item for empty slots + gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); + + // List creator + List categoryList; + List productList; + if (categoryObj == null) { + categoryList = WebStoreHelper.getCategories(player.getName()); + productList = new ArrayList<>(); + } else { + categoryList = categoryObj.getSubCategories(); + productList = categoryObj.getProductList(); + } + + GuiElementGroup elementGroup = new GuiElementGroup('e'); + // Category group creator + GuiElementGroup categoryGroup = new GuiElementGroup('c'); + if (!categoryList.isEmpty()) { + // Element group + addCategoriesToGroup(categoryList, elementGroup, player); + + // Category group + addCategoriesToGroup(categoryList, categoryGroup, player); + categoryGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); + gui.addElement(categoryGroup); + } + // Product group creator + GuiElementGroup productGroup = new GuiElementGroup('p'); + if (!productList.isEmpty()) { + // Element group + addProductsToGroup(productList, elementGroup, player, gui); + + // Product Group + addProductsToGroup(productList, productGroup, player, gui); + productGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); + gui.addElement(productGroup); + } + elementGroup.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); + gui.addElement(elementGroup); + + // Credits icon + User user = User.getUser(player.getName()); + double credit = user == null ? 0.00 : user.getCredit(); + gui.addElement(GuiHelper.addCreditIcon(credit)); + + // Next and previous page icons + gui.addElement(GuiHelper.createNextPage(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getNextPage().getItem())); + gui.addElement(GuiHelper.createPreviousPage(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getPreviousPage().getItem())); + gui.show(player); + + RequestUtil.invalidate(player.getUniqueId()); + }); } /** diff --git a/shared/src/main/java/net/leaderos/shared/model/request/impl/bazaar/RemoveBazaarItemRequest.java b/shared/src/main/java/net/leaderos/shared/model/request/impl/bazaar/RemoveBazaarItemRequest.java index d678e979bb..ec540d63a5 100644 --- a/shared/src/main/java/net/leaderos/shared/model/request/impl/bazaar/RemoveBazaarItemRequest.java +++ b/shared/src/main/java/net/leaderos/shared/model/request/impl/bazaar/RemoveBazaarItemRequest.java @@ -7,7 +7,7 @@ public class RemoveBazaarItemRequest extends DeleteRequest { - public RemoveBazaarItemRequest(String userId, String id) throws IOException { + public RemoveBazaarItemRequest(String userId, int id) throws IOException { super("bazaar/storages/" + userId + "/items/" + id, new HashMap<>()); }