23
23
import org .bukkit .entity .Player ;
24
24
25
25
import java .util .Collections ;
26
+ import java .util .Comparator ;
26
27
import java .util .List ;
27
28
import java .util .Optional ;
28
29
@@ -37,9 +38,12 @@ public class WarpInventory {
37
38
private static final int GUI_ROW_SIZE_WITHOUT_BORDER = 9 ;
38
39
private static final int GUI_ROW_SIZE_WITH_BORDER = 7 ;
39
40
41
+ private static final int BORDER_ROW_COUNT = 2 ;
42
+ private static final int UGLY_BORDER_ROW_COUNT = 1 ;
43
+
40
44
private final TranslationManager translationManager ;
41
45
private final LanguageService languageService ;
42
- private final WarpService warpManager ;
46
+ private final WarpService warpService ;
43
47
private final Server server ;
44
48
private final MiniMessage miniMessage ;
45
49
private final WarpTeleportService warpTeleportService ;
@@ -50,7 +54,7 @@ public class WarpInventory {
50
54
WarpInventory (
51
55
TranslationManager translationManager ,
52
56
LanguageService languageService ,
53
- WarpService warpManager ,
57
+ WarpService warpService ,
54
58
Server server ,
55
59
MiniMessage miniMessage ,
56
60
WarpTeleportService warpTeleportService ,
@@ -59,7 +63,7 @@ public class WarpInventory {
59
63
) {
60
64
this .translationManager = translationManager ;
61
65
this .languageService = languageService ;
62
- this .warpManager = warpManager ;
66
+ this .warpService = warpService ;
63
67
this .server = server ;
64
68
this .miniMessage = miniMessage ;
65
69
this .warpTeleportService = warpTeleportService ;
@@ -90,8 +94,8 @@ private Gui createInventory(Player player, Language language) {
90
94
}
91
95
else {
92
96
switch (warpSection .border ().fillType ()) {
93
- case BORDER , ALL -> rowsCount = (size + 1 ) / GUI_ROW_SIZE_WITH_BORDER + 3 ;
94
- case TOP , BOTTOM -> rowsCount = (size + 1 ) / GUI_ROW_SIZE_WITHOUT_BORDER + 2 ;
97
+ case BORDER , ALL -> rowsCount = (size - 1 ) / GUI_ROW_SIZE_WITH_BORDER + 1 + BORDER_ROW_COUNT ;
98
+ case TOP , BOTTOM -> rowsCount = (size - 1 ) / GUI_ROW_SIZE_WITHOUT_BORDER + 1 + UGLY_BORDER_ROW_COUNT ;
95
99
default -> throw new IllegalStateException ("Unexpected value: " + warpSection .border ().fillType ());
96
100
}
97
101
}
@@ -163,7 +167,7 @@ private void createDecorations(WarpInventorySection warpSection, Gui gui) {
163
167
164
168
private void createWarpItems (Player player , WarpInventorySection warpSection , Gui gui ) {
165
169
warpSection .items ().values ().forEach (item -> {
166
- Optional <Warp > warpOptional = this .warpManager .findWarp (item .warpName ());
170
+ Optional <Warp > warpOptional = this .warpService .findWarp (item .warpName ());
167
171
168
172
if (warpOptional .isEmpty ()) {
169
173
return ;
@@ -215,30 +219,14 @@ private BaseItemBuilder createItem(ConfigItem item) {
215
219
}
216
220
217
221
public void addWarp (Warp warp ) {
218
- if (!this .warpManager . isExist (warp .getName ())) {
222
+ if (!this .warpService . exists (warp .getName ())) {
219
223
return ;
220
224
}
221
225
222
226
for (Language language : this .translationManager .getAvailableLanguages ()) {
223
227
AbstractTranslation translation = (AbstractTranslation ) this .translationManager .getMessages (language );
224
228
Translation .WarpSection .WarpInventorySection warpSection = translation .warp ().warpInventory ();
225
-
226
- int size = warpSection .items ().size ();
227
- int slot ;
228
-
229
- if (!warpSection .border ().enabled ()) {
230
- slot = GUI_ITEM_SLOT_WITHOUT_BORDER + size ;
231
- }
232
- else {
233
- switch (warpSection .border ().fillType ()) {
234
- case BORDER -> slot = GUI_ITEM_SLOT_WITH_BORDER + size + ((size / GUI_ROW_SIZE_WITH_BORDER ) * 2 );
235
- case ALL -> slot = GUI_ITEM_SLOT_WITH_ALL_BORDER + size + ((size / GUI_ROW_SIZE_WITH_BORDER ) * 2 );
236
- case TOP -> slot = GUI_ITEM_SLOT_WITH_TOP_BORDER + size ;
237
- case BOTTOM -> slot = size ;
238
- default -> throw new IllegalStateException ("Unexpected value: " + warpSection .border ().fillType ());
239
- }
240
- }
241
-
229
+ int slot = getSlot (warpSection );
242
230
243
231
warpSection .addItem (warp .getName (),
244
232
WarpInventoryItem .builder ()
@@ -258,23 +246,51 @@ public void addWarp(Warp warp) {
258
246
}
259
247
}
260
248
261
- public boolean removeWarp (String warpName ) {
249
+ private int getSlot (Translation .WarpSection .WarpInventorySection warpSection ) {
250
+ int size = warpSection .items ().size ();
251
+ if (!warpSection .border ().enabled ()) {
252
+ return GUI_ITEM_SLOT_WITHOUT_BORDER + size ;
253
+ }
262
254
263
- if (!this .warpManager .isExist (warpName )) {
264
- return false ;
255
+ return switch (warpSection .border ().fillType ()) {
256
+ case BORDER -> GUI_ITEM_SLOT_WITH_BORDER + size + ((size / WarpInventory .GUI_ROW_SIZE_WITH_BORDER ) * 2 );
257
+ case ALL -> GUI_ITEM_SLOT_WITH_ALL_BORDER + size + ((size / WarpInventory .GUI_ROW_SIZE_WITH_BORDER ) * 2 );
258
+ case TOP -> GUI_ITEM_SLOT_WITH_TOP_BORDER + size ;
259
+ case BOTTOM -> size ;
260
+ };
261
+ }
262
+
263
+ public void removeWarp (String warpName ) {
264
+ if (!this .config .warp .autoAddNewWarps ) {
265
+ return ;
265
266
}
266
267
267
268
for (Language language : this .translationManager .getAvailableLanguages ()) {
268
-
269
269
AbstractTranslation translation = (AbstractTranslation ) this .translationManager .getMessages (language );
270
270
Translation .WarpSection .WarpInventorySection warpSection = translation .warp ().warpInventory ();
271
+ WarpInventoryItem removed = warpSection .removeItem (warpName );
271
272
272
- warpSection .removeItem (warpName );
273
+ if (removed != null ) {
274
+ this .shiftWarpItems (removed , warpSection );
275
+ }
273
276
274
277
this .configurationManager .save (translation );
275
-
276
278
}
279
+ }
280
+
281
+ private void shiftWarpItems (WarpInventoryItem removed , Translation .WarpSection .WarpInventorySection warpSection ) {
282
+ int removedSlot = removed .warpItem .slot ;
283
+ List <WarpInventoryItem > itemsToShift = warpSection .items ().values ().stream ()
284
+ .filter (item -> item .warpItem .slot > removedSlot )
285
+ .sorted (Comparator .comparingInt (item -> item .warpItem .slot ))
286
+ .toList ();
277
287
278
- return true ;
288
+ int currentShift = removedSlot ;
289
+ for (WarpInventoryItem item : itemsToShift ) {
290
+ int nextShift = item .warpItem .slot ;
291
+ item .warpItem .slot = currentShift ;
292
+ currentShift = nextShift ;
293
+ }
279
294
}
295
+
280
296
}
0 commit comments