Better way of checking mulitiblocks?

Discussion in 'Plugin Development' started by Blackwing_Forged, Apr 5, 2018.

Thread Status:
Not open for further replies.
  1. So this is my code for checking my mulitblock structure, it works on 1 side but not the others, is there a way i can check it on all sides for all directions easily and not have to do this code 4 times?


    Code:
    public boolean isSmeltery(Block furnace)
        {
            Block block1 = furnace.getRelative(1, -1, 2);
            Block block2 = furnace.getRelative(0, -1, 2);
            Block block3 = furnace.getRelative(-1, -1, 2);
            Block block4 = furnace.getRelative(1, -1, 1);
            Block block5 = furnace.getRelative(0, -1, 1);
            Block block6 = furnace.getRelative(-1, -1, 1);
            Block block7 = furnace.getRelative(1, -1, 0);
            Block block8 = furnace.getRelative(0, -1, 0);
            Block block9 = furnace.getRelative(-1, -1, 0);
            //------------------------------------------
            Block block10 = furnace.getRelative(1, 0, 2);
            Block block11 = furnace.getRelative(0, 0, 2);
            Block block12 = furnace.getRelative(-1, 0, 2);
            Block block13 = furnace.getRelative(1, 0, 1);
            Block block14 = furnace.getRelative(0, 0, 1);
            Block block15 = furnace.getRelative(-1, 0, 1);
            Block block16 = furnace.getRelative(1, 0, 0);
            //FURNACE BLOCK : Block block17 = block.getRelative(0, -1, 0);
            Block block18 = furnace.getRelative(-1, 0, 0);
            //-------------------------------------------
            Block block19 = furnace.getRelative(1, 1, 2);
            Block block20 = furnace.getRelative(0, 1, 2);
            Block block21 = furnace.getRelative(-1, 1, 2);
            Block block22 = furnace.getRelative(1, 1, 1);
            Block block23 = furnace.getRelative(0, 1, 1);
            Block block24 = furnace.getRelative(-1, 1, 1);
            Block block25 = furnace.getRelative(1, 1, 0);
            Block block26 = furnace.getRelative(0, 1, 0);
            Block block27 = furnace.getRelative(-1, 1, 0);
           
            if(block1.getType() == Material.SMOOTH_BRICK && block2.getType() == Material.NETHER_BRICK && block3.getType() == Material.SMOOTH_BRICK
                    && block4.getType() == Material.NETHER_BRICK && block5.getType() == Material.NETHER_BRICK && block6.getType() == Material.NETHER_BRICK
                    && block7.getType() == Material.SMOOTH_BRICK && block8.getType() == Material.NETHER_BRICK && block9.getType() == Material.SMOOTH_BRICK
                    && block10.getType() == Material.NETHER_BRICK && block11.getType() == Material.STAINED_GLASS_PANE && block12.getType() == Material.NETHER_BRICK
                    && block13.getType() == Material.STAINED_GLASS_PANE && block14.getType() == Material.STATIONARY_LAVA && block15.getType() == Material.STAINED_GLASS_PANE
                    && block16.getType() == Material.NETHER_BRICK && block18.getType() == Material.NETHER_BRICK && block19.getType() == Material.STONE_SLAB2
                    && block20.getType() == Material.NETHER_BRICK_STAIRS && block21.getType() == Material.STONE_SLAB2 && block22.getType() == Material.NETHER_BRICK_STAIRS
                    && block23.getType() == Material.IRON_TRAPDOOR && block24.getType() == Material.NETHER_BRICK_STAIRS
                    && block25.getType() == Material.STONE_SLAB2 && block26.getType() == Material.NETHER_BRICK_STAIRS && block27.getType() == Material.STONE_SLAB2)
            {
                return true;
            }
           
            return false;
        }
     
  2. Offline

    Zombie_Striker

    @Blackwing_Forged
    1. Construct a three dimensional matrix sorting materials. This will be the 'schematic' for the smeltery.
    2. Set all of the blocks in the matrix. This will be the last time you have to 'build' the smeltery with code. Offset all of the values so the lowest values is 0.
    3. Create three for loops for the X, the Y, and the Z respectively. It will start off equaling -2, and loop up to 2.
    4. Get the block relative to the center using the XYZ as offsets.
    5. Then get the material from the matrix. Since negative numbers are not allowed, add the offset to the three dimentions so 0 is the lowest number.
    6. Compare if that material from the matrix is not equal to the block and if that material from the matrix is not null/air.
    7. If the above is true, then there is a block missing/incorrect, and it is not a smeltery.
    8. If all three for loops complete without any of the if statements saying it is incorrect, then it is a smeltery.
     
  3. @Zombie_Striker
    Can you explain what a three dimensional matrix is? ive never heard or worked with that before
     
  4. Offline

    Zombie_Striker

    @Blackwing_Forged
    Material[][][] matrix = new Material[5][5][5];
    //The above creates a matrix supporting 5 blocks in all directions
     
  5. @Zombie_Striker
    so then for all of my blocks i do matrix[-2][-2][1] = Material.FURNACE and so on, do i include air aswell? also i don't understand the offset thing that you said
     
    Last edited: Apr 5, 2018
  6. Offline

    Zombie_Striker

    @Blackwing_Forged
    You can't have negative numbers in the array, so you would offset the XYZ by 2. So a block that is -2 from the center would be offset to 0 for the array index.

    So, for your example above, it should look like:
    Code:
    int offset = 2;
    matrix[-2+offset][-2+offset][1+offset] = Material.FURNACE
    
    //or, more simply
    matrix[0][0][3] = Material.FURNACE
     
  7. @Zombie_Striker
    so then after i do that for all the materials the for loop should look like this?


    Code:
    int offset = 2;
    for(int x = -2; x < 2; x++)
        {
            for(int y = -2; y < 2; y++)
            {
                for(int z = -2; z < 2; z++)
                {
                    Material m = matrix[x+offset][y+offset][z+offset];
                    if(block.getRelative(x,y,z) != m && m != null)
                    {
                      //not a smeltery
                    }
                }
            }
        }
     
  8. Offline

    Zombie_Striker

    @Blackwing_Forged
    Close,
    What this will do is loop up to 1, since 2 is not less than 2. Change the < to a <= to allow 2 to be included in the for loop.

    Then all you need to do is return false where "//no a smeltery" and return true at the bottom of the method.
     
  9. @Zombie_Striker
    Thanks, ill let you know if it works or if i have issues

    Edit: @Zombie_Striker
    Im at work but just wondering that code will work no matter what direction i build the smeltery right?
    So both S: stone F: Furnace
    SSS
    FSS
    SSS
    and
    SSS
    SSS
    SFS

    Edit: tested it and it only works in one orientation, is there a simple way to check all 4 possible orientations it can be without having to construct a matrix for each one
     
    Last edited: Apr 7, 2018
  10. Offline

    Zombie_Striker

    @Blackwing_Forged
    Please do not edit old posts. I'm not notified when that happens.

    To check for all orientations, check when:
    1. The XYZ are all equal to themselves, checking if it is facing forwards.
    2. The X and Z are set to negitive, checking if the smeltery if 'facing' the opposite direction
    3. Swap X and Z, to check if it is facing to the right
    4. Swap X and Z and make X and Z negitive to check if it is facing left.
     
  11. Offline

    Zombie_Striker

    @Blackwing_Forged
    Yes. Same for loop. What would be different would be that the "getRelative" method would look like the following for each configuration:
    1. block.getRelative(x,y,z).getType() != m
    2. block.getRelative(-x,y,-z).getType() != m
    3. block.getRelative(z,y,x).getType() != m
    4. block.getRelative(-z,y,-x).getType() != m
    [Edit] before , you were just comparing if a Block is not equal to a Material, which it never will. Use getType to compare that the Material is not equal to a Material.
     
  12. @Zombie_Striker
    I tried this and doesn't work,
    Code:
    int offset = 2;
            for (int x = -2; x <= 2; x++)
            {
                for (int y = -2; y <= 2; y++)
                {
                    for (int z = -2; z <= 2; z++)
                    {
                        Material m = items.level2[x + offset][y + offset][z + offset];
                        if (b.getRelative(x, y, z).getType() != m && m != null)
                        {
                            return -1;
                        }
                        if (b.getRelative(-x, y, -z).getType() != m && m != null)
                        {
                            return -1;
                        }
                        if (b.getRelative(z, y, x).getType() != m && m != null)
                        {
                            return -1;
                        }
                        if (b.getRelative(-z, y, -x).getType() != m && m != null)
                        {
                            return -1;
                        }
                    }
                }
            }
    
            return 1;
     
  13. Offline

    Zombie_Striker

    @Blackwing_Forged
    Well, what you are currently doing is checking if the if the smeltry is facing every direction at once. What you want to do is check if the smeltery is facing one direction, and if not, start over and check for another direction.

    A simple way to fix this would be to create an int outside of the for loops. This will represent the direction you will be checking for. This will initially be set to 0.

    Then, before each of the b.getRelative checks, check if the state is equal to 0, 1, 2, or 3 of each respective direction checks.

    Then, to let the system start over and check another direction, replace the first three "return -1" lines with the following:
    Code:
    x = -2;
    y= -2;
    z = -2 -1;
    DIRECTION_INT++;
    continue;
    Where DIRECTION_INT is the int the int created above.
     
    Blackwing_Forged likes this.
  14. @Zombie_Striker
    Wow, that worked, thank you so much and thank you for introducing me to something new
     
Thread Status:
Not open for further replies.

Share This Page