Solved GUI - Get an item under

Discussion in 'Spigot Plugin Development' started by VojtaCRV, Jul 10, 2018.

  1. Hello.
    I have no clue how to make what the title says...
    How should it work? Well, I have a gui. When i put something(random item...) into it, i need to get the item under it to change...

    For example, i put a dsword into the 1st gui slot, and the item under it changes to dpickaxe...

    What should I do? Or how?
    Thanks..
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    http://wiki.vg/Inventory#Chest

    Inventory starts at 0, and each row is 9 slots. To get the item slot in the same column and one row below the current one simply add 9 to the current slot ID.
     
  3. Yeah i had the same idea.. But now, i realised even crazier thing about what i gotta do.. Now, this example is how it really should look like:
    I put a dsword on a slot 0(first one). The slot under it, the +9 one, changes to diamond with a display name "Reward: 200$". How to get the price from a config? Ive tried:
    Code (Java):
        public static ItemStack sellSideItem() {
            ItemStack sideItem = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIGHT_BLUE.getDyeData());
            ItemMeta sideItemM = sideItem.getItemMeta();
            sideItemM.setDisplayName("§a§lReward:§2§l§n "  //NOW, THIS IS WHERE THE PRICE SHOULD BE. HOW TO GET THE PRICE OF THE ITEM ABOVE (dsword) FROM A CONFIG? I have totally no clue...
            return sideItem;
        }
    PS: I know how to get an int from a config. I just dont know how to get the dsword, the item above. Then, I can do it myself..just how to get it.
     
  4. We don't exactly know what you want from the config.
    If you want the price of an item, just get the int from the configuration section.
    For a specific item, you will need values for each item.

    Lets just put it this way, if you want a price of any diamond sword, without caring about display name, lore, durability, etc, get a value from the config specifically for the Material "DIAMOND_SWORD"
    such as "rewards.DIAMOND_SWORD" with the int value of the price.
    So lets say you want a diamond sword to be worth $200.
    In the config:
    Code (YAML):
    rewards:
      DIAMOND_SWORD
    : 200
    Then, you can get the price of the item with
    Code (Java):
    config.getInt("rewards." + item.getType());
    But it would only work with the materials you have in your config, which would be only a Diamond Sword, if you used the config above.

    If you want to figure out how to put the int into the string, just use concatenation.
    Code (Java):
    setDisplayName("Rewards: $" + price);
    Reply if you have questions,
    and if you figure it out, mark as solved with the solution posted. (for people with the problem in the future)
     
  5. I have an idea of doing it like:
    (this is inventory, ok? :D )

    [EMPTY SLOT]
    [BLACK GLASS - with no DisplayName]

    but then, i put the diamond sword above the black glass. The black glass changes to green glass (i can do that..) and renames to Reward: a-price-from-the-config... And idk how to get the price of the item, thats 9 slots behind abd add it to the glass name.
     
  6. Get putted items slot then subtract it from 9. Then set the item in your subtracted result.

    For example you've putted item in 18 slot. And you want to get the item under that slot which in this case is the slot 9. So you have to subtract 9 from 18. And then you set the item in the 9 slot.

    Hope this helps !
    P.S Sorry for bad English.
     
  7. Got it! Thanks!
    Figured it out with:
    Code (Java):
        public static int getPrice(ItemStack item) {
            String itemname = item.getItemMeta().getDisplayName();
            return main.getConfig().getInt(itemname);
        }
    ................some other things..............

                    inv.setItem(i2, sellSideItem());
                    ItemMeta itemmeta = item2.getItemMeta();
                    itemmeta.setDisplayName("§a§lReward:§2§l§n " + getPrice(item));

     
    i2 is the +9 slot