Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.eternalcode.core.configuration.migrations;

import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;

import eu.okaeri.configs.migrate.builtin.NamedMigration;

public class Migration_0010_Move_WarpInventory_to_dedicated_config extends NamedMigration {
Migration_0010_Move_WarpInventory_to_dedicated_config() {
super(
"Move WarpInventory items from messages to dedicated warp-inventory.yml config and clean up old fields",
move("warpInventory.title", "warpInventory.display.title"),
move("warpInventory.border.enabled", "warpInventory.border.enabled"),
move("warpInventory.border.material", "warpInventory.border.material"),
move("warpInventory.border.fillType", "warpInventory.border.fillType"),
move("warpInventory.border.name", "warpInventory.border.name"),
move("warpInventory.border.lore", "warpInventory.border.lore"),
move("warpInventory.decorationItems.items", "warpInventory.decorationItems.items"),
move("warpInventory.items", "warpInventory.items")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Migrations {
new Migration_0006_Move_alert_to_broadcast_section(),
new Migration_0007_Move_clear_to_dedicated_section(),
new Migration_0008_Move_repair_to_dedicated_section(),
new Migration_0009_Improve_Homes_Config()
new Migration_0009_Improve_Homes_Config(),
new Migration_0010_Move_WarpInventory_to_dedicated_config()
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpInventory;
import com.eternalcode.core.feature.warp.inventory.WarpInventory;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpInventory;
import com.eternalcode.core.feature.warp.inventory.WarpInventory;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.feature.warp.WarpSettings;
import com.eternalcode.core.injector.annotations.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpInventory;
import com.eternalcode.core.feature.warp.inventory.WarpInventory;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.feature.warp.WarpSettings;
import com.eternalcode.core.feature.warp.WarpTeleportService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.eternalcode.core.feature.warp;
package com.eternalcode.core.feature.warp.inventory;

import com.eternalcode.commons.adventure.AdventureUtil;
import com.eternalcode.commons.concurrent.FutureHandler;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.configuration.ConfigurationManager;
import com.eternalcode.core.configuration.contextual.ConfigItem;
import com.eternalcode.core.feature.warp.messages.WarpMessages;
import com.eternalcode.core.feature.warp.messages.WarpMessages.WarpInventorySection;
import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpInventoryItem;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.feature.warp.WarpSettings;
import com.eternalcode.core.feature.warp.WarpTeleportService;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.translation.AbstractTranslation;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import dev.triumphteam.gui.builder.item.BaseItemBuilder;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui;
Expand Down Expand Up @@ -39,75 +39,75 @@ public class WarpInventory {
private static final int BORDER_ROW_COUNT = 2;
private static final int UGLY_BORDER_ROW_COUNT = 1;

private final TranslationManager translationManager;
private final WarpService warpService;
private final Server server;
private final MiniMessage miniMessage;
private final WarpTeleportService warpTeleportService;
private final ConfigurationManager configurationManager;
private final WarpSettings warpSettings;
private final Scheduler scheduler;
private final WarpInventoryConfigService warpInventoryConfigService;

@Inject
WarpInventory(
TranslationManager translationManager,
WarpService warpService,
Server server,
MiniMessage miniMessage,
WarpTeleportService warpTeleportService,
ConfigurationManager configurationManager,
WarpSettings warpSettings,
Scheduler scheduler
Scheduler scheduler,
WarpInventoryConfigService warpInventoryConfigService
) {
this.translationManager = translationManager;
this.warpService = warpService;
this.server = server;
this.miniMessage = miniMessage;
this.warpTeleportService = warpTeleportService;
this.configurationManager = configurationManager;
this.warpSettings = warpSettings;
this.scheduler = scheduler;
this.warpInventoryConfigService = warpInventoryConfigService;
}

public void openInventory(Player player) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WarpInventory#openInventory is redundant. Cut it to open()

Gui gui = this.createInventory(player);
this.scheduler.run(() -> gui.open(player));
this.warpInventoryConfigService.getWarpInventoryData()
.thenAccept(warpData -> {
this.scheduler.run(() -> {
Gui gui = this.createInventory(player, warpData);
gui.open(player);
});
})
.exceptionally(FutureHandler::handleException);
}

private Gui createInventory(Player player) {
Translation translation = this.translationManager.getMessages();
WarpMessages.WarpInventorySection warpSection = translation.warp().warpInventory();

private Gui createInventory(Player player, WarpInventoryConfigService.WarpInventoryConfigData warpData) {
int rowsCount;
int size = warpSection.items().size();
int size = warpData.items().size();

if (!warpSection.border().enabled()) {
if (!warpData.border().enabled()) {
rowsCount = (size + 1) / GUI_ROW_SIZE_WITHOUT_BORDER + 1;
}
else {
switch (warpSection.border().fillType()) {
switch (warpData.border().fillType()) {
case BORDER, ALL -> rowsCount = (size - 1) / GUI_ROW_SIZE_WITH_BORDER + 1 + BORDER_ROW_COUNT;
case TOP, BOTTOM -> rowsCount = (size - 1) / GUI_ROW_SIZE_WITHOUT_BORDER + 1 + UGLY_BORDER_ROW_COUNT;
default -> throw new IllegalStateException("Unexpected value: " + warpSection.border().fillType());
default -> throw new IllegalStateException("Unexpected value: " + warpData.border().fillType());
}
}

Gui gui = Gui.gui()
.title(this.miniMessage.deserialize(warpSection.title()))
.title(this.miniMessage.deserialize(warpData.title()))
.rows(rowsCount)
.disableAllInteractions()
.create();

this.createWarpItems(player, warpSection, gui);
this.createBorder(warpSection, gui);
this.createDecorations(warpSection, gui);
this.createWarpItems(player, warpData, gui);
this.createBorder(warpData, gui);
this.createDecorations(warpData, gui);

return gui;
}

private void createBorder(WarpInventorySection warpSection, Gui gui) {
if (warpSection.border().enabled()) {
WarpInventorySection.BorderSection borderSection = warpSection.border();
private void createBorder(WarpInventoryConfigService.WarpInventoryConfigData warpData, Gui gui) {
if (warpData.border().enabled()) {
WarpInventoryConfig.BorderSection borderSection = warpData.border();

ItemBuilder borderItem = ItemBuilder.from(borderSection.material());

Expand All @@ -134,8 +134,8 @@ private void createBorder(WarpInventorySection warpSection, Gui gui) {
}
}

private void createDecorations(WarpInventorySection warpSection, Gui gui) {
for (ConfigItem item : warpSection.decorationItems().items()) {
private void createDecorations(WarpInventoryConfigService.WarpInventoryConfigData warpData, Gui gui) {
for (ConfigItem item : warpData.decorationItems().items()) {
BaseItemBuilder baseItemBuilder = this.createItem(item);
GuiItem guiItem = baseItemBuilder.asGuiItem();

Expand All @@ -157,8 +157,8 @@ private void createDecorations(WarpInventorySection warpSection, Gui gui) {
}
}

private void createWarpItems(Player player, WarpInventorySection warpSection, Gui gui) {
warpSection.items().values().forEach(item -> {
private void createWarpItems(Player player, WarpInventoryConfigService.WarpInventoryConfigData warpData, Gui gui) {
warpData.items().values().forEach(item -> {
Optional<Warp> warpOptional = this.warpService.findWarp(item.warpName());

if (warpOptional.isEmpty()) {
Expand Down Expand Up @@ -215,34 +215,35 @@ public void addWarp(Warp warp) {
return;
}

AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages();
WarpMessages.WarpInventorySection warpSection = translation.warp().warpInventory();
int slot = getSlot(warpSection);

warpSection.addItem(
warp.getName(),
WarpInventoryItem.builder()
.withWarpName(warp.getName())
.withWarpItem(ConfigItem.builder()
.withName(this.warpSettings.itemNamePrefix() + warp.getName())
.withLore(Collections.singletonList(this.warpSettings.itemLore()))
.withMaterial(this.warpSettings.itemMaterial())
.withTexture(this.warpSettings.itemTexture())
.withSlot(slot)
.withGlow(true)
.build())
.build());

this.configurationManager.save(translation);
this.warpInventoryConfigService.getWarpInventoryData()
.thenAccept(warpData -> {
int slot = getSlot(warpData);

WarpInventoryItem warpInventoryItem = WarpInventoryItem.builder()
.withWarpName(warp.getName())
.withWarpItem(ConfigItem.builder()
.withName(this.warpSettings.itemNamePrefix() + warp.getName())
.withLore(Collections.singletonList(this.warpSettings.itemLore()))
.withMaterial(this.warpSettings.itemMaterial())
.withTexture(this.warpSettings.itemTexture())
.withSlot(slot)
.withGlow(true)
.build())
.build();

this.warpInventoryConfigService.addWarpItem(warp.getName(), warpInventoryItem)
.exceptionally(FutureHandler::handleException);
})
.exceptionally(FutureHandler::handleException);
}

private int getSlot(WarpMessages.WarpInventorySection warpSection) {
int size = warpSection.items().size();
if (!warpSection.border().enabled()) {
private int getSlot(WarpInventoryConfigService.WarpInventoryConfigData warpData) {
int size = warpData.items().size();
if (!warpData.border().enabled()) {
return GUI_ITEM_SLOT_WITHOUT_BORDER + size;
}

return switch (warpSection.border().fillType()) {
return switch (warpData.border().fillType()) {
case BORDER -> GUI_ITEM_SLOT_WITH_BORDER + size + ((size / WarpInventory.GUI_ROW_SIZE_WITH_BORDER) * 2);
case ALL -> GUI_ITEM_SLOT_WITH_ALL_BORDER + size + ((size / WarpInventory.GUI_ROW_SIZE_WITH_BORDER) * 2);
case TOP -> GUI_ITEM_SLOT_WITH_TOP_BORDER + size;
Expand All @@ -255,29 +256,32 @@ public void removeWarp(String warpName) {
return;
}

AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages();
WarpMessages.WarpInventorySection warpSection = translation.warp().warpInventory();
WarpInventoryItem removed = warpSection.removeItem(warpName);

if (removed != null) {
this.shiftWarpItems(removed, warpSection);
}

this.configurationManager.save(translation);
this.warpInventoryConfigService.removeWarpItem(warpName)
.thenAccept(removed -> {
if (removed != null) {
this.shiftWarpItems(removed);
}
})
.exceptionally(FutureHandler::handleException);
}

private void shiftWarpItems(WarpInventoryItem removed, WarpMessages.WarpInventorySection warpSection) {
private void shiftWarpItems(WarpInventoryItem removed) {
int removedSlot = removed.warpItem.slot;
List<WarpInventoryItem> itemsToShift = warpSection.items().values().stream()
.filter(item -> item.warpItem.slot > removedSlot)
.sorted(Comparator.comparingInt(item -> item.warpItem.slot))
.toList();

int currentShift = removedSlot;
for (WarpInventoryItem item : itemsToShift) {
int nextShift = item.warpItem.slot;
item.warpItem.slot = currentShift;
currentShift = nextShift;
}
this.warpInventoryConfigService.getWarpItems()
.thenAccept(items -> {
List<WarpInventoryItem> itemsToShift = items.values().stream()
.filter(item -> item.warpItem.slot > removedSlot)
.sorted(Comparator.comparingInt(item -> item.warpItem.slot))
.toList();

int currentShift = removedSlot;
for (WarpInventoryItem item : itemsToShift) {
int nextShift = item.warpItem.slot;
item.warpItem.slot = currentShift;
currentShift = nextShift;
}
})
.exceptionally(FutureHandler::handleException);
}
}
Loading