Skip to content

Commit

Permalink
Attempt fix, rename isExist to exists()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakubk15 committed Jan 18, 2025
1 parent 046c0df commit c519398
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 {

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

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

Optional<Warp> findWarp(String name);

Expand Down
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 @@ -343,9 +343,12 @@ public static class Warp {
@Description("# Warp inventory auto add new warps")
public boolean autoAddNewWarps = true;

@Description("# Should item slots be recalculated after warp deletion")
public boolean recalculateWarpSlots = false;

@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 @@ -22,7 +22,9 @@
import org.bukkit.Server;
import org.bukkit.entity.Player;

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

Expand All @@ -46,6 +48,8 @@ public class WarpInventory {
private final ConfigurationManager configurationManager;
private final PluginConfiguration config;

private Gui gui;

@Inject
WarpInventory(
TranslationManager translationManager,
Expand Down Expand Up @@ -96,17 +100,17 @@ private Gui createInventory(Player player, Language language) {
}
}

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

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

return gui;
return this.gui;
}

private void createBorder(WarpInventorySection warpSection, Gui gui) {
Expand Down Expand Up @@ -215,7 +219,7 @@ private BaseItemBuilder createItem(ConfigItem item) {
}

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

Expand Down Expand Up @@ -260,21 +264,46 @@ public void addWarp(Warp warp) {

public boolean removeWarp(String warpName) {

if (!this.warpManager.isExist(warpName)) {
/*if (!this.warpManager.exists(warpName)) {
System.out.println("Warp does not exist");
return false;
}
}*/

for (Language language : this.translationManager.getAvailableLanguages()) {

AbstractTranslation translation = (AbstractTranslation) this.translationManager.getMessages(language);
Translation.WarpSection.WarpInventorySection warpSection = translation.warp().warpInventory();

warpSection.removeItem(warpName);

this.configurationManager.save(translation);

}
if (this.config.warp.recalculateWarpSlots) {
List<WarpInventoryItem> toAdd = new ArrayList<>();
List<WarpInventoryItem> toRemove = new ArrayList<>();
// Recalculate slots, decrement the slot of each item and replace the items in the GUI
warpSection.items().values().stream()
.sorted(Comparator.comparingInt(item -> item.warpItem().slot()))
.forEach(item -> {
WarpInventoryItem clonedItem = new WarpInventoryItem(item.warpName(), item.warpItem());
toRemove.add(item);

while (this.gui.getGuiItem(clonedItem.warpItem().slot() - 1) == null) {
clonedItem.warpItem().slot -= 1;
}
toAdd.add(clonedItem);
});

for (WarpInventoryItem warp : toRemove) {
warpSection.removeItem(warp.warpName());
}

for (WarpInventoryItem warp : toAdd) {
warpSection.addItem(warp.warpName(), warp);
}

}
this.configurationManager.save(translation);
}
return true;
}
}
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,7 +91,7 @@ private Warp modifyPermissions(String warpName, Consumer<List<String>> modifier)
}

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

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

0 comments on commit c519398

Please sign in to comment.