Skip to content

Commit

Permalink
Fix warp inventory items shifting
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jan 18, 2025
1 parent d2409af commit 172c2d6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WarpInventory {
private static final int GUI_ROW_SIZE_WITH_BORDER = 7;

private final TranslationManager translationManager;
private final WarpService warpManager;
private final WarpService warpService;
private final Server server;
private final MiniMessage miniMessage;
private final WarpTeleportService warpTeleportService;
Expand All @@ -47,15 +47,15 @@ public class WarpInventory {
@Inject
WarpInventory(
TranslationManager translationManager,
WarpService warpManager,
WarpService warpService,
Server server,
MiniMessage miniMessage,
WarpTeleportService warpTeleportService,
ConfigurationManager configurationManager,
PluginConfiguration config
) {
this.translationManager = translationManager;
this.warpManager = warpManager;
this.warpService = warpService;
this.server = server;
this.miniMessage = miniMessage;
this.warpTeleportService = warpTeleportService;
Expand Down Expand Up @@ -153,7 +153,7 @@ private void createDecorations(WarpInventorySection warpSection, Gui gui) {

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

if (warpOptional.isEmpty()) {
return;
Expand Down Expand Up @@ -205,30 +205,14 @@ private BaseItemBuilder createItem(ConfigItem item) {
}

public void addWarp(Warp warp) {
if (!this.warpManager.isExist(warp.getName())) {
if (!this.warpService.isExist(warp.getName())) {
return;
}

for (Language language : this.translationManager.getAvailableLanguages()) {
AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages(language);
Translation.WarpSection.WarpInventorySection warpSection = translation.warp().warpInventory();

int size = warpSection.items().size();
int slot;

if (!warpSection.border().enabled()) {
slot = GUI_ITEM_SLOT_WITHOUT_BORDER + size;
}
else {
switch (warpSection.border().fillType()) {
case BORDER -> slot = GUI_ITEM_SLOT_WITH_BORDER + size + ((size / GUI_ROW_SIZE_WITH_BORDER) * 2);
case ALL -> slot = GUI_ITEM_SLOT_WITH_ALL_BORDER + size + ((size / GUI_ROW_SIZE_WITH_BORDER) * 2);
case TOP -> slot = GUI_ITEM_SLOT_WITH_TOP_BORDER + size;
case BOTTOM -> slot = size;
default -> throw new IllegalStateException("Unexpected value: " + warpSection.border().fillType());
}
}

int slot = getSlot(warpSection);

warpSection.addItem(warp.getName(),
WarpInventoryItem.builder()
Expand All @@ -248,23 +232,54 @@ public void addWarp(Warp warp) {
}
}

public boolean removeWarp(String warpName) {
private int getSlot(Translation.WarpSection.WarpInventorySection warpSection) {
int size = warpSection.items().size();
if (!warpSection.border().enabled()) {
return GUI_ITEM_SLOT_WITHOUT_BORDER + size;
}

if (!this.warpManager.isExist(warpName)) {
return false;
return switch (warpSection.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;
case BOTTOM -> size;
};
}

public void removeWarp(String warpName) {
if (!this.config.warp.autoAddNewWarps) {
return;
}

for (Language language : this.translationManager.getAvailableLanguages()) {
if (!this.warpService.isExist(warpName)) {
return;
}

for (Language language : this.translationManager.getAvailableLanguages()) {
AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages(language);
Translation.WarpSection.WarpInventorySection warpSection = translation.warp().warpInventory();
WarpInventoryItem removed = warpSection.removeItem(warpName);

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

this.configurationManager.save(translation);

}
}

private void shiftWarpItems(WarpInventoryItem removed, Translation.WarpSection.WarpInventorySection warpSection) {
int removedSlot = removed.warpItem.slot;
List<WarpInventoryItem> iterator = warpSection.items().values().stream()
.filter(item -> item.warpItem.slot > removedSlot)
.toList();

return true;
int currentShift = removedSlot;
for (WarpInventoryItem item : iterator) {
int nextShift = item.warpItem.slot;
item.warpItem.slot = currentShift;
currentShift = nextShift;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ default void addItem(String name, WarpInventoryItem item) {
this.setItems(items);
}

default void removeItem(String name) {
default WarpInventoryItem removeItem(String name) {
Map<String, WarpInventoryItem> items = new HashMap<>(this.items());
items.remove(name);
WarpInventoryItem removed = items.remove(name);

this.setItems(items);
return removed;
}

BorderSection border();
Expand Down

0 comments on commit 172c2d6

Please sign in to comment.