简体   繁体   English

如何简化Minecraft结构生成BlockPos变量

[英]How can I simplify Minecraft structure generation BlockPos variables

I have a structure gen class in Minecraft. 我在Minecraft中有一个结构gen类。 There are 79 blocks in the structure. 结构中有79个街区。 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 List s, arrays and Map s, but either I am incapable of applying them to my objective, or it is more complicated than that. 我曾就Java List ,数组和Map咨询过Google,但我要么无法将它们应用于我的目标,要么比这更复杂。

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/' 将.nbt文件放在正确的文件夹'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 . 在这里,可以使用给定settings生成在Dimension world pos位置loc下找到的Structure的函数。

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. 如果可以构建该结构,它将返回true,如果在逻辑客户端上运行或无法加载模板,则它可能会失败。

Example for ResourceLocation and Folder: ResourceLocation和文件夹的示例:

Example 1: 范例1:

Modid = "test" Modid =“测试”
Structure file name = "test_struct.nbt" 结构文件名称=“ test_struct.nbt”
Folder: "resource_root/test/structures/" ResourceLocation: new ResourceLocation("test","test_struct") 文件夹:“ resource_root / test / structures /” ResourceLocation: new ResourceLocation("test","test_struct")

Example 2: 范例2:

Modid = "test" Modid =“测试”
Structure file name = "test_struct.nbt" 结构文件名称=“ test_struct.nbt”
Folder: "resource_root/test/structures/subfolder/" ResourceLocation: new ResourceLocation("test","subfolder/test_struct") 文件夹:“ resource_root / test / structures / subfolder /” ResourceLocation: new ResourceLocation("test","subfolder/test_struct")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM