Skip to content

Commit

Permalink
Make some actions run async using the new RequestUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
efekurbann committed Jan 13, 2024
1 parent d2701ef commit 6e738b8
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static class Messages extends OkaeriConfig {

private String cannotCreateFull = "{prefix} &cPlease create some space in your inventory and try again.";

private String haveRequestOngoing = "&cPlease wait for your current request to be done!";

/**
* Help commands message
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.leaderos.plugin.Bukkit;
import net.leaderos.plugin.helpers.ChatUtil;
import net.leaderos.plugin.helpers.MDChat.MDChatAPI;
import net.leaderos.shared.helpers.RequestUtil;
import net.leaderos.shared.modules.auth.AuthHelper;
import org.bukkit.entity.Player;

Expand All @@ -27,6 +28,13 @@ public class AuthCommand extends BaseCommand {
@Default
@Permission("leaderos.auth")
public void defaultCommand(Player player) {
if (!RequestUtil.canRequest(player.getUniqueId())) {
ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getHaveRequestOngoing());
return;
}

RequestUtil.addRequest(player.getUniqueId());

String link = AuthHelper.getAuthLink(player.getName(), player.getUniqueId());
if (link != null)
player.spigot().sendMessage(
Expand All @@ -37,5 +45,7 @@ public void defaultCommand(Player player) {
.replace("{prefix}", Bukkit.getInstance().getLangFile().getMessages().getPrefix()))));
else
ChatUtil.sendMessage(player, Bukkit.getInstance().getLangFile().getMessages().getAuth().getNoLink());

RequestUtil.invalidate(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,86 +62,86 @@ public static void showGui(Player player, int itemAmount) {
gui.addElement(new GuiStorageElement('i', inv));
// Close action area (event)
gui.setCloseAction(close -> {
// Calculating storage amounts
int maxStorageAmount = GameUtil.getAmountFromPerm(player,
"bazaar.maxstorage.",
Bukkit.getInstance().getModulesFile().getBazaar().getDefaultStorageSize());
// Calculating storage amounts
int maxStorageAmount = GameUtil.getAmountFromPerm(player,
"bazaar.maxstorage.",
Bukkit.getInstance().getModulesFile().getBazaar().getDefaultStorageSize());

int canStoreAmount = maxStorageAmount - itemAmount;
// Items which stored (airs included)
List<ItemStack> items = Arrays.stream(inv.getContents()).collect(Collectors.toList());
String userId = User.getUser(player.getName()).getId();
int serverId = BazaarModule.getServerId();
int canStoreAmount = maxStorageAmount - itemAmount;
// Items which stored (airs included)
List<ItemStack> items = Arrays.stream(inv.getContents()).collect(Collectors.toList());
String userId = User.getUser(player.getName()).getId();
int serverId = BazaarModule.getServerId();

// If player maxed out storage limit items will be added to
// this list then gives back to player.
List<ItemStack> returnItems = new ArrayList<>();
// If player maxed out storage limit items will be added to
// this list then gives back to player.
List<ItemStack> returnItems = new ArrayList<>();

// item loop
for (ItemStack item : items) {
// Checks if item is empty or null (can be AIR etc.)
if (item == null)
continue;
if (item.getType() == null)
continue;
if (item.getType().equals(Material.AIR))
continue;
// Checks if area is filler item
if (item.equals(fillerItem))
continue;
// item loop
for (ItemStack item : items) {
// Checks if item is empty or null (can be AIR etc.)
if (item == null)
continue;
if (item.getType() == null)
continue;
if (item.getType().equals(Material.AIR))
continue;
// Checks if area is filler item
if (item.equals(fillerItem))
continue;

// Calculates storage amount
if (canStoreAmount > 0)
canStoreAmount--;
// If maxed out then add items to temp array
else {
returnItems.add(item);
continue;
}
// Item info
XMaterial material = XMaterial.matchXMaterial(item);
String name = ItemUtil.getName(item);
String lore = (item.hasItemMeta() && item.getItemMeta().hasLore()) ?
String.join("\n", item.getItemMeta().getLore()) : null;
int amount = item.getAmount();
int maxDurability = item.getType().getMaxDurability();
int durability = ItemUtil.getDurability(item, maxDurability);
String base64 = ItemUtil.toBase64(item);
double price = 0.0;
String creationDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String modelId = ItemUtil.getModelId(item);
String enchantment = ItemUtil.getEnchantments(item);
// Calculates storage amount
if (canStoreAmount > 0)
canStoreAmount--;
// If maxed out then add items to temp array
else {
returnItems.add(item);
continue;
}
// Item info
XMaterial material = XMaterial.matchXMaterial(item);
String name = ItemUtil.getName(item);
String lore = (item.hasItemMeta() && item.getItemMeta().hasLore()) ?
String.join("\n", item.getItemMeta().getLore()) : null;
int amount = item.getAmount();
int maxDurability = item.getType().getMaxDurability();
int durability = ItemUtil.getDurability(item, maxDurability);
String base64 = ItemUtil.toBase64(item);
double price = 0.0;
String creationDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String modelId = ItemUtil.getModelId(item);
String enchantment = ItemUtil.getEnchantments(item);

// Sends response
try {
Response postBazaarItem = new AddBazaarItemRequest(userId, name, lore, amount, maxDurability, durability, base64, price, creationDate, modelId, enchantment, serverId, material.name()).getResponse();
if (postBazaarItem.getResponseCode() == HttpURLConnection.HTTP_OK
&& postBazaarItem.getResponseMessage().getBoolean("status")) {
ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders(
Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getAddItemMessage(),
new Placeholder("%item_name%", name)
));
}
else throw new Exception();
} catch (Exception e) {
// TODO error msg
e.printStackTrace();
// If something occur when adding item it will pop item back to player inventory
returnItems.add(item);
}
}
org.bukkit.Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getInstance(), () -> {
// Sends response
try {
Response postBazaarItem = new AddBazaarItemRequest(userId, name, lore, amount, maxDurability, durability, base64, price, creationDate, modelId, enchantment, serverId, material.name()).getResponse();
if (postBazaarItem.getResponseCode() == HttpURLConnection.HTTP_OK
&& postBazaarItem.getResponseMessage().getBoolean("status")) {
ChatUtil.sendMessage(player, ChatUtil.replacePlaceholders(
Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getAddItemMessage(),
new Placeholder("%item_name%", name)
));
} else throw new Exception();
} catch (Exception e) {
// TODO error msg
e.printStackTrace();
// If something occur when adding item it will pop item back to player inventory
returnItems.add(item);
}
if (!returnItems.isEmpty()) {
PlayerInventory playerInventory = player.getInventory();

// Gives items back to player
if (!returnItems.isEmpty()) {
PlayerInventory playerInventory = player.getInventory();
returnItems.forEach(playerInventory::addItem);
String returnMessage = Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getReturnItemMessage();
returnMessage = returnMessage.replace("%max_amount%", String.valueOf(maxStorageAmount))
.replace("%amount%", String.valueOf(returnItems.size()));
ChatUtil.sendMessage(player, returnMessage);
}
return false; // Don't go back to the previous GUI (true would automatically go back to the previously opened one)
});
returnItems.forEach(playerInventory::addItem);
String returnMessage = Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getReturnItemMessage();
returnMessage = returnMessage.replace("%max_amount%", String.valueOf(maxStorageAmount))
.replace("%amount%", String.valueOf(returnItems.size()));
ChatUtil.sendMessage(player, returnMessage);
}
});
}
return false; // Don't go back to the previous GUI (true would automatically go back to the previously opened one)
});
gui.show(player);
}
}
Loading

0 comments on commit 6e738b8

Please sign in to comment.