Skip to content

Commit

Permalink
fix: rcon and command block permissions are now correctly set when us…
Browse files Browse the repository at this point in the history
…ing vane-permissions (fixes #54)
  • Loading branch information
oddlama committed Oct 26, 2021
1 parent bd4c275 commit 716dea6
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import java.util.UUID;

import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.event.server.RemoteServerCommandEvent;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionDefault;

Expand Down Expand Up @@ -106,6 +110,41 @@ public void on_player_quit(PlayerQuitEvent event) {
unregister_player(event.getPlayer());
}

private final Map<CommandSender, PermissionAttachment> sender_attachments = new HashMap<>();
private void add_console_permissions(final CommandSender sender) {
// Register attachment for sender if not done already
if (!sender_attachments.containsKey(sender)) {
final var attachment = sender.addAttachment(this);
sender_attachments.put(sender, attachment);

final var attached_perms = console_attachment.getPermissions();
attached_perms.forEach((p, v) -> {
attachment.setPermission(p, v);
});
}
}

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void on_server_command_event(ServerCommandEvent event) {
final var sender = event.getSender();
if (sender instanceof ConsoleCommandSender) {
// Console command sender will always have the correct permission attachment
return;
}

if (sender.isOp()) {
add_console_permissions(sender);
}
}

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void on_remote_server_command_event(RemoteServerCommandEvent event) {
final var sender = event.getSender();
if (sender.isOp()) {
add_console_permissions(sender);
}
}

/** Resolve references to other permission groups in the hierarchy. */
private void flatten_groups() {
permission_groups.clear();
Expand Down

0 comments on commit 716dea6

Please sign in to comment.