-
-
Notifications
You must be signed in to change notification settings - Fork 20
GH-959 Move warp contents from language files. #1172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vLuckyyy
wants to merge
3
commits into
master
Choose a base branch
from
move-warp-inventory-from-language
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+609
−439
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
.../core/configuration/migrations/Migration_0010_Move_WarpInventory_to_dedicated_config.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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()); | ||
|
||
|
@@ -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(); | ||
|
||
|
@@ -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()) { | ||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
vLuckyyy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.