Solved Permission not working

Discussion in 'Plugin Development' started by SimplyCode, Jul 9, 2014.

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

    SimplyCode

    I've recently been making a plugin that will tp you to people's log off location. I'm trying to add a permission node to it and all works well except for having a return message if the permission node is not in your permissions.

    If I try to add an extra else statement at the bottom it won't work. I just need to know where to put the way to respond if you don't have the permission. Help?

    Code:java
    1. if(args.length == 1){
    2. String target = args[0];
    3.  
    4. if (player.hasPermission("mgv.staff")) {
    5. if(getConfig().contains(target)){
    6. //Grab all the config values
    7. int x = getConfig().getInt(target + ".x");
    8. int y = getConfig().getInt(target + ".y");
    9. int z = getConfig().getInt(target + ".z");
    10. String world = getConfig().getString(target + ".world");
    11. Location loc = new Location(Bukkit.getWorld(world), x, y, z); //Build the location object
    12.  
    13. player.teleport(loc); //Teleport the player
    14. } else {
    15. player.sendMessage(ChatColor.RED + "That player hasn't played before!");
    16. }
    17. }
    18. else {
    19. player.sendMessage(ChatColor.RED + "Usage: /lastlog <player>");
    20. }
    21. }
    22. }
    23. return false;

    Note the code up top is only part of the code, if you need the entire code, I will happily post it.

    Thanks.
     
  2. Offline

    xTigerRebornx

    SimplyCode An else statement would work perfectly fine
    Code:
    if(player.hasPermission("perm.node"){
     
    } else {
     
    }
    Either the Player will have the permission, or they won't.
    Else is ran if they don't.
     
  3. Offline

    SimplyCode

    But where do I put the else statment in my code, as I have two other else statements at the bottom.

    Thanks.
     
  4. Offline

    xTigerRebornx

    SimplyCode simply make the else statement for the if statement that checks for the permission.
     
  5. Offline

    wesley27

    I'm a little confused by the code you posted as to whether there are mistakes with the brackets, or if those are intentional and you left the code there out(you said it isn't the full code). So here's your original code(formatted, as your post's isn't), and I left comments asking where the extra brackets/mistakes are. Correct me if you took code out and I'm wrong :)
    Code:java
    1. if(args.length == 1) {
    2. String target = args[0];
    3.  
    4. if(player.hasPermission("mgv.staff")) {
    5. if(getConfig().contains(target)) {
    6. //Grab all the config values
    7. int x = getConfig().getInt(target + ".x");
    8. int y = getConfig().getInt(target + ".y");
    9. int z = getConfig().getInt(target + ".z");
    10. String world = getConfig().getString(target + ".world");
    11. Location loc = new Location(Bukkit.getWorld(world), x, y, z); //Build the location object
    12.  
    13. player.teleport(loc); //Teleport the player
    14. }
    15. else {
    16. player.sendMessage(ChatColor.RED + "That player hasn't played before!");
    17. }
    18. }
    19. else {
    20. player.sendMessage(ChatColor.Red + "Usage: /lastlog <player>");
    21. }
    22. }
    23. } //this bracket is an extra, it is outside the if(args.length ==1)? Is there more code you left out or is this a mistake?
    24. return false;


    Now, assuming that the mistakes in the code are indeed mistakes, I've fixed the brackets and here's the code showing you where to add the else statement.
    Code:java
    1. if(args.length == 1) {
    2. String target = args[0];
    3.  
    4. if(player.hasPermission("mgv.staff")) {
    5. if(getConfig().contains(target)) {
    6. //Grab all the config values
    7. int x = getConfig().getInt(target + ".x");
    8. int y = getConfig().getInt(target + ".y");
    9. int z = getConfig().getInt(target + ".z");
    10. String world = getConfig().getString(target + ".world");
    11. Location loc = new Location(Bukkit.getWorld(world), x, y, z); //Build the location object
    12.  
    13. player.teleport(loc); //Teleport the player
    14. }
    15. else {
    16. player.sendMessage(ChatColor.RED + "That player hasn't played before!");
    17. }
    18. }
    19. else {
    20. player.sendMessage(ChatColor.Red + "Usage: /lastlog <player>");
    21. player.sendMessage(//put your you dont have permission message here)
    22. //this else ^ is just outside of the if(player.hasPermission), so you would put that here.
    23. }
    24. }
    25. return false;


    *That should be all correct, but I apologize if there is a small error because it is late at night :)
     
Thread Status:
Not open for further replies.

Share This Page