Permissions for Custom Recipes

Discussion in 'Spigot Plugin Development' started by RapidTurtle, Jul 2, 2015.

  1. -------------------------------------------------------------------------------------
    Hey everyone, on a plugin, I am developing, I am trying to make it so
    you need the permission "bw.craft" to craft a Barbed wire, and if you don't have it, it reads off a text,
    heres the code;

    Code (Text):
          @EventHandler(priority = EventPriority.HIGHEST)
            public void onItemCraft(CraftItemEvent e)
            {
                HumanEntity he = e.getWhoClicked();
                Player p = (Player) he;
                if(!(e.getWhoClicked() instanceof Player)) return; //Make sure it was a player
                if(e.getRecipe() != barbedWire) return; //Check of it was your recipe
                if(!p.hasPermission("bw.craft")) { //Check if they don't have the bypass permission
                e.setCancelled(true); //Cancel the event
                p.sendMessage("Thou art permission");} //Send the player a no permission message
                else {
                    p.sendMessage("Granted Permission");
    The plugin is working but the permission part isn't. It's not recognizing the permissions, heres the plugin yml
    Code (Text):
    name: GSBOBarbedWire
    main: com.Shlobdon.GSBOBarbedWire.BarbedWire
    version: 1.4.4
    description: Adds barbed wire to the game in the form of cobwebs.
    author: Shlobdon
    database: false

    commands:
       bw:
          description: This gives a player a barbed wire.
          usage: |
              /<command> give <amount>
              /<command> ticket <message>
     
    permissions:
        bw.give:
            description: Gives a player access to using the /bw give command, to give him a BarbedWire.
            default: op
        bw.ticket:
            description: Gives a player access to submitting tickets directly to the plugin author with /bw ticket.
            default: op
        bw.craft:
            description: Gives a player access to craft barbed wire.
            default: op
    Soo yeeahh thank you ~~
     
  2. sothatsit

    Patron

    If you see "Granted Permission" it is a permissions issue.

    Otherwise, you are not supposed to use != or == when comparing objects. You are only supposed to use them when comparing primitive data types (Boolean, int, enums).

    Use .equals() instead.
     
  3. You should follow sothatsit's advice, as well as add some curly braces. Sometimes it literally makes the difference between 1 and 0.
     
  4. yeah try adding some curly braces and understand the code instead of just copy pasting it from a tutorial.
     
  5. ...? This is not from a tutorial xd?


    I'll try when i get home, thanks! :)
     
  6. 1) You are casting HumanEntity to Player before you are checking if its actually a player.
    2) You ignore the fact that you stored event.getWhoClicked() in a variable, and invoke that method again when you check if its a player.
    3) You can never have too many curly brackets (as mentioned before), but you can definitely have too few.
    4) I would suggest returning after you send "Thou art permission" to the player. Then you wont need that last else block.
    5) Speaking of blocks. Indent them.
    6) The code as-is, should be working (although its crap code). Make sure you are not OP when you are testing.
     
  7. comments give it away...