[SOLVED] Why won't this set crafting permissions?

Discussion in 'Plugin Development' started by Jnorr44, Jul 17, 2012.

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

    Jnorr44

    I am trying to set permissions for crafting Ice and Sponges, which I have added working crafting recipes for. For some reason, these permissions don't work for this. The events are registered in the main class, and the config values do exist and are correct, and the plugin.yml has the permissions.

    Code:
    package com.github.JamesNorris.Flow;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
     
    public class CraftingPermissions implements Listener {
        public CraftingPermissions(Flow instance) {
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void CIE(CraftItemEvent event){
            Player p = (Player) event.getView().getPlayer();
            if (!p.hasPermission("flow.craft.sponge") && event.getResult().equals(Material.SPONGE)){
                p.sendMessage(ChatColor.RED + "You don't have permission to craft SPONGE!");
                event.setCancelled(true);
            }
            if (!p.hasPermission("flow.craft.ice") && event.getResult().equals(Material.ICE)){
                p.sendMessage(ChatColor.RED + "You don't have permission to craft ICE!");
                event.setCancelled(true);
            }
        }
    }
     
  2. Offline

    r0306

    Jnorr44
    Try adding this before cancelling the event.
    Code:
    event.setResult(new ItemStack(Material.AIR, 1));
    Also, make sure your permissions are working correctly by debugging to console.
     
  3. Offline

    Jnorr44

    r0306
    error on setResult() - not applicable for arguments ItemStack

    EDIT - And the if statement with the perms isn't working correctly, even though the permissions are set up right. I think its the part where I am getting the result...

    That returns a result and not an itemstack

    my code: (it only gets to 3)

    Code:
    package com.github.JamesNorris.Flow;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    //TODO get these permissions working
    public class CraftingPermissions implements Listener {
        public CraftingPermissions(Flow instance) {
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void CIE(CraftItemEvent event){
            Player p = (Player) event.getView().getPlayer();
            System.out.println("3");
            if (!p.hasPermission("flow.craft.sponge") && event.getResult().equals(Material.SPONGE)){
                System.out.println("1");
                p.sendMessage(ChatColor.RED + "You don't have permission to craft SPONGE!");
                event.setCancelled(true);
            }
            if (!p.hasPermission("flow.craft.ice") && event.getResult().equals(Material.ICE)){
                System.out.println("2");
                p.sendMessage(ChatColor.RED + "You don't have permission to craft ICE!");
                event.setCancelled(true);
            }
        }
    }
     
  4. Offline

    r0306

    Jnorr44
    Hmm, this:
    Code:
    event.setResult(Result.DENY);
     
  5. Offline

    Jnorr44

    r0306
    That works, but the part that says:

    && event.getResult().equals(Material.SPONGE)

    does not. I removed it and the permission worked fine, but I need a way of making sure the result is a sponge and ice
     
  6. Offline

    jamietech

    EDIT: Maybe I should read threads before commenting on them.
     
  7. Offline

    r0306

    Jnorr44
    I believe it goes something like this:
    Code:
    if (event.getRecipe().getResult().getType() == Material.SPONGE || event.getRecipe().getResult().getType() == Material.ICE)
     
  8. Offline

    Jnorr44

    Tried that, but that isn't correct... I just figured it out though. For your future knowledge (in case you care):
    && event.getCurrentItem().getType() == Material.SPONGE

    that gets the item that you click in the result slot
     
  9. You can just use PrepareItemCraftEvent and set the result item to null if you want it blocked, that way, the item won't even appear in the box and the player will know he can't craft.
    And no, clicking the box won't spawn the item out of nowhere, it's safe.

    EDIT: event.getInventory().setResult(null) would be it.
     
    r0306 likes this.
  10. Offline

    Jnorr44

    Digi, I got that working, but now I have an NPE on line 39 (I have a license at the top so I won't include that and just point out line 39)

    EDIT: you have to scroll to the right

    Code:
    package com.github.JamesNorris.Flow;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
     
    public class CraftingPermissions implements Listener {
        public CraftingPermissions(Flow instance) {
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void PICE(PrepareItemCraftEvent event){
            Player p = (Player) event.getView().getPlayer();
            if (!p.hasPermission("flow.craft.sponge") && event.getInventory().getResult().getType() == Material.SPONGE){
                p.sendMessage(ChatColor.RED + "You don't have permission to craft SPONGE!");
                event.getInventory().setResult(null);
            }
            if (!p.hasPermission("flow.craft.ice") && event.getInventory().getResult().getType() == Material.ICE){//LINE 39!!!!!!!!!!!!!!!!!!!!!!!!!!!
                p.sendMessage(ChatColor.RED + "You don't have permission to craft ICE!");
                event.getInventory().setResult(null);
            }
        }
    }
     
  11. You're setting result to null from the first statement and in the 2nd code you're using .getType() on it, that's why the NPE :)
    Use "else if" instead.
     
  12. Offline

    Jnorr44

    Ahh, okay. I was going to use else if, but didn't know if it would mess it up in any way. It seems like lately I have been changing a bunch of small things and screwing everything up haha. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page