Solved GUI Spacing Items Out (Math Stuff)

Discussion in 'Spigot Plugin Development' started by ZevoGaems, Mar 30, 2020.

  1. I wanted to create a GUI, but I want the items in the GUI to automatically space themselves. How could I get this result using some simple math?

    The max is 5 items spaced out like so. The GUI will place them like so if it is a odd number.


    If the number of items in the GUI is even I would place them like this


    My solution was very complicated and long and was looking for something smaller and simpler.
  2. Assuming you are only placing the items on that single line...

    Code (Java):

    int middleSlot = 13;
    int startSlot = middleSlot - (items.size() - 1);
    int slot = startSlot;
    for(int i=0; i<items.size()){
      //Place item in inventory using slot as the spot in the inventory
      slot += 2; (for a slot in between)
    Hope that helps
    • Agree Agree x 1
  3. That works thank you =)
    • Like Like x 1