diff --git a/src/main/java/com/shweit/poll/Poll.java b/src/main/java/com/shweit/poll/Poll.java index 269c391..71957f9 100644 --- a/src/main/java/com/shweit/poll/Poll.java +++ b/src/main/java/com/shweit/poll/Poll.java @@ -1,6 +1,7 @@ package com.shweit.poll; import com.shweit.poll.commands.CreatePollCommand; +import com.shweit.poll.commands.VoteCommand; import com.shweit.poll.commands.pollDetailsCommand.PollDetailGuiListener; import com.shweit.poll.commands.pollsCommand.PollsCommand; import com.shweit.poll.commands.pollsCommand.PollsGuiListener; @@ -30,6 +31,7 @@ public void onEnable() { setupDatabase(); getCommand("createpoll").setExecutor(new CreatePollCommand()); getCommand("polls").setExecutor(new PollsCommand()); + getCommand("vote").setExecutor(new VoteCommand()); getServer().getPluginManager().registerEvents(new PollsGuiListener(), this); getServer().getPluginManager().registerEvents(new PollDetailGuiListener(), this); } diff --git a/src/main/java/com/shweit/poll/commands/VoteCommand.java b/src/main/java/com/shweit/poll/commands/VoteCommand.java new file mode 100644 index 0000000..8559bf9 --- /dev/null +++ b/src/main/java/com/shweit/poll/commands/VoteCommand.java @@ -0,0 +1,79 @@ +package com.shweit.poll.commands; + +import com.shweit.poll.commands.pollDetailsCommand.PollDetailsCommand; +import com.shweit.poll.utils.ConnectionManager; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import com.google.gson.Gson; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class VoteCommand implements CommandExecutor, TabExecutor { + private final Gson gson = new Gson(); + + @Override + public boolean onCommand(final CommandSender commandSender, final Command command, final String s, final String[] args) { + if (!(commandSender instanceof Player player)) { + commandSender.sendMessage(ChatColor.RED + "You must be a player to execute this command."); + return false; + } + + if (!player.hasPermission("polls.vote")) { + player.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return false; + } + + new PollDetailsCommand().openPollDetails(player, Integer.parseInt(args[0])); + return true; + } + + @Override + public List onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] args) { + if (args.length == 1) { + List> openPolls = getOpenPolls(); + List pollIds = new ArrayList<>(); + + for (Map poll : openPolls) { + pollIds.add(poll.get("id")); + } + + return pollIds; + } + + return new ArrayList<>(); + } + + private List> getOpenPolls() { + List> openPolls = new ArrayList<>(); + String query = "SELECT id, question, uuid, created_at FROM polls WHERE isOpen = 1"; + + try (Connection connection = new ConnectionManager().getConnection(); + PreparedStatement statement = connection.prepareStatement(query); + ResultSet results = statement.executeQuery()) { + + while (results.next()) { + Map pollData = new HashMap<>(); + pollData.put("id", String.valueOf(results.getInt("id"))); + pollData.put("question", results.getString("question")); + pollData.put("creator", results.getString("uuid")); + pollData.put("created_at", results.getString("created_at")); + openPolls.add(pollData); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return openPolls; + } +} diff --git a/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailGuiListener.java b/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailGuiListener.java index c4c6dc9..9386af8 100644 --- a/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailGuiListener.java +++ b/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailGuiListener.java @@ -62,18 +62,15 @@ public void onInventoryClick(final InventoryClickEvent event) { if (isSelectedAnswer(playerUUID, pollId, answer, connection)) { // Remove the answer if it is already selected removeVote(playerUUID, pollId, answer, connection); - player.sendMessage(ChatColor.RED + "You deselected: " + answer); } else if (allowsMultipleAnswers(pollId, connection)) { // Add a new answer if multiple answers are allowed addVote(playerUUID, pollId, answer, connection); - player.sendMessage(ChatColor.GREEN + "You selected: " + answer); } else { player.sendMessage(ChatColor.RED + "You have already voted. Multiple answers are not allowed."); } } else { // Add the answer if no answer has been selected yet addVote(playerUUID, pollId, answer, connection); - player.sendMessage(ChatColor.GREEN + "You selected: " + answer); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailsCommand.java b/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailsCommand.java index e53f68a..02753e3 100644 --- a/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailsCommand.java +++ b/src/main/java/com/shweit/poll/commands/pollDetailsCommand/PollDetailsCommand.java @@ -29,6 +29,10 @@ public final class PollDetailsCommand { * @param pollId The ID of the poll to display. */ public void openPollDetails(final Player player, final int pollId) { + if (!player.hasPermission("polls.vote")) { + player.sendMessage(ChatColor.RED + "You do not have permission to view poll details."); + } + Inventory pollDetailsInventory = Bukkit.createInventory(null, 54, ChatColor.BLUE + "Poll Details"); PollDetails pollDetails = getPollDetails(pollId); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d0b5f30..b62ba69 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -15,3 +15,8 @@ commands: description: Creates a new poll with a question and up to 10 answers. usage: /createpoll "" "" "" ... [--multi] permission: poll.create + + vote: + description: Open a poll to vote on. + usage: /vote + permission: poll.vote