0

I have a structure gen class in Minecraft. There are 79 blocks in the structure. Each block's position is declared as a variable:

int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
BlockPos pos1 = new BlockPos(x + 3, y + -1, z + 0);
BlockPos pos2 = new BlockPos(x + 4, y + -1, z + 0);
BlockPos pos3 = new BlockPos(x + 5, y + -1, z + 0);
BlockPos pos4 = new BlockPos(x + 2, y + -1, z + 1);
BlockPos pos5 = new BlockPos(x + 3, y + -1, z + 1);
BlockPos pos6 = new BlockPos(x + 4, y + -1, z + 1);
BlockPos pos7 = new BlockPos(x + 5, y + -1, z + 1);
BlockPos pos8 = new BlockPos(x + 6, y + -1, z + 1);
BlockPos pos9 = new BlockPos(x + 1, y + -1, z + 2);
BlockPos pos10 = new BlockPos(x + 2, y + -1, z + 2);
BlockPos pos11 = new BlockPos(x + 3, y + -1, z + 2);
BlockPos pos12 = new BlockPos(x + 4, y + -1, z + 2);
BlockPos pos13 = new BlockPos(x + 5, y + -1, z + 2);
BlockPos pos14 = new BlockPos(x + 6, y + -1, z + 2);
BlockPos pos15 = new BlockPos(x + 7, y + -1, z + 2);
BlockPos pos16 = new BlockPos(x + 0, y + -1, z + 3);
BlockPos pos17 = new BlockPos(x + 1, y + -1, z + 3);
BlockPos pos18 = new BlockPos(x + 2, y + -1, z + 3);
BlockPos pos19 = new BlockPos(x + 3, y + -1, z + 3);
BlockPos pos20 = new BlockPos(x + 4, y + -1, z + 3);
BlockPos pos21 = new BlockPos(x + 5, y + -1, z + 3);
BlockPos pos22 = new BlockPos(x + 6, y + -1, z + 3);
BlockPos pos23 = new BlockPos(x + 7, y + -1, z + 3);
BlockPos pos24 = new BlockPos(x + 8, y + -1, z + 3);
BlockPos pos25 = new BlockPos(x + 0, y + -1, z + 4);
BlockPos pos26 = new BlockPos(x + 1, y + -1, z + 4);
BlockPos pos27 = new BlockPos(x + 2, y + -1, z + 4);
BlockPos pos28 = new BlockPos(x + 3, y + -1, z + 4);
BlockPos pos29 = new BlockPos(x + 4, y + -1, z + 4);
BlockPos pos30 = new BlockPos(x + 5, y + -1, z + 4);
BlockPos pos31 = new BlockPos(x + 6, y + -1, z + 4);
BlockPos pos32 = new BlockPos(x + 7, y + -1, z + 4);
BlockPos pos33 = new BlockPos(x + 8, y + -1, z + 4);
BlockPos pos34 = new BlockPos(x + 0, y + -1, z + 5);
BlockPos pos35 = new BlockPos(x + 1, y + -1, z + 5);
BlockPos pos36 = new BlockPos(x + 2, y + -1, z + 5);
BlockPos pos37 = new BlockPos(x + 3, y + -1, z + 5);
BlockPos pos38 = new BlockPos(x + 4, y + -1, z + 5);
BlockPos pos39 = new BlockPos(x + 5, y + -1, z + 5);
BlockPos pos40 = new BlockPos(x + 6, y + -1, z + 5);
BlockPos pos41 = new BlockPos(x + 7, y + -1, z + 5);
BlockPos pos42 = new BlockPos(x + 8, y + -1, z + 5);
BlockPos pos43 = new BlockPos(x + 1, y + -1, z + 6);
BlockPos pos44 = new BlockPos(x + 2, y + -1, z + 6);
BlockPos pos45 = new BlockPos(x + 3, y + -1, z + 6);
BlockPos pos46 = new BlockPos(x + 4, y + -1, z + 6);
BlockPos pos47 = new BlockPos(x + 5, y + -1, z + 6);
BlockPos pos48 = new BlockPos(x + 6, y + -1, z + 6);
BlockPos pos49 = new BlockPos(x + 7, y + -1, z + 6);
BlockPos pos50 = new BlockPos(x + 2, y + -1, z + 7);
BlockPos pos51 = new BlockPos(x + 3, y + -1, z + 7);
BlockPos pos52 = new BlockPos(x + 4, y + -1, z + 7);
BlockPos pos53 = new BlockPos(x + 5, y + -1, z + 7);
BlockPos pos54 = new BlockPos(x + 6, y + -1, z + 7);
BlockPos pos55 = new BlockPos(x + 3, y + -1, z + 8);
BlockPos pos56 = new BlockPos(x + 4, y + -1, z + 8);
BlockPos pos57 = new BlockPos(x + 5, y + -1, z + 8);
BlockPos pos58 = new BlockPos(x + 2, y + 0, z + 2);
BlockPos pos59 = new BlockPos(x + 6, y + 0, z + 2);
BlockPos pos60 = new BlockPos(x + 3, y + 0, z + 3);
BlockPos pos61 = new BlockPos(x + 4, y + 0, z + 3);
BlockPos pos62 = new BlockPos(x + 5, y + 0, z + 3);
BlockPos pos63 = new BlockPos(x + 3, y + 0, z + 4);
BlockPos pos64 = new BlockPos(x + 4, y + 0, z + 4);
BlockPos pos65 = new BlockPos(x + 5, y + 0, z + 4);
BlockPos pos66 = new BlockPos(x + 3, y + 0, z + 5);
BlockPos pos67 = new BlockPos(x + 4, y + 0, z + 5);
BlockPos pos68 = new BlockPos(x + 5, y + 0, z + 5);
BlockPos pos69 = new BlockPos(x + 2, y + 0, z + 6);
BlockPos pos70 = new BlockPos(x + 6, y + 0, z + 6);
BlockPos pos71 = new BlockPos(x + 3, y + 1, z + 3);
BlockPos pos72 = new BlockPos(x + 5, y + 1, z + 3);
BlockPos pos73 = new BlockPos(x + 3, y + 1, z + 5);
BlockPos pos74 = new BlockPos(x + 5, y + 1, z + 5);
BlockPos pos75 = new BlockPos(x + 4, y + 2, z + 3);
BlockPos pos76 = new BlockPos(x + 3, y + 2, z + 4);
BlockPos pos77 = new BlockPos(x + 5, y + 2, z + 4);
BlockPos pos78 = new BlockPos(x + 4, y + 2, z + 5);
BlockPos pos79 = new BlockPos(x + 4, y + 3, z + 4);

