Adding permissions to example plugin

Discussion in 'Plugin Development' started by amunro, Jul 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    amunro

    Hi, I am trying to add permissions to the scrapbook plugin for the command /clearinventory.

    Here is current code:
    Show Spoiler

    Code:
    package com.dinnerbone.bukkit.scrap.commands;
    
    import com.dinnerbone.bukkit.scrap.ScrapBukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class ClearPluginCommand implements CommandExecutor {
        private final ScrapBukkit plugin;
    
        public ClearPluginCommand(ScrapBukkit plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!sender.isOp()) {
                sender.sendMessage("You do not have permission to clean players' inventories");
                return false;
            }
            if (args.length > 1) {
                return false;
            }
    
            Player player = null;
    
            if (args.length == 1) {
                player = plugin.matchPlayer(args, sender);
                if (player == null) return false;
            } else if (plugin.anonymousCheck(sender)) {
                return false;
            } else {
                player = (Player)sender;
            }
    
            sender.sendMessage("Cleared inventory of " + player.getDisplayName());
            player.getInventory().clear();
            return true;
        }

    I have added the permissions methods to check if permissions is enabled, and then I have created two nodes for "basic.clear.self" and "basic.clear.other"

    Now this is where I am stuck, distinguishing between wether they are checking another player or themselves. I want it so that if the playername was Bob and he had the permission for .self:

    /clearinventory Bob would work fine
    /clearinventory would work fine
    /clearinventory tim would throw up a permission error

    can someone please paste an example of the easiest way to do this?

    Never mind, I think I managed to do it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
Thread Status:
Not open for further replies.

Share This Page