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

@ -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) + " репортов");
}
}
}

View file

@ -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);
}
}
}

View file

@ -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() {

View file

@ -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 + " дней";
}
}