Checking if a player has already opened a chest

Discussion in 'Plugin Development' started by Rprrr, Dec 24, 2012.

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

    Rprrr

    Hi,

    I'm trying to check if a player has already opened a chest.

    When a player opens a chest for the first time, I store his data in an .yml file like so:
    Code:
    Rprrr:
      chests: '-44,64,252:-37,64,252:-37,64,252:
    So:
    Code:
    Playername:
      chests: x,y,z:x,y,z:x,y,z: //et cetera
    This is already working. Now, I want to check if the chest has already been opened (if the coordinates of the chest are stored behind the player's name).

    I first split the string into just the coords; that's what the ":" is for. Then, I loop through all the strings that I got with this, and split them in 3 more strings (I use the "," for this). One for the X, one for the Y, one for the Z. I then check if the X, Y and Z match, and if they do, I know the chest has already been opened.

    My code looks like this:
    Code:
                                String[] a = ((String) plugin.getConfig().getString(n + ".chests")).split(":");                                     
                             
                             
                                for (int i = 0; i < a.length; i++){
                                    String[] c = a[i].split(",");
                                    if (c[0].equals(b.getX())){
                                        if (c[1].equals(b.getY())){
                                            if (c[2].equals(b.getZ())){                                 
                                                //Already opened!
                                                chestopen.put(n, false);
                                            }
                                        }
                                    }
                                }
    'n' stands for the player's name, 'b' is the clicked block in the PlayerInteractEvent. I thought this would work, but it does not.

    The plugin does not do "chestopen.put(n, false);" ..

    What am I doing wrong? And is this a good way to check if a chest has already been opened, or are there simpler ways?

    Thanks in advance!
     
  2. Offline

    fireblast709

    A simpler way is to create a String representation of the location.
    Code:java
    1. StringBuilder loc = new StringBuilder();
    2. loc.append(block.getX()).append(",");
    3. loc.append(block.getY()).append(",");
    4. loc.append(block.getZ());
    5. if(a[i].equals(loc.toString())
    6. {
    7. // Already opened
    8. }[/i]
     
    Rprrr likes this.
  3. Offline

    Rprrr

    fireblast709
    Yup, I decided to go with that. Everything is working fine now. :) Thanks!
     
Thread Status:
Not open for further replies.

Share This Page