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

Hey Guys,

i'm trying to generate my structure only on places which make sense. My Structure is basically a small cave which should only generate on the bottom of hills in the wall.

 

How it looks right now:

How it should look: https://prnt.sc/O7A7FfdhneAv

 

How would i do that?

Edited by Skyriis

Generally, this would be the responsibility of the world carver, but there are ways to get around it. You could do something where you check if the entire placement area is within blocks and the opening to the cave is touching some other air block. In general though, you would need to program most of your logic from scratch since I don't believe Minecraft does anything like this itself. The closest I can recommend as an example would probably be the GeodeFeature, though I don't think that's gonna get you anywhere to close to what you would need.

1 hour ago, Skyriis said:

How would i check that area?

You should have access within the structure to some form of the level that allows you to read the blocks in the area. As for checking the entire area, it depends on how much you want to check or optimize. In general, you could probably get away with only checking the borders. If you really don't care how much spawns outside a mountain, you just need to check the corners.

  • Author

i'm using the json stuff to generate my structure. i got no clue how i would access the level from that? :S

So, after a quick read up, you're better of making your structure a feature to generate. The only terrain adaption which makes sense for you is the bury one which would fail for air. You could attempt to set the `project_start_to_heightmap` field to the world surface and see it that gives a better chance for it to spawn connected to the air. Otherwise, you would have to generate the pieces manually similar to how buried treasure works if you wanted to keep a structure or a custom feature which could then correctly check the sides to see if it is touching air.

  • Author

how would i make a jigsaw structure a feature? or... how would i make a feature 1.19 anyway 

How would i generate the nbt files (like a jigsaw structure would do it) and then check the size and blocks around?

Edited by Skyriis

2 hours ago, Skyriis said:

how would i make a jigsaw structure a feature? or... how would i make a feature 1.19 anyway 

How would i generate the nbt files (like a jigsaw structure would do it) and then check the size and blocks around?

This tutorial might be helpful to you:

https://misode.github.io/guides/adding-custom-structures/

As for saving the NBT file for the structure itself, you can just use the structure blocks in game to save your structure in the generated folder of your world map.

  • Author

i already got the nbt files but how do i place them in a feature? I got no clue how to check and generate them in the Feature#place method

Apologies for the poor explanation on my part. If you want to use a feature, you can't easily use a template. You would need to get it through the StructureTemplateManager through the ServerLevel through the WorldGenLevel, load the data, and then generate it. TelepathicGrunt does have a tutorial on creating a custom structure through which you could use to add an extra spawn check if there is air around you. Though, imo, you're still better off with a feature since you should be able to more safely check whether you can replace the block there.

  • Author

alright so... i got this as my feature:

@Override
    public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> pContext) {
        BlockPos origin = pContext.origin();
        ServerLevel level = pContext.level().getLevel();
        StructureTemplateManager templateManager = level.getStructureManager();
        // Get structure parts
        StructureTemplate entranceTemplate = templateManager.getOrCreate(ENTRANCE);
        StructureTemplate understairTemplate = templateManager.getOrCreate(UNDERSTAIR);
        StructureTemplate roomTemplate = templateManager.getOrCreate(ROOMS[level.getRandom().nextIntBetweenInclusive(0, ROOMS.length - 1)]);


        Vec3i entranceSize = entranceTemplate.getSize();
        // Check top and bottom entrance
        for (int i = -1; i < entranceSize.getX(); i++) {
            BlockState bottomState = level.getBlockState(origin.offset(i, -1, 0));
            BlockState topState = level.getBlockState(origin.offset(i, entranceSize.getY(), 0));
            // Exit if air
            if (bottomState.isAir() || topState.isAir()) return false;
        }

        // check left and right entrance
        for (int i = -1; i < entranceSize.getY(); i++) {
            BlockState leftState = level.getBlockState(origin.offset(-1, i, 0));
            BlockState rightState = level.getBlockState(origin.offset(entranceSize.getX(), i, 0));
            //Exit if air
            if (leftState.isAir() || rightState.isAir()) return false;
        }

        // Ensure air is in front of the cave entrance
        for (int x = 1; x < entranceSize.getX() - 1; x++) {
            for (int y = 1; y < entranceSize.getY() - 1; y++) {
                if (!level.getBlockState(origin.offset(x, y, -1)).isAir()) return false;
            }
        }

        boolean entrancePlaceSuccess = entranceTemplate.placeInWorld(level, origin, origin, new StructurePlaceSettings(), level.getRandom(), 2);

        return false;
    }

and the place method get's invoked (i checked that with a break point and the intellij debugger) problem is that the world gen get's stuck :S When i create a new world it get's stuck here:

image.png

 

Any ideas what cause this?

