From 3cd7ff8e8e4e600d9e2334bd3c928e323366a572 Mon Sep 17 00:00:00 2001 From: FirephoenixX02 Date: Sat, 4 May 2024 14:36:24 +0200 Subject: [PATCH] Add report listing in gui --- .../commands/ReportGUICommand.java | 70 +++++++++++++++++-- .../rapidreport/utils/DataBaseManager.java | 5 +- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/firephoenix/rapidreport/commands/ReportGUICommand.java b/src/main/java/me/firephoenix/rapidreport/commands/ReportGUICommand.java index 94c11db..5b5760e 100644 --- a/src/main/java/me/firephoenix/rapidreport/commands/ReportGUICommand.java +++ b/src/main/java/me/firephoenix/rapidreport/commands/ReportGUICommand.java @@ -3,12 +3,23 @@ package me.firephoenix.rapidreport.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.Player; +import dev.simplix.protocolize.api.ClickType; import dev.simplix.protocolize.api.Protocolize; import dev.simplix.protocolize.api.chat.ChatElement; import dev.simplix.protocolize.api.inventory.Inventory; +import dev.simplix.protocolize.api.item.BaseItemStack; +import dev.simplix.protocolize.api.item.ItemStack; import dev.simplix.protocolize.api.player.ProtocolizePlayer; +import dev.simplix.protocolize.data.ItemType; import dev.simplix.protocolize.data.inventory.InventoryType; import me.firephoenix.rapidreport.RapidReport; +import me.firephoenix.rapidreport.utils.Report; + +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; /** @@ -26,13 +37,64 @@ public class ReportGUICommand implements SimpleCommand { return; } + // Fetch all unresolved reports from the database + CompletableFuture future = RapidReport.INSTANCE.getDataBaseManager().getSQLStatementResultAsync( + "SELECT * FROM rapid_report_reports WHERE status = 'Unresolved' LIMIT 36"); - Inventory inventory = new Inventory(InventoryType.GENERIC_9X4); - inventory.title(ChatElement.ofLegacyText("§9Inventory")); + future.thenAccept(result -> { + ArrayList reports = new ArrayList<>(); + try { + while (result.next()) { + reports.add(new Report(result.getString("reporterName"), result.getString("reportedName"), + UUID.fromString(result.getString("reportedUUID")), result.getString("reason"), + result.getString("status"))); + } + result.close(); + } catch (Exception e) { + e.printStackTrace(); + } - ProtocolizePlayer protocolizePlayer = Protocolize.playerProvider().player(((Player) commandSource).getUniqueId()); - protocolizePlayer.openInventory(inventory); + Inventory inventory = new Inventory(InventoryType.GENERIC_9X6); + inventory.title(ChatElement.ofLegacyText("§cReports")); + + //Top Row of Glass + for (int i = 0; i < 9; i++) { + inventory.item(i, new ItemStack(ItemType.GRAY_STAINED_GLASS_PANE)); + } + + // Populate the inventory with reports once they are fetched + int slot = 10; + for (Report report : reports) { + if (slot > 43) break; + List> lore = new ArrayList<>(); + lore.add(ChatElement.ofLegacyText("§cReported by: §7" + report.reporterPlayerName)); + lore.add(ChatElement.ofLegacyText("§cReason: §7" + report.reason)); + lore.add(ChatElement.ofLegacyText("§cStatus: §7" + report.status)); + + ItemStack reportItem = new ItemStack(ItemType.PAPER); + + reportItem.displayName(ChatElement.ofLegacyText("§c" + report.reportedPlayerName)); + reportItem.lore(lore); + + + + inventory.item(slot, reportItem); + slot++; + } + + // Bottom Row of Glass + for (int i = 45; i < 54; i++) { + inventory.item(i, new ItemStack(ItemType.GRAY_STAINED_GLASS_PANE)); + } + + // Open the inventory for the player + ProtocolizePlayer protocolizePlayer = Protocolize.playerProvider().player(((Player) commandSource).getUniqueId()); + protocolizePlayer.openInventory(inventory); + }).exceptionally(ex -> { + commandSource.sendRichMessage(RapidReport.INSTANCE.getChatPrefix() + "Error fetching reports."); + return null; + }); } @Override diff --git a/src/main/java/me/firephoenix/rapidreport/utils/DataBaseManager.java b/src/main/java/me/firephoenix/rapidreport/utils/DataBaseManager.java index 03c8bb9..36c6625 100644 --- a/src/main/java/me/firephoenix/rapidreport/utils/DataBaseManager.java +++ b/src/main/java/me/firephoenix/rapidreport/utils/DataBaseManager.java @@ -66,11 +66,8 @@ public class DataBaseManager { public void runStatementAsync(String statement) { RapidReport.INSTANCE.proxy.getScheduler().buildTask(RapidReport.INSTANCE, () -> { - try { - Connection connection = hikariCP.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(statement); + try (Connection connection = hikariCP.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(statement)) { preparedStatement.execute(); - preparedStatement.close(); } catch (SQLException e) { throw new RuntimeException(e); }