Jump to content

Recommended Posts

Posted

I have structures that spawn in the Nether, albeit incorreclty.

Here is the code I use to determine whether a structure can spawn at a certain position.

 

        //Offsets the structure to even ground level
        protected boolean offsetToEvenGround(World world, StructureBoundingBox structureBB, int yOffset)
        {
            if(hPos >= 0)
            {
                return true;
            }
            else
            {
                int i = 0;
                int j = 0;
                BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();

                for(int k = boundingBox.minZ; k <= boundingBox.maxZ; ++k)
                {
                    for(int l = boundingBox.minX; l <= boundingBox.maxX; ++l)
                    {
                        pos.setPos(l, boundingBox.maxY, k);

                        if(structureBB.isVecInside(pos))
                        {
                            Tuple<BlockPos, Boolean> tuple = getSuitableGroundPos(world, pos);

                            if(!tuple.getSecond())
                            {
                                return false;
                            }

                            i += tuple.getFirst().getY();
                            j++;
                        }
                    }
                }

                if(j == 0)
                {
                    return false;
                }
                else
                {
                    hPos = i / j;
                    boundingBox.offset(0, hPos - boundingBox.minY + yOffset, 0);
                    return true;
                }
            }
        }
        
        //My version on World::getTopSolidOrLiquidBlock
        //Returns this position and whether it is suitable.
        public Tuple<BlockPos, Boolean> getSuitableGroundPos(World world, BlockPos pos)
        {
            boolean isSuitable = false;

            while(!isSuitable && pos.getY() > 32)
            {
                Block block = world.getBlockState(pos).getBlock();
                
                if(world.isAirBlock(pos.up()) && block.isBlockSolid(world, pos, EnumFacing.DOWN) && block != Blocks.NETHER_BRICK && block != Blocks.NETHER_BRICK_FENCE && block != Blocks.NETHER_BRICK_STAIRS)
                {
                    isSuitable = true;
                }

                pos = pos.down();
            }

            return new Tuple<>(pos.up(), isSuitable);
        }
    }

 

It should be getting the average ground level at a certain position based on the size of the structure but, sometimes it places the structure in the air, ground or walls.

Posted

This is where you need a debugger. Set breakpoints both inside and outside your loops, then step through a couple iterations looking at the variable values. After convincing yourself that the variables are being assigned in the right places and used in the right tests, remove the outer breakpoints and set one inside the approval branch.

 

Run until the method hits it. Look at the conditions that got you there. If those are sane, then look at how your approved coords are translated/transmitted to the construction method. Somewhere along the way, palm should hit face. You'll see it live in the debugger much more easily than we can in dry code.

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.

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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