Skip to content

Commit

Permalink
Merge pull request #2 from Shweit/dvdb/vote-command
Browse files Browse the repository at this point in the history
Added `/vote <poll_id>` command
  • Loading branch information
Shweit authored Sep 3, 2024
2 parents eeb7a79 + 98d8685 commit 6ecee24
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/shweit/poll/Poll.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/shweit/poll/commands/VoteCommand.java
Original file line number Diff line number Diff line change
@@ -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<String> onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] args) {
if (args.length == 1) {
List<Map<String, String>> openPolls = getOpenPolls();
List<String> pollIds = new ArrayList<>();

for (Map<String, String> poll : openPolls) {
pollIds.add(poll.get("id"));
}

return pollIds;
}

return new ArrayList<>();
}

private List<Map<String, String>> getOpenPolls() {
List<Map<String, String>> 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<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ commands:
description: Creates a new poll with a question and up to 10 answers.
usage: /createpoll "<question>" "<answer1>" "<answer2>" ... [--multi]
permission: poll.create

vote:
description: Open a poll to vote on.
usage: /vote <poll_id>
permission: poll.vote

0 comments on commit 6ecee24

Please sign in to comment.