Skip to content

Commit

Permalink
loading items into menu from config and pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dan28000 committed Aug 12, 2024
1 parent 1da433a commit 94d3c11
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,24 @@

import com.badbones69.crazycrates.api.builders.InventoryBuilder;
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.gacha.ultimatemenu.ComponentBuilder;
import com.badbones69.crazycrates.api.objects.gacha.enums.ShopID;
import com.badbones69.crazycrates.api.objects.gacha.shop.ShopManager;
import com.badbones69.crazycrates.api.objects.gacha.ultimatemenu.UltimateMenuStuff;
import com.ryderbelserion.vital.paper.builders.items.ItemBuilder;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;

import java.util.HashMap;
import java.util.Map;

public class ShopMenu extends InventoryBuilder {
private static final Map<Integer, String> names = new HashMap<>();
private final int selectedCrate;
static {
names.put(0, "rare shop");
names.put(1, "less rare shop");
names.put(2, "vote shop");
}

public ShopMenu(Crate crate, Player player, Component title, int selectedCrate) {
super(crate, player, 54, title);
this.selectedCrate = selectedCrate;
}

private ShopMenu(ShopMenu menu, int page) {
this(menu.getCrate(), menu.getPlayer(), ComponentBuilder.shop(menu.getPlayer(), names.get(page)), page);
}
private final ShopManager shopManager;

public ShopMenu(Crate crate, Player player) {
this(crate, player, ComponentBuilder.shop(player, names.get(0)), 0);
public ShopMenu(Crate crate, Player player, Component title, ShopManager shopManager) {
super(crate, player, 54, title);
this.shopManager = shopManager;
}

@Override
public InventoryBuilder build() {
setTopCrates();
setTextureGlass();
setBackButton();
return this;
}

Expand All @@ -52,61 +33,24 @@ public void run(InventoryClickEvent e) {

Player player = shopMenu.getPlayer();

int newCrateNum;
int shopID;

if (slot < 3) {
newCrateNum = 0;
shopID = 0;
} else if (slot < 6) {
newCrateNum = 1;
shopID = 1;
} else if (slot < 9) {
newCrateNum = 2;
} else newCrateNum = -1;
shopID = 2;
} else shopID = -1;

if (newCrateNum != -1) {
if (shopID != -1) {
player.playSound(UltimateMenuStuff.CLICK);
player.openInventory(new ShopMenu(shopMenu, newCrateNum).build().getInventory());
System.out.println("slot: " + slot);
shopManager.openID(getCrate(), getPlayer(), shopID);
}

if (slot == 49) {
plugin.getCrateManager().getDatabaseManager().getUltimateMenuManager().open(player, shopMenu.getCrate());
}
}

private void setTopCrates() {
int slot = 0;

ItemBuilder selectedMain = UltimateMenuStuff.SHOP_SELECTED;
ItemBuilder unselectedMain = UltimateMenuStuff.SHOP_UNSELECTED;

for (int crate = 0; crate < 3; crate++) {
selectedMain.setDisplayName("<green><b>" + names.get(crate));
unselectedMain.setDisplayName("<red><b>" + names.get(crate));

if (crate == selectedCrate) {
selectedMain.setCustomModelData(1000005 + crate);
getInventory().setItem(slot, selectedMain.getStack());

selectedMain.setCustomModelData(1000003);
getInventory().setItem(slot + 1, selectedMain.getStack());
getInventory().setItem(slot + 2, selectedMain.getStack());
} else {
unselectedMain.setCustomModelData(1000005 + crate);
getInventory().setItem(slot, unselectedMain.getStack());

unselectedMain.setCustomModelData(1000001);
getInventory().setItem(slot + 1, unselectedMain.getStack());
getInventory().setItem(slot + 2, unselectedMain.getStack());
}

slot += 3;
}
}

private void setTextureGlass() {
getInventory().setItem(45, UltimateMenuStuff.SHOP_BANNER.getStack());
}

private void setBackButton() {
getInventory().setItem(49, UltimateMenuStuff.MAIN_MENU_SHOP.getStack());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UltimateMenu extends InventoryBuilder {
private static final DatabaseManager manager = JavaPlugin.getPlugin(CrazyCrates.class).getCrateManager().getDatabaseManager();
private final int selectedCrate;
private final int totalPageAmount;
private int currentPage;
private final int currentPage;
private boolean voteShop, premiumShop, store = false;
private OpenData openData = null;

Expand Down Expand Up @@ -85,15 +85,16 @@ private UltimateMenu(UltimateMenu ultimateMenu) {
);
}

private UltimateMenu(CrateSettings settings, Player player) {
this(settings.getCrate(), player, ComponentBuilder.mainMenu(player, settings));
}


@Override
public InventoryBuilder build() {
setTopCrates();

setTextureGlass();

setItemsPlayerInv();

return this;
}

Expand Down Expand Up @@ -123,8 +124,12 @@ public void run(InventoryClickEvent e) {
}

if (newCrateNum != -1) {
List<CrateSettings> settings = manager.getCrateSettingsSplit().get(ultimateMenu.currentPage);
if (settings.size() <= newCrateNum) return;

CrateSettings newCrate = settings.get(newCrateNum);

player.playSound(UltimateMenuStuff.CLICK);
CrateSettings newCrate = manager.getCrateSettingsSplit().get(ultimateMenu.currentPage).get(newCrateNum);
Component component = ComponentBuilder.mainMenu(player, newCrate);

player.openInventory(new UltimateMenu(ultimateMenu, component, newCrateNum).build().getInventory());
Expand Down Expand Up @@ -176,13 +181,21 @@ public void run(InventoryClickEvent e) {
case 83 -> {
player.playSound(UltimateMenuStuff.CLICK);
close(player);
player.openInventory(new ShopMenu(crate, player).build().getInventory());
manager.getShopManager().openFirst(crate, player);
}
case 84 -> {
//TODO: handle page back
if (totalPageAmount == 1 || currentPage == 0) return;
if (currentPage > 0) {
CrateSettings first = manager.getCrateSettingsSplit().get(currentPage - 1).getFirst();
player.openInventory(new UltimateMenu(first, player).build().getInventory());
}
}
case 85 -> {
//TODO: handle page forward
if (totalPageAmount == 1 || currentPage == totalPageAmount - 1) return;
if (currentPage < totalPageAmount - 1) {
CrateSettings first = manager.getCrateSettingsSplit().get(currentPage + 1).getFirst();
player.openInventory(new UltimateMenu(first, player).build().getInventory());
}
}
case 86, 87 -> open1(player, crate);
case 88, 89 -> open10(player, crate);
Expand Down Expand Up @@ -348,14 +361,13 @@ private void setItemsPlayerInv() {
playerInventory.setItem(2, UltimateMenuStuff.SHOP.getStack());

if (totalPageAmount > 1) {
if (currentPage > 0) {
if (currentPage == 0) {
playerInventory.setItem(4, UltimateMenuStuff.FORWARD.getStack());
} else if (currentPage < totalPageAmount - 1) {
playerInventory.setItem(3, UltimateMenuStuff.BACK_ITEM.getStack());
playerInventory.setItem(30, UltimateMenuStuff.ARROW_LEFT.getStack());
}

if (currentPage < totalPageAmount - 1) {
playerInventory.setItem(4, UltimateMenuStuff.FORWARD.getStack());
playerInventory.setItem(31, UltimateMenuStuff.ARROW_RIGHT.getStack());
} else if (currentPage == totalPageAmount - 1) {
playerInventory.setItem(3, UltimateMenuStuff.BACK_ITEM.getStack());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

public class ItemManager {
private final DatabaseConnection connection;
private final Map<Integer, ItemStack> allItems = new HashMap<>();
private final Map<Integer, ItemStack> shopItems = new HashMap<>();
private final Map<Table, Map<Integer, ItemStack>> items = new HashMap<>();

/**
* Constructor for the ItemManager class.
Expand All @@ -24,8 +23,9 @@ public class ItemManager {
*/
public ItemManager(DatabaseConnection connection) {
this.connection = connection;
allItems.putAll(getAllItems(Table.ALL_ITEMS));
shopItems.putAll(getAllItems(Table.SHOP_ITEMS));
for (Table table : Table.values()) {
items.put(table, getAllItems(table));
}
}

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ public ItemStack getItemFromCache(int id, Table table) {
}

public Map<Integer, ItemStack> getAllItemsFromCache(Table table) {
return table.equals(Table.ALL_ITEMS) ? allItems : shopItems;
return items.get(table);
}

/**
Expand Down Expand Up @@ -94,11 +94,7 @@ public int addItem(ItemStack item, Table table) {

if (id == -1) return id;

if (table.equals(Table.ALL_ITEMS)) {
allItems.put(id, item);
} else {
shopItems.put(id, item);
}
getAllItemsFromCache(table).put(id, item);

return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

import com.badbones69.crazycrates.api.objects.gacha.enums.CurrencyType;
import com.badbones69.crazycrates.api.objects.gacha.enums.ShopID;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public record ShopData(ShopID shopID,
CurrencyType currencyType,
String shopName,
List<ShopItem> items) {
List<ShopItem> items)
implements Comparable<ShopData> {

@Override
public String toString() {
return "ShopData{" +
"shopID=" + shopID +
", currencyType=" + currencyType +
", shopName='" + shopName + '\'' +
", items=" + items +
'}';
}

@Override
public int compareTo(@NotNull ShopData o) {
return shopID.compareTo(o.shopID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ public record ShopItem(ItemStack stack,
int limit,
int place)
implements Comparable<ShopItem> {

@Override
public int compareTo(@NotNull ShopItem o) {
return Integer.compare(place, o.place);
}

@Override
public String toString() {
return "ShopItem{" +
"stack=" + stack +
", price=" + price +
", limit=" + limit +
", place=" + place +
'}';
}
}
Loading

0 comments on commit 94d3c11

Please sign in to comment.