diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/DynamicInventoryBuilder.java b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/DynamicInventoryBuilder.java new file mode 100644 index 0000000..13d33dd --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/DynamicInventoryBuilder.java @@ -0,0 +1,134 @@ +package com.badbones69.crazyauctions.api.builders.gui; + +import com.badbones69.crazyauctions.api.builders.ItemBuilder; +import com.badbones69.crazyauctions.configs.impl.gui.AuctionKeys; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.GuiItem; +import com.ryderbelserion.vital.paper.api.builders.gui.types.PaginatedGui; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; +import java.util.function.Consumer; + +public abstract class DynamicInventoryBuilder extends InventoryBuilder { + + private final PaginatedGui gui; + + public DynamicInventoryBuilder(final Player player, final String title, final int rows) { + super(player); + + this.gui = Gui.paginated().setTitle(title).setRows(rows).disableInteractions().create(); + } + + /** + * Opens the {@link PaginatedGui}. + */ + public void open() { + open(null); + } + + /** + * Opens the {@link PaginatedGui}. + * + * @param consumer {@link Consumer(DynamicInventoryBuilder)} + */ + public void open(@Nullable final Consumer consumer) { + if (consumer != null) { + consumer.accept(this); + } + } + + /** + * Gets the {@link Player}. + * + * @return {@link Player} + */ + public final Player getPlayer() { + return this.player; + } + + /** + * Gets the {@link PaginatedGui}. + * + * @return {@link PaginatedGui} + */ + public final PaginatedGui getGui() { + return this.gui; + } + + // Adds the back button + public void setBackButton(final int row, final int column) { + if (this.gui.getCurrentPageNumber() <= 1) { + return; + } + + this.gui.setItem(row, column, new ItemBuilder().withType(Material.ARROW).asGuiItem(event -> { + event.setCancelled(true); + + this.gui.previous(); + + final int page = this.gui.getCurrentPageNumber(); + + if (page <= 1) { + this.gui.removeItem(row, column); + + this.gui.setItem(row, column, new GuiItem(Material.BLACK_STAINED_GLASS_PANE)); + } else { + setBackButton(row, column); + } + + if (page < this.gui.getMaxPages()) { + setNextButton(6, 6); + } + })); + } + + // Adds the next button + public void setNextButton(final int row, final int column) { + if (this.gui.getCurrentPageNumber() >= this.gui.getMaxPages()) { + return; + } + + this.gui.setItem(row, column, new ItemBuilder().withType(Material.ARROW).asGuiItem(event -> { + event.setCancelled(true); + + this.gui.next(); + + final int page = this.gui.getCurrentPageNumber(); + + if (page >= this.gui.getMaxPages()) { + this.gui.removeItem(row, column); + + this.gui.setItem(row, column, new GuiItem(Material.BLACK_STAINED_GLASS_PANE)); + } else { + setNextButton(row, column); + } + + if (page <= 1) { + this.gui.removeItem(6, 4); + + this.gui.setItem(6, 4, new GuiItem(Material.BLACK_STAINED_GLASS_PANE)); + } else { + setBackButton(6, 4); + } + })); + } + + /** + * Sets the bidding item + */ + public void setBiddingItem() { + this.auctions.getProperty(AuctionKeys.bidding_item_button).setItem(event -> { + setSellingItem(); + }, this.gui); + } + + /** + * Sets the selling item + */ + public void setSellingItem() { + this.auctions.getProperty(AuctionKeys.selling_item_button).setItem(event -> { + setBiddingItem(); + }, this.gui); + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/InventoryBuilder.java b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/InventoryBuilder.java new file mode 100644 index 0000000..fdd9eaa --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/InventoryBuilder.java @@ -0,0 +1,32 @@ +package com.badbones69.crazyauctions.api.builders.gui; + +import ch.jalu.configme.SettingsManager; +import com.badbones69.crazyauctions.CrazyAuctions; +import com.badbones69.crazyauctions.api.CrazyManager; +import com.badbones69.crazyauctions.configs.ConfigManager; +import com.badbones69.crazyauctions.configs.enums.Files; +import com.ryderbelserion.vital.paper.api.enums.Support; +import me.clip.placeholderapi.PlaceholderAPI; +import org.bukkit.Server; +import org.bukkit.entity.Player; + +public abstract class InventoryBuilder { + + protected final CrazyAuctions plugin = CrazyAuctions.getPlugin(); + protected final CrazyManager crazyManager = this.plugin.getCrazyManager(); + protected final Server server = this.plugin.getServer(); + + protected final SettingsManager config = ConfigManager.getConfig(); + + protected final SettingsManager auctions = ConfigManager.getCustomConfig(Files.auctions.getFileName()); + + protected final Player player; + + public InventoryBuilder(final Player player) { + this.player = player; + } + + public final String parse(final Player player, final String title) { + return Support.placeholder_api.isEnabled() ? PlaceholderAPI.setPlaceholders(player, title) : title; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/StaticInventoryBuilder.java b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/StaticInventoryBuilder.java new file mode 100644 index 0000000..d16c5a1 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/gui/StaticInventoryBuilder.java @@ -0,0 +1,65 @@ +package com.badbones69.crazyauctions.api.builders.gui; + +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui; +import org.bukkit.entity.Player; + +public abstract class StaticInventoryBuilder extends InventoryBuilder { + + private final Player player; + + private final Gui gui; + + /** + * Builds an inventory with a set title/rows + * + * @param player {@link Player} + * @param title {@link String} + * @param rows {@link Integer} + */ + public StaticInventoryBuilder(final Player player, final String title, final int rows) { + super(player); + + this.gui = Gui.gui().setTitle(parse(player, title)).setRows(rows).disableInteractions().create(); + + this.player = player; + } + + public abstract void open(); + + /** + * Gets the {@link Player}. + * + * @return {@link Player} + */ + public final Player getPlayer() { + return this.player; + } + + /** + * Gets the title of the gui. + * + * @return the title of the gui + */ + public final String getTitle() { + return this.gui.getTitle(); + } + + /** + * Checks if the title contains a message. + * + * @param message the message to check + * @return true or false + */ + public final boolean contains(final String message) { + return getTitle().contains(message); + } + + /** + * Gets the {@link Gui}. + * + * @return {@link Gui} + */ + public final Gui getGui() { + return this.gui; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/api/builders/types/AuctionMenu.java b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/types/AuctionMenu.java new file mode 100644 index 0000000..3a9aef1 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazyauctions/api/builders/types/AuctionMenu.java @@ -0,0 +1,53 @@ +package com.badbones69.crazyauctions.api.builders.types; + +import com.badbones69.crazyauctions.api.builders.gui.DynamicInventoryBuilder; +import com.badbones69.crazyauctions.api.enums.misc.Files; +import com.badbones69.crazyauctions.configs.impl.ConfigKeys; +import com.badbones69.crazyauctions.configs.impl.gui.AuctionKeys; +import com.badbones69.crazyauctions.utils.AuctionUtils; +import com.badbones69.crazyauctions.utils.MiscUtils; +import com.ryderbelserion.vital.paper.api.builders.gui.types.PaginatedGui; +import net.kyori.adventure.sound.Sound; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +public class AuctionMenu extends DynamicInventoryBuilder { + + public AuctionMenu(final Player player, final String title, final int rows) { + super(player, title, rows); + } + + private final Player player = getPlayer(); + private final PaginatedGui gui = getGui(); + + @Override + public void open() { // put the category option here maybe? + if (this.config.getProperty(ConfigKeys.category_page_opens_first)) { + //todo() open gui with specific category. + + return; + } + + this.gui.setDefaultTopClickAction(event -> MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER)); + + final YamlConfiguration configuration = Files.data.getConfiguration(); + + AuctionUtils.getItems(configuration.getConfigurationSection("Items")).forEach(itemStack -> { + this.gui.addItem(itemStack.asGuiItem(event -> { + + })); + }); + + this.auctions.getProperty(AuctionKeys.expired_item_button).setItem(event -> { + }, this.gui); + + // add selling item + setSellingItem(); + + this.auctions.getProperty(AuctionKeys.sold_item_button).setItem(event -> { + + }, this.gui); + + this.gui.open(this.player); + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/BaseCommand.java b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/BaseCommand.java index 45c8560..86172f2 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/BaseCommand.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/commands/v2/BaseCommand.java @@ -1,21 +1,12 @@ package com.badbones69.crazyauctions.commands.v2; -import com.badbones69.crazyauctions.api.enums.misc.Files; +import com.badbones69.crazyauctions.api.builders.types.AuctionMenu; import com.badbones69.crazyauctions.api.enums.other.Permissions; -import com.badbones69.crazyauctions.configs.impl.ConfigKeys; import com.badbones69.crazyauctions.configs.impl.gui.AuctionKeys; -import com.badbones69.crazyauctions.utils.AuctionUtils; -import com.badbones69.crazyauctions.utils.MiscUtils; import com.mojang.brigadier.tree.LiteralCommandNode; -import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui; -import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.GuiItem; -import com.ryderbelserion.vital.paper.api.builders.gui.types.PaginatedGui; import com.ryderbelserion.vital.paper.commands.context.PaperCommandInfo; import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.Commands; -import net.kyori.adventure.sound.Sound; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public class BaseCommand extends AbstractCommand { @@ -28,50 +19,7 @@ public class BaseCommand extends AbstractCommand { return; } - final Player player = info.getPlayer(); - - if (this.config.getProperty(ConfigKeys.category_page_opens_first)) { - //todo() open gui with specific category. - - return; - } - - final @NotNull PaginatedGui builder = Gui.paginated() - .pageSize(54) - .setRows(6) - .disableInteractions() - .setTitle(this.auctions.getProperty(AuctionKeys.gui_name)).create(); - - final YamlConfiguration configuration = Files.data.getConfiguration(); - - AuctionUtils.getItems(configuration.getConfigurationSection("Items")).forEach(itemStack -> { - builder.addItem(new GuiItem(itemStack, consumer -> { - - MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER); - })); - }); - - this.auctions.getProperty(AuctionKeys.expired_item_button).setItem(consumer -> { - - MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER); - }, builder); - - this.auctions.getProperty(AuctionKeys.selling_item_button).setItem(consumer -> { - - MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER); - }, builder); - - this.auctions.getProperty(AuctionKeys.bidding_item_button).setItem(consumer -> { - - MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER); - }, builder); - - this.auctions.getProperty(AuctionKeys.sold_item_button).setItem(consumer -> { - - MiscUtils.play(player, player.getLocation(), this.config.getProperty(ConfigKeys.click_item_sound), Sound.Source.PLAYER); - }, builder); - - builder.open(player); + new AuctionMenu(info.getPlayer(), this.auctions.getProperty(AuctionKeys.gui_name), 6).open(); } @Override diff --git a/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/ButtonProperty.java b/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/ButtonProperty.java index bd40f9c..aad245d 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/ButtonProperty.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/configs/beans/ButtonProperty.java @@ -9,7 +9,6 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; -import java.util.function.Consumer; public class ButtonProperty { @@ -28,36 +27,36 @@ public class ButtonProperty { public final ButtonProperty populate(final String buttonName) { switch (buttonName) { case "expired-item" -> { - this.displayName = "Expired Items"; + this.displayName = "(!) Expired Items"; this.displayLore = List.of( - "(!) Click here to view, and collect all items that have expired." + "Click to view items that have expired." ); this.material = "poisonous_potato"; this.slot = 46; } case "selling-item" -> { - this.displayName = "Currently looking at items being sold"; + this.displayName = "(!) Items that are being sold"; this.displayLore = List.of( - "(!) Click here to see items that you can bid on." + "Click to see items to bid on." ); this.material = "emerald"; this.slot = 49; } case "bidding-item" -> { - this.displayName = "Currently looking at items that can be bid on"; + this.displayName = "(!) Items that can be bid on"; this.displayLore = List.of( - "(!) Click here to see items that you can buy at a price." + "Click to see items that can be bought." ); this.material = "magma_cream"; this.slot = 49; } case "sold-item" -> { - this.displayName = "Items you are currently selling"; + this.displayName = "(!) Items you are currently selling"; this.displayLore = List.of( - "(!) Click to view all the items you, currently up for auction." + "Click to view all items you have up for auction." ); this.material = "gold_ingot"; this.slot = 52; diff --git a/paper/src/main/java/com/badbones69/crazyauctions/utils/AuctionUtils.java b/paper/src/main/java/com/badbones69/crazyauctions/utils/AuctionUtils.java index 3d06c45..5f4dc85 100644 --- a/paper/src/main/java/com/badbones69/crazyauctions/utils/AuctionUtils.java +++ b/paper/src/main/java/com/badbones69/crazyauctions/utils/AuctionUtils.java @@ -1,22 +1,19 @@ package com.badbones69.crazyauctions.utils; -import com.ryderbelserion.vital.paper.util.ItemUtil; +import com.badbones69.crazyauctions.api.builders.ItemBuilder; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; public class AuctionUtils { - public static List getItems(final ConfigurationSection section) { + public static List getItems(final ConfigurationSection section) { if (section == null) return List.of(); - final List itemStacks = new ArrayList<>(); + final List itemStacks = new ArrayList<>(); for (final String key : section.getKeys(false)) { - final ItemStack builder = ItemUtil.fromBase64(section.getString(key + ".Item", "")); - - itemStacks.add(builder); + itemStacks.add(new ItemBuilder().fromBase64(section.getString(key + ".Item", ""))); } return itemStacks;