Diagonals

Discussion in 'Plugin Development' started by Butter1484, Aug 1, 2015.

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

    Butter1484

    How would I go about changing blocks along a diagonal line? The code I currently have works fine for N S E W
    but that is because I am using a cuboid region, so when the corners are diagonal each other it does the whole region, not just the diagonal between them.
     
  2. Offline

    Tecno_Wizard

    @Butter1484, there might be a better way to do this with some complicated math, but math isn't my strong suit.

    Take 2 locations and take a single y coordinate. Iterate through the blocks increasing by an increment of .25 with x and z (I swear notch, why did you have to screw up the variable letters?). If that iteration lands the number inside a block that has not been already selected, add it until you reach the other location. Do this for each y coordinate.
     
  3. Offline

    Butter1484

    So basically instead of
    Code:
    for (int x = (int) x1; x <= x2; x++){
                            for (int y = (int) y1; y <= y2; y++){
                                for (int z = (int) z1; z <= z2; z++){
                                    Location pos = new Location(p.getWorld(), x, y, z);
                                    Block toChange = pos.getBlock();
    
                        if (toChange.getType() == Material.WOOL){
                            toChange.setType(Material.WOOL);
                            if (GameManager.green.contains(p.getName())){
                            toChange.setData(DyeColor.LIME.getData());
                            }
                            if (GameManager.purple.contains(p.getName())){
                                toChange.setData(DyeColor.PURPLE.getData());
                                }
                            }
                                }
                            }
                        }
    
    I should do this?
    Code:
    for (int x = (int) x1; x <= x2; x +=  .25){
                            for (int y = (int) y1; y <= y2; y++){
                                for (int z = (int) z1; z <= z2; z +=  .25){
                                    Location pos = new Location(p.getWorld(), x, y, z);
                                    Block toChange = pos.getBlock();
    
                        if (toChange.getType() == Material.WOOL){
                            toChange.setType(Material.WOOL);
                            if (GameManager.green.contains(p.getName())){
                            toChange.setData(DyeColor.LIME.getData());
                            }
                            if (GameManager.purple.contains(p.getName())){
                                toChange.setData(DyeColor.PURPLE.getData());
                                }
                            }
                                }
                            }
                        }
    
    Ok, that didn't work, just crashed the server

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    Gater12

  5. Offline

    Zombie_Striker

    @Butter1484
    The reason that did not work is because you are using Integers (int) instead of Doubles. Integers are whole numbers while doubles work with decimals. By adding 0.25 to the number, the number always stays at x1 or z1 (creating a never ending loop). Change ints to doubles and see if that works.
     
  6. Offline

    Butter1484

  7. Offline

    Zombie_Striker

    @Butter1484
    By that, do you mean that the server no longer crashes (which was what I was trying to fix with my post), but that you are creating a cuboid. If so, then use the x double for the x and z variable of the location.
     
  8. Offline

    Butter1484

    @Zombie_Striker The server no longer crashes, but I only want to change blocks in a line in any direction, even diagonally, not the whole area between the two locations.
     
  9. Offline

    Zombie_Striker

    Code:
    int xlowest = 0;
    int xhighest = 10;
    int zlowest = 0;
    int zhighest = 10;
    int y = 80;
    
    for(int x = xlowest; x < xhighest; x++){
    if(x > zlowest && x < zhighest){
    Location tosoutheast = new Location(world,x,y,x);
    Location tonortheast = new Location(world,zhighest-x,y,x);
    //this would grab all the blocks in a diagonal cross between each x,z value.
    
    tonortheast.setBlock();
    tosoutheast.setBlock();
    
    }
    }
    Try something like this. This is the correct mathmatics to get a cross.
     
  10. Offline

    Butter1484

    Tried that, but no blocks are changing now
     
  11. Offline

    Zombie_Striker

    @Butter1484
    Post what you currently have/ what you tried.
     
  12. Offline

    Butter1484

    @Zombie_Striker I have
    Code:
    for (int x = (int) x1; x < x2; x++){
                            for (int y = (int) y1; y <= y2; y++){
                                
                                if(x > z1 && x < z2){
                                    Location tosoutheast = new Location(p.getWorld(),x,y,x);
                                    Location tonortheast = new Location(p.getWorld(),z2-x,y,x);
                                    //this would grab all the blocks in a diagonal cross between each x,z value.
                                    
    
    
                                    Block toChange = tonortheast.getBlock();
                                    Block toChange2 = tosoutheast.getBlock();
    
                        if (toChange.getType() == Material.WOOL){
                            toChange.setType(Material.WOOL);
                            if (GameManager.green.contains(p.getName())){
                            toChange.setData(DyeColor.LIME.getData());
                            toChange2.setData(DyeColor.LIME.getData());
                            }
                            if (GameManager.purple.contains(p.getName())){
                                toChange.setData(DyeColor.PURPLE.getData());
                                toChange2.setData(DyeColor.PURPLE.getData());
                                }
                            }
                                }
                            }
                        }
    Although now that I really look at it, shouldn't the last variable in the location be a z not an x
     
  13. Offline

    Zombie_Striker

    @Butter1484
    I assume z1 and z2 are the zmin and zmax right? (You should stick with names that are clearer than that, but for a private plugin this shouldn't be a big deal)

    The reason I have x,y,x is because you want the x variable to be the same as the z variable (although, you might have switched the z value for x (the z2 - x should be the last))

    Test it out, see what blocks it selects, and if anything is wrong, post what happened and try to figure out the math that's wrong.
     
  14. Offline

    Butter1484

    Here's an example of what it's setting as it's points
    [11:36:22] [Client thread/INFO]: [CHAT] X: 32
    [11:36:22] [Client thread/INFO]: [CHAT] Y: 64
    [11:36:22] [Client thread/INFO]: [CHAT] Z1: 7.835545627769427 Z2: 14.0
    [11:36:22] [Client thread/INFO]: [CHAT] Y: 65
    [11:36:22] [Client thread/INFO]: [CHAT] Z1: 7.835545627769427 Z2: 14.0
    [11:36:22] [Client thread/INFO]: [CHAT] Y: 66
    [11:36:22] [Client thread/INFO]: [CHAT] Z1: 7.835545627769427 Z2: 14.0
     
Thread Status:
Not open for further replies.

Share This Page