diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/permission/WarpAddPermissionCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/permission/WarpAddPermissionCommand.java index 69057d289..86ff4159c 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/permission/WarpAddPermissionCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/command/permission/WarpAddPermissionCommand.java @@ -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; @@ -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; } @@ -38,7 +43,22 @@ void addPermission(@Context Player player, @Arg Warp warp, @Arg String... permis return; } - this.warpService.addPermissions(warp.getName(), permissions); + Collection currentPermissions = warp.getPermissions(); + + List 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) @@ -46,5 +66,4 @@ void addPermission(@Context Player player, @Arg Warp warp, @Arg String... permis .notice(translation -> translation.warp().addPermissions()) .send(); } - } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataDataRepositoryImpl.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataRepositoryImpl.java similarity index 83% rename from eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataDataRepositoryImpl.java rename to eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataRepositoryImpl.java index 153a446ff..854af27ee 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataDataRepositoryImpl.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/warp/data/WarpDataRepositoryImpl.java @@ -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; @@ -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 @@ -53,6 +52,7 @@ public CompletableFuture addWarp(Warp warp) { public CompletableFuture removeWarp(String warp) { return CompletableFuture.runAsync(() -> this.edit(warps -> warps.remove(warp))); } + @Override public CompletableFuture addPermissions(String warp, String... permissions) { return CompletableFuture.runAsync(() -> this.edit(warps -> { @@ -61,10 +61,7 @@ public CompletableFuture addPermissions(String warp, String... permissions return; } - List newPermissions = new ArrayList<>(warpDataConfigRepresenter.permissions); - newPermissions.addAll(List.of(permissions)); - - warpDataConfigRepresenter.permissions = newPermissions; + warpDataConfigRepresenter.permissions.addAll(List.of(permissions)); })); } @@ -77,10 +74,7 @@ public CompletableFuture removePermission(String warp, String permission) return; } - List newPermissions = new ArrayList<>(warpDataConfigRepresenter.permissions); - newPermissions.remove(permission); - - warpDataConfigRepresenter.permissions = newPermissions; + warpDataConfigRepresenter.permissions.remove(permission); })); } @@ -96,7 +90,10 @@ private void edit(Consumer> editor) { @Override public CompletableFuture> 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 @@ -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<>())) ) )); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java index 8317275c3..2dbc258c9 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java @@ -156,6 +156,7 @@ interface WarpSection { Notice addPermissions(); Notice removePermission(); Notice permissionDoesNotExist(); + Notice permissionAlreadyExist(); Notice noPermissionsProvided(); Notice noPermissionAssigned(); Notice missingWarpName(); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java index eac3396dc..28ff3db8e 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java @@ -387,6 +387,7 @@ public static class ENWarpSection implements WarpSection { public Notice removePermission = Notice.chat("Removed permission {PERMISSION} from warp {WARP}!"); public Notice noPermissionsProvided = Notice.chat("No permissions provided!"); public Notice permissionDoesNotExist = Notice.chat("Permission {PERMISSION} doesn't exist!"); + public Notice permissionAlreadyExist = Notice.chat("Permission {PERMISSION} already exists!"); public Notice noPermissionAssigned = Notice.chat("There are no permissions assigned to this warp!"); public Notice missingWarpName = Notice.chat("You must provide a warp name!"); public Notice listPermission = Notice.chat("Permissions for warp {WARP}: {PERMISSIONS}"); diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java index 078a3becf..373bd9468 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java @@ -390,6 +390,7 @@ public static class PLWarpSection implements WarpSection { public Notice removePermission = Notice.chat("Usunięto uprawnienie {PERMISSION} z warpa {WARP}!"); public Notice noPermissionsProvided = Notice.chat("Nie podano żadnych uprawnień!"); public Notice permissionDoesNotExist = Notice.chat("Podane uprawnienie nie istnieje ({PERMISSION})!"); + public Notice permissionAlreadyExist = Notice.chat("Podane uprawnienie już istnieje ({PERMISSION})!"); public Notice noPermissionAssigned = Notice.chat("Ten warp nie ma przypisanych żadnych permisji"); public Notice missingWarpName = Notice.chat("Musisz podać nazwę warpu!"); public Notice listPermission = Notice.chat("Lista uprawnień dla warpa {WARP}: {PERMISSIONS}!");