added grant tag

This commit is contained in:
rockyhawk64 2025-08-07 12:34:38 +10:00
parent 6d1ce5d4c7
commit 517f5aa8be
2 changed files with 49 additions and 0 deletions

View File

@ -26,6 +26,7 @@ public class CommandRunner {
resolvers.add(new SessionTag());
resolvers.add(new DataTag());
resolvers.add(new ChatTag());
resolvers.add(new GrantTag());
resolvers.add(new ServerTag());
resolvers.add(new MessageTag());
resolvers.add(new GiveTag());

View File

@ -0,0 +1,48 @@
package me.rockyhawk.commandpanels.interaction.commands.tags;
import me.rockyhawk.commandpanels.Context;
import me.rockyhawk.commandpanels.interaction.commands.CommandTagResolver;
import me.rockyhawk.commandpanels.session.Panel;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
public class GrantTag implements CommandTagResolver {
@Override
public boolean isCorrectTag(String tag) {
return tag.startsWith("[grant]");
}
@Override
public void handle(Context ctx, Panel panel, Player player, String raw, String command) {
Bukkit.getScheduler().runTask(ctx.plugin, () -> {
// Split arguments
String[] parts = command.split("\\s+", 2);
if (parts.length < 2) {
ctx.text.sendError(player, "Invalid syntax. Usage: [grant] permission command");
return;
}
String permission = parts[0];
String commandToExecute = parts[1];
boolean hadPermission = player.hasPermission(permission) || player.isOp();
PermissionAttachment attachment = null;
if (!hadPermission) {
attachment = player.addAttachment(ctx.plugin);
attachment.setPermission(permission, true);
}
// Perform chat operation with the permission attached
player.chat(commandToExecute);
// Cleanup: remove permission only if it was not already granted
if (attachment != null) {
player.removeAttachment(attachment);
}
});
}
}