Skip to content

Commit

Permalink
Follow mr. @Rollczi review.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Jan 12, 2025
1 parent 2a67403 commit 95f8ed3
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface Warp {

List<String> getPermissions();

boolean hasPermission(String permission);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@

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

public interface WarpService {

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

void delete(String warp);
void removeWarp(String warp);

void addPermissions(String warp, String... permissions);

@Experimental
void removePermission(String warp, String permission);

boolean exists(String name);

boolean hasPermission(String warp, String permission);
boolean isExist(String name);

Optional<Warp> findWarp(String name);

Collection<String> getAllNames();

boolean isEmpty();
Collection<Warp> getWarps();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public List<String> getPermissions() {
return Collections.unmodifiableList(this.permissions);
}

@Override
public boolean hasPermission(String permission) {
return this.permissions.contains(permission);
}

public void setPermissions(List<String> permissions) {
this.permissions = permissions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void openInventory(Player player, Language language) {
}

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

Expand Down Expand Up @@ -243,7 +243,7 @@ public void addWarp(Warp warp) {

public boolean removeWarp(String warpName) {

if (!this.warpManager.exists(warpName)) {
if (!this.warpManager.isExist(warpName)) {
return false;
}

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

import com.eternalcode.annotations.scan.feature.FeatureDocs;
import com.eternalcode.commons.bukkit.position.PositionAdapter;
import com.eternalcode.core.feature.warp.data.WarpDataRepository;
import com.eternalcode.core.feature.warp.repository.WarpRepository;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import java.util.ArrayList;
Expand All @@ -23,10 +23,10 @@
class WarpServiceImpl implements WarpService {

private final Map<String, Warp> warps = new ConcurrentHashMap<>();
private final WarpDataRepository warpRepository;
private final WarpRepository warpRepository;

@Inject
private WarpServiceImpl(WarpDataRepository warpRepository) {
private WarpServiceImpl(WarpRepository warpRepository) {
this.warpRepository = warpRepository;

warpRepository.getWarps().thenAcceptAsync(warps -> {
Expand All @@ -37,7 +37,7 @@ private WarpServiceImpl(WarpDataRepository warpRepository) {
}

@Override
public Warp create(String name, Location location) {
public Warp createWarp(String name, Location location) {
Warp warp = new WarpImpl(name, PositionAdapter.convert(location), new ArrayList<>());

this.warps.put(name, warp);
Expand All @@ -47,7 +47,7 @@ public Warp create(String name, Location location) {
}

@Override
public void delete(String warp) {
public void removeWarp(String warp) {
Warp remove = this.warps.remove(warp);

if (remove == null) {
Expand Down Expand Up @@ -108,33 +108,17 @@ private void modifyPermissions(String warpName, Consumer<List<String>> modifier)
}

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

@Override
public boolean hasPermission(String warpName, String permission) {
Warp warp = this.warps.get(warpName);

if (warp == null) {
return false;
}

return warp.getPermissions().contains(permission);
}

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

@Override
public Collection<String> getAllNames() {
return Collections.unmodifiableCollection(this.warps.keySet());
}

@Override
public boolean isEmpty() {
return this.warps.isEmpty();
public Collection<Warp> getWarps() {
return Collections.unmodifiableCollection(this.warps.values());
}
}
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.exists(name)) {
if (!this.warpService.isExist(name)) {
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.warp().notExist())
Expand All @@ -47,7 +47,7 @@ private void removeWarp(Player player, String name) {
return;
}

this.warpService.delete(name);
this.warpService.removeWarp(name);
this.warpInventory.removeWarp(name);

this.noticeService.create()
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.exists(warp)) {
if (this.warpService.isExist(warp)) {
this.noticeService.create()
.player(uniqueId)
.notice(translation -> translation.warp().warpAlreadyExists())
Expand All @@ -54,7 +54,7 @@ private void createWarp(Player player, String warp, UUID uniqueId) {
return;
}

Warp createdWarp = this.warpService.create(warp, player.getLocation());
Warp createdWarp = this.warpService.createWarp(warp, player.getLocation());

this.noticeService.create()
.player(uniqueId)
Expand All @@ -63,7 +63,7 @@ private void createWarp(Player player, String warp, UUID uniqueId) {
.send();

if (this.config.warp.autoAddNewWarps) {
if (this.warpService.getAllNames().size() <= MAX_WARPS_IN_GUI) {
if (this.warpService.getWarps().size() <= MAX_WARPS_IN_GUI) {
this.warpInventory.addWarp(createdWarp);

this.noticeService.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import java.util.Optional;
import java.util.stream.Collectors;
import org.bukkit.command.CommandSender;

@LiteArgument(type = Warp.class)
Expand Down Expand Up @@ -58,8 +59,11 @@ public SuggestionResult suggest(
Argument<Warp> argument,
SuggestionContext context
) {
return this.warpService.getAllNames().stream()
.collect(SuggestionResult.collector());
return SuggestionResult.of(
this.warpService.getWarps().stream()
.map(Warp::getName)
.collect(Collectors.toList())
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import java.util.List;
import org.bukkit.entity.Player;

@RootCommand
Expand Down Expand Up @@ -45,16 +46,18 @@ class WarpCommand {
@DescriptionDocs(description = "Open warp inventory, optionally you can disable this feature in config, if feature is disabled eternalcore will show all available warps")
void warp(@Context Player player, @Context User user) {
if (!this.config.warp.inventoryEnabled) {
List<String> list = this.warpService.getWarps().stream().map(Warp::getName).toList();

this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.warp().available())
.placeholder("{WARPS}", String.join(", ", this.warpService.getAllNames()))
.placeholder("{WARPS}", String.join(this.config.format.separator, list))
.send();

return;
}

if (!this.warpService.isEmpty()) {
if (this.warpService.getWarps().isEmpty()) {
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.warp().noWarps())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.feature.warp.command.permission.argument.WarpPermissionMultipleResolverEntry;
import com.eternalcode.core.feature.warp.command.permission.argument.WarpPermissionEntry;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import dev.rollczi.litecommands.annotations.argument.Arg;
Expand All @@ -28,7 +28,7 @@ public WarpRemovePermissionCommand(WarpService warpService, NoticeService notice
@Execute
void removePermission(
@Context Player player,
@Arg WarpPermissionMultipleResolverEntry entry
@Arg WarpPermissionEntry entry
) {
Warp warp = entry.warp();
String permission = entry.permission();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import com.eternalcode.core.feature.warp.Warp;

public record WarpPermissionMultipleResolverEntry(Warp warp, String permission) {
public record WarpPermissionEntry(Warp warp, String permission) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.util.Optional;
import org.bukkit.command.CommandSender;

@LiteArgument(type = WarpPermissionMultipleResolverEntry.class)
@LiteArgument(type = WarpPermissionEntry.class)
public class WarpPermissionMultiArgumentResolver
implements MultipleArgumentResolver<CommandSender, WarpPermissionMultipleResolverEntry> {
implements MultipleArgumentResolver<CommandSender, WarpPermissionEntry> {

private static final String WARP_PLACEHOLDER_PREFIX = "{WARP}";
private static final String PERMISSION_PLACEHOLDER_PREFIX = "{PERMISSION}";
Expand All @@ -47,9 +47,9 @@ public WarpPermissionMultiArgumentResolver(
}

@Override
public ParseResult<WarpPermissionMultipleResolverEntry> parse(
public ParseResult<WarpPermissionEntry> parse(
Invocation<CommandSender> invocation,
Argument<WarpPermissionMultipleResolverEntry> argument,
Argument<WarpPermissionEntry> argument,
RawInput rawInput
) {
Viewer viewer = this.viewerService.any(invocation.sender());
Expand Down Expand Up @@ -94,25 +94,29 @@ public ParseResult<WarpPermissionMultipleResolverEntry> parse(

String permission = rawInput.next();

return ParseResult.success(new WarpPermissionMultipleResolverEntry(warp.get(), permission));
return ParseResult.success(new WarpPermissionEntry(warp.get(), permission));
}

@Override
public Range getRange(Argument<WarpPermissionMultipleResolverEntry> argument) {
public Range getRange(Argument<WarpPermissionEntry> argument) {
return Range.of(2);
}

@Override
public SuggestionResult suggest(
Invocation<CommandSender> invocation,
Argument<WarpPermissionMultipleResolverEntry> argument,
Argument<WarpPermissionEntry> argument,
SuggestionContext context
) {
Suggestion current = context.getCurrent();
int index = current.lengthMultilevel();

if (index == 1) {
return SuggestionResult.of(this.warpService.getAllNames());
return SuggestionResult.of(
this.warpService.getWarps().stream()
.map(Warp::getName)
.toList()
);
}

if (index == 2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.eternalcode.core.feature.warp.data;
package com.eternalcode.core.feature.warp.repository;

import com.eternalcode.commons.bukkit.position.Position;
import java.util.List;
import net.dzikoysk.cdn.entity.Contextual;

@Contextual
public class WarpDataConfigRepresenter {
class WarpConfigRepresenter {

public Position position;
public List<String> permissions;

public WarpDataConfigRepresenter() {
WarpConfigRepresenter() {
}

public WarpDataConfigRepresenter(Position position, List<String> permissions) {
public WarpConfigRepresenter(Position position, List<String> permissions) {
this.position = position;
this.permissions = permissions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.core.feature.warp.data;
package com.eternalcode.core.feature.warp.repository;

import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
Expand All @@ -11,13 +11,13 @@
import net.dzikoysk.cdn.source.Source;

@ConfigurationFile
public class WarpDataConfig implements ReloadableConfig {
class WarpDataConfig implements ReloadableConfig {

@Exclude
public static final String WARP_DATA_FILE_PATH = "data" + File.separator + "warps.yml";

@Description({"# Warps data", "# These are warp locations, for your own safety, please don't touch it."})
public Map<String, WarpDataConfigRepresenter> warps = new HashMap<>();
public Map<String, WarpConfigRepresenter> warps = new HashMap<>();

@Override
public Resource resource(File folder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.eternalcode.core.feature.warp.data;
package com.eternalcode.core.feature.warp.repository;

import com.eternalcode.core.feature.warp.Warp;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public interface WarpDataRepository {
public interface WarpRepository {

CompletableFuture<Void> addWarp(Warp warp);

Expand Down
Loading

0 comments on commit 95f8ed3

Please sign in to comment.