Skip to content

Commit

Permalink
Some code refactoring
Browse files Browse the repository at this point in the history
done by adding classes dedicated to requests
  • Loading branch information
efekurbann committed Jan 6, 2024
1 parent f91b021 commit d2701ef
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package net.leaderos.plugin.modules.bazaar.gui;

import com.cryptomorin.xseries.XMaterial;
import de.themoep.inventorygui.*;
import de.themoep.inventorygui.GuiStorageElement;
import de.themoep.inventorygui.InventoryGui;
import lombok.SneakyThrows;
import net.leaderos.plugin.Bukkit;
import net.leaderos.plugin.helpers.ChatUtil;
import net.leaderos.plugin.helpers.GameUtil;
import net.leaderos.plugin.helpers.GuiHelper;
import net.leaderos.plugin.modules.cache.model.User;
import net.leaderos.plugin.modules.bazaar.BazaarModule;
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.helpers.Placeholder;
import net.leaderos.shared.model.Response;
import net.leaderos.shared.model.request.PostRequest;
import net.leaderos.shared.model.request.impl.bazaar.AddBazaarItemRequest;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
Expand All @@ -22,7 +23,10 @@

import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -108,28 +112,9 @@ public static void showGui(Player player, int itemAmount) {
String modelId = ItemUtil.getModelId(item);
String enchantment = ItemUtil.getEnchantments(item);

Map<String, String> body = new HashMap<>();
body.put("owner", userId);
body.put("name", name);
if (lore != null)
body.put("lore", lore);
body.put("amount", String.valueOf(amount));
body.put("maxDurability", String.valueOf(maxDurability));
body.put("durability", String.valueOf(durability));

body.put("base64", base64);
body.put("price", String.valueOf(price));
body.put("creationDate", creationDate);
if (modelId != null)
body.put("modelID", modelId);
if (enchantment != null)
body.put("enchantment", enchantment);
body.put("serverID", String.valueOf(serverId));
body.put("itemID", material.name());

// Sends response
try {
Response postBazaarItem = new PostRequest("bazaar/storages/" + userId + "/items", body).getResponse();
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import lombok.SneakyThrows;
import net.leaderos.plugin.Bukkit;
import net.leaderos.plugin.helpers.ChatUtil;
import net.leaderos.plugin.modules.bazaar.BazaarModule;
import net.leaderos.plugin.helpers.ItemUtil;
import net.leaderos.plugin.modules.bazaar.BazaarModule;
import net.leaderos.shared.model.Response;
import net.leaderos.shared.model.request.DeleteRequest;
import net.leaderos.shared.model.request.GetRequest;
import net.leaderos.shared.model.request.impl.bazaar.GetBazaarItemsRequest;
import net.leaderos.shared.model.request.impl.bazaar.RemoveBazaarItemRequest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -77,7 +78,7 @@ public ItemStack getItem() {
*/
@SneakyThrows
public boolean withdrawItem(Player player) {
Response deleteRequest = new DeleteRequest("bazaar/storages/" + getUserId() + "/items/" + getId()).getResponse();
Response deleteRequest = new RemoveBazaarItemRequest(getUserId(), getUserId()).getResponse();
if (deleteRequest.getResponseCode() == HttpURLConnection.HTTP_OK
&& deleteRequest.getResponseMessage().getBoolean("status")) {
ItemStack item = ItemUtil.fromBase64(getBase64());
Expand All @@ -97,7 +98,7 @@ public boolean withdrawItem(Player player) {
public static List<PlayerBazaar> getBazaarStorage(String userId) {
try {
int serverId = BazaarModule.getServerId();
GetRequest getRequest = new GetRequest("bazaar/storages/" + userId + "/items?serverID=" + serverId);
GetRequest getRequest = new GetBazaarItemsRequest(userId, String.valueOf(serverId));
JSONObject response = getRequest.getResponse().getResponseMessage();
List<PlayerBazaar> playerBazaarList = new ArrayList<>();
response.getJSONArray("array").forEach(bazaar -> playerBazaarList.add(new PlayerBazaar((JSONObject) bazaar, userId)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.SneakyThrows;
import net.leaderos.plugin.Bukkit;
import net.leaderos.shared.model.request.GetRequest;
import net.leaderos.shared.model.request.impl.user.GetUserRequest;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -90,7 +91,7 @@ public User(@NotNull JSONObject user) {
this.creationDate = format.parse(user.getString("creationDate"));

// Adds data to cache
userList.remove(username);
userList.remove(username); // why remove? instead, can't we just add the new object? afaik, it replaces the old value
userList.put(username, this);
}

Expand Down Expand Up @@ -151,7 +152,7 @@ public static void loadAllPlayers() {
*/
public static void loadPlayerCache(Player player) {
try {
GetRequest getRequest = new GetRequest("users/" + player.getName());
GetRequest getRequest = new GetUserRequest(player.getName());
new User(getRequest.getResponse().getResponseMessage());
}
catch (Exception ignored) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.leaderos.plugin.modules.donations.model.DonationType;
import net.leaderos.plugin.modules.donations.model.Donation;
import net.leaderos.plugin.modules.donations.model.DonationType;
import net.leaderos.shared.model.Response;
import net.leaderos.shared.model.request.GetRequest;
import net.leaderos.shared.model.request.impl.donations.GetDonationsRequest;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -105,7 +105,7 @@ private void loadDonationsData() {
this.donations = new ArrayList<>();
List<Donation> donations = new ArrayList<>();
try {
Response recentDonationResponse = new GetRequest("store/donations/?type=" + getType().getRequest() + "&limit=10").getResponse();
Response recentDonationResponse = new GetDonationsRequest(getType().getRequest()).getResponse();
if (recentDonationResponse.getResponseCode() == HttpURLConnection.HTTP_OK) {
JSONArray data = recentDonationResponse.getResponseMessage().getJSONArray("array");
for (int i = 0; i < data.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@
import net.leaderos.plugin.helpers.ChatUtil;
import net.leaderos.plugin.helpers.MDChat.MDChatAPI;
import net.leaderos.plugin.modules.cache.model.User;
import net.leaderos.plugin.modules.webstore.model.Product;
import net.leaderos.shared.model.Response;
import net.leaderos.shared.model.request.PostRequest;
import net.leaderos.shared.model.request.impl.store.BuyRequest;
import net.leaderos.shared.modules.auth.AuthHelper;
import net.leaderos.shared.modules.credit.enums.UpdateType;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;

public class WebStoreHelper {
public static void buyItem(Player player, String productId) {
if (User.isPlayerAuthed(player)) {
User user = User.getUser(player.getName());
Map<String, String> body = new HashMap<>();
body.put("user", user.getId());
body.put("products[0][id]", productId);
body.put("products[0][quantity]", "1");
// Titles
String title = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreTitle());
String subtitleError = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getBuyWebStoreError());
Expand All @@ -35,7 +28,7 @@ public static void buyItem(Player player, String productId) {
player.sendTitle(title, subtitleProgress);
// Buy progress
try {
Response buyRequest = new PostRequest("store/buy", body).getResponse();
Response buyRequest = new BuyRequest(user.getId(), productId).getResponse();
if (buyRequest.getResponseCode() == HttpURLConnection.HTTP_OK) {
// Calls UpdateCache event for update player's cache
double credits = buyRequest.getResponseMessage().getJSONObject("data").getDouble("credits");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.leaderos.plugin.helpers.ItemUtil;
import net.leaderos.shared.exceptions.RequestException;
import net.leaderos.shared.model.request.GetRequest;
import net.leaderos.shared.model.request.impl.store.ListingRequest;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
Expand Down Expand Up @@ -144,7 +145,7 @@ public static void loadAllCategories() throws IOException, RequestException {
if (!categories.isEmpty())
categories.clear();

GetRequest getRequest = new GetRequest("store/listing");
GetRequest getRequest = new ListingRequest();
JSONObject response = getRequest.getResponse().getResponseMessage();
response.getJSONArray("categories").forEach(jsonObj -> new Category((JSONObject) jsonObj));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.leaderos.shared.model.request.impl.auth;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class AuthRequest extends PostRequest {

public AuthRequest(String username, String uuid) throws IOException {
super("auth/generate-link", new HashMap<String, String>() {{
put("username", username);
put("uuid", uuid);
}});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.leaderos.shared.model.request.impl.bazaar;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class AddBazaarItemRequest extends PostRequest {

public AddBazaarItemRequest(String userId, String name, String lore, int amount, int maxDurability, int durability, String base64, double price, String creationDate, String modelId, String enchantment, int serverId, String item) throws IOException {
super("bazaar/storages/" + userId + "/items", new HashMap<String, String>() {{
put("owner", userId);
put("name", name);
if (lore != null)
put("lore", lore);
put("amount", String.valueOf(amount));
put("maxDurability", String.valueOf(maxDurability));
put("durability", String.valueOf(durability));
put("base64", base64);
put("price", String.valueOf(price));
put("creationDate", String.valueOf(creationDate));
if (modelId != null)
put("modelID", modelId);
if (enchantment != null)
put("enchantment", enchantment);
put("serverID", String.valueOf(serverId));
put("itemID", item);
}});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.leaderos.shared.model.request.impl.bazaar;

import net.leaderos.shared.model.request.GetRequest;

import java.io.IOException;

public class GetBazaarItemsRequest extends GetRequest {

public GetBazaarItemsRequest(String userId, String serverId) throws IOException {
super("bazaar/storages/" + userId + "/items?serverID=" + serverId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.leaderos.shared.model.request.impl.bazaar;

import net.leaderos.shared.model.request.DeleteRequest;

import java.io.IOException;
import java.util.HashMap;

public class RemoveBazaarItemRequest extends DeleteRequest {

public RemoveBazaarItemRequest(String userId, String id) throws IOException {
super("bazaar/storages/" + userId + "/items/" + id, new HashMap<>());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.leaderos.shared.model.request.impl.credit;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class AddCreditsRequest extends PostRequest {

public AddCreditsRequest(String target, double amount) throws IOException {
super("credits/" + target + "/add", new HashMap<String, String>() {{
put("amount", String.valueOf(amount));
}});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.leaderos.shared.model.request.impl.credit;

import net.leaderos.shared.model.request.GetRequest;

import java.io.IOException;

public class GetCreditsRequest extends GetRequest {

public GetCreditsRequest(String username) throws IOException {
super("credits/" + username);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.leaderos.shared.model.request.impl.credit;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class RemoveCreditsRequest extends PostRequest {
public RemoveCreditsRequest(String target, double amount) throws IOException {
super("credits/" + target + "/remove", new HashMap<String, String>() {{
put("amount", String.valueOf(amount));
}});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.leaderos.shared.model.request.impl.credit;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class SendCreditsRequest extends PostRequest {
public SendCreditsRequest(String sender, String target, double amount) throws IOException {
super("credits/" + sender + "/send", new HashMap<String, String>() {{
put("target", target);
put("amount", String.valueOf(amount));
}});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.leaderos.shared.model.request.impl.credit;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class SetCreditsRequest extends PostRequest {
public SetCreditsRequest(String target, double amount) throws IOException {
super("credits/" + target + "/set", new HashMap<String, String>() {{
put("amount", String.valueOf(amount));
}});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.leaderos.shared.model.request.impl.discord;

import net.leaderos.shared.model.request.PostRequest;

import java.io.IOException;
import java.util.HashMap;

public class DiscordSyncRequest extends PostRequest {
public DiscordSyncRequest(String username) throws IOException {
super("integrations/discord/sync", new HashMap<String, String>() {{
put("user", username);
}});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.leaderos.shared.model.request.impl.donations;

import net.leaderos.shared.model.request.GetRequest;

import java.io.IOException;

public class GetDonationsRequest extends GetRequest {
/**
* Request constructor
*
* @param api of request
* @throws IOException for HttpUrlConnection
*/
public GetDonationsRequest(String type) throws IOException {
super("store/donations/?type=" + type + "&limit=10");
}
}
Loading

0 comments on commit d2701ef

Please sign in to comment.