Skip to content

Commit b265477

Browse files
committed
Refactor WarpInventory-related components for consistent formatting, improved async handling, and better configuration management; introduce Warp interface and enhance migration logging.
1 parent 85d4c99 commit b265477

File tree

4 files changed

+111
-77
lines changed

4 files changed

+111
-77
lines changed

eternalcore-core/src/main/java/com/eternalcode/core/configuration/migrations/Migration_0035_Move_warp_inventory_to_dedicated_file.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
import java.io.IOException;
1010
import java.util.LinkedHashMap;
1111
import java.util.Map;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
1214
import lombok.NonNull;
1315
import org.yaml.snakeyaml.DumperOptions;
1416
import org.yaml.snakeyaml.Yaml;
1517

1618
public class Migration_0035_Move_warp_inventory_to_dedicated_file implements ConfigMigration {
1719

20+
private static final Logger LOGGER = Logger
21+
.getLogger(Migration_0035_Move_warp_inventory_to_dedicated_file.class.getName());
1822
private static final String ROOT_KEY = "warpInventory";
1923
private static final String NESTED_KEY = "warp.warpInventory";
2024
private static final String NESTED_SECTION = "warp";
@@ -26,8 +30,7 @@ public boolean migrate(@NonNull OkaeriConfig config, @NonNull RawConfigView view
2630

2731
if (warpInventory != null) {
2832
foundKey = ROOT_KEY;
29-
}
30-
else {
33+
} else {
3134
warpInventory = this.getFromView(view, NESTED_KEY);
3235
if (warpInventory != null) {
3336
foundKey = NESTED_KEY;
@@ -50,8 +53,7 @@ public boolean migrate(@NonNull OkaeriConfig config, @NonNull RawConfigView view
5053

5154
if (foundKey != null) {
5255
view.remove(foundKey);
53-
}
54-
else {
56+
} else {
5557
view.remove(ROOT_KEY);
5658
view.remove(NESTED_KEY);
5759
}
@@ -89,9 +91,8 @@ private Map<String, Object> getFromFileFallback(OkaeriConfig config) {
8991
return (Map<String, Object>) warpSection.get(ROOT_KEY);
9092
}
9193
}
92-
}
93-
catch (Exception exception) {
94-
exception.printStackTrace();
94+
} catch (Exception exception) {
95+
LOGGER.log(Level.SEVERE, "Failed to read configuration file: " + bindFile.getAbsolutePath(), exception);
9596
}
9697
return null;
9798
}
@@ -142,9 +143,8 @@ private boolean saveNewConfig(OkaeriConfig config, Map<String, Object> content)
142143
try (FileWriter writer = new FileWriter(destFile)) {
143144
new Yaml(options).dump(content, writer);
144145
return true;
145-
}
146-
catch (IOException e) {
147-
e.printStackTrace();
146+
} catch (IOException e) {
147+
LOGGER.log(Level.SEVERE, "Failed to save new configuration file: " + destFile.getAbsolutePath(), e);
148148
return false;
149149
}
150150
}
@@ -169,9 +169,9 @@ private boolean targetIsSafeToWrite(File file) {
169169
}
170170
Object items = existing.get("items");
171171
return items instanceof Map && ((Map<?, ?>) items).isEmpty();
172-
}
173-
catch (Exception exception) {
174-
exception.printStackTrace();
172+
} catch (Exception exception) {
173+
LOGGER.log(Level.SEVERE, "Failed to check if target file is safe to write: " + file.getAbsolutePath(),
174+
exception);
175175
return false;
176176
}
177177
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.eternalcode.core.feature.warp;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.entity.Player;
5+
import java.util.List;
6+
7+
public interface Warp {
8+
9+
String getName();
10+
11+
Location getLocation();
12+
13+
List<String> getPermissions();
14+
15+
default boolean hasPermissions(Player player) {
16+
if (this.getPermissions().isEmpty()) {
17+
return true;
18+
}
19+
20+
for (String permission : this.getPermissions()) {
21+
if (player.hasPermission(permission)) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
}
28+
29+
}

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

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public class WarpInventory {
5151

5252
@Inject
5353
public WarpInventory(
54-
WarpService warpService,
55-
Server server,
56-
MiniMessage miniMessage,
57-
WarpTeleportService warpTeleportService,
58-
WarpSettings warpSettings,
59-
Scheduler scheduler,
60-
WarpInventoryConfigService warpInventoryConfigService) {
54+
WarpService warpService,
55+
Server server,
56+
MiniMessage miniMessage,
57+
WarpTeleportService warpTeleportService,
58+
WarpSettings warpSettings,
59+
Scheduler scheduler,
60+
WarpInventoryConfigService warpInventoryConfigService) {
6161
this.warpService = warpService;
6262
this.server = server;
6363
this.miniMessage = miniMessage;
@@ -69,23 +69,23 @@ public WarpInventory(
6969

7070
public void open(Player player) {
7171
this.warpInventoryConfigService.getWarpInventoryData()
72-
.thenAccept(warpData -> {
73-
this.scheduler.run(() -> {
74-
Gui gui = this.create(player, warpData);
75-
gui.open(player);
76-
});
77-
})
78-
.exceptionally(FutureHandler::handleException);
72+
.thenAccept(warpData -> {
73+
this.scheduler.run(() -> {
74+
Gui gui = this.create(player, warpData);
75+
gui.open(player);
76+
});
77+
})
78+
.exceptionally(FutureHandler::handleException);
7979
}
8080

8181
private Gui create(Player player, WarpInventoryConfigService.WarpInventoryConfigData warpData) {
8282
int rows = calculateRowsCount(warpData);
8383

8484
Gui gui = Gui.gui()
85-
.title(this.miniMessage.deserialize(warpData.title()))
86-
.rows(rows)
87-
.disableAllInteractions()
88-
.create();
85+
.title(this.miniMessage.deserialize(warpData.title()))
86+
.rows(rows)
87+
.disableAllInteractions()
88+
.create();
8989

9090
this.createWarpItems(player, warpData, gui);
9191
this.createBorder(warpData, gui);
@@ -133,9 +133,9 @@ private GuiItem createBorderItem(WarpInventoryConfig.BorderSection borderSection
133133

134134
if (!borderSection.lore().isEmpty()) {
135135
List<Component> loreComponents = borderSection.lore()
136-
.stream()
137-
.map(entry -> AdventureUtil.resetItalic(this.miniMessage.deserialize(entry)))
138-
.toList();
136+
.stream()
137+
.map(entry -> AdventureUtil.resetItalic(this.miniMessage.deserialize(entry)))
138+
.toList();
139139
borderItem.lore(loreComponents);
140140
}
141141

@@ -205,22 +205,22 @@ private BaseItemBuilder createItem(ConfigItem item) {
205205
Component name = AdventureUtil.resetItalic(this.miniMessage.deserialize(item.name()));
206206

207207
List<Component> lore = item.lore()
208-
.stream()
209-
.map(entry -> AdventureUtil.resetItalic(this.miniMessage.deserialize(entry)))
210-
.toList();
208+
.stream()
209+
.map(entry -> AdventureUtil.resetItalic(this.miniMessage.deserialize(entry)))
210+
.toList();
211211

212212
if (item.material() == Material.PLAYER_HEAD && !item.texture().isEmpty()) {
213213
return ItemBuilder.skull()
214-
.name(name)
215-
.lore(lore)
216-
.texture(item.texture())
217-
.glow(item.glow());
218-
}
219-
220-
return ItemBuilder.from(item.material())
221214
.name(name)
222215
.lore(lore)
216+
.texture(item.texture())
223217
.glow(item.glow());
218+
}
219+
220+
return ItemBuilder.from(item.material())
221+
.name(name)
222+
.lore(lore)
223+
.glow(item.glow());
224224
}
225225

226226
public CompletableFuture<Void> addWarp(Warp warp) {
@@ -229,28 +229,28 @@ public CompletableFuture<Void> addWarp(Warp warp) {
229229
}
230230

231231
return this.warpInventoryConfigService.getWarpInventoryData()
232-
.thenCompose(warpData -> {
233-
int slot = getSlot(warpData);
232+
.thenCompose(warpData -> {
233+
int slot = getSlot(warpData);
234234

235-
WarpInventoryItem warpInventoryItem = createWarpInventoryItem(warp, slot);
235+
WarpInventoryItem warpInventoryItem = createWarpInventoryItem(warp, slot);
236236

237-
return this.warpInventoryConfigService.addWarpItem(warp.getName(), warpInventoryItem);
238-
})
239-
.exceptionally(FutureHandler::handleException);
237+
return this.warpInventoryConfigService.addWarpItem(warp.getName(), warpInventoryItem);
238+
})
239+
.exceptionally(FutureHandler::handleException);
240240
}
241241

242242
private WarpInventoryItem createWarpInventoryItem(Warp warp, int slot) {
243243
return WarpInventoryItem.builder()
244-
.warpName(warp.getName())
245-
.warpItem(ConfigItem.builder()
246-
.withName(this.warpSettings.itemNamePrefix() + warp.getName())
247-
.withLore(Collections.singletonList(this.warpSettings.itemLore()))
248-
.withMaterial(this.warpSettings.itemMaterial())
249-
.withTexture(this.warpSettings.itemTexture())
250-
.withSlot(slot)
251-
.withGlow(true)
252-
.build())
253-
.build();
244+
.warpName(warp.getName())
245+
.warpItem(ConfigItem.builder()
246+
.withName(this.warpSettings.itemNamePrefix() + warp.getName())
247+
.withLore(Collections.singletonList(this.warpSettings.itemLore()))
248+
.withMaterial(this.warpSettings.itemMaterial())
249+
.withTexture(this.warpSettings.itemTexture())
250+
.withSlot(slot)
251+
.withGlow(true)
252+
.build())
253+
.build();
254254
}
255255

256256
private int getSlot(WarpInventoryConfigService.WarpInventoryConfigData warpData) {
@@ -274,21 +274,22 @@ public CompletableFuture<Void> removeWarp(String warpName) {
274274
}
275275

276276
return this.warpInventoryConfigService.removeWarpItem(warpName)
277-
.thenCompose(removed -> {
278-
if (removed != null) {
279-
return this.shiftWarpItems(removed, items);
280-
}
281-
return CompletableFuture.completedFuture(null);
282-
});
277+
.thenCompose(removed -> {
278+
if (removed != null) {
279+
return this.shiftWarpItems(removed, items)
280+
.thenCompose(v -> this.warpInventoryConfigService.save());
281+
}
282+
return CompletableFuture.completedFuture(null);
283+
});
283284
}
284285

285286
private CompletableFuture<Void> shiftWarpItems(WarpInventoryItem removed, Map<String, WarpInventoryItem> items) {
286287
int removedSlot = removed.warpItem().slot;
287288

288289
List<WarpInventoryItem> itemsToShift = items.values().stream()
289-
.filter(item -> item.warpItem().slot > removedSlot)
290-
.sorted(Comparator.comparingInt(item -> item.warpItem().slot))
291-
.toList();
290+
.filter(item -> item.warpItem().slot > removedSlot)
291+
.sorted(Comparator.comparingInt(item -> item.warpItem().slot))
292+
.toList();
292293

293294
int currentShift = removedSlot;
294295
for (WarpInventoryItem item : itemsToShift) {

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.eternalcode.core.feature.warp.inventory.WarpInventoryConfig.DecorationItemsSection;
88
import com.eternalcode.core.injector.annotations.Inject;
99
import com.eternalcode.core.injector.annotations.component.Service;
10-
1110
import java.util.HashMap;
1211
import java.util.Map;
1312
import java.util.concurrent.CompletableFuture;
@@ -23,8 +22,7 @@ public class WarpInventoryConfigService {
2322
public WarpInventoryConfigService(
2423
ConfigurationManager configurationManager,
2524
WarpInventoryConfig config,
26-
Scheduler scheduler
27-
) {
25+
Scheduler scheduler) {
2826
this.configurationManager = configurationManager;
2927
this.config = config;
3028
this.scheduler = scheduler;
@@ -36,8 +34,8 @@ public CompletableFuture<WarpInventoryConfigData> getWarpInventoryData() {
3634
config.display().title(),
3735
config.border(),
3836
config.decorationItems(),
39-
items
40-
));
37+
items)
38+
);
4139
}
4240

4341
public CompletableFuture<Void> addWarpItem(String warpName, WarpInventoryItem item) {
@@ -53,7 +51,7 @@ public CompletableFuture<WarpInventoryItem> removeWarpItem(String warpName) {
5351
return CompletableFuture.completedFuture(null);
5452
}
5553

56-
return scheduler.<WarpInventoryItem>completeAsync(() -> config.items().get(warpName))
54+
return scheduler.completeAsync(() -> config.items().get(warpName))
5755
.thenCompose(item -> {
5856
if (item == null) {
5957
return CompletableFuture.completedFuture(null);
@@ -68,6 +66,13 @@ public CompletableFuture<WarpInventoryItem> removeWarpItem(String warpName) {
6866
});
6967
}
7068

69+
public CompletableFuture<Void> save() {
70+
return scheduler.completeAsync(() -> {
71+
configurationManager.save(config);
72+
return null;
73+
});
74+
}
75+
7176
public Map<String, WarpInventoryItem> getWarpItems() {
7277
return new HashMap<>(config.items());
7378
}
@@ -80,7 +85,6 @@ public record WarpInventoryConfigData(
8085
String title,
8186
BorderSection border,
8287
DecorationItemsSection decorationItems,
83-
Map<String, WarpInventoryItem> items
84-
) {
88+
Map<String, WarpInventoryItem> items) {
8589
}
8690
}

0 commit comments

Comments
 (0)