Skip to content

Commit

Permalink
Add methods for spark-paper to check permissions (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Sep 2, 2024
1 parent 0cd9b01 commit 55b3839
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static net.kyori.adventure.text.Component.space;
import static net.kyori.adventure.text.Component.text;
Expand Down Expand Up @@ -341,6 +343,15 @@ private List<Command> getAvailableCommands(CommandSender sender) {
.collect(Collectors.toList());
}

public Set<String> getAllSparkPermissions() {
return Stream.concat(
Stream.of("spark"),
this.commands.stream()
.map(Command::primaryAlias)
.map(alias -> "spark." + alias)
).collect(Collectors.toSet());
}

public boolean hasPermissionForAnyCommand(CommandSender sender) {
return !getAvailableCommands(sender).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public List<String> tabComplete(CommandSender sender, String[] args) {
return this.platform.tabCompleteCommand(new PaperCommandSender(sender), args);
}

@Override
public boolean hasPermission(CommandSender sender) {
return this.platform.hasPermissionForAnyCommand(new PaperCommandSender(sender));
}

@Override
public Collection<String> getPermissions() {
return this.platform.getAllSparkPermissions();
}

@Override
public void onServerTickStart() {
this.tickHook.onTick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.bukkit.Server;
import org.bukkit.command.CommandSender;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -94,6 +96,21 @@ static PaperSparkModule create(Compatibility compatibility, Server server, Logge
*/
List<String> tabComplete(CommandSender sender, String[] args);

/**
* Gets if the sender has permission to execute any spark commands.
*
* @param sender the sender
* @return if the sender has permission
*/
boolean hasPermission(CommandSender sender);

/**
* Gets a collection of all known spark permissions.
*
* @return a set of permissions
*/
Collection<String> getPermissions();

/**
* Called by Paper at the start of each server tick.
*/
Expand Down

0 comments on commit 55b3839

Please sign in to comment.