Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -1,14 +1,14 @@
package com.eternalcode.core.feature.warp;

import org.bukkit.Location;
import org.jetbrains.annotations.ApiStatus.Experimental;

import java.util.Collection;
import java.util.Optional;
import org.jetbrains.annotations.ApiStatus.Experimental;

public interface WarpService {

Warp createWarp(String name, Location location);
Warp createWarp(String warp, Location location);

void removeWarp(String warp);

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

boolean isExist(String name);
boolean exists(String warp);

Optional<Warp> findWarp(String name);
Optional<Warp> findWarp(String warp);

Collection<Warp> getWarps();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.eternalcode.core.feature.jail.JailSettings;
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
import com.eternalcode.core.feature.spawn.SpawnSettings;
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
import net.dzikoysk.cdn.entity.Contextual;
import net.dzikoysk.cdn.entity.Description;
import net.dzikoysk.cdn.entity.Exclude;
Expand Down Expand Up @@ -345,7 +345,7 @@ public static class Warp {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

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

private static final int BORDER_ROW_COUNT = 2;
private static final int UGLY_BORDER_ROW_COUNT = 1;

private final TranslationManager translationManager;
private final LanguageService languageService;
private final WarpService warpManager;
private final WarpService warpService;
private final Server server;
private final MiniMessage miniMessage;
private final WarpTeleportService warpTeleportService;
Expand All @@ -50,7 +54,7 @@ public class WarpInventory {
WarpInventory(
TranslationManager translationManager,
LanguageService languageService,
WarpService warpManager,
WarpService warpService,
Server server,
MiniMessage miniMessage,
WarpTeleportService warpTeleportService,
Expand All @@ -59,7 +63,7 @@ public class WarpInventory {
) {
this.translationManager = translationManager;
this.languageService = languageService;
this.warpManager = warpManager;
this.warpService = warpService;
this.server = server;
this.miniMessage = miniMessage;
this.warpTeleportService = warpTeleportService;
Expand Down Expand Up @@ -90,8 +94,8 @@ private Gui createInventory(Player player, Language language) {
}
else {
switch (warpSection.border().fillType()) {
case BORDER, ALL -> rowsCount = (size + 1) / GUI_ROW_SIZE_WITH_BORDER + 3;
case TOP, BOTTOM -> rowsCount = (size + 1) / GUI_ROW_SIZE_WITHOUT_BORDER + 2;
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());
}
}
Expand Down Expand Up @@ -163,7 +167,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 @@ -215,30 +219,14 @@ private BaseItemBuilder createItem(ConfigItem item) {
}

public void addWarp(Warp warp) {
if (!this.warpManager.isExist(warp.getName())) {
if (!this.warpService.exists(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 @@ -258,23 +246,51 @@ 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()) {

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> itemsToShift = warpSection.items().values().stream()
.filter(item -> item.warpItem.slot > removedSlot)
.sorted(Comparator.comparingInt(item -> item.warpItem.slot))
.toList();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.eternalcode.core.feature.warp.repository.WarpRepository;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import org.bukkit.Location;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -13,7 +15,6 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.bukkit.Location;

@FeatureDocs(
name = "Warp System",
Expand Down Expand Up @@ -90,13 +91,13 @@ private Warp modifyPermissions(String warpName, Consumer<List<String>> modifier)
}

@Override
public boolean isExist(String name) {
return this.warps.containsKey(name);
public boolean exists(String warp) {
return this.warps.containsKey(warp);
}

@Override
public Optional<Warp> findWarp(String name) {
return Optional.ofNullable(this.warps.get(name));
public Optional<Warp> findWarp(String warp) {
return Optional.ofNullable(this.warps.get(warp));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void remove(@Context Player player, @Arg Warp warp) {
}

private void removeWarp(Player player, String name) {
if (!this.warpService.isExist(name)) {
if (!this.warpService.exists(name)) {
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.warp().notExist())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void add(@Context Player player, @Arg String warpName) {
}

private void createWarp(Player player, String warp, UUID uniqueId) {
if (this.warpService.isExist(warp)) {
if (this.warpService.exists(warp)) {
this.noticeService.create()
.player(uniqueId)
.notice(translation -> translation.warp().warpAlreadyExists())
Expand Down
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
Loading