permissions and gui

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

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

    KarimAKL

    @TerroDoor
    1. Don't create the items outside the event, instead do it inside the loop.
    2. Set the items in the inventory inside the loop. (that way you can just add a new kit without having to specify the slot)
    To do this you'll have to change the loop from an enhanced for loop to just a for loop. (When you do this, you can get the kit name by using kits.get(index))
    You can also open the inventory after adding the items.
    3. When you've created the item in the loop, set the name of the item to the name from the loop.
    Also:
    1. Don't use 'toString()' on a String.
    2. Don't check if it equals a specific kit.
     
    TerroDoor likes this.
  2. Online

    CraftCreeper6

    @TerroDoor
    It's really hard to follow your code and it's prone to problems (no offence intended). If it's working, then keep it how it is, but if you run into any problems later on then this is how I would lay it out:

    I would start by making a class that holds information about a kit.
    Code:
        public class Kit
        {
            private String name
            private List<ItemStack> contents
          
            public Kit(String name, List<ItemStack> contents)
            {
                this#name = name
                this#contents = contents
            }
          
            public String getName()
            {
                return this#name
            }
          
            public List<ItemStack> getContents()
            {
                return this#contents
            }
        }
    All it will do is hold the name of the kit, and the contents of the kit. That's it.

    I would then make a list to store all Kits.
    Code:
    private List<Kit> kits = ...
    I would store this list in a class that manages the Kits.

    Then I would make some form of initialisation, be it from a configuration file or hard programmed.
    Code:
    private void init()
        {
            this#kits.add(new Kit("Archer", contents))
            this#kits.add(new Kit("Fighter", contents))
        }
    Now, I have a list of Kits that contains every kit that I have made. In my case, it only contains the Archer and the Fighter kit.

    Then, in an EventHandler listening for PlayerInteractEvent, check if the interacted ItemStack is a feather; if so, create a new inventory (Or you can re-use one which will be less intensive and require less data storage).

    Using an enhanced for loop I would loop over all Kits in the kits List.
    Code:
    for (Kit k : this#kits)
        {
          
        }

    Inside the for loop, I would include a permission check like so
    Code:
        for (Kit k : this#kits)
        {
            String lore = ""
            lore = player#hasPermission("permission." + k#getName()) ? "UNLOCKED" : "LOCKED";
          
            ItemStack i = new ItemStack()...
            ItemMeta im = i#getItemMeta()
            im#setLore(new List<String>() {lore})
            i#setItemMeta(im)
          
            inventory#addItem(i)
          
        }
    Update the inventory and open it for the player.
     
    TerroDoor and KarimAKL like this.
  3. Offline

    TerroDoor

    @KarimAKL, @CraftCreeper6 Thank's a ton for the help, can't thank you guys enough. I've found this really difficult but im determined to accomplish this.

    Code:
    String lore = ""
    lore = player#hasPermission("permission." + k#getName()) ? "UNLOCKED" : "LOCKED";
    
    With this part of your code posted above, I can't use k.getName() and im unsure as to why, I have k listed as a String in my enhanced loop. Can i please have the code broken down abit as i dont really understand the declaration.

    Also with this, is the new List '{lore}' just adding the "" to the "unlocked" and locked" section listed above?

    Code:
    im#setLore(new List<String>() {lore})
    i#setItemMeta(im)
    
    inventory#addItem(i)
    
    Thanks so much.
     
  4. Online

    CraftCreeper6

    @TerroDoor
    You can't call k.getName() because you don't have a Kit class. You're storing it as a String and the String doesn't have getName() as a method. You'll have to make your own class for the Kit like above:
    Yes, all that code does it add either UNLOCKED or LOCKED to the lore of the ItemStack.
     
  5. Offline

    TerroDoor

    Very Informative, Thankyou once again. When adding the items into the inv, is it possible to have the kits that are owned placed first in the inventory followed after by the ones that are 'locked'. Or does this happen automatically during the loop process?


    Sent from my ZTE BLADE A602 using Tapatalk
     
  6. Online

    CraftCreeper6

    You can do two for loops, one for the unlocked and another for the locked?

    Sent from my LYA-L09 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page