refactor: Улучшено качество кода MEDIUM приоритета

- Вынесены 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
This commit is contained in:
loki 2026-04-09 00:34:47 +02:00
parent bf65a1f9ae
commit 56d7b386fb
4 changed files with 75 additions and 66 deletions

View file

@ -11,6 +11,8 @@ import java.util.List;
public class ReportStatsCommand { public class ReportStatsCommand {
private static final int MAX_RECENT_REPORTS = 5;
private final LorepPlugin plugin; private final LorepPlugin plugin;
public ReportStatsCommand(LorepPlugin plugin) { public ReportStatsCommand(LorepPlugin plugin) {
@ -55,7 +57,7 @@ public class ReportStatsCommand {
// Recent reports (max 5) // Recent reports (max 5)
player.sendMessage("§7Последние репорты:"); 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++) { for (int i = 0; i < shown; i++) {
Report report = reports.get(i); Report report = reports.get(i);
String timeAgo = TimeUtil.formatTimeAgo(report.createdAt().toEpochMilli()); String timeAgo = TimeUtil.formatTimeAgo(report.createdAt().toEpochMilli());
@ -64,8 +66,8 @@ public class ReportStatsCommand {
"%time%", timeAgo)); "%time%", timeAgo));
} }
if (reports.size() > 5) { if (reports.size() > MAX_RECENT_REPORTS) {
player.sendMessage("§7... и ещё " + (reports.size() - 5) + " репортов"); player.sendMessage("§7... и ещё " + (reports.size() - MAX_RECENT_REPORTS) + " репортов");
} }
} }
} }

View file

