Permissions Help

Discussion in 'Plugin Development' started by tyler15555, Jan 31, 2012.

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

    tyler15555

    Hi,

    I'm writing a classes plugin, and I'd like to allow users to run a command(preferably from within the plugin, not a external source) that would grant them a certain permission to run commands for that class. Is there anything I can add to my plugin(Like player.addPermission("some.permission")). Thank you for you help and suggestions. Also as a side note, I have tried permissionsEX. It created numerous errors(I made sure to import it into my project), so unfortunately that has not worked for me.
     
  2. Offline

    LaLa

    I think your looking for
    player.addAttachment(plugin, "some.permission", true); // add permission
    player.addAttachment(plugin, "some.permission", false); // deny permission
     
    tyler15555 likes this.
  3. Offline

    tyler15555

    Thank you, for the most part this works. This is probably going to sound really stupid, but should it really be:
    player.addAttachment(plugin,"some.permission",true); or something different. Whenever I try to use plugin, the java class name, or the plugins name I get errors. Thank you so much for your help
     
  4. Offline

    Steeveeo

    If that line is in your main JavaPlugin class, just used the "this" pointer instead of "plugin". However, if you have that in another class, you're going to need to get the actual object of your plugin to that class somehow, either by having it require a Plugin variable in the constructor, or using Bukkit.getPluginManager().getPlugin("pluginName").
     
  5. Offline

    tyler15555

    Thank you, just one last question, whenever I add: public void onPlayerInventoryEvent(PlayerInventoryEvent event) {
    codeishere
    }

    I get an error that says that void is an invalid type for the variable onPlayerInventoryEvent, I also get an error: Syntax error on token")" expected ). However when I add another parentheses it says I should add a semi colon to complete the statement, which I can't do because I need to brackets to signal code execution on this event. Any advice would be appreciated.
     
  6. Offline

    Jozeth

    Code:
    if (player.hasPermission("ENTER.PERMISSION.HERE"))
    ???
     
  7. Offline

    Steeveeo

    Check your import list. IDEs like Eclipse are nice enough to let you hover over an error to correct it. In this case adding the following would fix it:

    Code:java
    1. import org.bukkit.event.player.PlayerInventoryEvent
     
  8. Offline

    tyler15555

    I have checked and PlayerInventoryEvent is imported
     
  9. Offline

    Steeveeo

    Well then, I would suggest posting the entire class here, I have a feeling that you're adding it in a strange place.
     
    9903286 likes this.
  10. Offline

    tyler15555

    This is going to be a lot of code:

    Code:
    package com.CloudCraft.CloudClasses;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerInventoryEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class Merchant extends JavaPlugin {
     
        Logger log = Logger.getLogger("MineCraft");
       
        public void onEnable() {
            log.info("The Merchant class is now enabled");
        }
       
        public void onDisable() {
            log.info("CloudClass is now disabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("Merchant")) {
                Player player = (Player) sender;
                player.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"),"CloudClasses.Merchant", true);
                player.sendMessage("You are now a Merchant");
                return true;
                }
            if(cmd.getName().equalsIgnoreCase("MerchantKit")) {
                Player user = (Player) sender; //Variable "User" is == Player, needed to change the name for some reason :P
                if(user.hasPermission("CloudClass.Merchant")) {
                    PlayerInventory inventory = user.getInventory();
                    ItemStack fence = new ItemStack(Material.FENCE, 15);
                    ItemStack wool = new ItemStack(Material.WOOL, 20);
                    ItemStack sign = new ItemStack(Material.SIGN, 10);
                    ItemStack chest = new ItemStack(Material.CHEST);
                    inventory.addItem(fence, wool, sign, chest);
                    user.sendMessage("You supplies have been restocked");
                }else{
                    user.sendMessage("Only Merchants can run this command!");
                }
            }
            public void onPlayerInventoryEvent(PlayerInventoryEvent event) {
                ItemStack Isword = new ItemStack(Material.IRON_SWORD);
                ItemStack Gsword = new ItemStack(Material.GOLD_SWORD);
                ItemStack Dsword = new ItemStack(Material.DIAMOND_SWORD);
                ItemStack TNT = new ItemStack(Material.TNT);
                Player player = event.getPlayer();
                PlayerInventory inventory = player.getInventory();
               
               
                if (inventory.contains(Isword)) {
                    inventory.remove(Isword);
                    player.sendMessage("Merchants cannot carry a Iron Sword!");
                }
                if(inventory.contains(Gsword)) {
                    inventory.remove(Gsword);
                    player.sendMessage("Merchants cannot carry a Gold Sword!");
                }
                if(inventory.contains(Dsword)) {
                    inventory.remove(Dsword);
                    player.sendMessage("Merchants cannot carry a Diamond Sword");
                }
        }
     
           
        }
     
       
       
    }
    
     
  11. Offline

    tyler15555

    I posted my code. Sorry, forgot to make it a reply >.<
     
  12. Offline

    Steeveeo

    I thought as much, you're defining a function within a function (onPlayerInventoryEvent is in onCommand), which you do not want to do, and cannot do unless you know what you're doing.

    Move the onPlayerInventoryEvent method out of onCommand and it should work.

    (BTW, sorry for the late reply, it's been a busy start of a week.)
     
  13. Offline

    tyler15555

    Thank you, that fixed that bug, now I just have 1 more problem, whenever I try to execute the commands for the plugin, they don't work and I get the error message "Unknown Command". I'm typing them exactly as I wrote them in the plugins code. Thank you so much for your help :)
     
  14. Offline

    Steeveeo

    Please post the contents of your "plugin.yml" file.
     
  15. Offline

    tyler15555

    I'm going to take a guess and assume I have to add my commands to the plugin.YML. It currently goes as follows:
    Code:
    name: CloudClasses
    main: com.CloudCraft.CloudClasses.CloudClasses
    version: 0.1
    author: tyler15555
    I've updated my plugin.yml to the following but commands still don't work:
    Code:
    name: CloudClasses
    main: com.CloudCraft.CloudClasses.CloudClasses
    version: 0.1
    author: tyler15555
    commands:
      ccinfo:
        description: Displays class info
        usage: /ccinfo
      warrior:
        description: Assigns the warrior class
        usage: /warrior
      warriorkit:
        description: Gives a user his class kit
        usage: /warriorkit
        permission: CloudClasses.Warrior
        permission-message: Only warriors can run this command
      Ranger:
        description: Assigns the Ranger class
        usage: /ranger
      RangerKit:
        description: Gives the user the ranger kit
        usage: /rangerkit
        permission: CloudClasses.Ranger
        permission-message: Only rangers can run this command
      Miner:
        description: Assigns the miner class
        usage: /miner
      MinerKit:
        description: Gives users the miner kit
        usage: /minerkit
        permission: CloudClass.Miner
        permission-message: Only miners can run this command
      Merchant:
        description: Assigns the user to the merchant class
        usage: /merchant
      MerchantKit:
        description: Gives the user the merchant kit
        usage: /merchantkit
        permission: Gives the user the Merchant Kit
        permission-message: ONly Merchants can use this command!    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  16. Offline

    Steeveeo

    Hmm, I don't see anything wrong right off the bat. Are there any errors that record to the log when you start it up?
     
  17. Offline

    tyler15555

    I fixed the command bug(needed to change some stuff in my main class), however, permissions don't seem to be working. The plugin does not assign permissions, is there anything else I need to do? Server side or in the plugin?
     
  18. Offline

    Steeveeo

    Have you read this?
     
  19. Offline

    tyler15555

    I don't really understand it, can you better explain it to me? Also, I'm trying to add to my plugin that whenever a player gets killed by another player, they get ignited for a brief moment and have a message sent to them. My code is as follows:
    Code:
    public void onPlayerDeathEvent(PlayerDeathEvent event) {
        Player player = event.getPlayer();
        if(player.hasPermission("CloudClass.Warrior")) {
        Player Killer = event.getKiller();
        Killer.setFireTicks(9);
        Killer.sendMessage("Message");
        player.sendMessage("Message");   
        }
    }
    But I get errors on getKiller() and getPlayer() saying that they aren't defined types for the type PlayerDeathEvent. Can you help me out with this?
     
  20. Offline

    Steeveeo

    It basically says that, if you have a permissions plugin installed, all you need to do to check it is "player.hasPermission("path.to.node")".

    That's because they are not (though I am curious as to why PlayerDeathEvent doesn't even have a getPlayer() method...).

    Seems that the only way to do that is still the really hackish way of watching EntityDamageByEntityEvent and seeing if a player did enough damage to another player to kill them. I could have SWORN that PlayerDeathEvent had methods related to what you're trying to do, but I guess I was wrong.
     
  21. Offline

    tyler15555

    Hi :), I have yet another problem. I've been trying to add some more commands, but after adding a certain amount commands stop functioning. Can you help me out? Heres my code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ){
        if(cmd.getName().equalsIgnoreCase("cclist")) {
            Player player = (Player) sender;
            player.sendMessage(ChatColor.GREEN + "[CloudClasses]" + "Here are all the classes as of Alpha release 0.3");
            player.sendMessage("Warriors should run /warrior");
            player.sendMessage("Archers should run /archer");
            player.sendMessage("Merchants should run /merchant");
            player.sendMessage("Miners should run /miner");
            player.sendMessage("Engineers should run /engineer");
            player.sendMessage("Brewers should run /brewer");
            player.sendMessage("Chemist should run /chemist");
            player.sendMessage("This is all that is available right now, make sure to tell your server admin to check for updates regulary!");
        }
        if(cmd.getName().equalsIgnoreCase("ccinfo")){
            Player player = (Player) sender;
            player.sendMessage(ChatColor.GREEN + "[CloudClasses]" + "Your server is running CloudClasses 0.3 Alpha, codenamed BlackFriday, developed by tyler15555");
        }
        if(cmd.getName().equalsIgnoreCase("Ranger")) {
            Player player = (Player) sender;
            player.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"), "CloudClass.Ranger", true);
            player.sendMessage(ChatColor.DARK_GRAY + "[Ranger]" + "You are now a Ranger");
        }
        if(cmd.getName().equalsIgnoreCase("RangerKit")) {
            Player player = (Player) sender;
            if(player.hasPermission("CloudClass.Ranger")) {
                PlayerInventory inventory = player.getInventory();
                ItemStack map = new ItemStack (Material.MAP);
                ItemStack compass = new ItemStack(Material.COMPASS);
                ItemStack food = new ItemStack(Material.RAW_FISH);
                ItemStack axe = new ItemStack(Material.IRON_AXE);
                ItemStack weapon = new ItemStack(Material.WOOD_SWORD);
                inventory.addItem(map, compass, food, axe, weapon);
                player.sendMessage(ChatColor.DARK_GRAY + "[Ranger]" + "Your equipment has been refilled");
            }else{
                player.sendMessage("Only Rangers can use this command!");
            }
        }
        if(cmd.getName().equalsIgnoreCase("Merchant")) {
            Player player = (Player) sender;
            player.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"),"CloudClasses.Merchant", true);
            player.sendMessage(ChatColor.GOLD + "[Merchant]" + "You are now a Merchant");
           
            }
        if(cmd.getName().equalsIgnoreCase("MerchantKit")) {
            Player user = (Player) sender; //Variable "User" is == Player, needed to change the name for some reason :P
            if(user.hasPermission("CloudClass.Merchant")) {
                PlayerInventory inventory = user.getInventory();
                ItemStack fence = new ItemStack(Material.FENCE, 15);
                ItemStack wool = new ItemStack(Material.WOOL, 20);
                ItemStack sign = new ItemStack(Material.SIGN, 10);
                ItemStack chest = new ItemStack(Material.CHEST);
                inventory.addItem(fence, wool, sign, chest);
                user.sendMessage(ChatColor.GOLD + "[Merchant]" + "You supplies have been restocked");
            }else{
                user.sendMessage("Only Merchants can run this command!");
            }
       
        }
        if(cmd.getName().equalsIgnoreCase("Warrior")) {
            Player player = (Player) sender;
            player.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"), "CloudClass.Warrior", true);
            player.sendMessage(ChatColor.WHITE + "[Warrior]" + "You are now a warrior");
        }
        if(cmd.getName().equalsIgnoreCase("WarriorKit")) {
            Player player = (Player) sender;
            if(player.hasPermission("CloudClass.Warrior")) {
                PlayerInventory inventory = player.getInventory();
                ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
                ItemStack HArmor = new ItemStack(Material.LEATHER_HELMET, 1);
                ItemStack CArmor = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
                ItemStack LArmor = new ItemStack(Material.LEATHER_LEGGINGS, 1);
                ItemStack FArmor = new ItemStack(Material.LEATHER_BOOTS, 1);
                inventory.addItem(sword, HArmor, CArmor, LArmor, FArmor);
                player.sendMessage(ChatColor.WHITE + "[Warrior]" + "Your inventory has been refreshed");
            }
    }
       
       
       
               
            if(cmd.getName().equalsIgnoreCase("Engineer")) {
                Player engineer = (Player) sender;
                engineer.addAttachment(this, "CloudClass.Engineer", true);
                engineer.sendMessage(ChatColor.GREEN + "You are now an engineer");
            }
            if(cmd.getName().equalsIgnoreCase("EngineerKit")) {
                Player engineer = (Player) sender;
                if(engineer.hasPermission("CloudClass.Engineer")){
                PlayerInventory inventory = engineer.getInventory();
                ItemStack rails = new ItemStack(Material.RAILS, 20);
                ItemStack pRails = new ItemStack(Material.POWERED_RAIL, 20);
                ItemStack redstone = new ItemStack(Material.REDSTONE, 10);
                ItemStack rTorch = new ItemStack(Material.REDSTONE_TORCH_ON, 10);
                ItemStack iron = new ItemStack(Material.IRON_INGOT, 3);
                inventory.addItem(rails, pRails, redstone, rTorch, iron);
                engineer.sendMessage(ChatColor.GREEN + "Your supplies have been refilled");
            }else{
                engineer.sendMessage("Only engineers can run this command!");
            }
            if(cmd.getName().equalsIgnoreCase("Brewer")) {
                Player brewer = (Player) sender;
                brewer.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"), "CloudClass.Brewer", true);
                brewer.sendMessage("You are now a Brewer");
            }
            if(cmd.getName().equalsIgnoreCase("BrewerKit")) {
                Player brewer = (Player) sender;
                if(brewer.hasPermission("CloudClass.Brewer")) {
                PlayerInventory inventory = brewer.getInventory();
                ItemStack Bstand = new ItemStack(Material.BREWING_STAND, 1);
                ItemStack potion = new ItemStack(Material.POTION, 5);
                brewer.sendMessage(ChatColor.YELLOW + "[Brewer]" + "Your supplies have been refilled");
            }else{
                brewer.sendMessage("Only Brewers can run this command!");
            }
            }
            if(cmd.getName().equalsIgnoreCase("SuperPower")) {
                Player brewer = (Player) sender;
                brewer.sendMessage("This does nothing..... Yet");
            }
            if(cmd.getName().equalsIgnoreCase("Chemist")) {
                Player chemist = (Player) sender;
                chemist.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"), "CloudClass.Chemist", true);
                chemist.sendMessage(ChatColor.DARK_GREEN + "[Chemist]" + "You are now a chemist");
            }
            if(cmd.getName().equalsIgnoreCase("ChemistKit")) {
            Player chemist = (Player) sender;
            PlayerInventory inventory = chemist.getInventory();
            if(chemist.hasPermission("CloudClass.Chemist")) {
                ItemStack rStoneWire = new ItemStack(Material.REDSTONE_WIRE, 5);
                ItemStack gStoneDust = new ItemStack(Material.GLOWSTONE_DUST, 5);
                ItemStack nWart = new ItemStack(Material.NETHER_WARTS, 5);
                ItemStack Sugar = new ItemStack(Material.SUGAR);
                ItemStack gPowder = new ItemStack(Material.SULPHUR, 5);
                ItemStack caldrn = new ItemStack(Material.CAULDRON, 1);
                inventory.addItem(rStoneWire, gStoneDust, nWart, Sugar, gPowder, caldrn);
                chemist.sendMessage(ChatColor.DARK_GREEN + "[Chemist]" + "Your supplies have been refilled");
            }else{
                chemist.sendMessage("Only Chemist can run this command");
            }
            if(cmd.getName().equalsIgnoreCase("Farmer")) {
                Player farmer = (Player) sender;
                farmer.addAttachment(Bukkit.getPluginManager().getPlugin("CloudClasses"), "CloudClass.Farmer", true);
                farmer.sendMessage(ChatColor.YELLOW + "[Farmer]" + "You are now a farmer");
            }
            if(cmd.getName().equalsIgnoreCase("FarmerKit")) {
                Player farmer = (Player) sender;
                if(farmer.hasPermission("CloudClass.Farmer")) {
                PlayerInventory tools = farmer.getInventory(); //Had to rename "inventory" to "tools" for some reason
                    ItemStack gHoe = new ItemStack(Material.GOLD_HOE, 1);
                    ItemStack seeds = new ItemStack(Material.SEEDS, 10);
                    ItemStack dirt = new ItemStack(Material.DIRT, 10);
                    ItemStack bucket = new ItemStack(Material.WATER_BUCKET, 1);
                    tools.addItem(gHoe, seeds, dirt, bucket);
                }
            }
            }
            }
     
            return true;
     
           
    }
    Thats just the command part :p
     
  22. Offline

    Steeveeo

    Check your indentations and brackets. From your post it looks like you've defined more commands outside of the block that the others are, maybe even inside another command definition.
     
  23. Offline

    Ice_Sword

    After you've tried Steeveeo's suggestion, I have a suspicion. You have "return true;" outside of your command blocks. Try changing that to "return false;". Then add "return true;" at the end of every code block where you process a successful command.
     
  24. Offline

    tyler15555

    Thank you so much for your help :). I have on final question though. When I try to add an argument to a command(A players name) I get an error that says: "The method getPlayer(String) is undefined for the type getWorld(). Here is my code:
    Code:
    Player s = (Player) sender;
                Player employer = s.getWorld().getPlayer(args[0]);
    I notice that there is a getPlayers() function though. Can I use this a a argument(In the same sense as /command players name. I.E Not having to declare the individual players) as a replacement?
     
  25. Offline

    Steeveeo

    I know that this is a week overdue, Finals Week happened.

    The JavaDocs are your friend. Looking through them can tell you what classes can and cannot do. If you want to get a player from a name, try using Bukkit.matchPlayer(args[0]), which will return a list of players containing the given string in their names. Just check if the list is empty and get the first entry.

    (Just a note, I know I linked Server.matchPlayer(), and that's because it's the same as the static Bukkit.matchPlayer() but with a little more description on it).
     
Thread Status:
Not open for further replies.

Share This Page