What the hell is wrong with player.hasPermission

Discussion in 'Plugin Development' started by johnny_boy, Apr 10, 2017.

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

    johnny_boy

    i have already searched on the internet but it just doesn't work... Here is my code:
    Code:Java
    1.  
    2. package me.p250.firearrow;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.OfflinePlayer;
    7. import org.bukkit.Sound;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem;
    10. import org.bukkit.entity.Arrow;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.EntityType;
    13. import org.bukkit.entity.Item;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.entity.Projectile;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.EventPriority;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.Action;
    20. import org.bukkit.event.block.BlockPlaceEvent;
    21. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    22. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    23. import org.bukkit.event.entity.ProjectileHitEvent;
    24. import org.bukkit.event.player.PlayerInteractEvent;
    25. import org.bukkit.event.player.PlayerPickupItemEvent;
    26. import org.bukkit.inventory.ItemStack;
    27. import org.bukkit.permissions.Permission;
    28.  
    29. public class FireArrow implements Listener {
    30.  
    31. @EventHandler(priority=EventPriority.HIGH)
    32. public void onBreakEvent(PlayerInteractEvent e) {
    33. Player player = e.getPlayer();
    34.  
    35. Permission perm = new Permission("firearrow.use");
    36.  
    37. if (!(player.hasPermission(perm))) {
    38. return;
    39. }
    40.  
    41. Action act = e.getAction();
    42.  
    43. if ((act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) && player.getInventory().getItemInMainHand().getType() == Material.STICK) {
    44. Arrow arrow = player.launchProjectile(Arrow.class);
    45. arrow.setGravity(false);
    46. arrow.setVelocity(player.getLocation().getDirection().multiply(2.2));
    47. player.playSound(player.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 15, 1);
    48. }
    49. }
    50.  
    51. @EventHandler(priority=EventPriority.HIGH)
    52. public void onPickupItemEvent(ProjectileHitEvent e) {
    53. if (e.getEntity() instanceof Arrow) {
    54. e.getEntity().remove();
    55. }
    56. }
    57. }
    58.  
    59.  


    And my permission.yml
    Code:yml
    1.  
    2. groups:
    3. default:
    4. options:
    5. default: true
    6. permissions:
    7. - modifyworld.*
    8. schema-version: 1
    9.  
     
  2. Offline

    timtower Moderator Moderator

    @MemeplexOwner Well, you don't have the permission in your permission.yml.
    So what would be broken about it?
     
  3. Offline

    johnny_boy

    I am saying If I don't have the permission it should not run at all, but it still runs when I right click the stick EVEN THOUGH I don't have the permission.
     
  4. Offline

    timtower Moderator Moderator

  5. Offline

    johnny_boy

    System.out.println(return);

    ??

    EDIT: It seems that the hasPermission is never being ran at all...
     
  6. Offline

    timtower Moderator Moderator

  7. Offline

    johnny_boy

    It is returning nothing because my if statement is never being ran -_- For some reason...
     
  8. Offline

    timtower Moderator Moderator

    @MemeplexOwner 1. Remove the event priority stuff, you don't need it.
    2. Please post your onEnable and all methods that it calls.
     
  9. Offline

    johnny_boy

    onEnable
    Code:Java
    1.  
    2. package me.p250.firearrow;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class MainClass extends JavaPlugin {
    8.  
    9.  
    10. public void onEnable() {
    11. Bukkit.getPluginManager().registerEvents(new FireArrow(), this);
    12. Bukkit.getPluginManager().registerEvents(new SetDamage(), this);
    13.  
    14. }
    15.  
    16.  
    17.  
    18. }
    19.  


    FireArrow (Don't worry about SetDamage)
    Code:Java
    1.  
    2. package me.p250.firearrow;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.OfflinePlayer;
    7. import org.bukkit.Sound;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem;
    10. import org.bukkit.entity.Arrow;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.EntityType;
    13. import org.bukkit.entity.Item;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.entity.Projectile;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.EventPriority;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.Action;
    20. import org.bukkit.event.block.BlockPlaceEvent;
    21. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    22. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    23. import org.bukkit.event.entity.ProjectileHitEvent;
    24. import org.bukkit.event.player.PlayerInteractEvent;
    25. import org.bukkit.event.player.PlayerPickupItemEvent;
    26. import org.bukkit.inventory.ItemStack;
    27. import org.bukkit.permissions.Permission;
    28.  
    29. public class FireArrow implements Listener {
    30.  
    31. @EventHandler
    32. public void onBreakEvent(PlayerInteractEvent e) {
    33. Player player = e.getPlayer();
    34.  
    35. Permission perm = new Permission("firearrow.use");
    36.  
    37. if (!(player.hasPermission(perm))) {
    38. return;
    39. }
    40.  
    41. Action act = e.getAction();
    42.  
    43. if ((act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) && player.getInventory().getItemInMainHand().getType() == Material.STICK) {
    44. Arrow arrow = player.launchProjectile(Arrow.class);
    45. arrow.setGravity(false);
    46. arrow.setVelocity(player.getLocation().getDirection().multiply(2.2));
    47. player.playSound(player.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 15, 1);
    48. }
    49. }
    50.  
    51. @EventHandler
    52. public void onPickupItemEvent(ProjectileHitEvent e) {
    53. if (e.getEntity() instanceof Arrow) {
    54. e.getEntity().remove();
    55. }
    56. }
    57. }
    58.  
    59.  
     
  10. Offline

    timtower Moderator Moderator

  11. Offline

    johnny_boy

    It doesn't though... Other people are having issues with this also

    Can you try it and see if it works for you?
     
  12. Offline

    timtower Moderator Moderator

    I have more important things to do the next 24 hours. Not able to test plugins.
    Put the events in the main class, see if it works there, put a println in the first line of the method.
     
  13. Offline

    Erumeldor

    Just debug it, @MemeplexOwner . Put some println in it, and check the results.

    So, e.g. something like that.
    There you can see where it gets, and where the error is.

    Code:
    public void onBreakEvent(PlayerInteractEvent e) {
       
        System.out.println("Event started!");
    
        Player player = e.getPlayer();
        Permission perm = new Permission("firearrow.use");
       
        System.out.println("Player " + player.getName() + " has permission: " + player.hasPermission(perm));
       
        if (!(player.hasPermission(perm))) {
            return;
        }
        Action act = e.getAction();
        if ((act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) && player.getInventory().getItemInMainHand().getType() == Material.STICK) {
            Arrow arrow = player.launchProjectile(Arrow.class);
            arrow.setGravity(false);
            arrow.setVelocity(player.getLocation().getDirection().multiply(2.2));
            player.playSound(player.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 15, 1);
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page