-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PermissionServiceWrapper to wrap Vault permissions
Introduced PermissionServiceWrapper and WrappedPermissionHolder to encapsulate Vault's Permission functionalities. Modified ServicePlugin to register the PermissionServiceWrapper, ensuring permission management is integrated with Vault.
- Loading branch information
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
plugin/src/main/java/net/thenextlvl/service/wrapper/service/PermissionServiceWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package net.thenextlvl.service.wrapper.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.milkbowl.vault.permission.Permission; | ||
import net.thenextlvl.service.ServicePlugin; | ||
import net.thenextlvl.service.api.permission.PermissionController; | ||
import net.thenextlvl.service.api.permission.PermissionHolder; | ||
import net.thenextlvl.service.wrapper.service.model.WrappedPermissionHolder; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@RequiredArgsConstructor | ||
public class PermissionServiceWrapper implements PermissionController { | ||
private final Permission permission; | ||
private final ServicePlugin plugin; | ||
|
||
@Override | ||
public CompletableFuture<PermissionHolder> loadPermissionHolder(OfflinePlayer player) { | ||
return CompletableFuture.completedFuture(getPermissionHolder(player).orElse(null)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<PermissionHolder> loadPermissionHolder(OfflinePlayer player, World world) { | ||
return CompletableFuture.completedFuture(getPermissionHolder(player, world).orElse(null)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<PermissionHolder> loadPermissionHolder(UUID uuid) { | ||
return CompletableFuture.completedFuture(getPermissionHolder(uuid).orElse(null)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<PermissionHolder> loadPermissionHolder(UUID uuid, World world) { | ||
return CompletableFuture.completedFuture(getPermissionHolder(uuid, world).orElse(null)); | ||
} | ||
|
||
@Override | ||
public Optional<PermissionHolder> getPermissionHolder(OfflinePlayer player) { | ||
return getPermissionHolder(player.getPlayer(), null); | ||
} | ||
|
||
@Override | ||
public Optional<PermissionHolder> getPermissionHolder(OfflinePlayer player, World world) { | ||
return getPermissionHolder(player.getPlayer(), world); | ||
} | ||
|
||
@Override | ||
public Optional<PermissionHolder> getPermissionHolder(UUID uuid) { | ||
return getPermissionHolder(plugin.getServer().getPlayer(uuid), null); | ||
} | ||
|
||
@Override | ||
public Optional<PermissionHolder> getPermissionHolder(UUID uuid, World world) { | ||
return getPermissionHolder(plugin.getServer().getPlayer(uuid), world); | ||
} | ||
|
||
private Optional<PermissionHolder> getPermissionHolder(@Nullable Player player, @Nullable World world) { | ||
return Optional.ofNullable(player).map(online -> new WrappedPermissionHolder(world, online, permission)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return permission.getName(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...n/src/main/java/net/thenextlvl/service/wrapper/service/model/WrappedPermissionHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.thenextlvl.service.wrapper.service.model; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import net.kyori.adventure.util.TriState; | ||
import net.milkbowl.vault.permission.Permission; | ||
import net.thenextlvl.service.api.permission.PermissionHolder; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
@RequiredArgsConstructor | ||
public class WrappedPermissionHolder implements PermissionHolder { | ||
private final @Nullable World world; | ||
private final Player holder; | ||
private final Permission permission; | ||
|
||
@Override | ||
public TriState checkPermission(String permission) { | ||
return TriState.byBoolean(this.permission.playerHas(world != null ? world.getName() : null, holder, permission)); | ||
} | ||
|
||
@Override | ||
public boolean addPermission(String permission) { | ||
return this.permission.playerAdd(world != null ? world.getName() : null, holder, permission); | ||
} | ||
|
||
@Override | ||
public boolean removePermission(String permission) { | ||
return this.permission.playerRemove(world != null ? world.getName() : null, holder, permission); | ||
} | ||
|
||
@Override | ||
public <T> Optional<T> getInfoNode(String key, Function<@Nullable String, @Nullable T> mapper) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public boolean removeInfoNode(String key) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean setInfoNode(String key, String value) { | ||
return false; | ||
} | ||
} |