Skip to content

Commit

Permalink
base for shop
Browse files Browse the repository at this point in the history
  • Loading branch information
dan28000 committed Jul 8, 2024
1 parent d61a3df commit ab089d5
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.badbones69.crazycrates.api.builders.types.items;

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.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;
}

public ShopMenu(ShopMenu menu, int page) {
this(menu.getCrate(), menu.getPlayer(), ComponentBuilder.trans(menu.getPlayer(), names.get(page)), page);
}

public ShopMenu(Crate crate, Player player) {
this(crate, player, ComponentBuilder.trans(player, names.get(0)), 0);
}

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

@Override
public void run(InventoryClickEvent e) {
if (!(e.getInventory().getHolder() instanceof ShopMenu shopMenu)) return;

e.setCancelled(true);
int slot = e.getRawSlot();
if (slot < 0) return;

Player player = shopMenu.getPlayer();

int newCrateNum;

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

if (newCrateNum != -1) {
player.playSound(UltimateMenuStuff.CLICK);
player.openInventory(new ShopMenu(shopMenu, newCrateNum).build().getInventory());
}
}

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ public void run(InventoryClickEvent e) {
int slot = e.getRawSlot();
if (slot < 0) return;

System.out.println("Slot: " + slot);

CrazyCrates plugin = ultimateMenu.plugin;
Player player = ultimateMenu.getPlayer();
Crate crate = ultimateMenu.getCrate();
Expand Down Expand Up @@ -128,7 +126,9 @@ public void run(InventoryClickEvent e) {
plugin.getInventoryManager().openNewCratePreview(player, crate);
}
case 83 -> {
//TODO: Open shop
player.playSound(UltimateMenuStuff.CLICK);
close(player);
player.openInventory(new ShopMenu(crate, player).build().getInventory());
}
case 84 -> {
//TODO: handle page back
Expand All @@ -145,6 +145,7 @@ public void run(InventoryClickEvent e) {
close(player);
player.setSneaking(true);
open(ultimateMenu, player, crate, crateManager, 10);
player.setSneaking(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ public static Component trans(Player player, CrateSettings crateSettings) {
return builder.build();
}

public static Component trans(Player player, String name) {
TextComponent.Builder builder = text();

PlayerBaseProfile baseProfile = plugin.getBaseProfileManager().getPlayerBaseProfile(player.getName());

int spaceSize = getSize(name) + 62;
builder.append(text(name));
builder.append(translatable("space.-" + spaceSize, "").font(KEY));

String mystic = String.valueOf(baseProfile.getMysticTokens());
String stellar = String.valueOf(baseProfile.getStellarShards());
String vote = String.valueOf(baseProfile.getVoteTokens());

int mysticSpace = mystic.length() * 6;
int stellarSpace = stellar.length() * 6;
int virtualSpace = vote.length() * 6;

appendChars(builder, mystic, NumberType.TOP);

down(builder, stellar, mysticSpace, FILL_TOP, SPACE_BACK);

down(builder, vote, stellarSpace, FILL_DOWN, SPACE_PAGE);

while (virtualSpace < 45) {
builder.append(FILL_DOWN);
virtualSpace += 3;
}

return builder.build();
}

private static void pity(TextComponent.Builder builder, PlayerProfile playerProfile, Rarity rarity, RaritySettings settings) {
Pair<Integer, ResultType> result = playerProfile.getPity(rarity);
int currentPity = result.first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ public class UltimateMenuStuff {
.setDisplayName("<white><b>10x")
.addDisplayLore("<gray>Kliknutím otevřeš truhlu 10x");

private static ItemBuilder getItem(Material material, int modelData) {
return new ItemBuilder(material).setCustomModelData(modelData).setDisplayName("<white>");
}
public static final ItemBuilder SHOP_BANNER = getItem(Material.LIGHT_GRAY_STAINED_GLASS_PANE, 1000007);
public static final ItemBuilder SHOP_SELECTED = getItem(Material.GREEN_STAINED_GLASS_PANE, 1000007)
.addDisplayLore("<gray>Právě vybraný obchod");
public static final ItemBuilder SHOP_UNSELECTED = getItem(Material.RED_STAINED_GLASS_PANE, 1000007)
.addDisplayLore("<gray>Kliknutím vyberete tento obchod");

public static final Sound CLICK = Sound.sound(Key.key("crate", "click"), Sound.Source.MASTER, 1f, 1f);
public static final Sound BACK = Sound.sound(Key.key("crate", "back"), Sound.Source.MASTER, 1f, 1f);
public static final Sound ERROR = Sound.sound(Key.key("crate", "error"), Sound.Source.MASTER, 1f, 1f);
public static final Sound CRATE = Sound.sound(Key.key("crate", "crate"), Sound.Source.MASTER, 1f, 1f);
public static final Sound OPEN = Sound.sound(Key.key("crate", "open"), Sound.Source.MASTER, 1f, 1f);

private static ItemBuilder getItem(Material material, int modelData) {
return new ItemBuilder(material).setCustomModelData(modelData).setDisplayName("<white>");
}

}

0 comments on commit ab089d5

Please sign in to comment.