Skip to content

Commit

Permalink
1.1.1: performance improvements, critical bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benfiratkaya committed Jun 26, 2024
1 parent 94efdcf commit 74ff69d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -108,6 +114,8 @@ else if (error == Error.DELETE_ERROR)
org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> {
gui.show(player);
});

RequestUtil.invalidate(player.getUniqueId());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
}

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

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

Expand Down

0 comments on commit 74ff69d

Please sign in to comment.