Solved Inventory slots math

Discussion in 'Plugin Development' started by KingFaris11, Aug 18, 2014.

Thread Status:
Not open for further replies.
  1. Hi, basically I have a 54 slots inventory, and I want to make it so if player equals 1 - they can only click on the left half of the inventory - if player equals 2 - they can only click on the right half - else false. No one can click in the middle column of the inventory.

    At the moment I have this:
    Code:
        public boolean canClick(int player, int itemSlot) {
            return player == 1 ? (itemSlot >= 0 && itemSlot < 4) || (itemSlot >= 9 && itemSlot < 13) || (itemSlot >= 18 && itemSlot < 22) || (itemSlot >= 27 && itemSlot < 31) || (itemSlot >= 36 && itemSlot < 40) || (itemSlot >= 45 && itemSlot < 49) : (player == 2 ? (itemSlot > 4 && itemSlot <= 8) || (itemSlot > 13 && itemSlot <= 17) || (itemSlot > 22 && itemSlot <= 26) || (itemSlot > 31 && itemSlot <= 35) || (itemSlot > 40 && itemSlot <= 44) || (itemSlot > 49 && itemSlot <= 53) : false);
        }
    
    Though - it is one of the most long lines of code I've seen in my life. I'm definitely sure there's a simple Maths operation/formula to automatically do this, but my mind is blank at the moment and I just can't think. Can anyone else help me? :3

    Ronbo gave me this:
    Code:
    itemSlot % 9 < 4 ? left : itemSlot % 9 > 4 ? right : middle
    I'll test this.

    Edit: I cannot test until the plugin is made but I guess this'll work:
    Code:
    return player == 1 ? itemSlot % 9 < 4 : (player == 2 ? itemSlot % 9 > 4 : false);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Lactem

    So you're trying to simplify this?

    Shouldn't it be
    Code:java
    1. return player == 1 ? itemSlot % 9 < 4 : player == 2 && itemSlot % 9 > 4);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  3. That's (exactly) what he did, thanks, yeah. That worked :3
     
  4. Offline

    Lactem

    No problem, but who's "he?"
     
  5. Offline

    Goblom

    Lactem
     
    KingFaris11 and Lactem like this.
Thread Status:
Not open for further replies.

Share This Page