There are cases where the section hasn't been generated yet. As such, you would need to perform numerous safety checks. I would look at OreFeature and BulkSectionAccess for examples on how to safely check the block state of certain blocks.

  • Author

alright so i've changed my place method to this:

    @Override
    public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> pContext) {
        BlockPos origin = pContext.origin();
        WorldGenLevel level = pContext.level();
        StructureTemplateManager templateManager = level.getServer().getStructureManager();
        BulkSectionAccess sectionAccess = new BulkSectionAccess(level);
        // Get structure parts
        StructureTemplate entranceTemplate = templateManager.getOrCreate(ENTRANCE);
        StructureTemplate understairTemplate = templateManager.getOrCreate(UNDERSTAIR);
        StructureTemplate roomTemplate = templateManager.getOrCreate(ROOMS[level.getRandom().nextIntBetweenInclusive(0, ROOMS.length - 1)]);

        Vec3i entranceSize = entranceTemplate.getSize();

        // Ensure we can write in the entrance structure area
        for (int x = 0; x < entranceSize.getX(); x++) {
            for (int y = 0; y < entranceSize.getY(); y++) {
                for (int z = 0; z < entranceSize.getZ(); z++) {
                    if (!level.ensureCanWrite(origin.offset(x, y, z))) return false;
                }
            }
        }

        // Check top and bottom (front) is covered
        for (int i = -1; i < entranceSize.getX(); i++) {
            if (safeBlockStateCheck(sectionAccess, origin.offset(i, -1, 0), BlockBehaviour.BlockStateBase::isAir, true)) return false;
            if (safeBlockStateCheck(sectionAccess, origin.offset(i, entranceSize.getY(), 0), BlockBehaviour.BlockStateBase::isAir, true)) return false;
        }

        // check left and right (front) is covered
        for (int i = -1; i < entranceSize.getY(); i++) {
            if (safeBlockStateCheck(sectionAccess, origin.offset(-1, i, 0), BlockBehaviour.BlockStateBase::isAir, true)) return false;
            if (safeBlockStateCheck(sectionAccess, origin.offset(entranceSize.getX(), i, 0), BlockBehaviour.BlockStateBase::isAir, true)) return false;
        }

        // Ensure air is in front of the cave entrance
        for (int x = 2; x < entranceSize.getX() - 2; x++) {
            for (int y = 2; y < entranceSize.getY() - 2; y++) {
                if (safeBlockStateCheck(sectionAccess, origin.offset(x, y, 0), blockState -> !blockState.isAir())) return false;
            }
        }

        boolean entrancePlaceSuccess = entranceTemplate.placeInWorld(level, origin, origin, new StructurePlaceSettings(), level.getRandom(), 2);

        return false;
    }

    private boolean safeBlockStateCheck(BulkSectionAccess sectionAccess, BlockPos pos, Predicate<BlockState> check) {
        return safeBlockStateCheck(sectionAccess, pos, check, false);
    }

    private boolean safeBlockStateCheck(BulkSectionAccess sectionAccess, BlockPos pos, Predicate<BlockState> check, boolean resultWhenInaccessible) {
        LevelChunkSection chunkSection = sectionAccess.getSection(pos);
        // Ensure chunk section exists
        if (chunkSection == null) return resultWhenInaccessible;
        // Get target block state
        BlockState targetState = chunkSection.getBlockState(
            SectionPos.sectionRelative(pos.getX()),
            SectionPos.sectionRelative(pos.getY()),
            SectionPos.sectionRelative(pos.getZ())
        );
        // Return test result
        return check.test(targetState);
    }

and now i'm getting those errors in the first for loop (the ensure write thing):

image.png

 

why is that?

19 hours ago, Skyriis said:

why is that?

Well, features can only generate at most one chunk away from where they spawned. So, typically they are at most 16x16x256. Structures don't have this limitation. At a glance, it looked like your structure template fit into this, so that's why I recommended it.

  • Author

oh well, the whole structure is at least 3 chunks long so i probably need to use a custom structure instead of a feature. correct?

Yeah, apologies. In that case you can look at how the buried treasure pieces work to figure out how to check the surrounding blocks and then apply. Unfortunately, none of the existing structure settings or checks will work just purely because how you want the structure to generate.

On 2/2/2023 at 8:46 PM, ChampionAsh5357 said:

Generally, this would be the responsibility of the world carver, but there are ways to get around it. You could do something where you check if the entire placement area is within blocks and the opening to the cave is touching some other air block. In general though, you would need to program most of your logic from scratch since I don't believe Minecraft does anything like this itself. The closest I can recommend as an example would probably be the GeodeFeature, though I don't think that's gonna get you anywhere to close to what you would need.
 

Thanks, I will try it and if I still face this problem, then I will message you.

Edited by CraigDesir

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.