Skip to content

Commit

Permalink
Bulk Update
Browse files Browse the repository at this point in the history
includes async, custom error codes and more.
  • Loading branch information
efekurbann committed Jan 15, 2024
1 parent 44df4a8 commit 3c62524
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -104,7 +107,6 @@ public static void showGui(Player player) {

org.bukkit.Bukkit.getScheduler().runTask(Bukkit.getInstance(), () -> {
gui.show(player);
RequestUtil.invalidate(player.getUniqueId());
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

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

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


Expand Down
32 changes: 32 additions & 0 deletions shared/src/main/java/net/leaderos/shared/error/Error.java
Original file line number Diff line number Diff line change
@@ -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
;

}
Loading

0 comments on commit 3c62524

Please sign in to comment.