Skip to content

Commit 8d2279c

Browse files
Improve row/column safety
if you need an unsafe slot, use Slot.ofUnsafe, but you should now use Slot.of. Constructor will be made private on 2.1.0 to avoid boilerplate and safety issues (like for example, row being 7, column being 10, etc.)
1 parent ad90aaa commit 8d2279c

File tree

1 file changed

+69
-14
lines changed
  • core/src/main/java/me/flame/menus/menu

1 file changed

+69
-14
lines changed

core/src/main/java/me/flame/menus/menu/Slot.java

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,79 @@ public final class Slot {
4747
@CompileTimeConstant
4848
public static final Slot NaS = getNaS();
4949

50+
/**
51+
* Creates a new slot with the specified row and column.
52+
* @param row the row
53+
* @param column the col
54+
* @deprecated Use {@link #of(int, int)}, constructor will be private in 2.1.0
55+
*/
56+
@Deprecated
5057
public Slot(final int row, final int column) {
51-
this.row = row;
52-
this.column = column;
58+
this(row, column, false);
59+
checkRowColumnSafety(slot, row, column);
5360

5461
this.slot = (column + (row - 1) * 9) - 1;
5562
this.slot = this.slot >= 54 ? -1 : this.slot;
5663
}
5764

65+
private Slot(final int row, final int column, final int slot) {
66+
this(row, column, false);
67+
this.slot = slot >= 54 ? -1 : slot;
68+
checkRowColumnSafety(slot, row, column);
69+
}
70+
71+
/**
72+
* Creates a new slot with the specified row and column.
73+
* @param slot the slot
74+
* @deprecated Use {@link #of(int)}, constructor will be private in 2.1.0
75+
*/
76+
@Deprecated
5877
public Slot(final int slot) {
78+
this(slot / 9 + 1, slot % 9 + 1, false);
79+
checkRowColumnSafety(slot, row, column);
80+
5981
this.slot = slot >= 54 ? -1 : slot;
60-
this.row = (slot / 9) + 1;
61-
this.column = (slot % 9) + 1;
82+
}
83+
84+
private static void checkRowColumnSafety(int slot, int row, int column) {
85+
if (row > 6) {
86+
throw new IllegalArgumentException(
87+
"Row expected to be between 1 and 6, got " + row +
88+
"\nFix: slot = (column + (row - 1) * 9) - 1, slot = " + slot
89+
);
90+
}
91+
if (column > 9) {
92+
throw new IllegalArgumentException(
93+
"Column expected to be between 1 and 9, got " + column +
94+
"\nFix: slot = (column + (row - 1) * 9) - 1, slot = " + slot
95+
);
96+
}
97+
}
98+
99+
@Contract("_ -> new")
100+
public static @NotNull Slot of(int slot) {
101+
return new Slot(slot / 9 + 1, slot % 9 + 1, slot);
102+
}
103+
104+
@Contract("_, _ -> new")
105+
public static @NotNull Slot of(int row, int column) {
106+
return new Slot(row, column, (column + (row - 1) * 9) - 1);
107+
}
108+
109+
public static @NotNull Slot ofUnsafe(int slot) {
110+
Slot position = new Slot((slot / 9) + 1, (slot % 9) + 1, true);
111+
112+
int positionSlot = (position.column + (position.row - 1) * 9) - 1;
113+
position.slot = positionSlot >= 54 ? -1 : positionSlot;
114+
return position;
115+
}
116+
117+
public static Slot ofUnsafe(int row, int column) {
118+
if (column > 9 || row > 6) return Slot.NaS;
119+
Slot position = new Slot(row, column, true);
120+
int positionSlot = (column + (row - 1) * 9) - 1;
121+
position.slot = positionSlot >= 54 ? -1 : positionSlot;
122+
return position;
62123
}
63124

64125
// fast copy constructor
@@ -121,9 +182,7 @@ public Slot setSlot(final int slot) {
121182
*/
122183
@NotNull
123184
public Slot copy() {
124-
final Slot slot = new Slot(row, column, true);
125-
slot.slot = this.slot;
126-
return slot;
185+
return new Slot(row, column, slot);
127186
}
128187

129188
/**
@@ -138,7 +197,7 @@ public boolean isSlot() {
138197
}
139198

140199
/**
141-
* Check if the slot is a valid slot.
200+
* Check if the slot is valid.
142201
* @apiNote If the slot is equal to -1, it's invalid; fast check.
143202
* @return true if the slot does not equal -1.
144203
*/
@@ -149,17 +208,13 @@ public boolean isValid() {
149208
// faster NaS (Not A Slot) alternative to creating a Slot like new Slot(8, 1);
150209
@NotNull
151210
private static Slot getNaS() {
152-
Slot slot = new Slot(-1, -1, true);
153-
slot.slot = -1;
154-
return slot;
211+
return new Slot(-1, -1, -1);
155212
}
156213

157214
// faster FIRST slot compared to doing "new Slot(1, 1)"
158215
@NotNull
159216
public static Slot getFirst() {
160-
Slot slot = new Slot(1, 1, true);
161-
slot.slot = 0;
162-
return slot;
217+
return new Slot(1, 1, 0);
163218
}
164219

165220
@Override

0 commit comments

Comments
 (0)