Plugin Help: Location changing when it shouldn't be?

Discussion in 'Plugin Development' started by Connor Mahaffey, May 30, 2011.

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

    Connor Mahaffey

    Hi, in my plugin, I have two different commands that set sections of blocks, kind of like cuboid. So I made a method for setting a block like so:
    Code:
    public void changeBlock(Location location, double shiftX, double shiftY, double shiftZ, Material material){
    
            Block block;
    
            location.setX(location.getX() + shiftX);
            location.setY(location.getY() + shiftY);
            location.setZ(location.getZ() + shiftZ);
            block = location.getBlock();
            block.setType(material);
    
        }
    Problem is, whatever I change the location to, becomes the new location, even though I never set location again. For example, this code surrounds a user with blocks:
    Code:
                Location location;
                Material material = Material.getMaterial(mat);
                Miscellaneous M = new Miscellaneous();
    
                location = player.getLocation();
                M.changeBlock(location, -1, 0, 0, material);
                M.changeBlock(location, 2, 0, 0, material);
                M.changeBlock(location, -1, 2, 0, material);
                M.changeBlock(location, 0, -3, 0, material);
                M.changeBlock(location, 0, 1, -1, material);
                M.changeBlock(location, 0, 0, 2, material);
                M.changeBlock(location, 0, 1, 0, material);
                M.changeBlock(location, 0, 0, -2, material);
                M.changeBlock(location, 1, 0, 1, material);
                M.changeBlock(location, -2, 0, 0, material);
    You'll see the first one puts a block 1 to the left, for the next block to be one to the right, it must be 2 - 2 right from the new location is 1 right of the original. Make sense? I'd like it to work like I envisioned, be able to set it 1 left, and 1 right without issue.
     
  2. Offline

    captainawesome7

    Just make a new location that has the same values as the old location on chageBlock.. Like wouldn't this work:
    Code:
    public void changeBlock(Location location, double shiftX, double shiftY, double shiftZ, Material material){
    
            Block block;
    
            Location loc2 = new Location(location.getWorld(), location.getX(), location.getY(), location.getZ());
    
            loc2.setX(loc2.getX() + shiftX);
            loc2.setY(loc2.getY() + shiftY);
            loc2.setZ(loc2.getZ() + shiftZ);
            block = loc2.getBlock();
            block.setType(material);
    
        }
    Or is that not what you are trying to accomplish (I'm sort of confused)?
     
  3. Offline

    Connor Mahaffey


    Yes that worked thank you!

    Sorry if my question wasn't clear, but creating a new location variable fixed the issue. Why it was doing that I have no idea. But thanks so much, when I push the next build of my plugin through, I'll be sure to add your name to the list of people who helped me out :)
     
  4. Offline

    masteroftime

    This is because of the way Java handles Objects. When you pass an object as parameter to a function, or if you just copy it like this:
    Code:
    Location loc = new Location(world,x,y,z);
    Location loc2 = loc;
    There will not be two independent locations but a single location and both of the variables will point to the same Location object.

    Example:
    Code:
    loc1 = new Location(0,0,0);
    loc2 = loc1;
    loc2.setX(5);
    loc1.getX(); //will return 5
    Hope this will make it a bit clearer for you.
     
  5. Offline

    Connor Mahaffey

    Aaaah thanks for the explanation. Makes sense, I know I've run into this a few times. Being a self-taught programmer, these are things I miss frequently. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page