@ -30,17 +30,14 @@ public class GuiClickListener implements Listener {
int slot = event.getRawSlot(); int slot = event.getRawSlot();
// Navigation buttons are in bottom row (slots 45-53) // Navigation buttons are in bottom row
switch (slot) { if (slot == ReportGui.SLOT_PREV_PAGE) {
case 45 -> { // Previous page
if (gui.getPage() > 0) { if (gui.getPage() > 0) {
new ReportGui(plugin, gui.getPage() - 1).open(player); new ReportGui(plugin, gui.getPage() - 1).open(player);
} }
} } else if (slot == ReportGui.SLOT_CLOSE) {
case 47 -> { // Close
player.closeInventory(); player.closeInventory();
} } else if (slot == ReportGui.SLOT_NEXT_PAGE) {
case 53 -> { // Next page
int totalReports = plugin.getDatabaseManager().getTotalReportCount(); int totalReports = plugin.getDatabaseManager().getTotalReportCount();
int totalPages = PaginationUtil.calculateTotalPages(totalReports, ReportGui.PAGE_SIZE); int totalPages = PaginationUtil.calculateTotalPages(totalReports, ReportGui.PAGE_SIZE);
if (gui.getPage() < totalPages - 1) { if (gui.getPage() < totalPages - 1) {
@ -48,7 +45,6 @@ public class GuiClickListener implements Listener {
} }
} }
} }
}
@EventHandler @EventHandler
public void onInventoryDrag(InventoryDragEvent event) { public void onInventoryDrag(InventoryDragEvent event) {

View file

@ -20,6 +20,11 @@ public class ReportGui implements InventoryHolder {
public static final int PAGE_SIZE = 45; // 5 rows for reports 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 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 LorepPlugin plugin;
private final int page; private final int page;
private Inventory inventory; private Inventory inventory;
@ -79,7 +84,7 @@ public class ReportGui implements InventoryHolder {
private void addNavigationButtons(int totalReports) { private void addNavigationButtons(int totalReports) {
int totalPages = (int) Math.ceil((double) totalReports / PAGE_SIZE); int totalPages = (int) Math.ceil((double) totalReports / PAGE_SIZE);
// Previous page button (slot 45) // Previous page button
if (page > 0) { if (page > 0) {
ItemStack prevButton = new ItemStack(Material.ARROW); ItemStack prevButton = new ItemStack(Material.ARROW);
ItemMeta prevMeta = prevButton.getItemMeta(); ItemMeta prevMeta = prevButton.getItemMeta();
@ -87,10 +92,10 @@ public class ReportGui implements InventoryHolder {
prevMeta.setDisplayName("§a← Предыдущая страница"); prevMeta.setDisplayName("§a← Предыдущая страница");
prevButton.setItemMeta(prevMeta); 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); ItemStack pageInfo = new ItemStack(Material.PAPER);
ItemMeta pageMeta = pageInfo.getItemMeta(); ItemMeta pageMeta = pageInfo.getItemMeta();
if (pageMeta != null) { if (pageMeta != null) {
@ -100,9 +105,9 @@ public class ReportGui implements InventoryHolder {
pageMeta.setLore(lore); pageMeta.setLore(lore);
pageInfo.setItemMeta(pageMeta); pageInfo.setItemMeta(pageMeta);
} }
inventory.setItem(49, pageInfo); inventory.setItem(SLOT_PAGE_INFO, pageInfo);
// Next page button (slot 53) // Next page button
if (page < totalPages - 1) { if (page < totalPages - 1) {
ItemStack nextButton = new ItemStack(Material.ARROW); ItemStack nextButton = new ItemStack(Material.ARROW);
ItemMeta nextMeta = nextButton.getItemMeta(); ItemMeta nextMeta = nextButton.getItemMeta();
@ -110,17 +115,17 @@ public class ReportGui implements InventoryHolder {
nextMeta.setDisplayName("§aСледующая страница →"); nextMeta.setDisplayName("§aСледующая страница →");
nextButton.setItemMeta(nextMeta); 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); ItemStack closeButton = new ItemStack(Material.BARRIER);
ItemMeta closeMeta = closeButton.getItemMeta(); ItemMeta closeMeta = closeButton.getItemMeta();
if (closeMeta != null) { if (closeMeta != null) {
closeMeta.setDisplayName("§cЗакрыть"); closeMeta.setDisplayName("§cЗакрыть");
closeButton.setItemMeta(closeMeta); closeButton.setItemMeta(closeMeta);
} }
inventory.setItem(47, closeButton); inventory.setItem(SLOT_CLOSE, closeButton);
} }
public int getPage() { public int getPage() {

View file

@ -5,6 +5,12 @@ import java.time.Instant;
public class TimeUtil { 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) { public static String formatTimeAgo(long epochMillis) {
return formatTimeAgo(Instant.ofEpochMilli(epochMillis)); return formatTimeAgo(Instant.ofEpochMilli(epochMillis));
} }
@ -14,52 +20,52 @@ public class TimeUtil {
long seconds = duration.getSeconds(); long seconds = duration.getSeconds();
if (seconds < 60) { if (seconds < SECONDS_PER_MINUTE) {
return seconds + " сек. назад"; return seconds + " сек. назад";
} }
long minutes = seconds / 60; long minutes = seconds / SECONDS_PER_MINUTE;
if (minutes < 60) { if (minutes < MINUTES_PER_HOUR) {
return minutes + " мин. назад"; return minutes + " мин. назад";
} }
long hours = minutes / 60; long hours = minutes / MINUTES_PER_HOUR;
if (hours < 24) { if (hours < HOURS_PER_DAY) {
return hours + " ч. назад"; return hours + " ч. назад";
} }
long days = hours / 24; long days = hours / HOURS_PER_DAY;
if (days < 30) { if (days < DAYS_PER_MONTH) {
return days + " дн. назад"; return days + " дн. назад";
} }
long months = days / 30; long months = days / DAYS_PER_MONTH;
if (months < 12) { if (months < MONTHS_PER_YEAR) {
return months + " мес. назад"; return months + " мес. назад";
} }
long years = months / 12; long years = months / MONTHS_PER_YEAR;
return years + " г. назад"; return years + " г. назад";
} }
public static String formatDuration(Duration duration) { public static String formatDuration(Duration duration) {
long seconds = duration.getSeconds(); long seconds = duration.getSeconds();
if (seconds < 60) { if (seconds < SECONDS_PER_MINUTE) {
return seconds + " секунд"; return seconds + " секунд";
} }
long minutes = seconds / 60; long minutes = seconds / SECONDS_PER_MINUTE;
if (minutes < 60) { if (minutes < MINUTES_PER_HOUR) {
return minutes + " минут"; return minutes + " минут";
} }
long hours = minutes / 60; long hours = minutes / MINUTES_PER_HOUR;
if (hours < 24) { if (hours < HOURS_PER_DAY) {
return hours + " часов"; return hours + " часов";
} }
long days = hours / 24; long days = hours / HOURS_PER_DAY;
return days + " дней"; return days + " дней";
} }
} }