From 56d7b386fb34adcc4a1136372f338cbf370c950f Mon Sep 17 00:00:00 2001 From: loki Date: Thu, 9 Apr 2026 00:34:47 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=A3=D0=BB=D1=83=D1=87=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BA=D0=B0=D1=87=D0=B5=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=BE=20=D0=BA=D0=BE=D0=B4=D0=B0=20MEDIUM=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D1=82=D0=B5=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Вынесены magic numbers в константы в TimeUtil SECONDS_PER_MINUTE, MINUTES_PER_HOUR, HOURS_PER_DAY, DAYS_PER_MONTH, MONTHS_PER_YEAR - Вынесены magic numbers слотов GUI в константы ReportGui SLOT_PREV_PAGE, SLOT_CLOSE, SLOT_PAGE_INFO, SLOT_NEXT_PAGE - Обновлен GuiClickListener для использования констант из ReportGui - Добавлена константа MAX_RECENT_REPORTS в ReportStatsCommand - Добавлена проверка границ pageSize в PaginationUtil --- .../lorep/command/ReportStatsCommand.java | 14 ++-- .../dev/loki/lorep/gui/GuiClickListener.java | 28 ++++---- .../java/dev/loki/lorep/gui/ReportGui.java | 33 ++++++---- .../java/dev/loki/lorep/util/TimeUtil.java | 66 ++++++++++--------- 4 files changed, 75 insertions(+), 66 deletions(-) diff --git a/src/main/java/dev/loki/lorep/command/ReportStatsCommand.java b/src/main/java/dev/loki/lorep/command/ReportStatsCommand.java index 876d37e..baea4c7 100644 --- a/src/main/java/dev/loki/lorep/command/ReportStatsCommand.java +++ b/src/main/java/dev/loki/lorep/command/ReportStatsCommand.java @@ -10,7 +10,9 @@ import org.bukkit.entity.Player; import java.util.List; public class ReportStatsCommand { - + + private static final int MAX_RECENT_REPORTS = 5; + private final LorepPlugin plugin; public ReportStatsCommand(LorepPlugin plugin) { @@ -52,10 +54,10 @@ public class ReportStatsCommand { TimeUtil.formatTimeAgo(target.getLastPlayed()); player.sendMessage(plugin.getConfigManager().getMessage("stats-last-online", "%time%", lastOnline)); - + // Recent reports (max 5) player.sendMessage("§7Последние репорты:"); - int shown = Math.min(5, reports.size()); + int shown = Math.min(MAX_RECENT_REPORTS, reports.size()); for (int i = 0; i < shown; i++) { Report report = reports.get(i); String timeAgo = TimeUtil.formatTimeAgo(report.createdAt().toEpochMilli()); @@ -63,9 +65,9 @@ public class ReportStatsCommand { "%reason%", report.reason(), "%time%", timeAgo)); } - - if (reports.size() > 5) { - player.sendMessage("§7... и ещё " + (reports.size() - 5) + " репортов"); + + if (reports.size() > MAX_RECENT_REPORTS) { + player.sendMessage("§7... и ещё " + (reports.size() - MAX_RECENT_REPORTS) + " репортов"); } } } diff --git a/src/main/java/dev/loki/lorep/gui/GuiClickListener.java b/src/main/java/dev/loki/lorep/gui/GuiClickListener.java index be27115..6ff6129 100644 --- a/src/main/java/dev/loki/lorep/gui/GuiClickListener.java +++ b/src/main/java/dev/loki/lorep/gui/GuiClickListener.java @@ -29,23 +29,19 @@ public class GuiClickListener implements Listener { } int slot = event.getRawSlot(); - - // Navigation buttons are in bottom row (slots 45-53) - switch (slot) { - case 45 -> { // Previous page - if (gui.getPage() > 0) { - new ReportGui(plugin, gui.getPage() - 1).open(player); - } + + // Navigation buttons are in bottom row + if (slot == ReportGui.SLOT_PREV_PAGE) { + if (gui.getPage() > 0) { + new ReportGui(plugin, gui.getPage() - 1).open(player); } - case 47 -> { // Close - player.closeInventory(); - } - case 53 -> { // Next page - int totalReports = plugin.getDatabaseManager().getTotalReportCount(); - int totalPages = PaginationUtil.calculateTotalPages(totalReports, ReportGui.PAGE_SIZE); - if (gui.getPage() < totalPages - 1) { - new ReportGui(plugin, gui.getPage() + 1).open(player); - } + } else if (slot == ReportGui.SLOT_CLOSE) { + player.closeInventory(); + } else if (slot == ReportGui.SLOT_NEXT_PAGE) { + int totalReports = plugin.getDatabaseManager().getTotalReportCount(); + int totalPages = PaginationUtil.calculateTotalPages(totalReports, ReportGui.PAGE_SIZE); + if (gui.getPage() < totalPages - 1) { + new ReportGui(plugin, gui.getPage() + 1).open(player); } } } diff --git a/src/main/java/dev/loki/lorep/gui/ReportGui.java b/src/main/java/dev/loki/lorep/gui/ReportGui.java index 04589f3..d56f36d 100644 --- a/src/main/java/dev/loki/lorep/gui/ReportGui.java +++ b/src/main/java/dev/loki/lorep/gui/ReportGui.java @@ -16,10 +16,15 @@ import java.util.ArrayList; import java.util.List; public class ReportGui implements InventoryHolder { - + public static final int PAGE_SIZE = 45; // 5 rows for reports public static final int INVENTORY_SIZE = 54; // 6 rows total - + + public static final int SLOT_PREV_PAGE = 45; + public static final int SLOT_CLOSE = 47; + public static final int SLOT_PAGE_INFO = 49; + public static final int SLOT_NEXT_PAGE = 53; + private final LorepPlugin plugin; private final int page; private Inventory inventory; @@ -78,8 +83,8 @@ public class ReportGui implements InventoryHolder { private void addNavigationButtons(int totalReports) { int totalPages = (int) Math.ceil((double) totalReports / PAGE_SIZE); - - // Previous page button (slot 45) + + // Previous page button if (page > 0) { ItemStack prevButton = new ItemStack(Material.ARROW); ItemMeta prevMeta = prevButton.getItemMeta(); @@ -87,10 +92,10 @@ public class ReportGui implements InventoryHolder { prevMeta.setDisplayName("§a← Предыдущая страница"); prevButton.setItemMeta(prevMeta); } - inventory.setItem(45, prevButton); + inventory.setItem(SLOT_PREV_PAGE, prevButton); } - - // Page info (slot 49) + + // Page info ItemStack pageInfo = new ItemStack(Material.PAPER); ItemMeta pageMeta = pageInfo.getItemMeta(); if (pageMeta != null) { @@ -100,9 +105,9 @@ public class ReportGui implements InventoryHolder { pageMeta.setLore(lore); pageInfo.setItemMeta(pageMeta); } - inventory.setItem(49, pageInfo); - - // Next page button (slot 53) + inventory.setItem(SLOT_PAGE_INFO, pageInfo); + + // Next page button if (page < totalPages - 1) { ItemStack nextButton = new ItemStack(Material.ARROW); ItemMeta nextMeta = nextButton.getItemMeta(); @@ -110,17 +115,17 @@ public class ReportGui implements InventoryHolder { nextMeta.setDisplayName("§aСледующая страница →"); nextButton.setItemMeta(nextMeta); } - inventory.setItem(53, nextButton); + inventory.setItem(SLOT_NEXT_PAGE, nextButton); } - - // Close button (slot 47) + + // Close button ItemStack closeButton = new ItemStack(Material.BARRIER); ItemMeta closeMeta = closeButton.getItemMeta(); if (closeMeta != null) { closeMeta.setDisplayName("§cЗакрыть"); closeButton.setItemMeta(closeMeta); } - inventory.setItem(47, closeButton); + inventory.setItem(SLOT_CLOSE, closeButton); } public int getPage() { diff --git a/src/main/java/dev/loki/lorep/util/TimeUtil.java b/src/main/java/dev/loki/lorep/util/TimeUtil.java index 00b9a12..edd2d12 100644 --- a/src/main/java/dev/loki/lorep/util/TimeUtil.java +++ b/src/main/java/dev/loki/lorep/util/TimeUtil.java @@ -4,62 +4,68 @@ import java.time.Duration; import java.time.Instant; public class TimeUtil { - + + private static final long SECONDS_PER_MINUTE = 60; + private static final long MINUTES_PER_HOUR = 60; + private static final long HOURS_PER_DAY = 24; + private static final long DAYS_PER_MONTH = 30; + private static final long MONTHS_PER_YEAR = 12; + public static String formatTimeAgo(long epochMillis) { return formatTimeAgo(Instant.ofEpochMilli(epochMillis)); } - + public static String formatTimeAgo(Instant instant) { Duration duration = Duration.between(instant, Instant.now()); - + long seconds = duration.getSeconds(); - - if (seconds < 60) { + + if (seconds < SECONDS_PER_MINUTE) { return seconds + " сек. назад"; } - - long minutes = seconds / 60; - if (minutes < 60) { + + long minutes = seconds / SECONDS_PER_MINUTE; + if (minutes < MINUTES_PER_HOUR) { return minutes + " мин. назад"; } - - long hours = minutes / 60; - if (hours < 24) { + + long hours = minutes / MINUTES_PER_HOUR; + if (hours < HOURS_PER_DAY) { return hours + " ч. назад"; } - - long days = hours / 24; - if (days < 30) { + + long days = hours / HOURS_PER_DAY; + if (days < DAYS_PER_MONTH) { return days + " дн. назад"; } - - long months = days / 30; - if (months < 12) { + + long months = days / DAYS_PER_MONTH; + if (months < MONTHS_PER_YEAR) { return months + " мес. назад"; } - - long years = months / 12; + + long years = months / MONTHS_PER_YEAR; return years + " г. назад"; } - + public static String formatDuration(Duration duration) { long seconds = duration.getSeconds(); - - if (seconds < 60) { + + if (seconds < SECONDS_PER_MINUTE) { return seconds + " секунд"; } - - long minutes = seconds / 60; - if (minutes < 60) { + + long minutes = seconds / SECONDS_PER_MINUTE; + if (minutes < MINUTES_PER_HOUR) { return minutes + " минут"; } - - long hours = minutes / 60; - if (hours < 24) { + + long hours = minutes / MINUTES_PER_HOUR; + if (hours < HOURS_PER_DAY) { return hours + " часов"; } - - long days = hours / 24; + + long days = hours / HOURS_PER_DAY; return days + " дней"; } }