permissions and gui

Discussion in 'Plugin Development' started by TerroDoor, Aug 11, 2019.

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

    TerroDoor

    when the player opens the gui to select his loadout (clickevent) i need to check all the permissions that the specific player has. how would i get all the permissions that contain: "userloadouts.<loadout names>".

    for example, there is a bunch of different loadouts: "loadouts.assault" "loadouts.tactical" "loadouts.sniper"

    what method do i use to check thru all the "loadouts." perms and see what the player does and doesnt have

    im using permissionsEx for this, i dont want to check if the p.hasPermission for every kit i'd rather loop thru every loadout, please help! Thanks

    Edit: I'm trying to display each loadout as either "LOCKED" or "UNLOCKED" in the gui depending on per player permissions
     
  2. Offline

    KarimAKL

    @TerroDoor I think the best way would be to simply get all the loadouts, then use 'p.hasPermission("loadouts."+loadout.getName())' (that's example code), i know you said you don't want to do this but i think the only other way is to hook into PermissionsEx, but that would require a dependency, so unless you already depend on PermissionsEx, i don't think there's any reason to do that.
     
  3. Offline

    TerroDoor

    @KarinAKL thanks for your help! I'll use permissionsEx for this, if i have multiple loadouts in the "loadouts." permission, Can I simply get all the different loadouts just by using:

    if (p.hasPermission("loadouts.")) {
     
  4. Offline

    KarimAKL

    @TerroDoor I haven't tried it, but i don't think that's the case.
     
  5. Offline

    TerroDoor

    @KarimAKL So after the clickevent is fired and the gui opens, What would I have to do?
     
  6. Offline

    KarimAKL

    @TerroDoor Loop all the loadouts (you might have them in a list or something), then check if the player has the permission 'loadouts.<loadout>', replace <loadout> with the loadout's name. (get the name in the loop)
     
  7. Offline

    TerroDoor

    @KarimAKL Thankyou! your response has helped me heaps already, can i be shown a quick example of looping thru the "loadouts." list. I only have the abilities in lists for when the player has chosen the loadout. im just using the perms to check wether they can access the loadout or not.

    if im looping thru "loadouts." perms, do i need to check if p.hasPermission.<loadout name>

    Thanks for your time Karim.
     
  8. Offline

    CraftCreeper6

    @TerroDoor
    I think @KarimAKL means create a list of all possible loadouts (not the permission nodes), and then loop over each one, then, check p.hasPermission("loadouts." + loadoutName)
     
    TerroDoor likes this.
  9. Offline

    TerroDoor

    Could I be shown quickly? String sniper = "loadouts.sniper"? I'm sorry for making this hard
    (I'm learning loops)

    Thanks @CraftCreeper6

    Sent from my ZTE BLADE A602 using Tapatalk
     
  10. Offline

    CraftCreeper6

    @TerroDoor
    A list, not a string.

    A list can store multiple values of the same data type (usually).

    Read this: https://www.javatpoint.com/java-list

    On this website you'll find the following snippet:
    Code:
    List<String> al=new ArrayList<String>();  
    This is a list that can store strings. This is likely what you want (unless you have a unique data structure for each loadout).

    Also on that website you'll find list#add(toAdd);
    Code:
    al.add("Amit");  
    That code will add the word "Amit" to the list.

    Instead of Amit, you would want to add the name of the kit (and subsequently the name of the permission node) to the list. Then, use a for loop to iterate over each value in the list and check if p.hasPermission("loadouts." + loadoutName)
     
  11. Offline

    KarimAKL

    @TerroDoor I mean something like this:
    Code:Java
    1. // Loadout class
    2. public class Loadout {
    3.  
    4. private final String name;
    5.  
    6. public Loadout(String name) {
    7. this.name = name;
    8. }
    9.  
    10. public String getName() {return name;}
    11. }
    12.  
    13. // Inside some other class
    14. List<Loadout> list = new ArrayList<>();
    15. list.add(new Loadout("sniper"));
    16. list.add(new Loadout("something"));
    17.  
    18. for (Loadout loadout : list) {
    19. if (player.hasPermission("loadouts."+loadout.getName())) {
    20. // If they have permission
    21. } else {
    22. // If they do not have permission
    23. }
    24. }


    EDIT: You can make a private static list inside the Loadout class and then add the loadout to the list in the constructor, then make a public static method to access the list. (or a copy of the list)
     
    TerroDoor likes this.
  12. Offline

    TerroDoor

    Thankyou, I will get some revision and work done tonight I'll be back with what ive got! Appreciate your time @KarimAKL , @CraftCreeper6

    Sent from my ZTE BLADE A602 using Tapatalk
     
    KarimAKL and CraftCreeper6 like this.
  13. Offline

    TerroDoor

    I've done a little bit of revision and tried to grasp as much understanding as i could, here's what i've got but im stuck with displaying the items in the inv .

    Code:
    
    public class Kits implements Listener {
    
        private Inventory kitinv = Bukkit.createInventory(null, 9, "select your kit");
    
        private ArrayList<String> kits = new ArrayList<String>();
       
        String soldierkit = new String("soldier");
        String archerkit = new String("archer");
       //defining the strings
    
        @EventHandler
        public void onOpenInv(PlayerInteractEvent e) {
           
            Player p = (Player)e.getPlayer();
           
            kits.add(soldierkit);
            kits.add(archerkit);
           //putting the strings into my array
    
            if (p.getInventory().getItemInMainHand().getType() == Material.FEATHER) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                   
                    p.openInventory(kitinv);
                   //open the inventory
    
    //in my understanding, the loop is telling me that i will be looking for a string inside of my object(Array), correct me if im wrong as I'm new to this.
    
                    for (String k : kits) {
                       
                        if (p.hasPermission("jkits." + k)) {
                           
                            //setting them to "UNLOCKED"
    //Do i need to set Item Meta's?
                           
                        } else {
                           
                            //setting them to "LOCKED"?
                           
                        }
                    }
                }
               
            } else {
               
                return;
               
            }
        }
    }
    
    Thanks!
     
  14. Offline

    KarimAKL

    @TerroDoor
    1. You can just set the strings to "soldier" and "archer", there's no need to write 'new String("soldier")'.
    2. The loop gets every element from the list from lowest index to highest. ( 0 - List#getSize() )
    3. You need to set the ItemMeta if you want to change the name or lore.
     
  15. Offline

    TerroDoor

    @KarimAKL Thanks for the quick response, i've hooked it into PermissionsEx and have made it give me a diamond if i have the permission and a peice of coal if i dont, its only giving me the diamonds even when i dont have permission?
    Also, do i need to use "kits.getSize()" inside of my loop?

    EDIT: i was logged into a different account and de-opped the wrong user, it works guys thanks for your time!
     
  16. Offline

    CraftCreeper6

  17. Offline

    KarimAKL

    @TerroDoor
    1. Why have you decided to hook into PermissionsEx if you aren't using it?
    2. What code gives the diamonds/coal?
    3. No, you are using an enhanced for loop, not a for loop.
     
  18. Offline

    TerroDoor

    I'm using PermissionsEx so i can simply add or remove a permission to the player depending on the kits they have purchased. in my case im using "jkits.soldier" and "jkits.archer".

    I forgot to update my code I'm sorry to confuse you.

    Code:
    
    public class Kits implements Listener {
    
        private Inventory kitinv = Bukkit.createInventory(null, 9, "select your kit");
    
        private ArrayList<String> kits = new ArrayList<String>();
      
        String soldierkit = "soldier";
        String archerkit = "archer";
      
        @EventHandler
        public void onOpenInv(PlayerInteractEvent e) {
          
            Player p = (Player)e.getPlayer();
          
            kits.add(soldierkit);
            kits.add(archerkit);
          
            if (p.getInventory().getItemInMainHand().getType() == Material.FEATHER) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                  
                    p.openInventory(kitinv);
                  
                    for (String k : kits) {
                      
                        if (p.hasPermission("jkits." + k)) {
                          
                            p.getInventory().addItem(new ItemStack(Material.DIAMOND));
                          
                            //setting them to "UNLOCKED"? ItemMeta?
                          
                        } else {
                          
                            //setting them to "LOCKED"?
                            p.getInventory().addItem(new ItemStack(Material.COAL));
    
                        }
                    }
                }
              
            } else {
              
                return;
              
            }
        }
    }
    
    
    EDIT: I used the diamond's and coal to see if it worked. what I really want to do now is set every kit that the player has permission for to green text, and every kit they dont have perms for, set to red.
     
  19. Offline

    CraftCreeper6

  20. Offline

    TerroDoor

  21. Offline

    CraftCreeper6

    @TerroDoor
    When you're OP you have all permissions no matter what.
     
  22. Offline

    TerroDoor

    Yeah I realised shortly after, thanks a lot for the help, If the player did have permission and the item in the inventory already has a custom name, how would I add "unlocked" to it? As a lore

    Sent from my ZTE BLADE A602 using Tapatalk
     
  23. Offline

    CraftCreeper6

  24. Offline

    TerroDoor

    Is it possible? If I have an Item Meta and Lore set for each kit in the inventory, would I have to create two extra Lores called "Unlocked" and "locked" and add those in the loop?

    hasLocalizedName

    I was looking into the docs would this check for an existing Lore?

    Sent from my ZTE BLADE A602 using Tapatalk
     
  25. Offline

    KarimAKL

    @TerroDoor Use ItemMeta#hasLore() to check for a lore, and ItemMeta#hasDisplayName() to check for a custom name.
     
  26. Offline

    TerroDoor

    Thanks, where I'm stuck is getting all of the items in the inventory and changing the Lore's, could I please get some help

    Sent from my ZTE BLADE A602 using Tapatalk
     
  27. Offline

    KarimAKL

    @TerroDoor Get the item meta and change the lore inside of the loop, that way you'll go through every kit.
     
  28. Offline

    TerroDoor

    @KarimAKL I feel like I'm having trouble understanding, I apologise for dragging this on. I've fiddled with ItemMeta and tried setting the lore and display name in the loop. Nothing shows up and I need a list to change the Lore. Here's what I've got:

    Code:
    
    //declaring the item
        ItemStack soldieritem = new ItemStack(Material.DIAMOND_SWORD);
       
        @EventHandler
        public void onOpenInv(PlayerInteractEvent e) {
           
            Player p = (Player)e.getPlayer();
           
            kits.add(soldierkit);
            kits.add(archerkit);
           
    //declaring the itemmeta and strings
            ItemMeta soldierim = soldieritem.getItemMeta();
            String unlocked = "UNLOCKED";
            String locked = "LOCKED";
           
            if (p.getInventory().getItemInMainHand().getType() == Material.FEATHER) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                   
                    p.openInventory(kitinv);
       
    //add the item(works)             
                    kitinv.setItem(0, soldieritem);
                   
                   
                    for (String k : kits) {
                       
                        if (p.hasPermission("jkits." + k)) {
                           
    //changing display name to set string, doesn't change anything
    
                            soldierim.setDisplayName(unlocked);
    
                           
                        } else {
                           
                            //setting them to "LOCKED" doesnt work
    
                            soldierim.setDisplayName(locked);
                        }
    
    
     
  29. Offline

    KarimAKL

    @TerroDoor You are setting the displayname for the item in the player's hand. Instead, add the item to the inventory you are opening and then set the meta depending on the kit.
    When you are finished setting the name and lore, you also need to set the meta back using ItemStack#setItemMeta().
     
  30. Offline

    TerroDoor

    @KarimAKL Thanks for your time, i've gotten it to work(kinda). What's happening is when i remove all perms from the player, everything gets set to 'locked'. if I however add only one perm to the player eg: "jkits.archerkit", they all return green saying 'unlocked'.

    I want only the permissions the player does have set to unlocked while the rest remain locked, thanks again for your time.

    Code:
    
    
        private Inventory kitinv = Bukkit.createInventory(null, 9, "select your kit");
    
        private ArrayList<String> kits = new ArrayList<String>();
    
        String soldierkit = "soldierkit";
        String archerkit = "archerkit";
    
        ItemStack soldieritem = new ItemStack (Material.DIAMOND_SWORD);
        ItemMeta soldierim = soldieritem.getItemMeta();
    
        ItemStack archeritem = new ItemStack (Material.BOW);
        ItemMeta archerim = soldieritem.getItemMeta();
    
        @EventHandler
        public void onOpenInv(PlayerInteractEvent e) {
    
            Player p = (Player)e.getPlayer();
    
            kits.add(soldierkit);
            kits.add(archerkit);
    
            if (p.getInventory().getItemInMainHand().getType() == Material.FEATHER) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
                    kitinv.setItem(0, soldieritem);
                    kitinv.setItem(1, archeritem);
    
                    p.openInventory(kitinv);
    
    //I think it has something to do with this part?
                    for (String k : kits) {
    
                        if (p.hasPermission("jkits." + k)) {
    
                            soldierim.setDisplayName(ChatColor.GREEN + "unlocked");
                            soldieritem.setItemMeta(soldierim);
                         
                            archerim.setDisplayName(ChatColor.GREEN + "unlocked");
                            archeritem.setItemMeta(archerim);
    
                        } else {
    
                            soldierim.setDisplayName(ChatColor.RED + "locked");
                            soldieritem.setItemMeta(soldierim);
                         
                            archerim.setDisplayName(ChatColor.RED + "locked");
                            archeritem.setItemMeta(archerim);
                        }
    
                    }
                }
    
            } else {
    
                return;
    
            }
        }
    }
    
    
    It seems to work now, I added this line:

    Code:
    
                    for (String k : kits) {
    
                        if (p.hasPermission("jkits." + k)) {
    
    //added this line
                            if (k.toString().equals(soldierkit)) {
    
    
    Is this proper and correct?

    If one kit hasnt got permission it sets all to locked.
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Aug 14, 2019
Thread Status:
Not open for further replies.

Share This Page