Loops

Discussion in 'Spigot Plugin Development' started by TerroDoor, Oct 10, 2019.

  1. hi, I have a bunch of custom kits assigned to itemstacks, I want to loop thru every kit the player has and display them inside of the inventory.

    If the player does not have a certain kit, do not add it too the inventory

    I have been stuck on this for a while, thanks in advance!
     
  2. So you're saying you want to represent a kit as a certain item. So, for instance, an Iron Sword displays if they have the Knight class?

    For this I assume they have a permission for each kit. Loop through all kits, check if they have the permission for the kit, and in that case add it. That should allow only adding the kits they have unlocked
     
    • Agree Agree x 1
  3. Make an HashMap called unLockedKits (UUID, Kits)

    Then loop through each player, check if they have that kit permission, add them to the map

    Then on command, make an inventory, display all the results in hashmap (Key would be the players uuid) and then another for loop to get all the values)
     
  4. Bad example, no hashmap's are needed.
    Some old code I made from 2013ish
    Code (Java):
        List<String> otherKits = new ArrayList<>(); //The kits the player doesn't own
                            List<String> yourKits = new ArrayList<>(); // the kits the players do have
                            for(Hero k : classes.values()) { //Loop through your available kits
                            if(sender.hasPermission(k.perm())) { //If they have permission to that "class"
                                yourKits.add(k.name().toLowerCase());// Add the items to the list
                            }
                            else {
                                otherKits.add(k.name().toLowerCase()); //the kits they don't own
                            }
    You'll have two separate lists, I won't make it work for your code because none is shown. But basically only yourKits(the kits you owned) could be an ItemStack
     
    #4 Akkrite, Oct 10, 2019
    Last edited: Oct 10, 2019
  5. I'm guessing you placed this into a PlayerInfo like class?

    I think it would be better to have your kits represented directly rather than the name, which I'm guessing you are in fact using a HashMap to get the kit reference from the name.

    This isn't very flexible if your kits aren't singleton, an alternative would be to just reference the kit itself in the list. Also you should probably replace the List with a Set as I would imagine you don't really care about insertion order.

    More on that here: https://gist.github.com/psayre23/c30a821239f4818b0709
     
  6. It was more of a /kit command, showing what you've owned and didn't.
    This is how the actual GUI was handled. Instead of removing the locked items, I simply just wrote the lore as "locked"
    Code (Java):
    for(Hero heroes : HeroWrapper.classes.values()) {
                if(heroes.gItem() != null) {
       
                inv.setItem(n, item(p, heroes));
    }

     
    Code (Java):
     public ItemStack item(Player player, Hero hero) {
            ChatColor type = ChatColor.RED;
            if(p.hasPermission(hero.perm())) {
                type = ChatColor.GREEN;
            }
            ItemStack item = new ItemStack(hero.gItem());
    }
     
  7. Why aren't you just storing the itemstack in the Hero class?
     
  8. It is stored in the hero class.