Skip to content

Commit

Permalink
Add PermissionServiceWrapper to wrap Vault permissions
Browse files Browse the repository at this point in the history
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
NonSwag committed Aug 29, 2024
1 parent 67dfd99 commit 914e53d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.thenextlvl.service.wrapper.VaultPermissionServiceWrapper;
import net.thenextlvl.service.wrapper.service.ChatServiceWrapper;
import net.thenextlvl.service.wrapper.service.EconomyServiceWrapper;
import net.thenextlvl.service.wrapper.service.PermissionServiceWrapper;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.plugin.ServicePriority;
Expand Down Expand Up @@ -145,7 +146,10 @@ private void hookPermissionService(String name, Callable<? extends PermissionCon
}

private void loadServicePermissionWrapper() {

getServer().getServicesManager().getRegistrations(Permission.class).forEach(provider -> {
var wrapper = new PermissionServiceWrapper(provider.getProvider(), this);
getServer().getServicesManager().register(PermissionController.class, wrapper, provider.getPlugin(), provider.getPriority());
});
}

private void loadServiceEconomyWrapper() {
Expand Down
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();
}
}
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;
}
}

0 comments on commit 914e53d

Please sign in to comment.