Skip to content

Commit 7728064

Browse files
committed
🪄 refactor!: rename MenuUtils -> Menus
1 parent d8d681e commit 7728064

7 files changed

Lines changed: 40 additions & 37 deletions

File tree

src/main/java/com/cosimo/utilities/menu/IMenu.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cosimo.utilities.menu;
22

33
import com.cosimo.utilities.menu.type.AbstractMenu;
4-
import com.cosimo.utilities.menu.util.MenuUtils;
4+
import com.cosimo.utilities.menu.util.Menus;
55
import lombok.NonNull;
66
import org.bukkit.entity.HumanEntity;
77
import org.bukkit.event.inventory.InventoryAction;
@@ -29,7 +29,7 @@ public interface IMenu {
2929
* and any action on the menu are cancelled, but interaction with one's own inventory is allowed.
3030
*/
3131
default void onClick(@NonNull InventoryClickEvent event) {
32-
event.setCancelled(MenuUtils.shouldCancelMenuClick(event));
32+
event.setCancelled(Menus.shouldCancelMenuClick(event));
3333
}
3434

3535
/**
@@ -42,8 +42,8 @@ default void onClick(@NonNull InventoryClickEvent event) {
4242
*/
4343
default void onDrag(@NonNull InventoryDragEvent event) {
4444
if (event.getRawSlots()
45-
.stream()
46-
.anyMatch(rawSlot -> this.getInventory().equals(event.getView().getInventory(rawSlot)))) {
45+
.stream()
46+
.anyMatch(rawSlot -> this.getInventory().equals(event.getView().getInventory(rawSlot)))) {
4747
event.setCancelled(true);
4848
}
4949
}
@@ -100,12 +100,12 @@ default IMenu close() {
100100

101101
/**
102102
* Returns the amount of columns this {@link Inventory}'s size has according to
103-
* {@link MenuUtils#getColumns(Inventory)}.
103+
* {@link Menus#getColumns(Inventory)}.
104104
*
105105
* @return positive integer, at least 1
106106
*/
107107
default int getColumns() {
108-
return MenuUtils.getColumns(this.getInventory());
108+
return Menus.getColumns(this.getInventory());
109109
}
110110

111111
/**

src/main/java/com/cosimo/utilities/menu/MenuManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cosimo.utilities.menu;
22

3-
import com.cosimo.utilities.menu.util.MenuUtils;
3+
import com.cosimo.utilities.menu.util.Menus;
44
import com.google.common.base.Preconditions;
55
import lombok.NonNull;
66
import lombok.Setter;
@@ -211,7 +211,7 @@ public void onInventoryClick(@NonNull InventoryClickEvent event) {
211211
@EventHandler(priority = EventPriority.MONITOR)
212212
public void onInventoryClose(@NonNull InventoryCloseEvent event) {
213213
this.getMenu(event.getInventory()).ifPresent(menu -> {
214-
if (MenuUtils.isAboutToBecomeDisposable(event)) {
214+
if (Menus.isAboutToBecomeDisposable(event)) {
215215
this.unregisterMenu(menu);
216216
}
217217

src/main/java/com/cosimo/utilities/menu/type/AbstractMenu.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.cosimo.utilities.menu.IMenu;
44
import com.cosimo.utilities.menu.MenuManager;
55
import com.cosimo.utilities.menu.type.action.ActionMenu;
6-
import com.cosimo.utilities.menu.util.MenuUtils;
6+
import com.cosimo.utilities.menu.util.Menus;
77
import lombok.NonNull;
88
import org.bukkit.Bukkit;
99
import org.bukkit.entity.HumanEntity;
@@ -71,7 +71,7 @@ public AbstractMenu(@NonNull Inventory inventory) {
7171
*/
7272
@Override
7373
public void onClose(@NonNull InventoryCloseEvent event) {
74-
if (MenuUtils.isAboutToBecomeDisposable(event)) {
74+
if (Menus.isAboutToBecomeDisposable(event)) {
7575
this.attachBukkitTask(null);
7676
}
7777
}

src/main/java/com/cosimo/utilities/menu/util/MenuUtils.java renamed to src/main/java/com/cosimo/utilities/menu/util/Menus.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@
77
import org.bukkit.event.inventory.InventoryClickEvent;
88
import org.bukkit.event.inventory.InventoryCloseEvent;
99
import org.bukkit.inventory.Inventory;
10+
import org.jetbrains.annotations.ApiStatus;
1011
import org.jetbrains.annotations.Contract;
1112

1213
/**
1314
* Contains utility methods and common logic for working with {@link IMenu} and {@link AbstractMenu}, handling
1415
* menu-related events, determining inventory states, and performing inventory-related calculations.
1516
*/
16-
public class MenuUtils {
17+
public class Menus {
1718

1819
public static final int MAX_CHEST_ROWS = 6;
1920
public static final int CHEST_COLUMNS = 9;
2021

2122
/**
22-
* Determines if the inventory in the given {@link InventoryCloseEvent} is about to become disposable.
23+
* Determines if the inventory in the given {@link InventoryCloseEvent} handler is about to become disposable.
2324
*
2425
* <p>An inventory is considered disposable if it has fewer than two viewers remaining.</p>
2526
*
2627
* @param event the {@link InventoryCloseEvent} to check
2728
* @return {@code true} if the inventory is about to become disposable, {@code false} otherwise
2829
*/
2930
@Contract(pure = true)
31+
@ApiStatus.Internal
3032
public static boolean isAboutToBecomeDisposable(@NonNull InventoryCloseEvent event) {
3133
return event.getViewers().size() < 2;
3234
}
@@ -64,6 +66,7 @@ public static boolean isClickInsideInventory(@NonNull InventoryClickEvent event)
6466
* @return {@code true} if the click should be canceled, {@code false} otherwise
6567
*/
6668
@Contract(pure = true)
69+
@ApiStatus.Internal
6770
public static boolean shouldCancelMenuClick(@NonNull InventoryClickEvent event) {
6871
return isInventoryViewItemMixingAction(event.getAction()) || isClickInsideInventory(event);
6972
}

src/main/java/com/cosimo/utilities/menu/util/Slot.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ private Zero() {
2323

2424
/**
2525
* Converts a row-column position to a slot index with zero-based indexing with default columns for chest
26-
* inventories using {@link MenuUtils#CHEST_COLUMNS}.
26+
* inventories using {@link Menus#CHEST_COLUMNS}.
2727
*
2828
* @param row The row of the slot position (starting from 1)
2929
* @param column The column of the slot position (starting from 1)
3030
* @return The calculated slot index
3131
*/
3232
@Contract(pure = true)
3333
public static int of(int row, int column) {
34-
return of(row, column, MenuUtils.CHEST_COLUMNS);
34+
return of(row, column, Menus.CHEST_COLUMNS);
3535
}
3636

3737
/**
@@ -59,7 +59,7 @@ public static int of(int row, int column, int columns) {
5959
*/
6060
@Contract(pure = true)
6161
public static int of(int row, int column, @NonNull Inventory inventory) {
62-
final int slot = of(row, column, MenuUtils.getColumns(inventory));
62+
final int slot = of(row, column, Menus.getColumns(inventory));
6363

6464
if (slot >= inventory.getSize()) {
6565
throw new IllegalArgumentException(
@@ -99,15 +99,15 @@ private One() {
9999

100100
/**
101101
* Converts a row-column position to a slot index with one-based indexing with default columns for chest
102-
* inventories using {@link MenuUtils#CHEST_COLUMNS}.
102+
* inventories using {@link Menus#CHEST_COLUMNS}.
103103
*
104104
* @param row The row of the slot position (starting from 1)
105105
* @param column The column of the slot position (starting from 1)
106106
* @return The calculated slot index
107107
*/
108108
@Contract(pure = true)
109109
public static int of(int row, int column) {
110-
return of(row, column, MenuUtils.CHEST_COLUMNS);
110+
return of(row, column, Menus.CHEST_COLUMNS);
111111
}
112112

113113
/**
@@ -135,7 +135,7 @@ public static int of(int row, int column, int columns) {
135135
*/
136136
@Contract(pure = true)
137137
public static int of(int row, int column, @NonNull Inventory inventory) {
138-
final int slot = of(row, column, MenuUtils.getColumns(inventory));
138+
final int slot = of(row, column, Menus.getColumns(inventory));
139139

140140
if (slot >= inventory.getSize()) {
141141
throw new IllegalArgumentException(

src/test/java/com/cosimo/utilities/menu/ChestMenuTests.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.cosimo.utilities.menu.type.action.ActionMenu;
44
import com.cosimo.utilities.menu.type.action.MenuAction;
5-
import com.cosimo.utilities.menu.util.MenuUtils;
5+
import com.cosimo.utilities.menu.util.Menus;
66
import org.bukkit.Bukkit;
77
import org.bukkit.inventory.Inventory;
88
import org.bukkit.inventory.ItemStack;
@@ -19,16 +19,16 @@
1919

2020
import java.util.stream.IntStream;
2121

22-
import static com.cosimo.utilities.menu.util.MenuUtils.CHEST_COLUMNS;
23-
import static com.cosimo.utilities.menu.util.MenuUtils.MAX_CHEST_ROWS;
22+
import static com.cosimo.utilities.menu.util.Menus.CHEST_COLUMNS;
23+
import static com.cosimo.utilities.menu.util.Menus.MAX_CHEST_ROWS;
2424
import static org.junit.jupiter.api.Assertions.*;
2525
import static org.mockito.Mockito.*;
2626

2727
public class ChestMenuTests {
2828

2929
private static final int INVENTORY_SIZE = MAX_CHEST_ROWS * CHEST_COLUMNS;
3030

31-
private MockedStatic<MenuUtils> mockedUtils;
31+
private MockedStatic<Menus> mockedUtils;
3232

3333
@Mock
3434
private BukkitTask mockedTask;
@@ -47,8 +47,8 @@ public void setUp() {
4747

4848
when(this.mockedInventory.getSize()).thenReturn(INVENTORY_SIZE);
4949

50-
this.mockedUtils = mockStatic(MenuUtils.class, InvocationOnMock::callRealMethod);
51-
this.mockedUtils.when(() -> MenuUtils.getColumns(any())).thenReturn(9);
50+
this.mockedUtils = mockStatic(Menus.class, InvocationOnMock::callRealMethod);
51+
this.mockedUtils.when(() -> Menus.getColumns(any())).thenReturn(9);
5252

5353
this.menu = spy(new ActionMenu(this.mockedInventory));
5454

@@ -71,7 +71,7 @@ public void setUp() {
7171
}
7272

7373
@AfterEach
74-
public void tearDown() {
74+
public void releaseMocks() {
7575
this.mockedUtils.close();
7676
}
7777

@@ -137,17 +137,17 @@ public void shouldHandleNullTaskGracefully() {
137137

138138
@Test
139139
public void testGetChestRowsForCount() {
140-
assertEquals(1, MenuUtils.getChestRowsForCount(5));
141-
assertEquals(3, MenuUtils.getChestRowsForCount(20));
142-
assertEquals(MenuUtils.MAX_CHEST_ROWS, MenuUtils.getChestRowsForCount(100));
143-
assertEquals(1, MenuUtils.getChestRowsForCount(0));
140+
assertEquals(1, Menus.getChestRowsForCount(5));
141+
assertEquals(3, Menus.getChestRowsForCount(20));
142+
assertEquals(Menus.MAX_CHEST_ROWS, Menus.getChestRowsForCount(100));
143+
assertEquals(1, Menus.getChestRowsForCount(0));
144144
}
145145

146146
@Test
147147
public void testGetChestSizeForCount() {
148-
assertEquals(9, MenuUtils.getChestSizeForCount(5));
149-
assertEquals(27, MenuUtils.getChestSizeForCount(20));
150-
assertEquals(MenuUtils.MAX_CHEST_ROWS * MenuUtils.CHEST_COLUMNS, MenuUtils.getChestSizeForCount(100));
151-
assertEquals(9, MenuUtils.getChestSizeForCount(0));
148+
assertEquals(9, Menus.getChestSizeForCount(5));
149+
assertEquals(27, Menus.getChestSizeForCount(20));
150+
assertEquals(Menus.MAX_CHEST_ROWS * Menus.CHEST_COLUMNS, Menus.getChestSizeForCount(100));
151+
assertEquals(9, Menus.getChestSizeForCount(0));
152152
}
153153
}

src/test/java/com/cosimo/utilities/menu/util/SlotTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.stream.IntStream;
1414
import java.util.stream.Stream;
1515

16-
import static com.cosimo.utilities.menu.util.MenuUtils.CHEST_COLUMNS;
16+
import static com.cosimo.utilities.menu.util.Menus.CHEST_COLUMNS;
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertThrows;
1919
import static org.mockito.Mockito.mockStatic;
@@ -23,7 +23,7 @@ class SlotTest {
2323

2424
private static final int ROWS = 4;
2525

26-
private MockedStatic<MenuUtils> mockedUtils;
26+
private MockedStatic<Menus> mockedUtils;
2727

2828
@Mock
2929
private Inventory mockedInventory;
@@ -32,8 +32,8 @@ class SlotTest {
3232
void setUp() {
3333
MockitoAnnotations.openMocks(this);
3434

35-
this.mockedUtils = mockStatic(MenuUtils.class);
36-
this.mockedUtils.when(() -> MenuUtils.getColumns(this.mockedInventory)).thenReturn(CHEST_COLUMNS);
35+
this.mockedUtils = mockStatic(Menus.class);
36+
this.mockedUtils.when(() -> Menus.getColumns(this.mockedInventory)).thenReturn(CHEST_COLUMNS);
3737

3838
when(this.mockedInventory.getSize()).thenReturn(getInventorySize());
3939
}

0 commit comments

Comments
 (0)