Search in sources :

Example 1 with SubjectVote

use of com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote in project buildinggame by stefvanschie.

the class SubjectMenu method initializePages.

/**
 * Initializes the pages for this gui
 *
 * @param gui the gui for which to initialize pages
 * @param paginatedPane the paginated pane
 * @since 5.6.0
 */
private void initializePages(@NotNull ChestGui gui, @NotNull PaginatedPane paginatedPane) {
    paginatedPane.clear();
    for (var page = 0; page < Math.ceil((float) subjects.size() / (paginatedPane.getHeight() * paginatedPane.getLength())); page++) {
        var pane = new OutlinePane(0, 0, paginatedPane.getLength(), paginatedPane.getHeight());
        pane.setOrientation(Orientable.Orientation.valueOf(CONFIG.getString("subject-gui.vote-items.orientation").toUpperCase(Locale.getDefault())));
        for (var index = 0; index < paginatedPane.getLength() * paginatedPane.getHeight(); index++) {
            if (subjects.size() - 1 < index + (page * paginatedPane.getLength() * paginatedPane.getHeight()))
                break;
            final String subject = ChatColor.stripColor(subjects.get(index + (page * paginatedPane.getLength() * paginatedPane.getHeight())));
            if (getSubjectVote(subject) == null)
                votes.add(new SubjectVote(subject, 0));
            Material material = SettingsManager.getInstance().getMaterial("subject-gui.vote-items.item.id", Material.BARRIER);
            var item = new ItemStack(material);
            var meta = item.getItemMeta();
            meta.setDisplayName(MessageManager.translate(MESSAGES.getString("subject-gui.subject.name").replace("%subject%", subject)));
            var lores = new ArrayList<String>();
            MESSAGES.getStringList("subject-gui.subject.lores").forEach(lore -> lores.add(MessageManager.translate(lore.replace("%votes%", getSubjectVote(subject).getVotes() + ""))));
            meta.setLore(lores);
            item.setItemMeta(meta);
            pane.addItem(new GuiItem(item, event -> {
                addVote((Player) event.getWhoClicked(), subject);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        initializePages(gui, paginatedPane);
                        update();
                    }
                }.runTaskLater(Main.getInstance(), 1L);
                event.setCancelled(true);
            }));
            if (CONFIG.getBoolean("subject-gui.percentage-bars.enable")) {
                int x;
                int y;
                if (pane.getOrientation() == Orientable.Orientation.HORIZONTAL) {
                    x = index % pane.getLength();
                    y = (int) Math.floor((double) index / pane.getLength());
                } else if (pane.getOrientation() == Orientable.Orientation.VERTICAL) {
                    x = (int) Math.floor((double) index / pane.getHeight());
                    y = index % pane.getHeight();
                } else {
                    throw new UnsupportedOperationException("Unknown orientation found");
                }
                int xOffset = CONFIG.getInt("subject-gui.percentage-bars.offset.x");
                int yOffset = CONFIG.getInt("subject-gui.percentage-bars.offset.y");
                int totalVotes = votes.stream().mapToInt(SubjectVote::getVotes).sum();
                int userVotes = getSubjectVote(subject).getVotes();
                var percentageBar = new PercentageBar(x + xOffset, y + yOffset, 7, 1);
                percentageBar.setPercentage(totalVotes == 0 ? 0 : (float) userVotes / totalVotes);
                gui.addPane(percentageBar);
            }
        }
        paginatedPane.addPane(page, pane);
    }
}
Also used : SubjectVote(com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote) HumanEntity(org.bukkit.entity.HumanEntity) OutlinePane(com.github.stefvanschie.inventoryframework.pane.OutlinePane) java.util(java.util) GuiItem(com.github.stefvanschie.inventoryframework.gui.GuiItem) ChestGui(com.github.stefvanschie.inventoryframework.gui.type.ChestGui) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MessageManager(com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager) Orientable(com.github.stefvanschie.inventoryframework.pane.Orientable) SettingsManager(com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager) Player(org.bukkit.entity.Player) SubjectVote(com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote) ItemStack(org.bukkit.inventory.ItemStack) Nullable(org.jetbrains.annotations.Nullable) Contract(org.jetbrains.annotations.Contract) Main(com.gmail.stefvanschiedev.buildinggame.Main) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PercentageBar(com.github.stefvanschie.inventoryframework.pane.component.PercentageBar) ChatColor(org.bukkit.ChatColor) NotNull(org.jetbrains.annotations.NotNull) Material(org.bukkit.Material) PaginatedPane(com.github.stefvanschie.inventoryframework.pane.PaginatedPane) GuiItem(com.github.stefvanschie.inventoryframework.gui.GuiItem) Player(org.bukkit.entity.Player) PercentageBar(com.github.stefvanschie.inventoryframework.pane.component.PercentageBar) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Material(org.bukkit.Material) OutlinePane(com.github.stefvanschie.inventoryframework.pane.OutlinePane) ItemStack(org.bukkit.inventory.ItemStack)

Example 2 with SubjectVote

use of com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote in project buildinggame by stefvanschie.

the class SubjectMenu method getHighestVote.

/**
 * Returns the theme that received the most votes. If there are multiple themes with the highest amount of votes,
 * one of those will be picked at random.
 *
 * @return the theme that received the highest amount of votes
 */
@Nullable
@Contract(pure = true)
public String getHighestVote() {
    if (forcedTheme != null)
        return forcedTheme;
    int highest = -1;
    for (SubjectVote subjectVote : votes) {
        if (subjectVote.getVotes() > highest)
            highest = subjectVote.getVotes();
    }
    List<String> subjects = new ArrayList<>();
    for (SubjectVote subjectVote : votes) {
        if (subjectVote.getVotes() == highest)
            subjects.add(subjectVote.getSubject());
    }
    return subjects.get(ThreadLocalRandom.current().nextInt(subjects.size()));
}
Also used : SubjectVote(com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

SubjectVote (com.gmail.stefvanschiedev.buildinggame.utils.SubjectVote)2 Contract (org.jetbrains.annotations.Contract)2 Nullable (org.jetbrains.annotations.Nullable)2 GuiItem (com.github.stefvanschie.inventoryframework.gui.GuiItem)1 ChestGui (com.github.stefvanschie.inventoryframework.gui.type.ChestGui)1 Orientable (com.github.stefvanschie.inventoryframework.pane.Orientable)1 OutlinePane (com.github.stefvanschie.inventoryframework.pane.OutlinePane)1 PaginatedPane (com.github.stefvanschie.inventoryframework.pane.PaginatedPane)1 PercentageBar (com.github.stefvanschie.inventoryframework.pane.component.PercentageBar)1 Main (com.gmail.stefvanschiedev.buildinggame.Main)1 SettingsManager (com.gmail.stefvanschiedev.buildinggame.managers.files.SettingsManager)1 MessageManager (com.gmail.stefvanschiedev.buildinggame.managers.messages.MessageManager)1 java.util (java.util)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 ChatColor (org.bukkit.ChatColor)1 Material (org.bukkit.Material)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1