From 3c62524d42891e4d9c2d705fcb3adfa7526c8746 Mon Sep 17 00:00:00 2001 From: Efe Kurban Date: Mon, 15 Jan 2024 22:51:24 +0300 Subject: [PATCH] Bulk Update includes async, custom error codes and more. --- .../api/modules/credit/CreditManager.java | 9 ++++-- .../plugin/configuration/Language.java | 8 +++++ .../plugin/configuration/lang/en.java | 3 +- .../plugin/configuration/lang/tr.java | 3 +- .../modules/bazaar/gui/BazaarAddItemGui.java | 3 ++ .../plugin/modules/bazaar/gui/BazaarGui.java | 14 ++++---- .../modules/bazaar/model/PlayerBazaar.java | 10 +++--- .../credit/commands/CreditCommand.java | 20 +++++++++--- .../voucher/listeners/VoucherListener.java | 23 +++++++++---- .../webstore/helpers/WebStoreHelper.java | 11 +++++-- .../bungee/configuration/Language.java | 2 ++ .../credit/commands/CreditCommand.java | 13 ++++++-- .../java/net/leaderos/shared/error/Error.java | 32 +++++++++++++++++++ .../net/leaderos/shared/model/Request.java | 6 ++-- .../net/leaderos/shared/model/Response.java | 6 ++++ 15 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 shared/src/main/java/net/leaderos/shared/error/Error.java diff --git a/bukkit/src/main/java/net/leaderos/plugin/api/modules/credit/CreditManager.java b/bukkit/src/main/java/net/leaderos/plugin/api/modules/credit/CreditManager.java index 707ab6795..6c155a0df 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/api/modules/credit/CreditManager.java +++ b/bukkit/src/main/java/net/leaderos/plugin/api/modules/credit/CreditManager.java @@ -1,5 +1,6 @@ package net.leaderos.plugin.api.modules.credit; +import net.leaderos.shared.error.Error; import net.leaderos.shared.model.Response; import net.leaderos.shared.modules.credit.CreditHelper; @@ -70,10 +71,14 @@ public boolean remove(String target, Double amount) { * @param target receiver of credit * @param amount to send */ - public boolean send(String sender, String target, Double amount) { + public Error send(String sender, String target, Double amount) { Response sendCreditResponse = CreditHelper.sendCreditRequest(sender, target, amount); - return (Objects.requireNonNull(sendCreditResponse).getResponseCode() == HttpURLConnection.HTTP_OK && sendCreditResponse.getResponseMessage().getBoolean("status")); + if ((Objects.requireNonNull(sendCreditResponse).getResponseCode() == HttpURLConnection.HTTP_OK && sendCreditResponse.getResponseMessage().getBoolean("status"))) { + return null; + } + + return sendCreditResponse.getError(); } } diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java index 2afb83d4b..87e16ddf1 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/Language.java @@ -261,6 +261,8 @@ public static class Credit extends OkaeriConfig { private String creditInfoOther = "{prefix} &b{target} &ahas &e{amount} &acredit(s)"; + private String cannotSendCreditsThisUser = "{prefix} &cCould not send credits to this user."; + private String cannotSendCreditYourself = "{prefix} &cYou can not send credit(s) to yourself."; private String cannotSendCreditNegative = "{prefix} &cPlease enter a valid amount. The amount must be higher than 0."; @@ -425,6 +427,12 @@ public static class WebStoreGui extends OkaeriConfig { */ private String buyWebStoreNotEnoughCredit = "&cNot enough credits."; + private String buyWebStoreOutOfStock = "&cOut of stock."; + + private String buyWebStoreProductNotFound = "&cProduct not found."; + + private String buyWebStoreUserNotFound = "&cUser not found."; + /** * withdraw item subtitle error */ diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java index efbc92871..d8ec9de47 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/en.java @@ -1,6 +1,5 @@ package net.leaderos.plugin.configuration.lang; -import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; import eu.okaeri.configs.annotation.NameModifier; import eu.okaeri.configs.annotation.NameStrategy; @@ -260,6 +259,8 @@ public static class Credit extends Language.Messages.Credit { private String creditInfoOther = "{prefix} &b{target} &ahas &e{amount} &acredit(s)"; + private String cannotSendCreditsThisUser = "{prefix} &cCould not send credits to this user."; + private String cannotSendCreditYourself = "{prefix} &cYou can not send credit(s) to yourself."; private String cannotSendCreditNegative = "{prefix} &cPlease enter a valid amount. The amount must be higher than 0."; diff --git a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java index 35d357fab..26ef7db3a 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java +++ b/bukkit/src/main/java/net/leaderos/plugin/configuration/lang/tr.java @@ -1,6 +1,5 @@ package net.leaderos.plugin.configuration.lang; -import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; import eu.okaeri.configs.annotation.NameModifier; import eu.okaeri.configs.annotation.NameStrategy; @@ -260,6 +259,8 @@ public static class Credit extends Language.Messages.Credit { private String creditInfoOther = "{prefix} &b{target} &aadlı oyuncunun &e{amount} &akredisi bulunuyor."; + private String cannotSendCreditsThisUser = "{prefix} &cBu kullanıcıya kredi gönderemezsin."; + private String cannotSendCreditYourself = "{prefix} &cKendine kredi gönderemezsin."; private String cannotSendCreditNegative = "{prefix} &cLütfen geçerli bir miktar giriniz."; diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java index 21dbb68e7..57ad17acc 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java @@ -11,6 +11,7 @@ import net.leaderos.plugin.helpers.ItemUtil; import net.leaderos.plugin.modules.bazaar.BazaarModule; import net.leaderos.plugin.modules.cache.model.User; +import net.leaderos.shared.error.Error; import net.leaderos.shared.helpers.Placeholder; import net.leaderos.shared.model.Response; import net.leaderos.shared.model.request.impl.bazaar.AddBazaarItemRequest; @@ -122,6 +123,8 @@ public static void showGui(Player player, int itemAmount) { Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getAddItemMessage(), new Placeholder("%item_name%", name) )); + } else if (postBazaarItem.getError() == Error.INSERT_ERROR) { + returnItems.add(item); } else throw new Exception(); } catch (Exception e) { // TODO error msg 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 b3dad923e..9fab26548 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 @@ -10,6 +10,7 @@ import net.leaderos.plugin.helpers.GuiHelper; import net.leaderos.plugin.modules.bazaar.model.PlayerBazaar; import net.leaderos.plugin.modules.cache.model.User; +import net.leaderos.shared.error.Error; import net.leaderos.shared.helpers.RequestUtil; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -39,9 +40,9 @@ public static void showGui(Player player) { return; } - org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { - RequestUtil.addRequest(player.getUniqueId()); + RequestUtil.addRequest(player.getUniqueId()); + org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getBazaar().getGui().getLayout().toArray(new String[0]); // Inventory object @@ -85,11 +86,13 @@ public static void showGui(Player player) { player.sendTitle(title, subtitleProgress); org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { - boolean withdrawStatus = playerBazaarItem.withdrawItem(player); - if (withdrawStatus) + Error error = playerBazaarItem.withdrawItem(player); + if (error == null) player.sendTitle(title, subtitleSuccess); - else + else if (error == Error.DELETE_ERROR) player.sendTitle(title, subtitleError); + + RequestUtil.invalidate(player.getUniqueId()); }); return false; @@ -104,7 +107,6 @@ public static void showGui(Player player) { 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 aca4c6a74..956286113 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 @@ -7,6 +7,7 @@ import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.helpers.ItemUtil; import net.leaderos.plugin.modules.bazaar.BazaarModule; +import net.leaderos.shared.error.Error; import net.leaderos.shared.model.Response; import net.leaderos.shared.model.request.GetRequest; import net.leaderos.shared.model.request.impl.bazaar.GetBazaarItemsRequest; @@ -77,17 +78,16 @@ public ItemStack getItem() { * @return status of withdraw */ @SneakyThrows - public boolean withdrawItem(Player player) { + public Error withdrawItem(Player player) { Response deleteRequest = new RemoveBazaarItemRequest(getUserId(), getUserId()).getResponse(); if (deleteRequest.getResponseCode() == HttpURLConnection.HTTP_OK && deleteRequest.getResponseMessage().getBoolean("status")) { ItemStack item = ItemUtil.fromBase64(getBase64()); player.getInventory().addItem(item); - return true; + return null; } - else - // TODO Throw exception - return false; + + return deleteRequest.getError(); } /** diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/credit/commands/CreditCommand.java b/bukkit/src/main/java/net/leaderos/plugin/modules/credit/commands/CreditCommand.java index 208c15edd..8ad5ade31 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/credit/commands/CreditCommand.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/credit/commands/CreditCommand.java @@ -10,6 +10,7 @@ import net.leaderos.plugin.api.LeaderOSAPI; import net.leaderos.plugin.api.handlers.UpdateCacheEvent; import net.leaderos.plugin.helpers.ChatUtil; +import net.leaderos.shared.error.Error; import net.leaderos.shared.helpers.MoneyUtil; import net.leaderos.shared.helpers.Placeholder; import net.leaderos.shared.helpers.RequestUtil; @@ -59,7 +60,7 @@ public void defaultCommand(Player player) { * Send credit command * @param player executor * @param target player - * @param amount of credit + * @param a amount of credit */ @SubCommand(value = "send", alias = {"gönder", "gonder"}) @Permission("leaderos.credit.send") @@ -72,23 +73,24 @@ public void sendCommand(Player player, String target, Double a) { RequestUtil.addRequest(player.getUniqueId()); org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> { - double amount = MoneyUtil.parseDouble(a); Player targetPlayer = org.bukkit.Bukkit.getPlayerExact(target); if (player.getName().equalsIgnoreCase(target)) { + RequestUtil.invalidate(player.getUniqueId()); ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditYourself()); return; } if (amount <= 0) { + RequestUtil.invalidate(player.getUniqueId()); ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNegative()); return; } - boolean sendCredit = LeaderOSAPI.getCreditManager().send(player.getName(), target, amount); + Error error = LeaderOSAPI.getCreditManager().send(player.getName(), target, amount); - if (sendCredit) { + if (error == null) { // 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( @@ -106,8 +108,16 @@ public void sendCommand(Player player, String target, Double a) { new Placeholder("{player}", player.getName()) )); } - } else + } else if (error == Error.NOT_ENOUGH_CREDITS || error == Error.USER_NOT_FOUND) { ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNotEnough()); + } else if (error == Error.INVALID_TARGET + || error == Error.TARGET_USER_NOT_FOUND) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditsThisUser()); + } else if (error == Error.INVALID_AMOUNT) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNegative()); + } else { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNotEnough()); + } RequestUtil.invalidate(player.getUniqueId()); }); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/listeners/VoucherListener.java b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/listeners/VoucherListener.java index 023867c29..0dfddaa25 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/listeners/VoucherListener.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/voucher/listeners/VoucherListener.java @@ -6,8 +6,10 @@ import net.leaderos.plugin.api.handlers.UpdateCacheEvent; import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.modules.voucher.VoucherModule; +import net.leaderos.shared.error.Error; 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; @@ -74,7 +76,12 @@ public void onRightClick(PlayerInteractEvent event) { return; } - remove(player, id); + if (!RequestUtil.canRequest(player.getUniqueId())) { + ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing()); + return; + } + + RequestUtil.addRequest(player.getUniqueId()); list.add(id); VoucherModule.getVoucherData().set("used", list); VoucherModule.getVoucherData().save(); @@ -84,19 +91,23 @@ public void onRightClick(PlayerInteractEvent event) { if (Objects.requireNonNull(depositResponse).getResponseCode() == HttpURLConnection.HTTP_OK && depositResponse.getResponseMessage().getBoolean("status")) { - awaitingVouchers.remove(String.valueOf(id)); - // Calls UpdateCache event for update player's cache - org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), amount, UpdateType.ADD))); + org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> { + org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), amount, UpdateType.ADD)); + awaitingVouchers.remove(String.valueOf(id)); + remove(player, id); + }); + ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders( Bukkit.getInstance().getLangFile().getMessages().getVouchers() .getSuccessfullyUsed(), new Placeholder("{amount}", MoneyUtil.format(amount)) )); - } - else { + } else if (depositResponse.getError() == Error.USER_NOT_FOUND) { ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getPlayerNotAvailable()); awaitingVouchers.remove(String.valueOf(id)); } + + RequestUtil.invalidate(player.getUniqueId()); }); } 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 997302d19..44ca2080d 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,6 +6,7 @@ import net.leaderos.plugin.helpers.ChatUtil; import net.leaderos.plugin.helpers.MDChat.MDChatAPI; import net.leaderos.plugin.modules.cache.model.User; +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; @@ -41,9 +42,15 @@ public static void buyItem(Player player, String productId) { org.bukkit.Bukkit.getPluginManager().callEvent(new UpdateCacheEvent(player.getName(), credits, UpdateType.SET)); player.sendTitle(title, subtitleSuccess); } - else if (buyRequest.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) + else if (buyRequest.getError() == Error.INVALID_QUANTITY + || buyRequest.getError() == Error.INSUFFICIENT_BALANCE) player.sendTitle(title, subtitleNotEnoughCredit); - else throw new IOException(); + else if (buyRequest.getError() == Error.OUT_OF_STOCK) + player.sendTitle(title, ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreOutOfStock())); + else if (buyRequest.getError() == Error.USER_NOT_FOUND) + player.sendTitle(title, ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreUserNotFound())); + else if (buyRequest.getError() == Error.PRODUCT_NOT_FOUND) + player.sendTitle(title, ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreProductNotFound())); } catch (IOException e) { player.sendTitle(title, subtitleError); } diff --git a/bungee/src/main/java/net/leaderos/bungee/configuration/Language.java b/bungee/src/main/java/net/leaderos/bungee/configuration/Language.java index e64e368d2..676afec75 100644 --- a/bungee/src/main/java/net/leaderos/bungee/configuration/Language.java +++ b/bungee/src/main/java/net/leaderos/bungee/configuration/Language.java @@ -207,6 +207,8 @@ public static class Credit extends OkaeriConfig { private String creditInfoOther = "{prefix} &b{target} &ahas &e{amount} &acredit(s)"; + private String cannotSendCreditsThisUser = "{prefix} &cCould not send credits to this user."; + private String cannotSendCreditYourself = "{prefix} &cYou can not send credit(s) to yourself."; private String cannotSendCreditNegative = "{prefix} &cPlease enter a valid amount. The amount must be higher than 0."; diff --git a/bungee/src/main/java/net/leaderos/bungee/modules/credit/commands/CreditCommand.java b/bungee/src/main/java/net/leaderos/bungee/modules/credit/commands/CreditCommand.java index 841aa36c3..b723db625 100644 --- a/bungee/src/main/java/net/leaderos/bungee/modules/credit/commands/CreditCommand.java +++ b/bungee/src/main/java/net/leaderos/bungee/modules/credit/commands/CreditCommand.java @@ -2,6 +2,7 @@ import net.leaderos.bungee.Bungee; import net.leaderos.bungee.helpers.ChatUtil; +import net.leaderos.shared.error.Error; import net.leaderos.shared.helpers.MoneyUtil; import net.leaderos.shared.helpers.Placeholder; import net.leaderos.shared.model.Response; @@ -183,9 +184,17 @@ public void sendCommand(CommandSender player, String target, Double amount) { new Placeholder("{amount}", MoneyUtil.format(amount)), new Placeholder("{player}", player.getName()) )); + } else { + if (sendCreditResponse.getError() == Error.NOT_ENOUGH_CREDITS) { + ChatUtil.sendMessage(player, Bungee.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNotEnough()); + } else if (sendCreditResponse.getError() == Error.INVALID_TARGET + || sendCreditResponse.getError() == Error.TARGET_USER_NOT_FOUND + || sendCreditResponse.getError() == Error.USER_NOT_FOUND) { + ChatUtil.sendMessage(player, Bungee.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditsThisUser()); + } else if (sendCreditResponse.getError() == Error.INVALID_AMOUNT) { + ChatUtil.sendMessage(player, Bungee.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNegative()); + } } - else - ChatUtil.sendMessage(player, Bungee.getInstance().getLangFile().getMessages().getCredit().getCannotSendCreditNotEnough()); } diff --git a/shared/src/main/java/net/leaderos/shared/error/Error.java b/shared/src/main/java/net/leaderos/shared/error/Error.java new file mode 100644 index 000000000..8d8379560 --- /dev/null +++ b/shared/src/main/java/net/leaderos/shared/error/Error.java @@ -0,0 +1,32 @@ +package net.leaderos.shared.error; + +public enum Error { + + // general + USER_NOT_FOUND, + + // store + INVALID_QUANTITY, + INSUFFICIENT_BALANCE, + OUT_OF_STOCK, + PRODUCT_NOT_FOUND, + + // donations + INVALID_LIMIT, + INVALID_TYPE, + + // bazaar + INSERT_ERROR, + DELETE_ERROR, + + // ticket + TICKET_NOT_FOUND, + + // credit + INVALID_AMOUNT, + NOT_ENOUGH_CREDITS, + INVALID_TARGET, + TARGET_USER_NOT_FOUND + ; + +} diff --git a/shared/src/main/java/net/leaderos/shared/model/Request.java b/shared/src/main/java/net/leaderos/shared/model/Request.java index 56526596b..3454856b6 100644 --- a/shared/src/main/java/net/leaderos/shared/model/Request.java +++ b/shared/src/main/java/net/leaderos/shared/model/Request.java @@ -3,6 +3,7 @@ import lombok.Getter; import lombok.SneakyThrows; import net.leaderos.shared.Shared; +import net.leaderos.shared.error.Error; import net.leaderos.shared.model.request.RequestType; import org.jetbrains.annotations.NotNull; import org.json.JSONArray; @@ -82,9 +83,10 @@ public Request(String api, Map body, @NotNull RequestType type) || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_OK); - String responseString = getStream(connection.getInputStream()); + String responseString = status ? getStream(connection.getInputStream()) : getStream(connection.getErrorStream()); try { - this.response = new Response(responseCode, status, getResponseObj(responseString)); + JSONObject obj = getResponseObj(responseString); + this.response = new Response(responseCode, status, obj, obj.has("error") ? obj.getEnum(Error.class, "error") : null); Shared.getDebugAPI().send(this.response.getResponseMessage().toString(), false); } catch (Exception e) { diff --git a/shared/src/main/java/net/leaderos/shared/model/Response.java b/shared/src/main/java/net/leaderos/shared/model/Response.java index cee1c1216..b76b44de9 100644 --- a/shared/src/main/java/net/leaderos/shared/model/Response.java +++ b/shared/src/main/java/net/leaderos/shared/model/Response.java @@ -3,6 +3,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; +import net.leaderos.shared.error.Error; import org.json.JSONObject; /** @@ -31,6 +32,11 @@ public class Response { */ private JSONObject responseMessage; + /** + * Error + */ + private Error error; + /** * Getter of responseMessage * @return JSONObject - message