Skip to content

Commit

Permalink
1.0.8: added default category for webstore gui, added webstore catego…
Browse files Browse the repository at this point in the history
…ry <id> command
  • Loading branch information
benfiratkaya committed Apr 21, 2024
1 parent 53bc0a0 commit 166195a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ public static class WebStoreGui extends OkaeriConfig {

private String buyWebStoreUserNotFound = "&cUser not found.";

private String webStoreCategoryNotFound = "&cCategory not found.";

/**
* withdraw item subtitle error
*/
Expand Down
23 changes: 10 additions & 13 deletions bukkit/src/main/java/net/leaderos/plugin/configuration/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,29 +291,26 @@ public static class NextPage extends OkaeriConfig {
*/
@Getter @Setter
public static class DefaultCategory extends OkaeriConfig {
/**
* Enable Default Category
*/
private boolean enable = false;

/**
* Default material
* Default Category ID
*/
private String material = "CHEST";
private String categoryId = "0";
}

/**
* DefaultProduct attributes
* Category default material
*/
private Gui.DefaultProduct defaultProduct = new Gui.DefaultProduct();
private String categoryDefaultMaterial = "CHEST";

/**
* DefaultProduct arguments class
* Product default material
*/
@Getter @Setter
public static class DefaultProduct extends OkaeriConfig {

/**
* Default material
*/
private String material = "DIAMOND";
}
private String productDefaultMaterial = "DIAMOND";

/**
* Credit attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ public static class WebStoreGui extends Language.Gui.WebStoreGui {

private String buyWebStoreUserNotFound = "&cUser not found.";

private String webStoreCategoryNotFound = "&cCategory not found.";

/**
* withdraw item subtitle error
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ public static class WebStoreGui extends Language.Gui.WebStoreGui {

private String buyWebStoreUserNotFound = "&cKullanıcı bulunamadı.";

private String webStoreCategoryNotFound = "&cKategori bulunamadı.";

/**
* purchase subtitle success
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import net.leaderos.plugin.api.managers.ModuleManager;
import net.leaderos.plugin.helpers.ChatUtil;
import net.leaderos.plugin.modules.webstore.gui.MainWebStoreGui;
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.plugin.modules.webstore.model.Product;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand All @@ -33,8 +35,37 @@ public class WebStoreCommand extends BaseCommand {
@Permission("leaderos.webstore.open")
public void defaultCommand(Player player) {
LeaderOSAPI.getModuleManager();
if (ModuleManager.getModule("WebStore").isEnabled())
if (!ModuleManager.getModule("WebStore").isEnabled()) return;

String categoryId = Bukkit.getInstance().getModulesFile().getWebStore().getGui().getDefaultCategory().getCategoryId();
if (Bukkit.getInstance().getModulesFile().getWebStore().getGui().getDefaultCategory().isEnable() && !categoryId.equals("0")) {
categoryCommand(player, categoryId);
} else {
MainWebStoreGui.showGui(player);
}
}

/**
* Open category command of webstore
* @param sender commandsender
* @param categoryId category id to open
*/
@Permission("leaderos.webstore.open")
@SubCommand("category")
public void categoryCommand(CommandSender sender, String categoryId) {
if (!(sender instanceof Player)) return;
if (!ModuleManager.getModule("WebStore").isEnabled()) return;

Player player = (Player) sender;

Category category = WebStoreHelper.findCategoryById(categoryId);

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 @@ -15,6 +15,7 @@
import net.leaderos.shared.modules.credit.enums.UpdateType;
import net.leaderos.plugin.modules.webstore.model.Category;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -129,4 +130,34 @@ private static Product findProductByIdRecursive(String productId, Category categ
// Product not found
return null;
}

public static Category findCategoryById(String categoryId) {
List<Category> categories = Category.getCategories();

for (Category category : categories) {
Category foundCategory = findCategoryByIdRecursive(categoryId, category);
if (foundCategory != null) {
return foundCategory;
}
}
return null;
}

private static Category findCategoryByIdRecursive(String categoryId, Category category) {
// Check if the category is this category
if (category.getCategoryId().equalsIgnoreCase(categoryId)) {
return category;
}

// Check if the category is in any sub-category
for (Category subCategory : category.getSubCategories()) {
Category foundCategory = findCategoryByIdRecursive(categoryId, subCategory);
if (foundCategory != null) {
return foundCategory;
}
}

// Category not found
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Category(@NotNull JSONObject category) {
this.material = XMaterial.matchXMaterial(category.getString("minecraftItem")).get();

if (material == null || !material.isSupported())
this.material = XMaterial.matchXMaterial(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getDefaultCategory().getMaterial()).orElse(XMaterial.CHEST);
this.material = XMaterial.matchXMaterial(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getCategoryDefaultMaterial()).orElse(XMaterial.CHEST);

// products
JSONArray products = category.getJSONArray("products");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Product(@NotNull JSONObject product) {
this.material = XMaterial.matchXMaterial(product.getString("minecraftItem")).get();

if (material == null || !material.isSupported())
this.material = XMaterial.matchXMaterial(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getDefaultProduct().getMaterial()).orElse(XMaterial.DIAMOND);
this.material = XMaterial.matchXMaterial(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getProductDefaultMaterial()).orElse(XMaterial.DIAMOND);
}

/**
Expand Down

0 comments on commit 166195a

Please sign in to comment.