Jump to content

[SOLVED] Need help with generating structures.


Sam__Wilde

Recommended Posts

Hello, I want to create an item that when used, generates a structure. (structure don't generates naturally in world)

Now I am having trouble generating a structure (jigsaw).
The code below works, BUT the structure does not appear in the world until I re-enter it. This is a significant flaw that I would like to get rid of.

My current code:

public class MineshaftSpawnerItem extends Item {

    public MineshaftSpawnerItem(Properties pProperties) {
        super(pProperties);
    }

    @Override
    public @NotNull InteractionResult useOn(UseOnContext pContext) {
        Level pLevel = pContext.getLevel();
        Player pPlayer = pContext.getPlayer();
        BlockPos pBlockPos = pContext.getClickedPos();
        ItemStack item = pContext.getItemInHand();
        if (!pLevel.isClientSide()) {
            assert pPlayer != null;
            item.setCount(0);
            String command = MessageFormat.format("place structure weirdoitems:mineshaft {0} {1} {2}",
                    pBlockPos.getX(),
                    pBlockPos.getY(),
                    pBlockPos.getZ());
            System.out.println(command);

            try {
                Objects.requireNonNull(pLevel.getServer()).getCommands().getDispatcher().execute(command, pLevel.getServer().createCommandSourceStack());
            } catch (CommandSyntaxException e) {
                throw new RuntimeException(e);
            }

        }
        return InteractionResult.PASS;
    }
}

I know about the PlaceCommand.placeStructure() method, but I can't figure out how to use it, and I can't find any documentation or examples of how to use it on the internet.

 

Edit:

I searched and tried normal options to solve this problem for a very long time, but nothing. I ended up doing it like this. And it works (in my case).

@Override
    public @NotNull InteractionResult useOn(UseOnContext pContext) {
        Level pLevel = pContext.getLevel();
        BlockPos pBlockPos = pContext.getClickedPos();
        ItemStack item = pContext.getItemInHand();
        if (!pLevel.isClientSide()) {
            item.setCount(0);
            JigsawBlockEntity jigsawBlockEntity = new JigsawBlockEntity(pBlockPos, Blocks.JIGSAW.defaultBlockState());
            jigsawBlockEntity.setPool(Pools.createKey("weirdoitems:mineshaft/tunnels"));
            jigsawBlockEntity.setName(new ResourceLocation("weirdoitems:tunnel"));
            jigsawBlockEntity.setTarget(new ResourceLocation("weirdoitems:tunnel"));
            jigsawBlockEntity.setFinalState("minecraft:oak_planks");
            jigsawBlockEntity.generate((ServerLevel) pLevel, 7, false);
        }
        return InteractionResult.CONSUME;
    }

 

Edited by Sam__Wilde
Solved
Link to comment
Share on other sites

  • Sam__Wilde changed the title to [SOLVED] Need help with generating structures.

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.