How can I simplify this?

I have consulted Google concerning Java Lists, arrays and Maps, but either I am incapable of applying them to my objective, or it is more complicated than that.

Skgland
  • 580
  • 5
  • 18
CJ3636
  • 23
  • 3
  • Possible duplicate of [How to declare an array](http://stackoverflow.com/questions/1200621/how-to-declare-an-array) – Jaroslaw Pawlak Jun 15 '16 at 18:17
  • I assume that after that you have a bunch of code along the lines of `world.setBlockState(pos1, Blocks.stone.getDefaultState())`? Or are you using the positions for something else or using different blocks? (In either case, yes, this definitely can and should be simplified; I'll get to how if I can get a bit of context) – Pokechu22 Jun 16 '16 at 01:53

1 Answers1

0

You could use the Structure Blocks in game to create a structure file, which can than can load.

  1. Build structure in the World.
  2. Save structure to file using Structure Blocks
  3. Put .nbt file in the correct folder 'resource_root/modid/structures/'
    You may place the file in a subfolder.
  4. Generate the Structure into the world.

Here a function that could generate the Structure found under loc at the position pos in the Dimension world with the given settings.

public static boolean placeStructureInWorld(BlockPos pos, World world,ResourceLocation loc, PlacementSettings settings){  
    if(!world.isRemote && world instanceof WorldServer){  
        WorldServer worldServer = (WorldServer) world;  
        Template tmp = worldServer
                      .getStructureTemplateManager()
                      .get(worldServer.getMinecraftServer(),loc);  
        if(tmp==null)return false;  
        tmp.addBlocksToWorldChunk(world,pos,settings);  
        return true;  
    }  
    return false;  
}  

It will return true if the Structure could be build, it may fail if run on the logical client or if the Template could not be loaded.

Example for ResourceLocation and Folder:

Example 1:

Modid = "test"
Structure file name = "test_struct.nbt"
Folder: "resource_root/test/structures/" ResourceLocation: new ResourceLocation("test","test_struct")

Example 2:

Modid = "test"
Structure file name = "test_struct.nbt"
Folder: "resource_root/test/structures/subfolder/" ResourceLocation: new ResourceLocation("test","subfolder/test_struct")

Skgland
  • 580
  • 5
  • 18