mirror of
https://github.com/rockyhawk64/CommandPanels.git
synced 2025-11-18 07:14:17 +01:00
added grant tag
This commit is contained in:
parent
6d1ce5d4c7
commit
517f5aa8be
@ -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());
|
||||
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user