Skip to content

Commit 93dd186

Browse files
RollcziJakubk15
andauthored
GH-893 Enhance Auto-Warp GUI and Fix Related Bugs (#893)
* Attempt fix, rename isExist to exists() * Fix warp inventory items shifting * Rename parameters of WarpService. Rename variables in shiftWarpItems method * Fix row count calculating * Remove recalculateWarpSlots field --------- Co-authored-by: Jakubk15 <[email protected]>
1 parent 4db61f0 commit 93dd186

File tree

7 files changed

+64
-46
lines changed

7 files changed

+64
-46
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.eternalcode.core.feature.warp;
22

33
import org.bukkit.Location;
4+
import org.jetbrains.annotations.ApiStatus.Experimental;
45

56
import java.util.Collection;
67
import java.util.Optional;
7-
import org.jetbrains.annotations.ApiStatus.Experimental;
88

99
public interface WarpService {
1010

11-
Warp createWarp(String name, Location location);
11+
Warp createWarp(String warp, Location location);
1212

1313
void removeWarp(String warp);
1414

@@ -18,9 +18,9 @@ public interface WarpService {
1818
@Experimental
1919
Warp removePermissions(String warp, String... permissions);
2020

21-
boolean isExist(String name);
21+
boolean exists(String warp);
2222

23-
Optional<Warp> findWarp(String name);
23+
Optional<Warp> findWarp(String warp);
2424

2525
Collection<Warp> getWarps();
2626
}

eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import com.eternalcode.core.feature.jail.JailSettings;
1111
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
1212
import com.eternalcode.core.feature.spawn.SpawnSettings;
13+
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
1314
import com.eternalcode.core.injector.annotations.Bean;
1415
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
15-
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
1616
import net.dzikoysk.cdn.entity.Contextual;
1717
import net.dzikoysk.cdn.entity.Description;
1818
import net.dzikoysk.cdn.entity.Exclude;
@@ -345,7 +345,7 @@ public static class Warp {
345345

346346
@Description({"# Options below allow you to customize item representing warp added to GUI, ",
347347
"# you can change almost everything inside langueage files, after the warp has been added to the inventory."})
348-
public String itemNamePrefix = "&8» &6Warp: &f";
348+
public String itemNamePrefix = "&8» &6Warp: &f";
349349

350350
public String itemLore = "&7Click to teleport!";
351351

eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpInventory.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bukkit.entity.Player;
2424

2525
import java.util.Collections;
26+
import java.util.Comparator;
2627
import java.util.List;
2728
import java.util.Optional;
2829

@@ -37,9 +38,12 @@ public class WarpInventory {
3738
private static final int GUI_ROW_SIZE_WITHOUT_BORDER = 9;
3839
private static final int GUI_ROW_SIZE_WITH_BORDER = 7;
3940

41+
private static final int BORDER_ROW_COUNT = 2;
42+
private static final int UGLY_BORDER_ROW_COUNT = 1;
43+
4044
private final TranslationManager translationManager;
4145
private final LanguageService languageService;
42-
private final WarpService warpManager;
46+
private final WarpService warpService;
4347
private final Server server;
4448
private final MiniMessage miniMessage;
4549
private final WarpTeleportService warpTeleportService;
@@ -50,7 +54,7 @@ public class WarpInventory {
5054
WarpInventory(
5155
TranslationManager translationManager,
5256
LanguageService languageService,
53-
WarpService warpManager,
57+
WarpService warpService,
5458
Server server,
5559
MiniMessage miniMessage,
5660
WarpTeleportService warpTeleportService,
@@ -59,7 +63,7 @@ public class WarpInventory {
5963
) {
6064
this.translationManager = translationManager;
6165
this.languageService = languageService;
62-
this.warpManager = warpManager;
66+
this.warpService = warpService;
6367
this.server = server;
6468
this.miniMessage = miniMessage;
6569
this.warpTeleportService = warpTeleportService;
@@ -90,8 +94,8 @@ private Gui createInventory(Player player, Language language) {
9094
}
9195
else {
9296
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;
9599
default -> throw new IllegalStateException("Unexpected value: " + warpSection.border().fillType());
96100
}
97101
}
@@ -163,7 +167,7 @@ private void createDecorations(WarpInventorySection warpSection, Gui gui) {
163167

164168
private void createWarpItems(Player player, WarpInventorySection warpSection, Gui gui) {
165169
warpSection.items().values().forEach(item -> {
166-
Optional<Warp> warpOptional = this.warpManager.findWarp(item.warpName());
170+
Optional<Warp> warpOptional = this.warpService.findWarp(item.warpName());
167171

168172
if (warpOptional.isEmpty()) {
169173
return;
@@ -215,30 +219,14 @@ private BaseItemBuilder createItem(ConfigItem item) {
215219
}
216220

217221
public void addWarp(Warp warp) {
218-
if (!this.warpManager.isExist(warp.getName())) {
222+
if (!this.warpService.exists(warp.getName())) {
219223
return;
220224
}
221225

222226
for (Language language : this.translationManager.getAvailableLanguages()) {
223227
AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages(language);
224228
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);
242230

243231
warpSection.addItem(warp.getName(),
244232
WarpInventoryItem.builder()
@@ -258,23 +246,51 @@ public void addWarp(Warp warp) {
258246
}
259247
}
260248

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+
}
262254

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;
265266
}
266267

267268
for (Language language : this.translationManager.getAvailableLanguages()) {
268-
269269
AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages(language);
270270
Translation.WarpSection.WarpInventorySection warpSection = translation.warp().warpInventory();
271+
WarpInventoryItem removed = warpSection.removeItem(warpName);
271272

272-
warpSection.removeItem(warpName);
273+
if (removed != null) {
274+
this.shiftWarpItems(removed, warpSection);
275+
}
273276

274277
this.configurationManager.save(translation);
275-
276278
}
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();
277287

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+
}
279294
}
295+
280296
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/WarpServiceImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.eternalcode.core.feature.warp.repository.WarpRepository;
66
import com.eternalcode.core.injector.annotations.Inject;
77
import com.eternalcode.core.injector.annotations.component.Service;
8+
import org.bukkit.Location;
9+
810
import java.util.ArrayList;
911
import java.util.Collection;
1012
import java.util.Collections;
@@ -13,7 +15,6 @@
1315
import java.util.Optional;
1416
import java.util.concurrent.ConcurrentHashMap;
1517
import java.util.function.Consumer;
16-
import org.bukkit.Location;
1718

1819
@FeatureDocs(
1920
name = "Warp System",
@@ -90,13 +91,13 @@ private Warp modifyPermissions(String warpName, Consumer<List<String>> modifier)
9091
}
9192

9293
@Override
93-
public boolean isExist(String name) {
94-
return this.warps.containsKey(name);
94+
public boolean exists(String warp) {
95+
return this.warps.containsKey(warp);
9596
}
9697

9798
@Override
98-
public Optional<Warp> findWarp(String name) {
99-
return Optional.ofNullable(this.warps.get(name));
99+
public Optional<Warp> findWarp(String warp) {
100+
return Optional.ofNullable(this.warps.get(warp));
100101
}
101102

102103
@Override

eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/DelWarpCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void remove(@Context Player player, @Arg Warp warp) {
3737
}
3838

3939
private void removeWarp(Player player, String name) {
40-
if (!this.warpService.isExist(name)) {
40+
if (!this.warpService.exists(name)) {
4141
this.noticeService.create()
4242
.player(player.getUniqueId())
4343
.notice(translation -> translation.warp().notExist())

eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/SetWarpCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void add(@Context Player player, @Arg String warpName) {
4444
}
4545

4646
private void createWarp(Player player, String warp, UUID uniqueId) {
47-
if (this.warpService.isExist(warp)) {
47+
if (this.warpService.exists(warp)) {
4848
this.noticeService.create()
4949
.player(uniqueId)
5050
.notice(translation -> translation.warp().warpAlreadyExists())

eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,12 @@ default void addItem(String name, WarpInventoryItem item) {
176176
this.setItems(items);
177177
}
178178

179-
default void removeItem(String name) {
179+
default WarpInventoryItem removeItem(String name) {
180180
Map<String, WarpInventoryItem> items = new HashMap<>(this.items());
181-
items.remove(name);
181+
WarpInventoryItem removed = items.remove(name);
182182

183183
this.setItems(items);
184+
return removed;
184185
}
185186

186187
BorderSection border();

0 commit comments

Comments
 (0)