Solved How to place / clone multiple blocks??

Discussion in 'Plugin Development' started by robertlit, Jul 19, 2019.

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

    robertlit

    How can I place a few blocks (or clone from a prebuilt template)??
    I know I can set one block by
    Code:
    Location loc = new Location(world, x, y, z);
    loc.getBlock().setType(Material.???);
    However, I need to place many block (I need to create an island) and this method would be inefficient, laggy and its code will be very long.

    (Thanks in advance)
     
  2. Offline

    Zombie_Striker

    @robertlit
    The best way to do this would be to do the following:
    1. Set up a system for saving block locations.
    2. Set up two points for the objects you want to save.
    3. Create a Material[][][] array that is the size of the distance between the XYZ. This is where you will store all of the blocks.
    4. For loop from one XYZ to the other XYZ.
    5. If the block at the for loop's XYZ is not air, save the material to the material array at Material[x][y][z]
    6. When that is done, you now have a list of all the blocks you want to copy/clone.
    7. Then, when you want to load/paste the clone, find a new location where you want the object to go, for loop through all the XYZ values for the Material array, and set each block type equal to the block type stored by the offset.
     
  3. Offline

    robertlit

    @Zombie_Striker
    A few questions:
    How can I save both the materials and the XYZ of the materials in an array?
    I don't see how will the pasting system work.. If the paste location is XYZ, let's assume that the block from the left of the start pasting postion will be X+1YZ, there is no other way to refer to it, thus when I copy (define the object) I will also have to calculate in relative to the starting point for example borders of copy are XYZ, XYZ I will have to calculate the difference in every dimension (between X and X, Y and Y, Z and Z), let's say the differnce in the X dimension is ten, I will have to store, X+1YZ,X+2YZ,X+3YZ....X+10YZ, and that is only for the first X row at the first Y and at the first Z.
    If I misunderstood please explain further..

    Thanks for your time!
     
    Last edited: Jul 20, 2019
  4. Offline

    Kars

    I would just create a Map<Location, Material>. Loop through all the coordinates in your copy area. If it's not air, save it. That is your copy done.

    For pasting, simply loop through the Map and set the block at Location + offset to Material. If you're pasting somewhere that isn't already void you empty the area first.

    @robertlit You asked how you store XYZ in an array. By saving Location objects.
    You determine the offset by substracting the coordinates of your copy off of the coordinates of your destination and adding the difference to the coordinates of your copy.
     
  5. Offline

    robertlit

    @Kars, thanks for your explaination.
    Is it possible to subtract Location objects?? (So that the X of Location 1 will be subtracted from the X of location 2)
    Or will I have to calculate the offset for X, Y and Z individually?

    Also, How would you recommend saving thr material and the location in one place lf the array?? I thought of saving arrays in the main array like so:
    Code:
     [[material, loc], [material, loc]] 
    So that I can refer to the first block like this: array[0][0] and to the first location like this: array[0][1].
     
    Last edited: Jul 21, 2019
  6. Offline

    CraftCreeper6

    @robertlit
    Location#subtract(location) ?


    If you do it that way, you can only have 1 block type at a certain location, block types are not unique, I.E: You can have multiple blocks that have the same type, locations are unique as you can't have two locations that are the same.

    HashMap<Location, Material>() would work, or even create your own object.

    Code:
    class BlockStruct
    {
          public Location location;
          public Material material;
    
          public BlockStruct(Location, Material)
          {
                this.location = ….;
                this.material = ….;
          }
    }

    Then have a List to store them:

    Code:
    List<BlockStruct> blocks = …;

    Then, loop over all the blocks
    Code:
    for (BlockStruct b in blocks)
    {
          // do something
    }
     
  7. Offline

    robertlit

    Thanks, to everyone who helped me I have successfuly coded the builder using @Zombie_Striker's suggestion!
     
Thread Status:
Not open for further replies.

Share This Page