Skip to content

Commit

Permalink
Fix repository name, check if permission already exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Jan 12, 2025
1 parent 708224f commit 2a67403
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.core.feature.warp.command.permission;

import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.feature.warp.Warp;
import com.eternalcode.core.feature.warp.WarpService;
import com.eternalcode.core.injector.annotations.Inject;
Expand All @@ -9,19 +10,23 @@
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.bukkit.entity.Player;

@Command(name = "warp-permission add")
@Permission("eternalcore.warp.changepermissions")
public class WarpAddPermissionCommand {

private final PluginConfiguration config;
private final WarpService warpService;
private final NoticeService noticeService;

@Inject
public WarpAddPermissionCommand(WarpService warpService, NoticeService noticeService) {
public WarpAddPermissionCommand(PluginConfiguration config, WarpService warpService, NoticeService noticeService) {
this.config = config;
this.warpService = warpService;
this.noticeService = noticeService;
}
Expand All @@ -38,13 +43,27 @@ void addPermission(@Context Player player, @Arg Warp warp, @Arg String... permis
return;
}

this.warpService.addPermissions(warp.getName(), permissions);
Collection<String> currentPermissions = warp.getPermissions();

List<String> newPermissions = Arrays.stream(permissions)
.filter(permission -> !currentPermissions.contains(permission))
.toList();

if (newPermissions.isEmpty()) {
this.noticeService.create()
.player(uniqueId)
.placeholder("{WARP}", warp.getName())
.placeholder("{PERMISSION}", String.join(this.config.format.separator, permissions))
.notice(translation -> translation.warp().permissionAlreadyExist())
.send();
return;
}
this.warpService.addPermissions(warp.getName(), newPermissions.toArray(new String[0]));

this.noticeService.create()
.player(uniqueId)
.placeholder("{WARP}", warp.getName())
.notice(translation -> translation.warp().addPermissions())
.send();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.eternalcode.core.feature.warp.WarpImpl;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -18,14 +17,14 @@
import java.util.stream.Collectors;

@Service
public class WarpDataDataRepositoryImpl implements WarpDataRepository {
public class WarpDataRepositoryImpl implements WarpDataRepository {

private final LocationsConfiguration locationsConfiguration;
private final WarpDataConfig warpDataConfig;
private final ConfigurationManager configurationManager;

@Inject
WarpDataDataRepositoryImpl(
WarpDataRepositoryImpl(
ConfigurationManager configurationManager,
LocationsConfiguration locationsConfiguration,
WarpDataConfig warpDataConfig
Expand Down Expand Up @@ -53,6 +52,7 @@ public CompletableFuture<Void> addWarp(Warp warp) {
public CompletableFuture<Void> removeWarp(String warp) {
return CompletableFuture.runAsync(() -> this.edit(warps -> warps.remove(warp)));
}

@Override
public CompletableFuture<Void> addPermissions(String warp, String... permissions) {
return CompletableFuture.runAsync(() -> this.edit(warps -> {
Expand All @@ -61,10 +61,7 @@ public CompletableFuture<Void> addPermissions(String warp, String... permissions
return;
}

List<String> newPermissions = new ArrayList<>(warpDataConfigRepresenter.permissions);
newPermissions.addAll(List.of(permissions));

warpDataConfigRepresenter.permissions = newPermissions;
warpDataConfigRepresenter.permissions.addAll(List.of(permissions));
}));
}

Expand All @@ -77,10 +74,7 @@ public CompletableFuture<Void> removePermission(String warp, String permission)
return;
}

List<String> newPermissions = new ArrayList<>(warpDataConfigRepresenter.permissions);
newPermissions.remove(permission);

warpDataConfigRepresenter.permissions = newPermissions;
warpDataConfigRepresenter.permissions.remove(permission);
}));
}

Expand All @@ -96,7 +90,10 @@ private void edit(Consumer<Map<String, WarpDataConfigRepresenter>> editor) {
@Override
public CompletableFuture<Optional<Warp>> getWarp(String name) {
return CompletableFuture.completedFuture(Optional.ofNullable(this.warpDataConfig.warps.get(name))
.map(warpDataConfigRepresenter -> new WarpImpl(name, warpDataConfigRepresenter.position, warpDataConfigRepresenter.permissions)));
.map(warpDataConfigRepresenter -> new WarpImpl(
name,
warpDataConfigRepresenter.position,
warpDataConfigRepresenter.permissions)));
}

@Override
Expand All @@ -117,8 +114,9 @@ private void migrateWarps() {
this.edit(warps -> warps.putAll(this.locationsConfiguration.warps
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry ->
new WarpDataConfigRepresenter(entry.getValue(), new ArrayList<>()))
.collect(Collectors.toMap(
Map.Entry::getKey, entry ->
new WarpDataConfigRepresenter(entry.getValue(), new ArrayList<>()))
)
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ interface WarpSection {
Notice addPermissions();
Notice removePermission();
Notice permissionDoesNotExist();
Notice permissionAlreadyExist();
Notice noPermissionsProvided();
Notice noPermissionAssigned();
Notice missingWarpName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ public static class ENWarpSection implements WarpSection {
public Notice removePermission = Notice.chat("<red>► <white>Removed permission <red>{PERMISSION}</red> <white>from warp <red>{WARP}<white>!");
public Notice noPermissionsProvided = Notice.chat("<red>✘ <dark_red>No permissions provided!");
public Notice permissionDoesNotExist = Notice.chat("<red>✘ <dark_red>Permission <red>{PERMISSION} <dark_red>doesn't exist!");
public Notice permissionAlreadyExist = Notice.chat("<red>✘ <dark_red>Permission <red>{PERMISSION} <dark_red>already exists!");
public Notice noPermissionAssigned = Notice.chat("<red>✘ <red>There are no permissions assigned to this warp!");
public Notice missingWarpName = Notice.chat("<red>✘ <dark_red>You must provide a warp name!");
public Notice listPermission = Notice.chat("<green>► <white>Permissions for warp <green>{WARP}<white>: <green>{PERMISSIONS}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ public static class PLWarpSection implements WarpSection {
public Notice removePermission = Notice.chat("<red>► <white>Usunięto uprawnienie <red>{PERMISSION}</red> z warpa <red>{WARP}</red>!");
public Notice noPermissionsProvided = Notice.chat("<red>✘ <red>Nie podano żadnych uprawnień!");
public Notice permissionDoesNotExist = Notice.chat("<red>✘ <red>Podane uprawnienie nie istnieje ({PERMISSION})!");
public Notice permissionAlreadyExist = Notice.chat("<red>✘ <red>Podane uprawnienie już istnieje ({PERMISSION})!");
public Notice noPermissionAssigned = Notice.chat("<red>✘ <red>Ten warp nie ma przypisanych żadnych permisji");
public Notice missingWarpName = Notice.chat("<red>✘ <dark_red>Musisz podać nazwę warpu!");
public Notice listPermission = Notice.chat("<green>► <white>Lista uprawnień dla warpa <green>{WARP}<white>: <green>{PERMISSIONS}!");
Expand Down

0 comments on commit 2a67403

Please sign in to comment.