Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I am trying to spawn a structure in the Nether. The structure is supposed to spawn in the air but it does not work correctly.

 

Here is the code I am using:

    public boolean generate(World world, Random rand, BlockPos pos)
    {
        rand = world.getChunkFromBlockCoords(pos).getRandomWithSeed(world.getSeed());

        Mirror[] mirrors = Mirror.values();
        Rotation[] rotations = Rotation.values();
        Mirror mirror = mirrors[rand.nextInt(mirrors.length)];
        Rotation rotation = rotations[rand.nextInt(rotations.length)];
        MinecraftServer server = world.getMinecraftServer();
        TemplateManager manager = world.getSaveHandler().getStructureTemplateManager();
        Template template = manager.getTemplate(server, WeightedUtil.getRandomStructure(rand, variants));

        PlacementSettings settings = new PlacementSettings().setMirror(mirror).setRotation(rotation).setReplacedBlock(Blocks.STRUCTURE_VOID).setRandom(rand);
        BlockPos structureSize = template.transformedSize(rotation);
        BlockPos newPos = new BlockPos(pos.getX() - structureSize.getX(), 96, pos.getZ() - structureSize.getZ());
        BlockPos spawnPos = getSuitableAirPos(world, template.getZeroPositionWithTransform(newPos, mirror, rotation), structureSize);

        if(spawnPos != BlockPos.ORIGIN)
        {
            template.addBlocksToWorld(world, spawnPos, settings, 3);
            return true;
        }
        return false;
    }

    private static BlockPos getSuitableAirPos(World world, BlockPos pos, BlockPos structureSize)
    {
        while(pos.getY() > 32)
        {
            float sizeX = structureSize.getX();
            float sizeZ = structureSize.getZ();
            float sizeY = structureSize.getY();

            int airBlocks = 0;

            for(int x = 0; x <= sizeX; x++)
            {
                for(int z = 0; z <= sizeZ; z++)
                {
                    for(int y = 0; y <= sizeY; y++)
                    {
                        BlockPos newPos = pos.add(x, y, z);

                        if(world.getBlockState(newPos) == Blocks.AIR.getDefaultState())
                        {
                            airBlocks++;
                        }
                    }
                }
            }

            if(airBlocks == sizeX * sizeY * sizeZ)
            {
                return pos;
            }

            pos = pos.down();
        }

        return BlockPos.ORIGIN;
    }
}

 

My code is based off of the fossil generation code.

 

Picture of the issue:

2017-03-08_21.06.22.png

Edited by LogicTechCorp

14 hours ago, LogicTechCorp said:

it does not work correctly.

Looks correct to me. What was I supposed to notice?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

  • Author
2 hours ago, jeffryfisher said:

Looks correct to me. What was I supposed to notice?

The structure was saved with the air blocks around it to give it some padding. There were blocks where the structure spawned and were removed when it spawned.

51 minutes ago, LogicTechCorp said:

The structure was saved with the air blocks around it to give it some padding. There were blocks where the structure spawned and were removed when it spawned.

 
 

If you don't want it to replace the blocks surrounding your structure with air, remove the air blocks from your template.

 

Also, you're just counting the air blocks that it detects at each y level you iterate over. Nothing happens if it detects a non-air block, whereas I suspect you want the counter to reset back to 0.

Edited by TheMasterGabriel
I'm guessing

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.