Skip to content

Commit

Permalink
Added Poll Voting
Browse files Browse the repository at this point in the history
  • Loading branch information
Shweit committed Sep 3, 2024
1 parent 96609d5 commit dd08795
Show file tree
Hide file tree
Showing 12 changed files with 968 additions and 22 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions src/main/java/com/shweit/poll/Poll.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.shweit.poll;

import com.shweit.poll.commands.CreatePollCommand;
import com.shweit.poll.commands.pollDetailsCommand.PollDetailGuiListener;
import com.shweit.poll.commands.pollsCommand.PollsCommand;
import com.shweit.poll.commands.pollsCommand.PollsGuiListener;
import com.shweit.poll.utils.ConnectionManager;
import com.shweit.poll.utils.Logger;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -10,22 +14,26 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public final class Poll extends JavaPlugin {

public static Connection connection;
public static FileConfiguration config;
private static Poll instance;

@Override
public void onEnable() {
createConfig();
config = getConfig();
instance = this;

setupDatabase();
getCommand("createpoll").setExecutor(new CreatePollCommand());
getCommand("polls").setExecutor(new PollsCommand());
getServer().getPluginManager().registerEvents(new PollsGuiListener(), this);
getServer().getPluginManager().registerEvents(new PollDetailGuiListener(), this);
}

@Override
Expand All @@ -36,33 +44,34 @@ public void onDisable() {
}
} catch (SQLException e) {
Logger.error(e.toString());
e.printStackTrace();
}
}

public static Poll getInstance() {
return instance;
}

private void setupDatabase() {
// Überprüfen, ob die Datei polls.sqlite existiert
File dbFile = new File(getDataFolder(), "polls.sqlite");

if (!dbFile.exists()) {
// Wenn sie nicht existiert, erstelle sie
try {
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}

connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getPath());
Logger.debug("Connected to new polls.sqlite database.");
Connection connection = new ConnectionManager().getConnection();

// Lade und führe das SQL-Skript aus
executeSqlScript(connection, "schema.sql");
executeSqlScript(connection, "sql/polls_table.sql");
executeSqlScript(connection, "sql/votes_table.sql");

} catch (SQLException e) {
e.printStackTrace();
}
} else {
// Wenn die Datei existiert, verbinde mit der Datenbank
try {
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getPath());
Connection connection = new ConnectionManager().getConnection();
Logger.debug("Connected to existing polls.sqlite database.");
} catch (SQLException e) {
Logger.error(e.toString());
Expand All @@ -72,7 +81,6 @@ private void setupDatabase() {

private void executeSqlScript(Connection connection, String fileName) {
try {
// Lade die SQL-Datei aus dem Ressourcen-Ordner
InputStream is = getResource(fileName);
if (is == null) {
Logger.error("SQL file not found: " + fileName);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/shweit/poll/commands/CreatePollCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.shweit.poll.commands;

import com.shweit.poll.Poll;
import com.shweit.poll.utils.Logger;
import org.bukkit.ChatColor;
import com.shweit.poll.utils.ConnectionManager;
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;
Expand All @@ -16,17 +16,16 @@
import java.util.List;
import java.util.UUID;

public class CreatePollCommand implements CommandExecutor, TabExecutor
{
public class CreatePollCommand implements CommandExecutor, TabExecutor {
private final Gson gson = new Gson();

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by players.");
return true;
}

Player player = (Player) sender;

if (args.length < 3) {
player.sendMessage("Usage: /createpoll \"<question>\" \"<answer1>\" \"<answer2>\" ... [--multi]");
return false;
Expand Down Expand Up @@ -90,18 +89,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

private String convertListToJson(List<String> list) {
return "[" + String.join(",", list) + "]";
return gson.toJson(list);
}

private void savePollToDatabase(UUID uniqueId, String string, String answersAsJsonString, boolean multi) throws SQLException {
Connection connection = Poll.connection;
String insertPollQuery = "INSERT INTO polls (uuid, question, answers, allowMultiple) VALUES (?, ?, ?, ?)";
private void savePollToDatabase(UUID uniqueId, String question, String answersAsJsonString, boolean multi) throws SQLException {
Connection connection = new ConnectionManager().getConnection();
String insertPollQuery = "INSERT INTO polls (uuid, question, answers, allowMultiple, isOpen) VALUES (?, ?, ?, ?, ?)";

try (PreparedStatement preparedStatement = connection.prepareStatement(insertPollQuery)) {
preparedStatement.setString(1, uniqueId.toString());
preparedStatement.setString(2, string);
preparedStatement.setString(2, question);
preparedStatement.setString(3, answersAsJsonString);
preparedStatement.setBoolean(4, multi);
preparedStatement.setBoolean(5, true);
preparedStatement.executeUpdate();
}
}
Expand Down
Loading

0 comments on commit dd08795

Please sign in to comment.